From 8915dbf564aac04e29d673a19ea599eaa725bea6 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 11 Apr 2023 00:18:55 +0900 Subject: [PATCH 1/4] Add logging --- src/utils/config.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/config.ts b/src/utils/config.ts index 17b7f435..0c773dcf 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -29,8 +29,9 @@ export default async function getConfig( throw new Error(`${path} does not point to a config file`); } catch (error: any) { + console.log(`Config file not found. owner=${owner}, repo=${repo}, path=${path}, ref=${ref}.`); if (error.status === 404) { - // TODO: add log + console.log(`Using default config.`); return defaultConfig; } From e8c5a9da861115d8ec95f814a97ed828b8094bc2 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 11 Apr 2023 00:43:46 +0900 Subject: [PATCH 2/4] Add read config from base.ref when PR from forked repo When pull request from forked repo, ref is fork-repo's ref. It is not exist base-repo. --- src/action.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/action.ts b/src/action.ts index a042f434..88e5a745 100644 --- a/src/action.ts +++ b/src/action.ts @@ -22,9 +22,17 @@ async function action(context: Context = github.context) { ); } - const ref: string = context.payload.pull_request.head.ref; - const config = await getConfig(octokit, configPath, context.repo, ref, defaultConfig); - const labelsToAdd = getLabelsToAdd(config, ref); + let readConfigRef: string; + if (context.payload.pull_request.head.full_name === context.payload.pull_request.base.full_name) { + readConfigRef = context.payload.pull_request.head.ref; + } else { + console.log("This pull request is from the forked repository. So read config from base.ref.") + readConfigRef = context.payload.pull_request.base.ref; + } + + const config = await getConfig(octokit, configPath, context.repo, readConfigRef, defaultConfig); + const headRef: string = context.payload.pull_request.head.ref; + const labelsToAdd = getLabelsToAdd(config, headRef); if (labelsToAdd.length > 0) { await octokit.issues.addLabels({ From 80e68b7cca7475e49638a38f8433a95ee0a0131b Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 11 Apr 2023 01:16:20 +0900 Subject: [PATCH 3/4] Fix tests --- __tests__/action.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/__tests__/action.test.ts b/__tests__/action.test.ts index ed39b93a..fbf063c1 100644 --- a/__tests__/action.test.ts +++ b/__tests__/action.test.ts @@ -143,6 +143,15 @@ function pullRequestOpenedFixture({ ref }: { ref: string }) { number: 1, head: { ref, + repo: { + full_name: 'Codertocat/Hello-World', + } + }, + base: { + ref: 'main', + repo: { + full_name: 'Codertocat/Hello-World', + } }, }, repository: { From b2f0e3d90020b3c8c5e8f0d0c9207b87df8a6102 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 11 Apr 2023 02:00:44 +0900 Subject: [PATCH 4/4] Add test case for when PR from forked repo --- __tests__/action.test.ts | 42 ++++++++++++++++++++++++++++++++++++++++ src/action.ts | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/__tests__/action.test.ts b/__tests__/action.test.ts index fbf063c1..6b2eedca 100644 --- a/__tests__/action.test.ts +++ b/__tests__/action.test.ts @@ -103,6 +103,22 @@ describe('pr-labeler-action', () => { await action(new MockContext(pullRequestOpenedFixture({ ref: 'hello_world' }))); }); + + it('adds the "fix" label for "fix/510-logging" branch from forked repo', async () => { + nock('https://api.github.com') + .get('/repos/Codertocat/Hello-World/contents/.github%2Fpr-labeler.yml?ref=main') + .reply(200, configFixture()) + .post('/repos/Codertocat/Hello-World/issues/1/labels', (body) => { + expect(body).toMatchObject({ + labels: ['fix'], + }); + return true; + }) + .reply(200); + + await action(new MockContext(pullRequestOpenedFixtureFromFork({ ref: 'fix/510-logging' }))); + expect.assertions(1); + }); }); class MockContext extends Context { @@ -163,6 +179,32 @@ function pullRequestOpenedFixture({ ref }: { ref: string }) { }; } +function pullRequestOpenedFixtureFromFork({ ref }: { ref: string }) { + return { + pull_request: { + number: 1, + head: { + ref, + repo: { + full_name: 'forked/Hello-World', + } + }, + base: { + ref: 'main', + repo: { + full_name: 'Codertocat/Hello-World', + } + }, + }, + repository: { + name: 'Hello-World', + owner: { + login: 'Codertocat', + }, + }, + }; +} + function setupEnvironmentVariables() { // reset process.env otherwise `Context` will use those variables process.env = {}; diff --git a/src/action.ts b/src/action.ts index 88e5a745..ae86f642 100644 --- a/src/action.ts +++ b/src/action.ts @@ -23,7 +23,7 @@ async function action(context: Context = github.context) { } let readConfigRef: string; - if (context.payload.pull_request.head.full_name === context.payload.pull_request.base.full_name) { + if (context.payload.pull_request.head.repo.full_name === context.payload.pull_request.base.repo.full_name) { readConfigRef = context.payload.pull_request.head.ref; } else { console.log("This pull request is from the forked repository. So read config from base.ref.")