UNPKG

1.71 kBJavaScriptView Raw
1const urlJoin = require('url-join');
2const got = require('got');
3const pRetry = require('p-retry');
4const getTravisUrl = require('./get-travis-url');
5
6/**
7 * Retrieve the Travis jobs for the current build.
8 *
9 * @param {Object} travisOpts Options to pass to the Travis client
10 * @param {String} travisOpts.enterprise The Travis enterprise URL
11 * @param {Boolean} travisOpts.pro `true` to use Travis Pro, `false` otherwise
12 * @param {String} travisToken The Travis Token retrieved from `auth/github`
13 * @param {Number} buildId The current build ID
14 * @param {Object} retryOpts Options to pass to `retry` (https://github.com/tim-kos/node-retry#retryoperationoptions)
15 * @return {Promise<Array>} Promise that resolves to the list of jobs
16 */
17module.exports = async (travisOpts, travisToken, buildId, retryOpts) => {
18 try {
19 return (await pRetry(
20 () =>
21 got(urlJoin(getTravisUrl(travisOpts), `builds/${buildId}`), {
22 json: true,
23 headers: {
24 'user-agent': 'Travis',
25 accept: 'application/vnd.travis-ci.2+json',
26 authorization: `token ${travisToken}`,
27 },
28 }),
29 Object.assign({retries: 5, factor: 2, minTimeout: 1000}, retryOpts)
30 )).body.jobs;
31 } catch (err) {
32 // https://github.com/semantic-release/travis-deploy-once/issues/3
33 // https://github.com/pwmckenna/node-travis-ci/issues/17
34 if (err.response && err.response.body && err.response.body.file === 'not found') {
35 throw new Error(
36 'The GitHub user of the "GH_TOKEN" has not authenticated Travis CI yet. Go to https://travis-ci.com/, login with the GitHub user of this token and then restart this job.'
37 );
38 }
39 throw err;
40 }
41};