ci: properly validate commit messages locally (#35035)

PR Close #35035
This commit is contained in:
Joey Perrott 2020-01-29 15:19:37 -08:00 committed by Miško Hevery
parent 8499bbdd17
commit f7d4cc9cab
1 changed files with 41 additions and 12 deletions

View File

@ -10,25 +10,54 @@
// tslint:disable:no-console // tslint:disable:no-console
module.exports = (gulp) => async() => { module.exports = (gulp) => async() => {
try { try {
if (process.env['CI'] === 'true' && process.env['CI_PULL_REQUEST'] === 'false') {
console.info(
`Since valid commit messages are enforced by PR linting on CI,\n` +
`we do not need to validate commit messages on CI runs on upstream branches.\n\n` +
`Skipping validate-commit-message check`);
process.exit();
}
const validateCommitMessage = require('../validate-commit-message'); const validateCommitMessage = require('../validate-commit-message');
const shelljs = require('shelljs'); const shelljs = require('shelljs');
const getRefsAndShasForTarget = require('../utils/get-refs-and-shas-for-target'); const getRefsAndShasForTarget = require('../utils/get-refs-and-shas-for-target');
shelljs.set('-e'); // Break on error. // Break on error.
shelljs.set('-e');
const target = await getRefsAndShasForTarget(process.env['CIRCLE_PR_NUMBER']);
// We need to fetch origin explicitly because it might be stale. let target = {};
// I couldn't find a reliable way to do this without fetch. if (process.env['CI'] === 'true') {
// Validation of commit messages on CI
if (process.env['CI_PULL_REQUEST'] === 'false') {
// Skip commit message validation on CI for non-PR branches as we are not testing new
// unreviewed commits. By enforcing correctness on the incoming changes in the PR
// branches, we are able to render this check unnecessary on non-PR branches.
console.info(
`Since valid commit messages are enforced by PR linting on CI,\n` +
`we do not need to validate commit messages on CI runs on upstream branches.\n\n` +
`Skipping validate-commit-message check`);
process.exit();
}
target = await getRefsAndShasForTarget(process.env['CI_PULL_REQUEST']);
} else {
// Validation of commit messages locally
const baseRef = 'master';
const headRef = shelljs.exec('git symbolic-ref --short HEAD', {silent: true}).trim();
const baseSha = shelljs.exec(`git rev-parse origin/master`, {silent: true}).trim();
const headSha = shelljs.exec(`git rev-parse HEAD`, {silent: true}).trim();
const commonAncestorSha =
shelljs.exec(`git merge-base origin/master ${headSha}`, {silent: true}).trim();
target = {
base: {
ref: baseRef,
sha: baseSha,
},
head: {
ref: headRef,
sha: headSha,
},
commonAncestorSha: commonAncestorSha,
latestShaOfTargetBranch: baseSha,
latestShaOfPrBranch: headSha,
};
}
const result = shelljs.exec( const result = shelljs.exec(
`git log --reverse --format=%s ${target.commonAncestorSha}..${target.latestShaOfPrBranch}`); `git log --reverse --format=%s ${target.commonAncestorSha}..${target.latestShaOfPrBranch}`,
{silent: true});
if (result.code) { if (result.code) {
throw new Error(`Failed to fetch commits: ${result.stderr}`); throw new Error(`Failed to fetch commits: ${result.stderr}`);