Files
codeql-action/lib/api-client.js
T

56 lines
2.6 KiB
JavaScript
Raw Normal View History

"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
2020-09-29 14:43:37 +01:00
const path = __importStar(require("path"));
const githubUtils = __importStar(require("@actions/github/lib/utils"));
const console_log_level_1 = __importDefault(require("console-log-level"));
2020-09-15 14:00:25 +01:00
const actions_util_1 = require("./actions-util");
const util_1 = require("./util");
var DisallowedAPIVersionReason;
(function (DisallowedAPIVersionReason) {
DisallowedAPIVersionReason[DisallowedAPIVersionReason["ACTION_TOO_OLD"] = 0] = "ACTION_TOO_OLD";
DisallowedAPIVersionReason[DisallowedAPIVersionReason["ACTION_TOO_NEW"] = 1] = "ACTION_TOO_NEW";
})(DisallowedAPIVersionReason = exports.DisallowedAPIVersionReason || (exports.DisallowedAPIVersionReason = {}));
exports.getApiClient = function (apiDetails, { allowLocalRun = false, allowExternal = false } = {}) {
if (util_1.isLocalRun() && !allowLocalRun) {
2020-09-14 10:44:43 +01:00
throw new Error("Invalid API call in local run");
}
const auth = (allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth;
return new githubUtils.GitHub(githubUtils.getOctokitOptions(auth, {
baseUrl: getApiUrl(apiDetails.url),
2020-07-06 16:04:02 +01:00
userAgent: "CodeQL Action",
2020-09-14 10:44:43 +01:00
log: console_log_level_1.default({ level: "debug" }),
}));
2020-07-06 16:04:02 +01:00
};
2020-08-25 16:19:15 +01:00
function getApiUrl(githubUrl) {
const url = new URL(githubUrl);
// If we detect this is trying to connect to github.com
2020-08-25 16:19:15 +01:00
// then return with a fixed canonical URL.
2020-09-14 10:44:43 +01:00
if (url.hostname === "github.com" || url.hostname === "api.github.com") {
return "https://api.github.com";
2020-08-25 16:19:15 +01:00
}
// Add the /api/v3 API prefix
2020-09-14 10:44:43 +01:00
url.pathname = path.join(url.pathname, "api", "v3");
2020-08-25 16:19:15 +01:00
return url.toString();
}
2020-08-11 12:43:27 +01:00
// Temporary function to aid in the transition to running on and off of github actions.
2020-11-20 11:35:59 +01:00
// Once all code has been converted this function should be removed or made canonical
2020-08-11 12:43:27 +01:00
// and called only from the action entrypoints.
function getActionsApiClient(allowLocalRun = false) {
const apiDetails = {
auth: actions_util_1.getRequiredInput("token"),
url: actions_util_1.getRequiredEnvParam("GITHUB_SERVER_URL"),
};
return exports.getApiClient(apiDetails, { allowLocalRun });
2020-08-11 12:43:27 +01:00
}
exports.getActionsApiClient = getActionsApiClient;
//# sourceMappingURL=api-client.js.map