-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve commit message functionality
- Loading branch information
1 parent
f7869e1
commit 91bbca5
Showing
9 changed files
with
304 additions
and
93 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { RenovateSharedConfig } from '../../../config/types'; | ||
import type { CommitMessage } from './commit-message'; | ||
import { CustomCommitMessage } from './custom-commit-message'; | ||
import { SemanticCommitMessage } from './semantic-commit-message'; | ||
|
||
type CommitMessageConfig = Pick< | ||
RenovateSharedConfig, | ||
| 'commitMessagePrefix' | ||
| 'semanticCommits' | ||
| 'semanticCommitScope' | ||
| 'semanticCommitType' | ||
>; | ||
|
||
export class CommitMessageFactory { | ||
private readonly config: CommitMessageConfig; | ||
|
||
constructor(config: CommitMessageConfig) { | ||
this.config = config; | ||
} | ||
|
||
create(): CommitMessage { | ||
const message = this.areSemanticCommitsEnabled | ||
? this.createSemanticCommitMessage() | ||
: this.createCustomCommitMessage(); | ||
|
||
return message; | ||
} | ||
|
||
private createSemanticCommitMessage(): SemanticCommitMessage { | ||
const message = new SemanticCommitMessage(); | ||
|
||
message.setType(this.config.semanticCommitType); | ||
message.setScope(this.config.semanticCommitScope); | ||
|
||
return message; | ||
} | ||
|
||
private createCustomCommitMessage(): CustomCommitMessage { | ||
const message = new CustomCommitMessage(); | ||
message.setPrefix(this.config.commitMessagePrefix); | ||
|
||
return message; | ||
} | ||
|
||
private get areSemanticCommitsEnabled(): boolean { | ||
return ( | ||
!this.config.commitMessagePrefix && | ||
this.config.semanticCommits === 'enabled' | ||
); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
lib/workers/repository/model/custom-commit-message.spec.ts
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,40 @@ | ||
import { CustomCommitMessage } from './custom-commit-message'; | ||
|
||
describe('workers/repository/model/custom-commit-message', () => { | ||
describe('CustomCommitMessage', () => { | ||
const TEST_CASES: ReadonlyArray< | ||
[message: string, prefix: string | undefined, result: string] | ||
> = [ | ||
['test', undefined, 'Test'], | ||
['test', '', 'Test'], | ||
[' test ', ' ', 'Test'], | ||
['test', 'fix', 'fix: test'], | ||
['test', 'fix:', 'fix: test'], | ||
[ | ||
'Message With Extra Whitespaces ', | ||
' refactor ', | ||
'refactor: message With Extra Whitespaces', | ||
], | ||
]; | ||
|
||
it.each(TEST_CASES)( | ||
'given %p and %p as arguments, returns %p', | ||
(subject, prefix, result) => { | ||
const commitMessage = new CustomCommitMessage(); | ||
commitMessage.setSubject(subject); | ||
commitMessage.setPrefix(prefix); | ||
|
||
expect(commitMessage.toString()).toEqual(result); | ||
} | ||
); | ||
|
||
it('should provide ability to set body and footer', () => { | ||
const commitMessage = new CustomCommitMessage(); | ||
commitMessage.setSubject('subject'); | ||
commitMessage.setBody('body'); | ||
commitMessage.setFooter('footer'); | ||
|
||
expect(commitMessage.toString()).toBe('Subject\n\nbody\n\nfooter'); | ||
}); | ||
}); | ||
}); |
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,26 @@ | ||
import { CommitMessage, CommitMessageJSON } from './commit-message'; | ||
|
||
export interface CustomCommitMessageJSON extends CommitMessageJSON { | ||
prefix?: string; | ||
} | ||
|
||
export class CustomCommitMessage extends CommitMessage { | ||
private _prefix?: string; | ||
|
||
setPrefix(prefix?: string): void { | ||
this._prefix = prefix?.trim(); | ||
} | ||
|
||
override toJSON(): CustomCommitMessageJSON { | ||
const json = super.toJSON(); | ||
|
||
return { | ||
...json, | ||
prefix: this._prefix, | ||
}; | ||
} | ||
|
||
protected get prefix(): string { | ||
return this._prefix; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
lib/workers/repository/model/semantic-commit-message.spec.ts
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,59 @@ | ||
import { SemanticCommitMessage } from './semantic-commit-message'; | ||
|
||
describe('workers/repository/model/semantic-commit-message', () => { | ||
it('should format message without prefix', () => { | ||
const message = new SemanticCommitMessage(); | ||
message.setSubject('test'); | ||
|
||
expect(message.toString()).toBe('Test'); | ||
}); | ||
|
||
it('should format sematic type', () => { | ||
const message = new SemanticCommitMessage(); | ||
message.setSubject('test'); | ||
message.setType(' fix '); | ||
|
||
expect(message.toString()).toBe('fix: test'); | ||
}); | ||
|
||
it('should format sematic prefix with scope', () => { | ||
const message = new SemanticCommitMessage(); | ||
message.setSubject('test'); | ||
message.setType(' fix '); | ||
message.setScope(' scope '); | ||
|
||
expect(message.toString()).toBe('fix(scope): test'); | ||
}); | ||
|
||
it('should create instance from string without scope', () => { | ||
const instance = SemanticCommitMessage.fromString('feat: ticket 123'); | ||
const json = instance.toJSON(); | ||
|
||
expect(SemanticCommitMessage.is(instance)).toBeTrue(); | ||
expect(json.type).toBe('feat'); | ||
expect(json.scope).toBeUndefined(); | ||
expect(json.subject).toBe('ticket 123'); | ||
}); | ||
|
||
it('should create instance from string with scope', () => { | ||
const instance = SemanticCommitMessage.fromString( | ||
'fix(dashboard): ticket 123' | ||
); | ||
const json = instance.toJSON(); | ||
|
||
expect(SemanticCommitMessage.is(instance)).toBeTrue(); | ||
expect(json.type).toBe('fix'); | ||
expect(json.scope).toBe('dashboard'); | ||
expect(json.subject).toBe('ticket 123'); | ||
}); | ||
|
||
it('should create instance from string with empty description', () => { | ||
const instance = SemanticCommitMessage.fromString('fix(deps): '); | ||
const json = instance.toJSON(); | ||
|
||
expect(SemanticCommitMessage.is(instance)).toBeTrue(); | ||
expect(json.type).toBe('fix'); | ||
expect(json.scope).toBe('deps'); | ||
expect(json.subject).toBe(''); | ||
}); | ||
}); |
Oops, something went wrong.