-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathupload.js
79 lines (73 loc) · 2.26 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
68
69
70
71
72
73
74
75
76
77
78
79
const github = require('@actions/github')
const core = require('@actions/core')
const fs = require('fs').promises
async function upload(
localPath,
{ token, remotePath, owner, repo, commitMessage, branchName }
) {
core.debug(
`upload params:${JSON.stringify({
token,
remotePath,
owner,
repo,
commitMessage,
branchName,
})}`
)
const octokit = github.getOctokit(token)
// Get SHA
let sha = ''
try {
const { data } = await octokit.rest.repos.getContent({
owner,
repo,
path: remotePath,
ref: branchName,
})
sha = data.sha
core.debug('getContent done')
core.debug(JSON.stringify(data))
} catch (error) {
core.debug('getContent catch')
core.debug(JSON.stringify(error.data))
if (error.status === 404) {
// 404 means remote repository does not have this file, so we do not need SHA
core.debug('sha does not exist')
sha = ''
} else {
const { status, message } = error
core.error(`getContent failed. status: ${status}, message: ${message}`)
throw new Error(message)
}
}
// Get file base64 content
const file = await fs.readFile(localPath, { encoding: 'base64' })
core.debug(`file base64: ${file}`)
// Create or update file
try {
const { data } = await octokit.rest.repos.createOrUpdateFileContents({
owner,
repo,
path: remotePath,
message: commitMessage,
content: file,
branch: branchName,
sha,
})
core.debug('createOrUpdateFileContents done')
core.debug(JSON.stringify(data))
if (data.content.sha === sha) {
// Won't create a new commit
core.debug('Same file content, SHA does not changed')
}
} catch ({ status, message, response }) {
core.error(
`createOrUpdateFileContents Failed. status: ${status}, message: ${message}`
)
core.debug(JSON.stringify(response))
throw new Error(message)
}
core.debug(`✔️ Upload ${localPath} Done`)
}
module.exports = upload