-
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 #119 from rkotze/repo-author-list
Git mob core extract the list of contributors and filter by author name or email.
- Loading branch information
Showing
14 changed files
with
236 additions
and
142 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
74 changes: 74 additions & 0 deletions
74
packages/git-mob-core/src/git-mob-api/git-authors/repo-author-list.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,74 @@ | ||
import os from 'node:os'; | ||
import { getRepoAuthors } from '../exec-command'; | ||
import { Author } from '../author'; | ||
import { repoAuthorList } from './repo-author-list'; | ||
|
||
jest.mock('../exec-command'); | ||
const mockedGetRepoAuthors = jest.mocked(getRepoAuthors); | ||
|
||
describe('Extract repository authors', function () { | ||
it('Given a list of authors extract the name and email', async function () { | ||
mockedGetRepoAuthors.mockResolvedValueOnce( | ||
` 33\tRichard Kotze <rkotze@email.com>${os.EOL} 53\tTony Stark <tony@stark.com>` | ||
); | ||
const listOfAuthors = await repoAuthorList(); | ||
expect(listOfAuthors).toEqual([ | ||
new Author('rkrk', 'Richard Kotze', 'rkotze@email.com'), | ||
new Author('tsto', 'Tony Stark', 'tony@stark.com'), | ||
]); | ||
}); | ||
|
||
it('author has one name', async function () { | ||
mockedGetRepoAuthors.mockResolvedValueOnce( | ||
` 33\tRichard <rkotze@email.com>${os.EOL} 53\tTony Stark <tony@stark.com>` | ||
); | ||
const listOfAuthors = await repoAuthorList(); | ||
expect(listOfAuthors).toEqual([ | ||
new Author('rrk', 'Richard', 'rkotze@email.com'), | ||
new Author('tsto', 'Tony Stark', 'tony@stark.com'), | ||
]); | ||
}); | ||
|
||
it('author uses a private GitHub email', async function () { | ||
mockedGetRepoAuthors.mockResolvedValueOnce( | ||
` 33\tRichard <rkotze@email.com>${os.EOL} 53\tTony Stark <20342323+tony[bot]@users.noreply.github.com>` | ||
); | ||
const listOfAuthors = await repoAuthorList(); | ||
expect(listOfAuthors).toEqual([ | ||
new Author('rrk', 'Richard', 'rkotze@email.com'), | ||
new Author( | ||
'ts20', | ||
'Tony Stark', | ||
'20342323+tony[bot]@users.noreply.github.com' | ||
), | ||
]); | ||
}); | ||
|
||
it('only one author on repository', async function () { | ||
mockedGetRepoAuthors.mockResolvedValueOnce( | ||
` 33\tRichard Kotze <rkotze@email.com>` | ||
); | ||
const listOfAuthors = await repoAuthorList(); | ||
expect(listOfAuthors).toEqual([ | ||
new Author('rkrk', 'Richard Kotze', 'rkotze@email.com'), | ||
]); | ||
}); | ||
|
||
it('author has special characters in name', async function () { | ||
mockedGetRepoAuthors.mockResolvedValueOnce( | ||
` 33\tRic<C4><8D>rd Kotze <rkotze@email.com>` | ||
); | ||
const listOfAuthors = await repoAuthorList(); | ||
expect(listOfAuthors).toEqual([ | ||
new Author('rkrk', 'Ric<C4><8D>rd Kotze', 'rkotze@email.com'), | ||
]); | ||
}); | ||
|
||
it('exclude if fails to match author pattern in list', async function () { | ||
mockedGetRepoAuthors.mockResolvedValueOnce( | ||
` 33\tRichard Kotze <rkotze.email.com` | ||
); | ||
const listOfAuthors = await repoAuthorList(); | ||
expect(listOfAuthors).toEqual(undefined); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
packages/git-mob-core/src/git-mob-api/git-authors/repo-author-list.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,36 @@ | ||
import { EOL } from 'node:os'; | ||
import { Author } from '../author'; | ||
import { getRepoAuthors } from '../exec-command'; | ||
|
||
export async function repoAuthorList( | ||
authorFilter?: string | ||
): Promise<Author[] | undefined> { | ||
const repoAuthorsString = await getRepoAuthors(authorFilter); | ||
const splitEndOfLine = repoAuthorsString.split(EOL); | ||
const authorList = splitEndOfLine | ||
.map(createRepoAuthor) | ||
.filter(author => author !== undefined) as Author[]; | ||
|
||
if (authorList.length > 0) return authorList; | ||
} | ||
|
||
function createRepoAuthor(authorString: string) { | ||
const regexList = /\d+\t(.+)\s<(.+)>/; | ||
const authorArray = regexList.exec(authorString); | ||
if (authorArray !== null) { | ||
const [, name, email] = authorArray; | ||
return new Author(genKey(name, email), name, email); | ||
} | ||
} | ||
|
||
function genKey(name: string, email: string) { | ||
const nameInitials = name | ||
.toLowerCase() | ||
.split(' ') | ||
.reduce(function (acc, cur) { | ||
return acc + cur[0]; | ||
}, ''); | ||
|
||
const domainFirstTwoLetters = email.slice(0, 2); | ||
return nameInitials + domainFirstTwoLetters; | ||
} |
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { EOL } from 'node:os'; | ||
import test from 'ava'; | ||
import { exec } from '../test-helpers/index.js'; | ||
|
||
test('Suggests coauthors using repo contributors', t => { | ||
const { stdout } = exec('git suggest-coauthors'); | ||
|
||
t.regex(stdout, /Here are some suggestions/); | ||
t.regex(stdout, /git add-coauthor rkri "Richard Kotze" richkotze@outlook.com/); | ||
t.regex(stdout, /Paste any line above/); | ||
}); | ||
|
||
test('Filter suggestions of coauthors', t => { | ||
const { stdout } = exec('git suggest-coauthors dennis i'); | ||
|
||
t.regex(stdout, /git add-coauthor diid "Dennis Ideler" ideler.dennis@gmail.com/); | ||
t.is(stdout.split(EOL).filter(a => a.includes('git add-coauthor')).length, 2); | ||
}); | ||
|
||
test('Prints help message', t => { | ||
const { stdout } = exec('git suggest-coauthors -h'); | ||
|
||
t.regex(stdout, /usage/i); | ||
t.regex(stdout, /options/i); | ||
t.regex(stdout, /example/i); | ||
}); |
Oops, something went wrong.