2020-01-28 13:08:03 +08:00
|
|
|
"use strict";
|
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
|
|
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
|
|
result["default"] = mod;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
// Originally pulled from https://github.com/JasonEtco/actions-toolkit/blob/master/src/github.ts
|
|
|
|
const graphql_1 = require("@octokit/graphql");
|
2020-03-02 21:16:42 +08:00
|
|
|
const rest_1 = require("@octokit/rest");
|
2020-01-28 13:08:03 +08:00
|
|
|
const Context = __importStar(require("./context"));
|
|
|
|
const httpClient = __importStar(require("@actions/http-client"));
|
|
|
|
// We need this in order to extend Octokit
|
2020-03-02 21:16:42 +08:00
|
|
|
rest_1.Octokit.prototype = new rest_1.Octokit();
|
2020-01-28 13:08:03 +08:00
|
|
|
exports.context = new Context.Context();
|
2020-03-02 21:16:42 +08:00
|
|
|
class GitHub extends rest_1.Octokit {
|
2020-01-28 13:08:03 +08:00
|
|
|
constructor(token, opts) {
|
|
|
|
super(GitHub.getOctokitOptions(GitHub.disambiguate(token, opts)));
|
|
|
|
this.graphql = GitHub.getGraphQL(GitHub.disambiguate(token, opts));
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Disambiguates the constructor overload parameters
|
|
|
|
*/
|
|
|
|
static disambiguate(token, opts) {
|
|
|
|
return [
|
|
|
|
typeof token === 'string' ? token : '',
|
|
|
|
typeof token === 'object' ? token : opts || {}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
static getOctokitOptions(args) {
|
|
|
|
const token = args[0];
|
|
|
|
const options = Object.assign({}, args[1]); // Shallow clone - don't mutate the object provided by the caller
|
2020-05-15 05:33:08 +08:00
|
|
|
// Base URL - GHES or Dotcom
|
|
|
|
options.baseUrl = options.baseUrl || this.getApiBaseUrl();
|
2020-01-28 13:08:03 +08:00
|
|
|
// Auth
|
|
|
|
const auth = GitHub.getAuthString(token, options);
|
|
|
|
if (auth) {
|
|
|
|
options.auth = auth;
|
|
|
|
}
|
|
|
|
// Proxy
|
2020-05-15 05:33:08 +08:00
|
|
|
const agent = GitHub.getProxyAgent(options.baseUrl, options);
|
2020-01-28 13:08:03 +08:00
|
|
|
if (agent) {
|
|
|
|
// Shallow clone - don't mutate the object provided by the caller
|
|
|
|
options.request = options.request ? Object.assign({}, options.request) : {};
|
|
|
|
// Set the agent
|
|
|
|
options.request.agent = agent;
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
static getGraphQL(args) {
|
|
|
|
const defaults = {};
|
2020-05-15 05:33:08 +08:00
|
|
|
defaults.baseUrl = this.getGraphQLBaseUrl();
|
2020-01-28 13:08:03 +08:00
|
|
|
const token = args[0];
|
|
|
|
const options = args[1];
|
|
|
|
// Authorization
|
|
|
|
const auth = this.getAuthString(token, options);
|
|
|
|
if (auth) {
|
|
|
|
defaults.headers = {
|
|
|
|
authorization: auth
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// Proxy
|
2020-05-15 05:33:08 +08:00
|
|
|
const agent = GitHub.getProxyAgent(defaults.baseUrl, options);
|
2020-01-28 13:08:03 +08:00
|
|
|
if (agent) {
|
|
|
|
defaults.request = { agent };
|
|
|
|
}
|
|
|
|
return graphql_1.graphql.defaults(defaults);
|
|
|
|
}
|
|
|
|
static getAuthString(token, options) {
|
|
|
|
// Validate args
|
|
|
|
if (!token && !options.auth) {
|
|
|
|
throw new Error('Parameter token or opts.auth is required');
|
|
|
|
}
|
|
|
|
else if (token && options.auth) {
|
|
|
|
throw new Error('Parameters token and opts.auth may not both be specified');
|
|
|
|
}
|
|
|
|
return typeof options.auth === 'string' ? options.auth : `token ${token}`;
|
|
|
|
}
|
2020-05-15 05:33:08 +08:00
|
|
|
static getProxyAgent(destinationUrl, options) {
|
2020-01-28 13:08:03 +08:00
|
|
|
var _a;
|
|
|
|
if (!((_a = options.request) === null || _a === void 0 ? void 0 : _a.agent)) {
|
2020-05-15 05:33:08 +08:00
|
|
|
if (httpClient.getProxyUrl(destinationUrl)) {
|
2020-01-28 13:08:03 +08:00
|
|
|
const hc = new httpClient.HttpClient();
|
2020-05-15 05:33:08 +08:00
|
|
|
return hc.getAgent(destinationUrl);
|
2020-01-28 13:08:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-05-15 05:33:08 +08:00
|
|
|
static getApiBaseUrl() {
|
|
|
|
return process.env['GITHUB_API_URL'] || 'https://api.github.com';
|
|
|
|
}
|
|
|
|
static getGraphQLBaseUrl() {
|
|
|
|
let url = process.env['GITHUB_GRAPHQL_URL'] || 'https://api.github.com/graphql';
|
|
|
|
// Shouldn't be a trailing slash, but remove if so
|
|
|
|
if (url.endsWith('/')) {
|
|
|
|
url = url.substr(0, url.length - 1);
|
|
|
|
}
|
|
|
|
// Remove trailing "/graphql"
|
|
|
|
if (url.toUpperCase().endsWith('/GRAPHQL')) {
|
|
|
|
url = url.substr(0, url.length - '/graphql'.length);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
2020-01-28 13:08:03 +08:00
|
|
|
}
|
|
|
|
exports.GitHub = GitHub;
|
|
|
|
//# sourceMappingURL=github.js.map
|