-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 (#13328)
* refactor: improve commit message functionality * refactor: fix test coverage * refactor: fix by comments * refactor: fix build * refactor: fix linting * refactor: fix export type * refactor: js private fields * refactor: static private fields * fix: lint * refactor: fix tsconfig * refactor: implement method normalizeInput * refactor: fix by comments * Update lib/workers/repository/model/commit-message.ts * refactor: fix by comments * refactor: use private typescript fields again * refactor: fix by comments Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
- Loading branch information
1 parent
be9902e
commit ed73d38
Showing
12 changed files
with
359 additions
and
112 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,5 @@ | ||
export interface CommitMessageJSON { | ||
body?: string; | ||
footer?: string; | ||
subject?: string; | ||
} |
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,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.type = this._config.semanticCommitType ?? ''; | ||
message.scope = this._config.semanticCommitScope ?? ''; | ||
|
||
return message; | ||
} | ||
|
||
private createCustomCommitMessage(): CustomCommitMessage { | ||
const message = new CustomCommitMessage(); | ||
message.prefix = this._config.commitMessagePrefix ?? ''; | ||
|
||
return message; | ||
} | ||
|
||
private get areSemanticCommitsEnabled(): boolean { | ||
return ( | ||
!this._config.commitMessagePrefix && | ||
this._config.semanticCommits === 'enabled' | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
52 changes: 52 additions & 0 deletions
52
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,52 @@ | ||
import { CustomCommitMessage } from './custom-commit-message'; | ||
|
||
describe('workers/repository/model/custom-commit-message', () => { | ||
describe('CustomCommitMessage', () => { | ||
it.each` | ||
subject | prefix | result | ||
${'test'} | ${''} | ${'Test'} | ||
${' test '} | ${' '} | ${'Test'} | ||
${'test'} | ${'fix'} | ${'fix: test'} | ||
${'test'} | ${'fix:'} | ${'fix: test'} | ||
${'Message With Extra Whitespaces '} | ${' refactor '} | ${'refactor: message With Extra Whitespaces'} | ||
`( | ||
'given subject $subject and prefix $prefix as arguments, returns $result', | ||
({ | ||
subject, | ||
prefix, | ||
result, | ||
}: { | ||
subject: string; | ||
prefix: string; | ||
result: string; | ||
}) => { | ||
const commitMessage = new CustomCommitMessage(); | ||
commitMessage.subject = subject; | ||
commitMessage.prefix = prefix; | ||
|
||
expect(commitMessage.toString()).toEqual(result); | ||
} | ||
); | ||
|
||
it('should provide ability to set body and footer', () => { | ||
const commitMessage = new CustomCommitMessage(); | ||
commitMessage.subject = 'subject'; | ||
commitMessage.body = 'body'; | ||
commitMessage.footer = 'footer'; | ||
|
||
expect(commitMessage.toJSON()).toEqual({ | ||
body: 'body', | ||
footer: 'footer', | ||
prefix: '', | ||
subject: 'subject', | ||
}); | ||
expect(commitMessage.toString()).toBe('Subject\n\nbody\n\nfooter'); | ||
}); | ||
|
||
it('should remove empty subject by default', () => { | ||
const commitMessage = new CustomCommitMessage(); | ||
|
||
expect(commitMessage.formatSubject()).toBe(''); | ||
}); | ||
}); | ||
}); |
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,27 @@ | ||
import type { CommitMessageJSON } from '../../../types'; | ||
import { CommitMessage } from './commit-message'; | ||
|
||
export interface CustomCommitMessageJSON extends CommitMessageJSON { | ||
prefix?: string; | ||
} | ||
|
||
export class CustomCommitMessage extends CommitMessage { | ||
private _prefix = ''; | ||
|
||
get prefix(): string { | ||
return this._prefix; | ||
} | ||
|
||
set prefix(value: string) { | ||
this._prefix = this.normalizeInput(value); | ||
} | ||
|
||
override toJSON(): CustomCommitMessageJSON { | ||
const json = super.toJSON(); | ||
|
||
return { | ||
...json, | ||
prefix: this._prefix, | ||
}; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
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,73 @@ | ||
import { SemanticCommitMessage } from './semantic-commit-message'; | ||
|
||
describe('workers/repository/model/semantic-commit-message', () => { | ||
it('should format message without prefix', () => { | ||
const message = new SemanticCommitMessage(); | ||
message.subject = 'test'; | ||
|
||
expect(message.toString()).toBe('Test'); | ||
}); | ||
|
||
it('should format sematic type', () => { | ||
const message = new SemanticCommitMessage(); | ||
message.subject = 'test'; | ||
message.type = ' fix '; | ||
|
||
expect(message.toString()).toBe('fix: test'); | ||
}); | ||
|
||
it('should format sematic prefix with scope', () => { | ||
const message = new SemanticCommitMessage(); | ||
message.subject = 'test'; | ||
message.type = ' fix '; | ||
message.scope = ' scope '; | ||
|
||
expect(message.toString()).toBe('fix(scope): test'); | ||
}); | ||
|
||
it('should create instance from string without scope', () => { | ||
const instance = SemanticCommitMessage.fromString('feat: ticket 123'); | ||
|
||
expect(SemanticCommitMessage.is(instance)).toBeTrue(); | ||
expect(instance.toJSON()).toEqual({ | ||
body: '', | ||
footer: '', | ||
scope: '', | ||
subject: 'ticket 123', | ||
type: 'feat', | ||
}); | ||
}); | ||
|
||
it('should create instance from string with scope', () => { | ||
const instance = SemanticCommitMessage.fromString( | ||
'fix(dashboard): ticket 123' | ||
); | ||
|
||
expect(SemanticCommitMessage.is(instance)).toBeTrue(); | ||
expect(instance.toJSON()).toEqual({ | ||
body: '', | ||
footer: '', | ||
scope: 'dashboard', | ||
subject: 'ticket 123', | ||
type: 'fix', | ||
}); | ||
}); | ||
|
||
it('should create instance from string with empty description', () => { | ||
const instance = SemanticCommitMessage.fromString('fix(deps): '); | ||
|
||
expect(SemanticCommitMessage.is(instance)).toBeTrue(); | ||
expect(instance.toJSON()).toEqual({ | ||
body: '', | ||
footer: '', | ||
scope: 'deps', | ||
subject: '', | ||
type: 'fix', | ||
}); | ||
}); | ||
|
||
it('should return undefined for invalid string', () => { | ||
const instance = SemanticCommitMessage.fromString('test'); | ||
expect(instance).toBeUndefined(); | ||
}); | ||
}); |
Oops, something went wrong.