Access to asset binaries of a release draft with Authorization {PAT} #77003
-
Select Topic AreaGeneral BodyHi there, I was trying to download assets of a release draft so their checksums can be programmatically appended in the release note before publication. The main logic of the script is as follows:
The script is as follows: const { pipeline } = require('node:stream/promises')
const { createWriteStream } = require('node:fs')
const { tmpdir } = require('node:os')
const { Octokit } = require('@octokit/rest')
const { PAT: TOKEN, OWNER, REPO } = process.env
const exec = async () => {
const octokit = new Octokit({ auth: TOKEN })
const releases = await octokit.repos.listReleases({
owner: OWNER,
repo: REPO,
})
const draft = releases.data.find((r) => r.draft) // find the release draft
if (!draft) {
throw new Error('No release draft found')
}
const assetList = await octokit.repos
.listReleaseAssets({
owner: OWNER,
repo: REPO,
release_id: draft.id,
})
.then((res) =>
res.data.map(({ id, name, browser_download_url }) => ({ id, name, download: browser_download_url }))
)
const tmp = tmpdir()
assetList.forEach(async ({ name, download }) => {
const path = `${tmp}/DEMO/${name}`
await pipeline(
await fetch(download, {
headers: {
Authorization: `Bearer ${TOKEN}`,
'Content-Type': 'application/octet-stream',
},
})
.then(async (res) => {
return res.body
})
.catch(() => {
throw new Error('Fail to fetch asset')
}),
createWriteStream(path)
)
console.log('end')
})
}
exec()The problem is at But this script works if - const draft = releases.data.find((r) => r.draft) // find the release draft
+ const draft = releases.data[0]I also tried fetching So, it's a permission issue, PAT of I've searched issues in this repo and found a similar one at https://github.com/orgs/community/discussions/76392, it's about access to assets of a private repo. I'm not sure if it is caused by the same reason under the hood, so I open this one for access to assets of a release draft My point: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Oh.. I found another similar issue(https://github.com/orgs/community/discussions/47453) and got the point. application/octet-stream should not only be set as Content-Type, but also be set as Accept when it's intended to download the binary. |
Beta Was this translation helpful? Give feedback.
Oh.. I found another similar issue(https://github.com/orgs/community/discussions/47453) and got the point.
application/octet-stream should not only be set as Content-Type, but also be set as Accept when it's intended to download the binary.