Skip to content

Commit

Permalink
Replace hard-coded references to GitHub.com (#296)
Browse files Browse the repository at this point in the history
* Replace hard-coded reference to GitHub.com with environment variables

* Address ESLint errors

* Apply prettier to src/git.js

* Update GitHub hostname variable declaration and naming

* Add clone URL to GitHub context

* Update tests

Co-authored-by: Linden Huhmann <[email protected]>
  • Loading branch information
ocean90 and lhuhmann authored Sep 6, 2021
1 parent 4a3fca0 commit 983f1c4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
23 changes: 16 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,15 +707,17 @@ function checkOutRemoteBranch(context) {
if (context.repository.hasFork) {
// Fork: Add fork repo as remote
core.info(`Adding "${context.repository.forkName}" fork as remote with Git`);
run(
`git remote add fork https://${context.actor}:${context.token}@github.com/${context.repository.forkName}.git`,
);
const cloneURl = new URL(context.repository.forkCloneUrl);
cloneURl.username = context.actor;
cloneURl.username = context.token;
run(`git remote add fork ${cloneURl.toString()}`);
} else {
// No fork: Update remote URL to include auth information (so auto-fixes can be pushed)
core.info(`Adding auth information to Git remote URL`);
run(
`git remote set-url origin https://${context.actor}:${context.token}@github.com/${context.repository.repoName}.git`,
);
const cloneURl = new URL(context.repository.cloneUrl);
cloneURl.username = context.actor;
cloneURl.username = context.token;
run(`git remote set-url origin ${cloneURl.toString()}`);
}

const remote = context.repository.hasFork ? "fork" : "origin";
Expand Down Expand Up @@ -864,7 +866,7 @@ async function createCheck(linterName, sha, context, lintResult, neutralCheckOnW
core.info(
`Creating GitHub check with ${conclusion} conclusion and ${annotations.length} annotations for ${linterName}…`,
);
await request(`https://api.github.com/repos/${context.repository.repoName}/check-runs`, {
await request(`${process.env.GITHUB_API_URL}/repos/${context.repository.repoName}/check-runs`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down Expand Up @@ -911,7 +913,9 @@ const { getEnv } = __nccwpck_require__(575);
* Information about the GitHub repository and its fork (if it exists)
* @typedef GithubRepository
* @property {string} repoName Repo name.
* @property {string} cloneUrl Repo clone URL.
* @property {string} forkName Fork name.
* @property {string} forkCloneUrl Fork repo clone URL.
* @property {boolean} hasFork Whether repo has a fork.
*/

Expand Down Expand Up @@ -980,17 +984,22 @@ function parseBranch(eventName, event) {
*/
function parseRepository(eventName, event) {
const repoName = event.repository.full_name;
const cloneUrl = event.repository.clone_url;
let forkName;
let forkCloneUrl;
if (eventName === "pull_request" || eventName === "pull_request_target") {
// "pull_request" events are triggered on the repository where the PR is made. The PR branch can
// be on the same repository (`forkRepository` is set to `null`) or on a fork (`forkRepository`
// is defined)
const headRepoName = event.pull_request.head.repo.full_name;
forkName = repoName === headRepoName ? undefined : headRepoName;
forkCloneUrl = event.pull_request.head.repo.clone_url;
}
return {
repoName,
cloneUrl,
forkName,
forkCloneUrl,
hasFork: forkName != null && forkName !== repoName,
};
}
Expand Down
14 changes: 8 additions & 6 deletions src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ function checkOutRemoteBranch(context) {
if (context.repository.hasFork) {
// Fork: Add fork repo as remote
core.info(`Adding "${context.repository.forkName}" fork as remote with Git`);
run(
`git remote add fork https://${context.actor}:${context.token}@github.com/${context.repository.forkName}.git`,
);
const cloneURl = new URL(context.repository.forkCloneUrl);
cloneURl.username = context.actor;
cloneURl.username = context.token;
run(`git remote add fork ${cloneURl.toString()}`);
} else {
// No fork: Update remote URL to include auth information (so auto-fixes can be pushed)
core.info(`Adding auth information to Git remote URL`);
run(
`git remote set-url origin https://${context.actor}:${context.token}@github.com/${context.repository.repoName}.git`,
);
const cloneURl = new URL(context.repository.cloneUrl);
cloneURl.username = context.actor;
cloneURl.username = context.token;
run(`git remote set-url origin ${cloneURl.toString()}`);
}

const remote = context.repository.hasFork ? "fork" : "origin";
Expand Down
2 changes: 1 addition & 1 deletion src/github/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function createCheck(linterName, sha, context, lintResult, neutralCheckOnW
core.info(
`Creating GitHub check with ${conclusion} conclusion and ${annotations.length} annotations for ${linterName}…`,
);
await request(`https://api.github.com/repos/${context.repository.repoName}/check-runs`, {
await request(`${process.env.GITHUB_API_URL}/repos/${context.repository.repoName}/check-runs`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
8 changes: 8 additions & 0 deletions src/github/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const { getEnv } = require("../utils/action");
* Information about the GitHub repository and its fork (if it exists)
* @typedef GithubRepository
* @property {string} repoName Repo name.
* @property {string} cloneUrl Repo clone URL.
* @property {string} forkName Fork name.
* @property {string} forkCloneUrl Fork repo clone URL.
* @property {boolean} hasFork Whether repo has a fork.
*/

Expand Down Expand Up @@ -88,17 +90,23 @@ function parseBranch(eventName, event) {
*/
function parseRepository(eventName, event) {
const repoName = event.repository.full_name;
const cloneUrl = event.repository.clone_url;
let forkName;
let forkCloneUrl;
if (eventName === "pull_request" || eventName === "pull_request_target") {
// "pull_request" events are triggered on the repository where the PR is made. The PR branch can
// be on the same repository (`forkRepository` is set to `null`) or on a fork (`forkRepository`
// is defined)
const headRepoName = event.pull_request.head.repo.full_name;
forkName = repoName === headRepoName ? undefined : headRepoName;
const headForkCloneUrl = event.pull_request.head.repo.clone_url;
forkCloneUrl = cloneUrl === headForkCloneUrl ? undefined : headForkCloneUrl;
}
return {
repoName,
cloneUrl,
forkName,
forkCloneUrl,
hasFork: forkName != null && forkName !== repoName,
};
}
Expand Down
12 changes: 12 additions & 0 deletions test/github/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,39 +106,51 @@ describe("parseRepository()", () => {
// Fork detection is not supported for "push" events
expect(parseRepository("push", pushEvent)).toEqual({
repoName: REPOSITORY,
cloneUrl: `https://github.com/${REPOSITORY}.git`,
forkName: undefined,
forkCloneUrl: undefined,
hasFork: false,
});
});

test('works with "pull_request" event on repository without fork', () => {
expect(parseRepository("pull_request", prOpenEvent)).toEqual({
repoName: REPOSITORY,
cloneUrl: `https://github.com/${REPOSITORY}.git`,
forkName: undefined,
forkCloneUrl: undefined,
hasFork: false,
});

expect(parseRepository("pull_request", prSyncEvent)).toEqual({
repoName: REPOSITORY,
cloneUrl: `https://github.com/${REPOSITORY}.git`,
forkName: undefined,
forkCloneUrl: undefined,
hasFork: false,
});
});

test('works with "pull_request" event on repository with fork', () => {
const prOpenEventMod = { ...prOpenEvent };
prOpenEventMod.pull_request.head.repo.full_name = FORK_REPOSITORY;
prOpenEventMod.pull_request.head.repo.clone_url = `https://github.com/${FORK_REPOSITORY}.git`;
expect(parseRepository("pull_request", prOpenEventMod)).toEqual({
repoName: REPOSITORY,
cloneUrl: `https://github.com/${REPOSITORY}.git`,
forkName: FORK_REPOSITORY,
forkCloneUrl: `https://github.com/${FORK_REPOSITORY}.git`,
hasFork: true,
});

const prSyncEventMod = { ...prSyncEvent };
prSyncEventMod.pull_request.head.repo.full_name = FORK_REPOSITORY;
prSyncEventMod.pull_request.head.repo.clone_url = `https://github.com/${FORK_REPOSITORY}.git`;
expect(parseRepository("pull_request", prSyncEventMod)).toEqual({
repoName: REPOSITORY,
cloneUrl: `https://github.com/${REPOSITORY}.git`,
forkName: FORK_REPOSITORY,
forkCloneUrl: `https://github.com/${FORK_REPOSITORY}.git`,
hasFork: true,
});
});
Expand Down

0 comments on commit 983f1c4

Please sign in to comment.