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

ci: prioritize labelling pull requests based on their title rather than their commits #1564

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`);
},
);
Loading