Skip to content

Commit

Permalink
other: Add commit message check in danger
Browse files Browse the repository at this point in the history
  • Loading branch information
viestat committed Jan 11, 2018
1 parent b0434c5 commit 4afb0fe
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions dangerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@ const { danger, warn, fail } = require('danger');
const fs = require('fs');

// The Danger DSL can be a bit verbose, so let's rename
const modified = danger.git.modified_files
const modified = danger.git.modified_files;
const commits = danger.git.commits;

const mdTableGen = (headers, rows) => {

const headerStr = `|${headers.join('|')}|`;
const temp = [];

for(let i = 0; i < headers.length; i++) {
temp.push('---');
}

const splitterStr = `|${temp.join('|')}|`;

const rowStr = rows.map(row => {
return `|${row.join('|')}|`;
}).join('\n');

return `${headerStr}\n${splitterStr}\n${rowStr}\n`;
};
// No PR is too small to include a description of why you made a change

if (danger.github.pr.body.length < 10) {
Expand All @@ -15,15 +33,33 @@ const modifiedTestFiles = modified.filter(filePath => {
return filePath.match(/test\/.+?\.(js|jsx|ts|tsx)$/);
});

const testFilesIncludeExclusion = modifiedTestFiles.reduce((acc, value) => {
const content = fs.readFileSync(value).toString();
const testFilesIncludeExclusion = modifiedTestFiles.reduce((acc, filePath) => {
const content = fs.readFileSync(filePath).toString();
const invalid = content.match(/it.only|describe.only/);
if (invalid) {
acc.push(value);
acc.push(filePath);
}
return acc;
}, []);

if (testFilesIncludeExclusion.length > 0) {
fail(`An \`only\` was left in tests (${testFilesIncludeExclusion})`);
}

// Validate if commit message in PR conforms conventional change log, notify if it doesn't
const invalidCommits = commits.reduce((acc, commit) => {
const invalid = !commit.message.match(/(breaking|build|ci|chore|docs|feat|fix|other|perf|refactor|revert|style|test)\:\s(.+)/);
if (invalid) {
// acc.push(`| ${commit.sha.substr(0, 7)} | ${commit.message} |\n`);
acc.push([
commit.sha.substr(0, 7),
commit.message
]);
}
return acc;
}, []);

if (invalidCommits.length > 0) {
warn(`There are invalid Commits: \n\n${mdTableGen(['sha', 'commit'], invalidCommits)}`);
}

0 comments on commit 4afb0fe

Please sign in to comment.