asset.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const get = require('simple-get')
  2. const util = require('./util')
  3. const proxy = require('./proxy')
  4. function findAssetId (opts, cb) {
  5. const downloadUrl = util.getDownloadUrl(opts)
  6. const apiUrl = util.getApiUrl(opts)
  7. const log = opts.log || util.noopLogger
  8. log.http('request', 'GET ' + apiUrl)
  9. const reqOpts = proxy({
  10. url: apiUrl,
  11. json: true,
  12. headers: {
  13. 'User-Agent': 'simple-get',
  14. Authorization: 'token ' + opts.token
  15. }
  16. }, opts)
  17. const req = get.concat(reqOpts, function (err, res, data) {
  18. if (err) return cb(err)
  19. log.http(res.statusCode, apiUrl)
  20. if (res.statusCode !== 200) return cb(err)
  21. // Find asset id in release
  22. for (const release of data) {
  23. if (release.tag_name === opts['tag-prefix'] + opts.pkg.version) {
  24. for (const asset of release.assets) {
  25. if (asset.browser_download_url === downloadUrl) {
  26. return cb(null, asset.id)
  27. }
  28. }
  29. }
  30. }
  31. cb(new Error('Could not find GitHub release for version'))
  32. })
  33. req.setTimeout(30 * 1000, function () {
  34. req.abort()
  35. })
  36. }
  37. module.exports = findAssetId