Skip to content

Commit

Permalink
Parse the PR merge title
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel1302 committed May 4, 2021
1 parent 2bfb28d commit 3d84a18
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
23 changes: 20 additions & 3 deletions tasks/lib/parse-raw-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ function parseLine(msg, line) {
}
}

function isPRMergeCommit(msg) {
var match = msg && msg.match(/Merge pull/);

return (match) ? true : false;
}

function parseRawCommit(raw) {
debug('parsing raw commit');
if (!raw) {
Expand All @@ -32,22 +38,33 @@ function parseRawCommit(raw) {
msg.breaking = match[1];
}

if (msg.subject && lines.length > 0 && isPRMergeCommit(msg.subject)) {
// Replace subject with first line of the merge commit
msg.subject = lines.shift();
}

msg.body = lines.join('\n');
match = msg.subject.match(/^(.*)\((.*)\)\:\s(.*)$/);

const subjectWithScopeRegex = /^(.*)\((.*)\)\:\s(.*)$/;
const subjectWithoutScopeRegex = /^(.*)\:\s(.*)$/;
match = msg.subject.match(subjectWithScopeRegex);

//@TODO: match merges and pull request messages
if (!match) {
match = msg.subject && msg.subject.match(/^(.*)\:\s(.*)$/);
match = msg.subject && msg.subject.match(subjectWithoutScopeRegex);

if (!match) {
//console.log(msg.subject, '------------');
this.log('warn', 'Incorrect message:', msg.hash, msg.subject);

//return null;
}
msg.type = match ? match[1] : null;
msg.subject = match ? match[2] : msg.subject;

return msg;
}

msg.type = match[1];
msg.component = match[2];
msg.subject = match[3];
Expand Down
58 changes: 58 additions & 0 deletions test/parse_commits.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';


const chai = require('chai');
const expect = chai.expect;
const sinonChai = require('sinon-chai');
const chaiAsPromised = require("chai-as-promised");

const changelog = require('../tasks/git_changelog_generate');

chai.use(sinonChai);
chai.use(chaiAsPromised);

describe('parse_commits.spec.js', function() {

before(function() {

});

describe('Changelog()', function() {


describe('.isPRMergeCommit()', function() {
it('pr merge commits without scope', function() {
const msg = changelog.parseRawCommit(
'9b1aff905b638aa274a5fc8f88662df446d374bd\n' +
'Merge pull request #187 from org/test-repo\n' +
'fix: Fixed problem with deactivating last token.\n' +
'Some output line\n' +
'Some additional output line \n');

expect(msg.type).to.equal('fix');
expect(msg.hash).to.equal('9b1aff905b638aa274a5fc8f88662df446d374bd');
expect(msg.subject).to.equal('Fixed problem with deactivating last token.');
expect(msg.body).to.equal('Some output line\n' +
'Some additional output line \n');
expect(msg.component).to.equal(undefined);
});

it('pr merge commits with scope', function() {
const msg = changelog.parseRawCommit(
'9b1aff905b638aa274a5fc8f88662df446d374bd\n' +
'Merge pull request #187 from org/test-repo\n' +
'fix(public): Fixed problem with deactivating last token.\n' +
'Some output line\n' +
'Some additional output line \n');

expect(msg.type).to.equal('fix');
expect(msg.hash).to.equal('9b1aff905b638aa274a5fc8f88662df446d374bd');
expect(msg.subject).to.equal('Fixed problem with deactivating last token.');
expect(msg.body).to.equal('Some output line\n' +
'Some additional output line \n');
expect(msg.component).to.equal("public");
});
});
});

});

0 comments on commit 3d84a18

Please sign in to comment.