MOBILE-3501 gulp: Validate last commit message in push task

main
Dani Palou 2020-07-24 08:57:36 +02:00
parent 22257e5b49
commit 719ffa2ff1
2 changed files with 67 additions and 0 deletions

View File

@ -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.
*

View File

@ -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;