From a63be2a3e3160df144f82eff8dbc6378b76af2bb Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Thu, 25 Apr 2024 10:58:07 +1200 Subject: [PATCH] ci: prioritize labelling pull requests based on their title rather than their commits (#1564) --- dangerfile.ts | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/dangerfile.ts b/dangerfile.ts index 940b69974..17eed3d6b 100644 --- a/dangerfile.ts +++ b/dangerfile.ts @@ -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; } }; @@ -39,6 +43,18 @@ const labelBasedOnRules = async () => { ); }; +const labelBasedOnTitle = async (): Promise => { + 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); @@ -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}`); + }, +);