forked from LasyIsLazy/github-upload-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
67 lines (65 loc) · 1.58 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* API: https://developer.github.com/v3/repos/contents
*/
const axios = require('axios')
const path = require('path')
const BASE_URL = 'https://api.github.com'
const core = require('@actions/core')
async function upload(
base64Content,
{ Authorization, remotePath, username, repo }
) {
const url =
BASE_URL +
path.posix.join(
`/repos/${username}/${repo}/contents`,
// GitHub API will decode the remotePath
remotePath.replace("\\","/")
)
core.debug(`Request URL: ${url}`)
// if content exists
const res = await axios({
method: 'get',
url,
responseType: 'application/json',
headers: {
Authorization,
'Content-Type': 'application/json'
}
}).catch(err => {
//if (err.toString() !== 'Error: Request failed with status code 404') {
// console.log(err)
//}
// 404 means remote repository does not have this file, so we do not need SHA
return { data: { sha: '' } }
})
const sha = (res.data && res.data.sha) || ''
core.debug(`Get SHA: ${sha}`)
return axios({
method: 'put',
url,
responseType: 'application/json',
headers: {
Authorization,
'Content-Type': 'application/json'
},
data: {
message: 'Push Product',
sha,
content: base64Content
}
}).then(({ data }) => {
const { path, sha: currentSha } = data.content
/**
* - sha: remote file's SHA
* - currentSha: uploaded file's SHA
* Can be use to identify if they are same file
*/
return {
uploadPath: path,
sha,
currentSha
}
})
}
module.exports = upload