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

Support Default Label #82

Open
wants to merge 3 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
16 changes: 16 additions & 0 deletions __tests__/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ describe('pr-labeler-action', () => {
expect.assertions(1);
});

it("adds the default-label if one has been provided and the branch doesn't match any patterns", async () => {
process.env['INPUT_DEFAULT-LABEL'] = 'unknown';
nock('https://api.github.com')
.get('/repos/Codertocat/Hello-World/contents/.github%2Fpr-labeler.yml?ref=wont_find_me')
.reply(404)
.post('/repos/Codertocat/Hello-World/issues/1/labels', (body) => {
expect(body).toMatchObject({
labels: ['unknown'],
});
return true;
})
.reply(200);

await action(new MockContext(pullRequestOpenedFixture({ ref: 'wont_find_me' })));
});

it("adds no labels if the branch doesn't match any patterns", async () => {
nock('https://api.github.com')
.get('/repos/Codertocat/Hello-World/contents/.github%2Fpr-labeler.yml?ref=hello_world')
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ inputs:
configuration-path:
description: 'Path to label configurations'
default: '.github/pr-labeler.yml'
default-label:
description: 'If the branch name does not match the label configuration, use this tag'
branding:
icon: 'tag'
color: 'white'
Expand Down
5 changes: 5 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async function action(context: Context = github.context) {
const GITHUB_TOKEN = process.env.GITHUB_TOKEN ?? core.getInput('repo-token', { required: true });
const octokit = github.getOctokit(GITHUB_TOKEN).rest;
const configPath = core.getInput('configuration-path', { required: true });
const defaultLabel = core.getInput('default-label');

if (!context.payload.pull_request) {
throw new Error(
Expand All @@ -26,6 +27,10 @@ async function action(context: Context = github.context) {
const config = await getConfig(octokit, configPath, context.repo, ref, defaultConfig);
const labelsToAdd = getLabelsToAdd(config, ref);

if (labelsToAdd.length == 0 && defaultLabel) {
labelsToAdd.push(defaultLabel);
}

if (labelsToAdd.length > 0) {
await octokit.issues.addLabels({
...context.repo,
Expand Down