From 72a57d44132ea9084559adb0da4cdee2ba1c4502 Mon Sep 17 00:00:00 2001 From: "Orhan \"aib\" Kavrakoglu" Date: Thu, 27 Jun 2024 11:56:54 +0300 Subject: [PATCH] Check multiple release pages when a GitHub token is given --- asset.js | 63 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/asset.js b/asset.js index 7a58e8b..c36541f 100644 --- a/asset.js +++ b/asset.js @@ -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