From 719ffa2ff10c3e88d5e1541656667c8f0283918a Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 24 Jul 2020 08:57:36 +0200 Subject: [PATCH] MOBILE-3501 gulp: Validate last commit message in push task --- gulp/git.js | 19 +++++++++++++++++++ gulp/task-push.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/gulp/git.js b/gulp/git.js index 0968e6e33..508112d04 100644 --- a/gulp/git.js +++ b/gulp/git.js @@ -156,6 +156,25 @@ class Git { }); } + /** + * Return the latest titles of the commit messages. + * + * @param count Number of commits to display. + * @param range Show only commits in the specified revision range. + * @param path Show only commits that are enough to explain how the files that match the specified paths came to be. + * @return Promise resolved with the list of titles. + */ + async messages(count, range, path) { + count = typeof count != 'undefined' ? count : 10; + + const messageList = await this.log(count, range, '%s', path); + + const messages = messageList.split('\n'); + messages.pop(); // Remove last element, it's an empty string. + + return messages; + } + /** * Push a branch. * diff --git a/gulp/task-push.js b/gulp/task-push.js index 6450b54d9..3ec238bba 100644 --- a/gulp/task-push.js +++ b/gulp/task-push.js @@ -13,6 +13,7 @@ // limitations under the License. const gulp = require('gulp'); +const inquirer = require('inquirer'); const DevConfig = require('./dev-config'); const Git = require('./git'); const Jira = require('./jira'); @@ -45,6 +46,15 @@ class PushTask { throw new Error('Cannot push HEAD branch'); } + const keepRunning = await this.validateLastCommitMessage(branch); + + if (!keepRunning) { + // Last commit not valid, stop. + console.log('Exiting...'); + done(); + return; + } + // Push the branch. console.log(`Pushing branch ${branch} to remote ${remote}...`); await Git.push(remote, branch, force); @@ -120,6 +130,44 @@ class PushTask { console.log('Setting tracker fields...'); await Jira.setCustomFields(branchData.issue, updates); } + + /** + * Validate last commit message comparing it with the branch name. + * + * @param branch Branch name. + * @return True if value is ok or the user wants to continue anyway, false to stop. + */ + async validateLastCommitMessage(branch) { + const branchData = Utils.parseBranch(branch); + + const messages = await Git.messages(1); + const message = messages[0]; + + const issue = Utils.getIssueFromCommitMessage(message); + + if (!issue || issue != branchData.issue) { + if (!issue) { + console.log('The issue number could not be found in the commit message.'); + console.log(`Commit: ${message}`); + } else if (issue != branchData.issue) { + console.log('The issue number in the last commit does not match the branch being pushed to.'); + console.log(`Branch: ${branchData.issue} vs. commit: ${issue}`); + } + + const answer = await inquirer.prompt([ + { + type: 'input', + name: 'confirm', + message: 'Are you sure you want to continue?', + default: 'n', + }, + ]); + + return answer.confirm == 'y'; + } + + return true; + } } module.exports = PushTask;