-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce processCommits hook for plugins (#1607)
feat: introduce `sentence-case` plugin that capitalizes commit messages
- Loading branch information
Showing
8 changed files
with
266 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {ManifestPlugin} from '../plugin'; | ||
import {GitHub} from '../github'; | ||
import {RepositoryConfig} from '../manifest'; | ||
import {Commit} from '../commit'; | ||
|
||
// A list of words that should not be converted to uppercase: | ||
const SPECIAL_WORDS = ['gRPC', 'npm']; | ||
|
||
/** | ||
* This plugin converts commit messages to sentence case, for the benefit | ||
* of the generated CHANGELOG. | ||
*/ | ||
export class SentenceCase extends ManifestPlugin { | ||
specialWords: Set<string>; | ||
constructor( | ||
github: GitHub, | ||
targetBranch: string, | ||
repositoryConfig: RepositoryConfig, | ||
specialWords?: Array<string> | ||
) { | ||
super(github, targetBranch, repositoryConfig); | ||
this.specialWords = new Set( | ||
specialWords ? [...specialWords] : SPECIAL_WORDS | ||
); | ||
} | ||
|
||
/** | ||
* Perform post-processing on commits, e.g, sentence casing them. | ||
* @param {Commit[]} commits The set of commits that will feed into release pull request. | ||
* @returns {Commit[]} The modified commit objects. | ||
*/ | ||
processCommits(commits: Commit[]): Commit[] { | ||
this.logger.info(`SentenceCase processing ${commits.length} commits`); | ||
for (const commit of commits) { | ||
// Check whether commit is in conventional commit format, if it is | ||
// we'll split the string by type and description: | ||
if (commit.message.includes(':')) { | ||
let [prefix, suffix] = commit.message.split(':'); | ||
prefix += ': '; | ||
suffix = suffix.trim(); | ||
// Extract the first word from the rest of the string: | ||
const match = /\s|$/.exec(suffix); | ||
if (match) { | ||
const endFirstWord = match.index; | ||
const firstWord = suffix.slice(0, endFirstWord); | ||
suffix = suffix.slice(endFirstWord); | ||
// Put the string back together again: | ||
commit.message = `${prefix}${this.toUpperCase(firstWord)}${suffix}`; | ||
} | ||
} | ||
} | ||
return commits; | ||
} | ||
|
||
/* | ||
* Convert a string to upper case, taking into account a dictionary of | ||
* common lowercase words, e.g., gRPC, npm. | ||
* | ||
* @param {string} word The original word. | ||
* @returns {string} The word, now upper case. | ||
*/ | ||
toUpperCase(word: string): string { | ||
if (this.specialWords.has(word)) { | ||
return word; | ||
} | ||
if (word.match(/^[a-z]/)) { | ||
return word.charAt(0).toUpperCase() + word.slice(1); | ||
} else { | ||
return word; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
import {describe, it} from 'mocha'; | ||
import {SentenceCase} from '../../src/plugins/sentence-case'; | ||
import {expect} from 'chai'; | ||
|
||
import {GitHub} from '../../src/github'; | ||
|
||
describe('SentenceCase Plugin', () => { | ||
let github: GitHub; | ||
beforeEach(async () => { | ||
github = await GitHub.create({ | ||
owner: 'googleapis', | ||
repo: 'node-test-repo', | ||
defaultBranch: 'main', | ||
}); | ||
}); | ||
describe('processCommits', () => { | ||
it('converts description to sentence case', async () => { | ||
const plugin = new SentenceCase(github, 'main', {}); | ||
const commits = await plugin.processCommits([ | ||
{ | ||
sha: 'abc123', | ||
message: 'fix: hello world', | ||
}, | ||
{ | ||
sha: 'abc123', | ||
message: 'fix: Goodnight moon', | ||
}, | ||
]); | ||
expect(commits[0].message).to.equal('fix: Hello world'); | ||
expect(commits[1].message).to.equal('fix: Goodnight moon'); | ||
}); | ||
it('leaves reserved words lowercase', async () => { | ||
const plugin = new SentenceCase(github, 'main', {}); | ||
const commits = await plugin.processCommits([ | ||
{ | ||
sha: 'abc123', | ||
message: 'feat: gRPC can now handle proxies', | ||
}, | ||
{ | ||
sha: 'abc123', | ||
message: 'fix: npm now rocks', | ||
}, | ||
]); | ||
expect(commits[0].message).to.equal('feat: gRPC can now handle proxies'); | ||
expect(commits[1].message).to.equal('fix: npm now rocks'); | ||
}); | ||
it('handles sentences with now breaks', async () => { | ||
const plugin = new SentenceCase(github, 'main', {}); | ||
const commits = await plugin.processCommits([ | ||
{ | ||
sha: 'abc123', | ||
message: 'feat: beep-boop-hello', | ||
}, | ||
{ | ||
sha: 'abc123', | ||
message: 'fix:log4j.foo.bar', | ||
}, | ||
]); | ||
expect(commits[0].message).to.equal('feat: Beep-boop-hello'); | ||
expect(commits[1].message).to.equal('fix: Log4j.foo.bar'); | ||
}); | ||
}); | ||
it('allows a custom list of specialWords to be provided', async () => { | ||
const plugin = new SentenceCase(github, 'main', {}, ['hello']); | ||
const commits = await plugin.processCommits([ | ||
{ | ||
sha: 'abc123', | ||
message: 'fix: hello world', | ||
}, | ||
{ | ||
sha: 'abc123', | ||
message: 'fix: Goodnight moon', | ||
}, | ||
]); | ||
expect(commits[0].message).to.equal('fix: hello world'); | ||
expect(commits[1].message).to.equal('fix: Goodnight moon'); | ||
}); | ||
}); |