diff --git a/tasks/lib/parse-raw-commit.js b/tasks/lib/parse-raw-commit.js index 5f58583..3c72a09 100644 --- a/tasks/lib/parse-raw-commit.js +++ b/tasks/lib/parse-raw-commit.js @@ -1,5 +1,7 @@ 'use strict'; +const { match } = require('sinon'); + var debug = require('debug')('changelog:parseRawCommit'); function parseLine(msg, line) { @@ -9,6 +11,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) { @@ -32,15 +40,27 @@ 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; @@ -48,6 +68,7 @@ function parseRawCommit(raw) { return msg; } + msg.type = match[1]; msg.component = match[2]; msg.subject = match[3]; diff --git a/test/parse_commits.spec.js b/test/parse_commits.spec.js new file mode 100644 index 0000000..f872ce3 --- /dev/null +++ b/test/parse_commits.spec.js @@ -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"); + }); + }); + }); + +});