Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check multiple release pages when a GitHub token is given #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 36 additions & 27 deletions asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,48 @@ function findAssetId (opts, cb) {
const downloadUrl = util.getDownloadUrl(opts)
const apiUrl = util.getApiUrl(opts)
const log = opts.log || util.noopLogger
const maxPages = 10

log.http('request', 'GET ' + apiUrl)
const reqOpts = proxy({
url: apiUrl,
json: true,
headers: {
'User-Agent': 'simple-get',
Authorization: 'token ' + opts.token
}
}, opts)

const req = get.concat(reqOpts, function (err, res, data) {
if (err) return cb(err)
log.http(res.statusCode, apiUrl)
if (res.statusCode !== 200) return cb(err)

// Find asset id in release
for (const release of data) {
if (release.tag_name === opts['tag-prefix'] + opts.pkg.version) {
for (const asset of release.assets) {
if (asset.browser_download_url === downloadUrl) {
return cb(null, asset.id)
const checkReleasePage = function (page) {
const reqOpts = proxy({
url: apiUrl + '?page=' + page,
json: true,
headers: {
'User-Agent': 'simple-get',
Authorization: 'token ' + opts.token
}
}, opts)

log.http('request', 'GET ' + reqOpts.url)
const req = get.concat(reqOpts, function (err, res, data) {
if (err) return cb(err)
log.http(res.statusCode, apiUrl)
if (res.statusCode !== 200) return cb(err)

// Find asset id in release
for (const release of data) {
if (release.tag_name === opts['tag-prefix'] + opts.pkg.version) {
for (const asset of release.assets) {
if (asset.browser_download_url === downloadUrl) {
return cb(null, asset.id)
}
}
}
}
}

cb(new Error('Could not find GitHub release for version'))
})
if (page >= maxPages) {
cb(new Error('Could not find GitHub release for version'))
} else {
return checkReleasePage(page + 1)
}
})

req.setTimeout(30 * 1000, function () {
req.abort()
})
}

req.setTimeout(30 * 1000, function () {
req.abort()
})
checkReleasePage(1)
}

module.exports = findAssetId