Skip to content

Commit

Permalink
ci: update danger to do more stuff (#1200)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Aug 19, 2022
1 parent 1e81a25 commit 24e85c8
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions dangerfile.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@
import { danger } from 'danger';
import { parse } from 'path';
import { danger, warn } from 'danger';

// Ensure that people include a description on their PRs
if (danger.github.pr.body.length === 0) {
fail('Please include a body for your PR');
}

if (
danger.git.created_files.find(filename => filename.startsWith('rules/')) &&
!danger.git.modified_files.includes('README.md')
) {
fail('Please update the README when new rules are added');
}
const createOrAddLabelSafely = async (name: string, color: string) => {
try {
await danger.github.utils.createOrAddLabel({
name,
color,
description: '',
});
} catch (error) {
console.warn(error);
warn(`Was unable to create or add label "${name}"`);
}
};

const labelBasedOnRules = async () => {
const affectedRules = [
...danger.git.created_files,
...danger.git.modified_files,
...danger.git.deleted_files,
]
.filter(filename => {
const { dir, ext } = parse(filename);

return dir === 'src/rules' && ext === '.ts';
})
.map(filename => parse(filename).name);

await Promise.all(
affectedRules.map(rule =>
createOrAddLabelSafely(`rule: ${rule}`, '#7d3abc'),
),
);
};

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

if (commits.some(commit => commit.startsWith('fix'))) {
await createOrAddLabelSafely('bug', '#ee0701');
}

if (commits.some(commit => commit.startsWith('feat'))) {
await createOrAddLabelSafely('enhancement', '#84b6eb');
}
};

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

0 comments on commit 24e85c8

Please sign in to comment.