From 8499bbdd17d9ab6b5921425ffae2ba2ea0b709dc Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Wed, 29 Jan 2020 09:47:54 -0800 Subject: [PATCH] ci: only lint commit messages in the PR (#35035) PR Close #35035 --- tools/gulp-tasks/validate-commit-message.js | 31 +++++++-------------- tools/rebase-pr.js | 1 - tools/utils/get-refs-and-shas-for-target.js | 18 +++++++----- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/tools/gulp-tasks/validate-commit-message.js b/tools/gulp-tasks/validate-commit-message.js index 0930c6f64f..7fbf56f7f5 100644 --- a/tools/gulp-tasks/validate-commit-message.js +++ b/tools/gulp-tasks/validate-commit-message.js @@ -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; diff --git a/tools/rebase-pr.js b/tools/rebase-pr.js index 53f13aa3d0..53d4280c71 100644 --- a/tools/rebase-pr.js +++ b/tools/rebase-pr.js @@ -107,4 +107,3 @@ async function _main(repository, prNumber) { await exec(`git rebase origin/${target.base.ref}`); console.log('Rebase successful.'); } - diff --git a/tools/utils/get-refs-and-shas-for-target.js b/tools/utils/get-refs-and-shas-for-target.js index e0f18b4fd3..42a8989069 100644 --- a/tools/utils/get-refs-and-shas-for-target.js +++ b/tools/utils/get-refs-and-shas-for-target.js @@ -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; +};