|
1 |
| -import { danger } from 'danger'; |
| 1 | +import { parse } from 'path'; |
| 2 | +import { danger, warn } from 'danger'; |
2 | 3 |
|
3 | 4 | // Ensure that people include a description on their PRs
|
4 | 5 | if (danger.github.pr.body.length === 0) {
|
5 | 6 | fail('Please include a body for your PR');
|
6 | 7 | }
|
7 | 8 |
|
8 |
| -if ( |
9 |
| - danger.git.created_files.find(filename => filename.startsWith('rules/')) && |
10 |
| - !danger.git.modified_files.includes('README.md') |
11 |
| -) { |
12 |
| - fail('Please update the README when new rules are added'); |
13 |
| -} |
| 9 | +const createOrAddLabelSafely = async (name: string, color: string) => { |
| 10 | + try { |
| 11 | + await danger.github.utils.createOrAddLabel({ |
| 12 | + name, |
| 13 | + color, |
| 14 | + description: '', |
| 15 | + }); |
| 16 | + } catch (error) { |
| 17 | + console.warn(error); |
| 18 | + warn(`Was unable to create or add label "${name}"`); |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +const labelBasedOnRules = async () => { |
| 23 | + const affectedRules = [ |
| 24 | + ...danger.git.created_files, |
| 25 | + ...danger.git.modified_files, |
| 26 | + ...danger.git.deleted_files, |
| 27 | + ] |
| 28 | + .filter(filename => { |
| 29 | + const { dir, ext } = parse(filename); |
| 30 | + |
| 31 | + return dir === 'src/rules' && ext === '.ts'; |
| 32 | + }) |
| 33 | + .map(filename => parse(filename).name); |
| 34 | + |
| 35 | + await Promise.all( |
| 36 | + affectedRules.map(rule => |
| 37 | + createOrAddLabelSafely(`rule: ${rule}`, '#7d3abc'), |
| 38 | + ), |
| 39 | + ); |
| 40 | +}; |
| 41 | + |
| 42 | +const labelBasedOnCommits = async () => { |
| 43 | + const commits = danger.github.commits.map(commits => commits.commit.message); |
| 44 | + |
| 45 | + if (commits.some(commit => commit.startsWith('fix'))) { |
| 46 | + await createOrAddLabelSafely('bug', '#ee0701'); |
| 47 | + } |
| 48 | + |
| 49 | + if (commits.some(commit => commit.startsWith('feat'))) { |
| 50 | + await createOrAddLabelSafely('enhancement', '#84b6eb'); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +Promise.all([labelBasedOnRules(), labelBasedOnCommits()]).catch(error => { |
| 55 | + console.error(error); |
| 56 | + fail(`Something went very wrong: ${error}`); |
| 57 | +}); |
0 commit comments