Skip to content

Commit 0fa68ba

Browse files
ci: download tag by commit id
1 parent 4280658 commit 0fa68ba

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

scripts/downloadGithubRelease.mjs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,56 @@ function bearer() {
4545
}
4646

4747
async function getGithubRelease() {
48-
const tag = process.env.TAG
48+
const commitId = process.env.COMMIT_ID
4949

50-
if (!tag) {
50+
if (!commitId) {
5151
return getLatestGithubRelease()
5252
}
5353

54-
return getGithubReleaseByTag(tag)
54+
console.info('Using commit id', commitId)
55+
56+
return getGithubReleaseByCommitId(commitId)
57+
}
58+
59+
async function listTags() {
60+
const url = `https://api.github.com/repos/${config.owner}/${config.repo}/tags`
61+
62+
console.debug('fetchTags url', url)
63+
64+
return await doGithubGetRequest(url)
65+
}
66+
67+
async function getGithubReleaseByCommitId(commitId) {
68+
const tag = await listTags().then((response) => findTagByCommitId(response, commitId))
69+
70+
if (!tag) {
71+
throw new Error(`Tag for commit ${commitId} not found`)
72+
}
73+
74+
return await getGithubReleaseByTag(tag.name)
5575
}
5676

5777
async function getGithubReleaseByTag(tag) {
5878
const url = `https://api.github.com/repos/${config.owner}/${config.repo}/releases/tags/${tag}`
5979

60-
console.debug('url', url)
80+
console.debug('getGithubReleaseByTag url', url)
6181

62-
const response = await fetch(url, {
63-
headers: config.token
64-
? {
65-
Authorization: bearer(config.token),
66-
}
67-
: undefined,
68-
})
82+
return await doGithubGetRequest(url)
83+
}
6984

70-
return response.json()
85+
function findTagByCommitId(tags, commitId) {
86+
return tags.find((tag) => tag?.commit?.sha === commitId)
7187
}
7288

7389
async function getLatestGithubRelease() {
7490
const url = `https://api.github.com/repos/${config.owner}/${config.repo}/releases/latest`
7591

76-
console.info('url', url)
92+
console.info('getLatestGithubRelease url', url)
93+
94+
return await doGithubGetRequest(url)
95+
}
7796

97+
async function doGithubGetRequest(url) {
7898
const response = await fetch(url, {
7999
headers: config.token
80100
? {

0 commit comments

Comments
 (0)