-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathupload-release.js
More file actions
107 lines (96 loc) · 3.71 KB
/
upload-release.js
File metadata and controls
107 lines (96 loc) · 3.71 KB
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
98
99
100
101
102
103
104
105
106
107
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/PDF files>')
process.exit(1)
}
const folder = process.argv[2]
let assets = []
try {
assets = (await fs.promises.readdir(folder)).filter((s) => !s.startsWith('.') && (s.endsWith('.dmg') || s.endsWith('.zip') || s.endsWith('.pdf')))
} catch (e) {
console.error(e.message)
process.exit(1)
}
console.log(`found ${assets.length} asset(s): ${assets}`)
const tag = process.env.RELEASE_TAG || (await getLatestTag())
const releaseId = await getReleaseIdFromTag(tag)
// Fetch the upload URL once — it's constant per release. Calling
// getUploadURL() per asset wastes API quota and risks rate limiting.
const uploadUrl = await getUploadURL(releaseId)
// Upload files concurrently (up to 4 at a time) for faster release publishing
const CONCURRENCY = 4
const uploadFile = async (filename) => {
console.log(`Uploading file ${filename} to https://github.com/${OWNER}/${REPOSITORY}/releases/${tag}`)
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: ${filename}`)
} catch (e) {
if (e.response && e.response.data) {
console.error(`error while uploading file ${filename}:`, e.response.data.errors)
} else {
console.error(`unknown error while uploading file ${filename}:`)
console.error(e)
}
}
}
// Process uploads in batches to avoid overwhelming the API
for (let i = 0; i < assets.length; i += CONCURRENCY) {
const batch = assets.slice(i, i + CONCURRENCY)
await Promise.all(batch.map(uploadFile))
}
}
main()
.catch((e) => {
console.error(e)
process.exit(1)
})