Skip to content

Commit

Permalink
ci: prioritize labelling pull requests based on their title rather th…
Browse files Browse the repository at this point in the history
…an their commits (#1564)
  • Loading branch information
G-Rath authored Apr 24, 2024
1 parent 589c82b commit a63be2a
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ if (danger.github.pr.body.length === 0) {
fail('Please include a body for your PR');
}

const createOrAddLabelSafely = async (name: string, color: string) => {
const createOrAddLabelSafely = async (name: string, color: string): boolean => {
try {
await danger.github.utils.createOrAddLabel({
name,
color: color.replace('#', ''),
description: '',
});

return true;
} catch (error) {
console.warn(error);
warn(`Was unable to create or add label "${name}"`);

return false;
}
};

Expand All @@ -39,6 +43,18 @@ const labelBasedOnRules = async () => {
);
};

const labelBasedOnTitle = async (): Promise<boolean> => {
if (danger.github.pr.title.startsWith('feat')) {
return createOrAddLabelSafely('enhancement', '#84b6eb');
}

if (danger.github.pr.title.startsWith('fix')) {
return createOrAddLabelSafely('bug', '#ee0701');
}

return false;
};

const labelBasedOnCommits = async () => {
const commits = danger.github.commits.map(commits => commits.commit.message);

Expand All @@ -51,7 +67,20 @@ const labelBasedOnCommits = async () => {
}
};

Promise.all([labelBasedOnRules(), labelBasedOnCommits()]).catch(error => {
console.error(error);
fail(`Something went very wrong: ${error}`);
});
const labelBasedOnTitleOrCommits = async () => {
// prioritize labeling based on the title since most pull requests will get
// squashed into a single commit with the title as the subject, but fallback
// to looking through the commits if we can't determine a label from the title
if (await labelBasedOnTitle()) {
return;
}

await labelBasedOnCommits();
};

Promise.all([labelBasedOnRules(), labelBasedOnTitleOrCommits()]).catch(
error => {
console.error(error);
fail(`Something went very wrong: ${error}`);
},
);

0 comments on commit a63be2a

Please sign in to comment.