Skip to content

Commit d7c47eb

Browse files
committed
github-release: add a function to download assets from a GitHub Release
It requires an amazingly unintuitive process to do something as commonplace as downloading release assets. That's why I originally preferred to let the GitHub CLI deal with it an be done with it. However, I want to avoid the necessity to have that GitHub CLI installed on self-hosted runners, so let's reimplement this using the plain (or actually, not so plain, as indicated above) REST API. Note that I still punt on replacing the `curl` call; It strikes me as not so bad, given that a `curl` version that seems good enough to handle my request is installed on all-but-ancient Windows versions by default (see https://curl.se/windows/microsoft.html for details). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 59cbfd5 commit d7c47eb

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

github-release.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,34 @@ const pushGitTag = (context, setSecret, token, owner, repo, tagName, bundlePath)
303303
context.log('Done pushing tag')
304304
}
305305

306+
const downloadReleaseAssets = async (context, setSecret, appId, privateKey, owner, repo, tagName, filenameMatcher) => {
307+
const { getAccessTokenForRepo } = require('./repository-updates.js')
308+
const token = await getAccessTokenForRepo(context, setSecret, appId, privateKey, owner, repo)
309+
310+
const githubApiRequest = require('./github-api-request.js')
311+
const release = await githubApiRequest(
312+
context,
313+
token,
314+
'GET',
315+
`https://api.github.com/repos/${owner}/${repo}/releases/tags/${tagName}`
316+
)
317+
318+
for (const asset of release.assets) {
319+
if (!filenameMatcher || filenameMatcher(asset.name)) {
320+
context.log(`Downloading ${asset.name}`)
321+
await download(token, asset.url, asset.name)
322+
}
323+
}
324+
}
325+
326+
const downloadReleaseAssetsFromURL = async (context, setSecret, appId, privateKey, releaseURL, filenameMatcher) => {
327+
const [, owner, repo, tagName] = releaseURL.match(
328+
/^https:\/\/github.com\/([^/]+)\/([^/]+)\/releases\/tag\/([^/]+)$/
329+
)
330+
if (!owner || !repo || !tagName) throw new Error(`Invalid release URL: ${releaseURL}`)
331+
return await downloadReleaseAssets(context, setSecret, appId, privateKey, owner, repo, tagName, filenameMatcher)
332+
}
333+
306334
module.exports = {
307335
createRelease,
308336
updateRelease,
@@ -318,5 +346,7 @@ module.exports = {
318346
calculateSHA256ForFile,
319347
checkSHA256Sums,
320348
uploadGitArtifacts,
321-
pushGitTag
322-
}
349+
pushGitTag,
350+
downloadReleaseAssets,
351+
downloadReleaseAssetsFromURL,
352+
}

0 commit comments

Comments
 (0)