UNPKG

1.09 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 * Authenticate with Travis and return the Travis token.
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} githubToken Github authentication token
13 * @param {Object} retryOpts Options to pass to `retry` (https://github.com/tim-kos/node-retry#retryoperationoptions)
14 * @return {String} The Travis token
15 */
16module.exports = async (travisOpts, githubToken, retryOpts) =>
17 (await pRetry(
18 () =>
19 got.post(urlJoin(getTravisUrl(travisOpts), 'auth/github'), {
20 json: true,
21 body: {github_token: githubToken}, // eslint-disable-line camelcase
22 headers: {'user-agent': 'Travis', accept: 'application/vnd.travis-ci.2+json'},
23 }),
24 Object.assign({retries: 5, factor: 2, minTimeout: 1000}, retryOpts)
25 )).body.access_token;