Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to override merged commit message #1161

Merged
merged 2 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,9 @@ export function parseConventionalCommits(
const conventionalCommits: ConventionalCommit[] = [];

for (const commit of commits) {
const commitMessage = preprocessCommitMessage(commit);
try {
for (const parsedCommit of parseCommits(commit.message)) {
for (const parsedCommit of parseCommits(commitMessage)) {
const breaking =
parsedCommit.notes.filter(note => note.title === 'BREAKING CHANGE')
.length > 0;
Expand Down Expand Up @@ -376,3 +377,18 @@ export function parseConventionalCommits(

return conventionalCommits;
}

function preprocessCommitMessage(commit: Commit): string {
// look for 'BEGIN_COMMIT_OVERRIDE' section of pull request body
if (commit.pullRequest) {
const overrideMessage = (
commit.pullRequest.body.split('BEGIN_COMMIT_OVERRIDE')[1] || ''
)
.split('END_COMMIT_OVERRIDE')[0]
.trim();
if (overrideMessage) {
return overrideMessage;
}
}
return commit.message;
}
40 changes: 40 additions & 0 deletions test/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,46 @@ describe('parseConventionalCommits', () => {
expect(metaCommit!.notes[0].text).to.eql('v3.0.0');
});

it('can override the commit message from BEGIN_COMMIT_OVERRIDE body', async () => {
const commit = buildMockCommit('chore: some commit');
const body = 'BEGIN_COMMIT_OVERRIDE\nfix: some fix\nEND_COMMIT_OVERRIDE';
commit.pullRequest = {
headBranchName: 'fix-something',
baseBranchName: 'main',
number: 123,
title: 'chore: some commit',
labels: [],
files: [],
body,
};

const conventionalCommits = parseConventionalCommits([commit]);
expect(conventionalCommits).lengthOf(1);
expect(conventionalCommits[0].type).to.eql('fix');
expect(conventionalCommits[0].bareMessage).to.eql('some fix');
});
it('can override the commit message from BEGIN_COMMIT_OVERRIDE body with a meta commit', async () => {
const commit = buildMockCommit('chore: some commit');
const body =
'BEGIN_COMMIT_OVERRIDE\nfix: some fix\n\nfeat: another feature\nEND_COMMIT_OVERRIDE';
commit.pullRequest = {
headBranchName: 'fix-something',
baseBranchName: 'main',
number: 123,
title: 'chore: some commit',
labels: [],
files: [],
body,
};

const conventionalCommits = parseConventionalCommits([commit]);
expect(conventionalCommits).lengthOf(2);
expect(conventionalCommits[0].type).to.eql('feat');
expect(conventionalCommits[0].bareMessage).to.eql('another feature');
expect(conventionalCommits[1].type).to.eql('fix');
expect(conventionalCommits[1].bareMessage).to.eql('some fix');
});

// it('ignores reverted commits', async () => {
// const commits = [
// {sha: 'sha1', message: 'feat: some feature', files: ['path1/file1.txt']},
Expand Down