only insert external repos token if supplied

This commit is contained in:
Robert
2021-01-19 15:28:05 +00:00
parent b0adc415a0
commit cb574a7d60
13 changed files with 92 additions and 23 deletions

View File

@@ -20,7 +20,7 @@ export interface GitHubApiDetails {
}
export interface GitHubApiExternalRepoDetails {
externalRepoAuth: string;
externalRepoAuth: string | undefined;
url: string;
}

View File

@@ -108,3 +108,35 @@ test("checkoutExternalQueries", async (t) => {
t.false(fs.existsSync(path.join(tmpDir, repoName, commit2Sha, "b")));
});
});
test("buildCheckoutURL", (t) => {
t.deepEqual(
externalQueries.buildCheckoutURL("foo/bar", {
url: "https://github.com",
externalRepoAuth: undefined,
}),
"https://github.com/foo/bar"
);
t.deepEqual(
externalQueries.buildCheckoutURL("foo/bar", {
url: "https://github.example.com/",
externalRepoAuth: undefined,
}),
"https://github.example.com/foo/bar"
);
t.deepEqual(
externalQueries.buildCheckoutURL("foo/bar", {
url: "https://github.com",
externalRepoAuth: "abc",
}),
"https://x-access-token:abc@github.com/foo/bar"
);
t.deepEqual(
externalQueries.buildCheckoutURL("foo/bar", {
url: "https://github.example.com/",
externalRepoAuth: "abc",
}),
"https://x-access-token:abc@github.example.com/foo/bar"
);
});

View File

@@ -29,13 +29,10 @@ export async function checkoutExternalRepository(
}
if (!fs.existsSync(checkoutLocation)) {
const repoCloneURL = new URL(apiDetails.url);
repoCloneURL.username = "x-access-token";
repoCloneURL.password = apiDetails.externalRepoAuth;
repoCloneURL.pathname += `/${repository}`;
const repoCloneURL = buildCheckoutURL(repository, apiDetails);
await new toolrunner.ToolRunner(await safeWhich.safeWhich("git"), [
"clone",
repoCloneURL.toString(),
repoCloneURL,
checkoutLocation,
]).exec();
await new toolrunner.ToolRunner(await safeWhich.safeWhich("git"), [
@@ -48,3 +45,19 @@ export async function checkoutExternalRepository(
return checkoutLocation;
}
export function buildCheckoutURL(
repository: string,
apiDetails: GitHubApiExternalRepoDetails
): string {
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();
}

View File

@@ -96,9 +96,7 @@ async function run() {
const apiDetails = {
auth: actionsUtil.getRequiredInput("token"),
externalRepoAuth:
actionsUtil.getOptionalInput("external-repository-token") ??
actionsUtil.getRequiredInput("token"),
externalRepoAuth: actionsUtil.getOptionalInput("external-repository-token"),
url: actionsUtil.getRequiredEnvParam("GITHUB_SERVER_URL"),
};

View File

@@ -155,7 +155,7 @@ program
const apiDetails = {
auth: cmd.githubAuth,
externalRepoAuth: cmd.externalRepositoryToken ?? cmd.githubAuth,
externalRepoAuth: cmd.externalRepositoryToken,
url: parseGithubUrl(cmd.githubUrl),
};