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

48 lines
2.1 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 });
const core = __importStar(require("@actions/core"));
2020-07-06 16:04:02 +01:00
const github = __importStar(require("@actions/github"));
const console_log_level_1 = __importDefault(require("console-log-level"));
2020-08-25 16:19:15 +01:00
const path = __importStar(require("path"));
2020-09-15 14:00:25 +01:00
const actions_util_1 = require("./actions-util");
const util_1 = require("./util");
2020-08-25 16:19:15 +01:00
exports.getApiClient = function (githubAuth, githubUrl, allowLocalRun = false) {
if (util_1.isLocalRun() && !allowLocalRun) {
2020-09-14 10:44:43 +01:00
throw new Error("Invalid API call in local run");
}
2020-08-11 12:43:27 +01:00
return new github.GitHub({
2020-08-27 16:45:28 +01:00
auth: githubAuth,
2020-08-25 16:19:15 +01:00
baseUrl: getApiUrl(githubUrl),
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 be to github.com
// 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.
// Once all code has been coverted this function should be removed or made canonical
// and called only from the action entrypoints.
function getActionsApiClient(allowLocalRun = false) {
2020-09-15 14:00:25 +01:00
return exports.getApiClient(core.getInput("token"), actions_util_1.getRequiredEnvParam("GITHUB_SERVER_URL"), allowLocalRun);
2020-08-11 12:43:27 +01:00
}
exports.getActionsApiClient = getActionsApiClient;
//# sourceMappingURL=api-client.js.map