Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading config file when pull request from forked repository #80

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions __tests__/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -143,6 +159,41 @@ 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: {
name: 'Hello-World',
owner: {
login: 'Codertocat',
},
},
};
}

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: {
Expand Down
14 changes: 11 additions & 3 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.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.")
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({
Expand Down
3 changes: 2 additions & 1 deletion src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down