-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from rkotze/refactor-message-format
Refactor to have a public message format function
- Loading branch information
Showing
10 changed files
with
128 additions
and
25 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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { type BasicResponse, httpFetch } from '../fetch/http-fetch'; | ||
import { AuthorTrailers } from '../git-message/message-formatter'; | ||
import { fetchGitHubAuthors, searchGitHubAuthors } from './fetch-github-authors'; | ||
|
||
jest.mock('../fetch/http-fetch'); | ||
|
@@ -68,6 +69,7 @@ test('Query for one GitHub user and return in AuthorList', async () => { | |
key: 'dideler', | ||
name: 'Dennis', | ||
email: '[email protected]', | ||
trailer: AuthorTrailers.CoAuthorBy, | ||
}, | ||
]); | ||
}); | ||
|
@@ -87,11 +89,13 @@ test('Query for two GitHub users and build AuthorList', async () => { | |
key: 'dideler', | ||
name: 'Dennis', | ||
email: '[email protected]', | ||
trailer: AuthorTrailers.CoAuthorBy, | ||
}, | ||
{ | ||
key: 'rkotze', | ||
name: 'Richard Kotze', | ||
email: '[email protected]', | ||
trailer: AuthorTrailers.CoAuthorBy, | ||
}, | ||
]); | ||
}); | ||
|
@@ -112,6 +116,7 @@ test('Handle GitHub user with no name', async () => { | |
key: 'kotze', | ||
name: 'kotze', | ||
email: '[email protected]', | ||
trailer: AuthorTrailers.CoAuthorBy, | ||
}, | ||
]); | ||
}); | ||
|
@@ -159,11 +164,13 @@ test('Search for users by name', async () => { | |
key: 'dideler', | ||
name: 'Dennis', | ||
email: '[email protected]', | ||
trailer: AuthorTrailers.CoAuthorBy, | ||
}, | ||
{ | ||
key: 'rkotze', | ||
name: 'Richard Kotze', | ||
email: '[email protected]', | ||
trailer: AuthorTrailers.CoAuthorBy, | ||
}, | ||
]); | ||
}); |
12 changes: 7 additions & 5 deletions
12
packages/git-mob-core/src/git-mob-api/git-message/index.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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
import { EOL } from 'node:os'; | ||
import { jest } from '@jest/globals'; | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
import { Author } from '../author.js'; | ||
import { gitMessage } from './index.js'; | ||
|
||
jest.mock('node:fs/promises'); | ||
|
||
test('Append co-authors to .gitmessage append file mock', async () => { | ||
const appendMock = jest.fn(async () => undefined); | ||
const message = gitMessage('.git/.gitmessage', appendMock); | ||
const message = gitMessage('.fake/.gitmessage'); | ||
await message.writeCoAuthors([ | ||
new Author('jd', 'Jane Doe', '[email protected]'), | ||
new Author('fb', 'Frances Bar', '[email protected]'), | ||
]); | ||
|
||
expect(appendMock).toHaveBeenCalledTimes(1); | ||
expect(appendMock).toHaveBeenCalledWith( | ||
expect(readFile).toHaveBeenCalledTimes(1); | ||
expect(writeFile).toHaveBeenCalledTimes(1); | ||
expect(writeFile).toHaveBeenCalledWith( | ||
expect.stringContaining('.gitmessage'), | ||
[ | ||
EOL, | ||
|
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
68 changes: 68 additions & 0 deletions
68
packages/git-mob-core/src/git-mob-api/git-message/message-formatter.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,68 @@ | ||
import { EOL } from 'node:os'; | ||
import { Author } from '../author'; | ||
import { messageFormatter } from './message-formatter'; | ||
|
||
test('MessageFormatter: No authors to append to git message', () => { | ||
const txt = `git message`; | ||
const message = messageFormatter(txt, []); | ||
|
||
expect(message).toBe(txt); | ||
}); | ||
|
||
test('MessageFormatter: Append co-authors to git message', () => { | ||
const txt = `git message`; | ||
const message = messageFormatter(txt, [ | ||
new Author('jd', 'Jane Doe', '[email protected]'), | ||
new Author('fb', 'Frances Bar', '[email protected]'), | ||
]); | ||
|
||
expect(message).toBe( | ||
[ | ||
txt, | ||
EOL, | ||
EOL, | ||
'Co-authored-by: Jane Doe <[email protected]>', | ||
EOL, | ||
'Co-authored-by: Frances Bar <[email protected]>', | ||
].join('') | ||
); | ||
}); | ||
|
||
test('MessageFormatter: Replace co-author in the git message', () => { | ||
const firstLine = 'git message'; | ||
const txt = [ | ||
firstLine, | ||
EOL, | ||
EOL, | ||
'Co-authored-by: Jane Doe <[email protected]>', | ||
].join(''); | ||
const message = messageFormatter(txt, [ | ||
new Author('fb', 'Frances Bar', '[email protected]'), | ||
]); | ||
|
||
expect(message).toBe( | ||
[ | ||
firstLine, | ||
EOL, | ||
EOL, | ||
'Co-authored-by: Frances Bar <[email protected]>', | ||
].join('') | ||
); | ||
}); | ||
|
||
test('MessageFormatter: Replace co-author in the git message with no line break', () => { | ||
const firstLine = 'git message'; | ||
const txt = [firstLine, 'Co-authored-by: Jane Doe <[email protected]>'].join(''); | ||
const message = messageFormatter(txt, [ | ||
new Author('fb', 'Frances Bar', '[email protected]'), | ||
]); | ||
|
||
expect(message).toBe( | ||
[ | ||
firstLine, | ||
EOL, | ||
EOL, | ||
'Co-authored-by: Frances Bar <[email protected]>', | ||
].join('') | ||
); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/git-mob-core/src/git-mob-api/git-message/message-formatter.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,19 @@ | ||
import { EOL } from 'node:os'; | ||
import { type Author } from '../author'; | ||
|
||
export enum AuthorTrailers { | ||
CoAuthorBy = 'Co-authored-by:', | ||
} | ||
|
||
export function messageFormatter(txt: string, authors: Author[]): string { | ||
const trailers = AuthorTrailers.CoAuthorBy; | ||
const regex = new RegExp(`(\r\n|\r|\n){0,2}(${trailers}).*`, 'g'); | ||
const message = txt.replaceAll(regex, ''); | ||
|
||
if (authors && authors.length > 0) { | ||
const authorTrailerTxt = authors.map(author => author.format()).join(EOL); | ||
return [message, EOL, EOL, authorTrailerTxt].join(''); | ||
} | ||
|
||
return message; | ||
} |
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 |
---|---|---|
|
@@ -225,7 +225,7 @@ $ git add-coauthor bb "Barry Butterworth" [email protected] | |
### Suggest co-authors | ||
Suggest co-authors to save based on contributors to the current Git repository. | ||
Suggest co-authors from contributors in local Git repository and add to `.git-coauthors` file. | ||
Optional author filter by name or email. | ||
|