github.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. var __importStar = (this && this.__importStar) || function (mod) {
  3. if (mod && mod.__esModule) return mod;
  4. var result = {};
  5. if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
  6. result["default"] = mod;
  7. return result;
  8. };
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. // Originally pulled from https://github.com/JasonEtco/actions-toolkit/blob/master/src/github.ts
  11. const graphql_1 = require("@octokit/graphql");
  12. const rest_1 = require("@octokit/rest");
  13. const Context = __importStar(require("./context"));
  14. const httpClient = __importStar(require("@actions/http-client"));
  15. // We need this in order to extend Octokit
  16. rest_1.Octokit.prototype = new rest_1.Octokit();
  17. exports.context = new Context.Context();
  18. class GitHub extends rest_1.Octokit {
  19. constructor(token, opts) {
  20. super(GitHub.getOctokitOptions(GitHub.disambiguate(token, opts)));
  21. this.graphql = GitHub.getGraphQL(GitHub.disambiguate(token, opts));
  22. }
  23. /**
  24. * Disambiguates the constructor overload parameters
  25. */
  26. static disambiguate(token, opts) {
  27. return [
  28. typeof token === 'string' ? token : '',
  29. typeof token === 'object' ? token : opts || {}
  30. ];
  31. }
  32. static getOctokitOptions(args) {
  33. const token = args[0];
  34. const options = Object.assign({}, args[1]); // Shallow clone - don't mutate the object provided by the caller
  35. // Auth
  36. const auth = GitHub.getAuthString(token, options);
  37. if (auth) {
  38. options.auth = auth;
  39. }
  40. // Proxy
  41. const agent = GitHub.getProxyAgent(options);
  42. if (agent) {
  43. // Shallow clone - don't mutate the object provided by the caller
  44. options.request = options.request ? Object.assign({}, options.request) : {};
  45. // Set the agent
  46. options.request.agent = agent;
  47. }
  48. return options;
  49. }
  50. static getGraphQL(args) {
  51. const defaults = {};
  52. const token = args[0];
  53. const options = args[1];
  54. // Authorization
  55. const auth = this.getAuthString(token, options);
  56. if (auth) {
  57. defaults.headers = {
  58. authorization: auth
  59. };
  60. }
  61. // Proxy
  62. const agent = GitHub.getProxyAgent(options);
  63. if (agent) {
  64. defaults.request = { agent };
  65. }
  66. return graphql_1.graphql.defaults(defaults);
  67. }
  68. static getAuthString(token, options) {
  69. // Validate args
  70. if (!token && !options.auth) {
  71. throw new Error('Parameter token or opts.auth is required');
  72. }
  73. else if (token && options.auth) {
  74. throw new Error('Parameters token and opts.auth may not both be specified');
  75. }
  76. return typeof options.auth === 'string' ? options.auth : `token ${token}`;
  77. }
  78. static getProxyAgent(options) {
  79. var _a;
  80. if (!((_a = options.request) === null || _a === void 0 ? void 0 : _a.agent)) {
  81. const serverUrl = 'https://api.github.com';
  82. if (httpClient.getProxyUrl(serverUrl)) {
  83. const hc = new httpClient.HttpClient();
  84. return hc.getAgent(serverUrl);
  85. }
  86. }
  87. return undefined;
  88. }
  89. }
  90. exports.GitHub = GitHub;
  91. //# sourceMappingURL=github.js.map