ci: only lint commit messages in the PR (#35035)

PR Close #35035
This commit is contained in:
Joey Perrott 2020-01-29 09:47:54 -08:00 committed by Miško Hevery
parent 51f47535e8
commit 8499bbdd17
3 changed files with 21 additions and 29 deletions

View File

@ -8,38 +8,27 @@
// tslint:disable:no-console
module.exports = (gulp) => () => {
module.exports = (gulp) => async() => {
try {
if (process.env['CIRCLECI'] === 'true' && !process.env['CIRCLE_PR_NUMBER']) {
if (process.env['CI'] === 'true' && process.env['CI_PULL_REQUEST'] === 'false') {
console.info(
`Since commit messages are validated as part of the PR review process,\n` +
`we do not need to commit messages on CI runs on upstream branches.\n\n` +
`Skipping validate-commit-message check`
)
`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 shelljs = require('shelljs');
const getRefsAndShasForTarget = require('../utils/get-refs-and-shas-for-target');
shelljs.set('-e'); // Break on error.
let baseBranch = 'master';
const currentVersion = require('semver').parse(require('../../package.json').version);
const baseHead =
shelljs
.exec(`git ls-remote --heads origin ${currentVersion.major}.${currentVersion.minor}.*`)
.trim()
.split('\n')
.pop();
if (baseHead) {
const match = /refs\/heads\/(.+)/.exec(baseHead);
baseBranch = match && match[1] || baseBranch;
}
const target = await getRefsAndShasForTarget(process.env['CIRCLE_PR_NUMBER']);
// We need to fetch origin explicitly because it might be stale.
// I couldn't find a reliable way to do this without fetch.
const result = shelljs.exec(
`git fetch origin ${baseBranch} && git log --reverse --format=%s origin/${baseBranch}..HEAD | grep -v "#34769"`);
`git log --reverse --format=%s ${target.commonAncestorSha}..${target.latestShaOfPrBranch}`);
if (result.code) {
throw new Error(`Failed to fetch commits: ${result.stderr}`);
@ -47,10 +36,10 @@ module.exports = (gulp) => () => {
const commitsByLine = result.trim().split(/\n/).filter(line => line != '');
console.log(`Examining ${commitsByLine.length} commit(s) between ${baseBranch} and HEAD`);
console.log(`Examining ${commitsByLine.length} commit(s) between ${target.base.ref} and HEAD`);
if (commitsByLine.length == 0) {
console.log(`There are zero new commits between ${baseBranch} and HEAD`);
console.log(`There are zero new commits between ${target.base.ref} and HEAD`);
}
const disallowSquashCommits = true;

View File

@ -107,4 +107,3 @@ async function _main(repository, prNumber) {
await exec(`git rebase origin/${target.base.ref}`);
console.log('Rebase successful.');
}

View File

@ -6,6 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/
// This script uses `console` to print messages to the user.
// tslint:disable:no-console
const https = require('https');
const util = require('util');
const child_process = require('child_process');
@ -68,18 +71,19 @@ module.exports = async function getRefsAndShasForTarget(prNumber) {
const {stdout: commonAncestorSha} =
await exec(`git merge-base origin/${result.base.ref} ${latestShaOfPrBranch}`);
return {
const output = {
base: {
ref: result.base.ref.trim(),
sha: result.base.sha.trim(),
ref: result.base.ref,
sha: result.base.sha,
},
head: {
ref: result.head.ref.trim(),
sha: result.head.sha.trim()
ref: result.head.ref,
sha: result.head.sha,
},
commonAncestorSha: commonAncestorSha.trim(),
latestShaOfTargetBranch: latestShaOfTargetBranch.trim(),
latestShaOfPrBranch: latestShaOfPrBranch.trim(),
}
};
}
return output;
};