Files
codeql-action/lib/external-queries.js
T

54 lines
2.2 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
2020-11-20 11:35:59 +01:00
const toolrunner = __importStar(require("@actions/exec/lib/toolrunner"));
const safeWhich = __importStar(require("@chrisgavin/safe-which"));
/**
2020-07-15 17:36:49 +01:00
* Check out repository at the given ref, and return the directory of the checkout.
*/
2021-01-04 12:31:55 +00:00
async function checkoutExternalRepository(repository, ref, apiDetails, tempDir, logger) {
2020-09-14 10:44:43 +01:00
logger.info(`Checking out ${repository}`);
2020-08-25 11:15:40 +02:00
const checkoutLocation = path.join(tempDir, repository, ref);
if (!checkoutLocation.startsWith(tempDir)) {
// this still permits locations that mess with sibling repositories in `tempDir`, but that is acceptable
throw new Error(`'${repository}@${ref}' is not a valid repository and reference.`);
}
if (!fs.existsSync(checkoutLocation)) {
const repoCloneURL = buildCheckoutURL(repository, apiDetails);
2020-11-20 11:35:59 +01:00
await new toolrunner.ToolRunner(await safeWhich.safeWhich("git"), [
2020-09-14 10:44:43 +01:00
"clone",
repoCloneURL,
2020-09-14 10:44:43 +01:00
checkoutLocation,
]).exec();
2020-11-20 11:35:59 +01:00
await new toolrunner.ToolRunner(await safeWhich.safeWhich("git"), [
2020-09-14 10:44:43 +01:00
`--work-tree=${checkoutLocation}`,
`--git-dir=${checkoutLocation}/.git`,
"checkout",
ref,
]).exec();
}
return checkoutLocation;
}
exports.checkoutExternalRepository = checkoutExternalRepository;
function buildCheckoutURL(repository, apiDetails) {
const repoCloneURL = new URL(apiDetails.url);
if (apiDetails.externalRepoAuth !== undefined) {
repoCloneURL.username = "x-access-token";
repoCloneURL.password = apiDetails.externalRepoAuth;
}
if (!repoCloneURL.pathname.endsWith("/")) {
repoCloneURL.pathname += "/";
}
repoCloneURL.pathname += `${repository}`;
return repoCloneURL.toString();
}
exports.buildCheckoutURL = buildCheckoutURL;
2020-05-13 16:31:24 +01:00
//# sourceMappingURL=external-queries.js.map