-
Notifications
You must be signed in to change notification settings - Fork 73
/
upload-release.js
97 lines (87 loc) · 3.14 KB
/
upload-release.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const child_process = require('child_process')
const fs = require('fs')
const { Octokit } = require("octokit")
const path = require('path')
/**
* Environment variables
* GITHUB_TOKEN (Required): GitHub authentication token
* REPOSITORY: Target repository to upload release asset. defaults to lablup/backend.ai-webui
* RELEASE_TAG: Release tag, defaults to latest tag
*/
const [OWNER, REPOSITORY] = (process.env.REPOSITORY || 'lablup/backend.ai-webui').split('/')
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN })
const execCommand = async (command, ...args) => {
return await new Promise((res, rej) => {
const proc = child_process.spawn(command, args)
let stdout = ''
let stderr = ''
proc.stdout.on('data', (d) => {
stdout += d.toString()
})
proc.stderr.on('data', (d) => {
stderr += d.toString()
})
proc.on('close', (code) => {
if (code === 0) res([stdout, stderr])
else rej([stdout, stderr])
})
})
}
const getLatestTag = async () => {
const [stdout, _] = await execCommand('/usr/bin/env', 'git', 'describe', '--tags', '--abbrev=0')
return stdout.trim()
}
const getReleaseIdFromTag = async (tag) => {
const { data: { id } } = await octokit.rest.repos.getReleaseByTag({ owner: OWNER, repo: REPOSITORY, tag })
return id
}
const getUploadURL = async (releaseId) => {
const { data: { upload_url } } = await octokit.rest.repos.getRelease({ owner: OWNER, repo: REPOSITORY, release_id: releaseId })
return upload_url
}
const main = async () => {
if (process.argv.length !== 3) {
console.error('usage: node upload-release.js <folder containing DMG/ZIP files>')
process.exit(1)
}
const folder = process.argv[2]
let DMGs = []
try {
DMGs = (await fs.promises.readdir(folder)).filter((s) => !s.startsWith('.') && (s.endsWith('.dmg') || s.endsWith('.zip')))
} catch (e) {
console.error(e.message)
process.exit(1)
}
console.log(`found ${DMGs.length} DMG(s): ${DMGs}`)
const tag = process.env.RELEASE_TAG || (await getLatestTag())
const releaseId = await getReleaseIdFromTag(tag)
for (const filename of DMGs) {
console.log(`Uploading file ${filename} to https://github.com/${OWNER}/${REPOSITORY}/releases/${tag}`)
const uploadUrl = await getUploadURL(releaseId)
const buf = await fs.promises.readFile(path.join(folder, filename))
try {
await octokit.request({
method: 'POST',
url: uploadUrl,
headers: {
'content-type': 'application/octet-stream',
},
data: buf,
name: filename,
})
console.log('completed uploading file')
} catch (e) {
if (e.response.data) {
console.error('error while uploading file:', e.response.data.errors)
} else {
console.error('unknown error while uploading file:')
console.error(e)
}
}
}
}
main()
.catch((e) => {
console.error(e)
process.exit(1)
})