-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix missing author in
git commit
(#386)
- Loading branch information
Showing
8 changed files
with
136 additions
and
84 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 |
---|---|---|
|
@@ -100,9 +100,9 @@ Array [ | |
"git", | ||
Array [ | ||
"-c", | ||
"user.name=\\"Søren Louv-Jansen\\"", | ||
"user.name=\\"Soren L\\"", | ||
"-c", | ||
"user.email=\\"soren[email protected]\\"", | ||
"user.email=\\"soren@louv.dk\\"", | ||
"cherry-pick", | ||
"-x", | ||
"mySha", | ||
|
@@ -112,6 +112,10 @@ Array [ | |
Array [ | ||
"git", | ||
Array [ | ||
"-c", | ||
"user.name=\\"Soren L\\"", | ||
"-c", | ||
"user.email=\\"[email protected]\\"", | ||
"commit", | ||
"--no-edit", | ||
], | ||
|
@@ -121,9 +125,9 @@ Array [ | |
"git", | ||
Array [ | ||
"-c", | ||
"user.name=\\"Søren Louv-Jansen\\"", | ||
"user.name=\\"Soren L\\"", | ||
"-c", | ||
"user.email=\\"soren[email protected]\\"", | ||
"user.email=\\"soren@louv.dk\\"", | ||
"cherry-pick", | ||
"-x", | ||
"mySha2", | ||
|
@@ -133,6 +137,10 @@ Array [ | |
Array [ | ||
"git", | ||
Array [ | ||
"-c", | ||
"user.name=\\"Soren L\\"", | ||
"-c", | ||
"user.email=\\"[email protected]\\"", | ||
"commit", | ||
"--no-edit", | ||
], | ||
|
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,42 @@ | ||
import { Commit } from '../entrypoint.module'; | ||
import { ValidConfigOptions } from '../options/options'; | ||
import { getGitConfig, getLocalSourceRepoPath } from './git'; | ||
|
||
export type GitConfigAuthor = { name?: string; email?: string }; | ||
export async function getGitConfigAuthor( | ||
options: ValidConfigOptions | ||
): Promise<GitConfigAuthor | undefined> { | ||
const localRepoPath = await getLocalSourceRepoPath(options); | ||
if (!localRepoPath) { | ||
return; | ||
} | ||
|
||
return { | ||
name: await getGitConfig({ dir: localRepoPath, key: 'user.name' }), | ||
email: await getGitConfig({ dir: localRepoPath, key: 'user.email' }), | ||
}; | ||
} | ||
|
||
export type CommitAuthor = Required<GitConfigAuthor>; | ||
export function getCommitAuthor({ | ||
options, | ||
commit, | ||
gitConfigAuthor, | ||
}: { | ||
options: ValidConfigOptions; | ||
commit: Commit; | ||
gitConfigAuthor?: GitConfigAuthor; | ||
}): CommitAuthor { | ||
if (options.resetAuthor) { | ||
return { | ||
name: options.authenticatedUsername, | ||
email: `<${options.authenticatedUsername}@users.noreply.github.com>`, | ||
}; | ||
} | ||
|
||
return { | ||
name: options.gitAuthorName ?? gitConfigAuthor?.name ?? commit.author.name, | ||
email: | ||
options.gitAuthorEmail ?? gitConfigAuthor?.email ?? commit.author.email, | ||
}; | ||
} |
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 |
---|---|---|
|
@@ -47,6 +47,8 @@ describe('cherrypickAndCreateTargetPullRequest', () => { | |
const options = { | ||
assignees: [] as string[], | ||
authenticatedUsername: 'sqren_authenticated', | ||
gitAuthorName: 'Soren L', | ||
gitAuthorEmail: '[email protected]', | ||
author: 'sqren', | ||
fork: true, | ||
interactive: true, | ||
|
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 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 |
---|---|---|
|
@@ -596,7 +596,9 @@ describe('commitChanges', () => { | |
.spyOn(childProcess, 'spawnPromise') | ||
.mockResolvedValueOnce({ stderr: '', stdout: '', code: 0, cmdArgs: [] }); | ||
|
||
await expect(await commitChanges(commit, options)).toBe(undefined); | ||
await expect(await commitChanges({ commit, commitAuthor, options })).toBe( | ||
undefined | ||
); | ||
}); | ||
|
||
it('should swallow error if changes have already been committed manaully', async () => { | ||
|
@@ -609,7 +611,9 @@ describe('commitChanges', () => { | |
}); | ||
|
||
jest.spyOn(childProcess, 'spawnPromise').mockRejectedValueOnce(err); | ||
await expect(await commitChanges(commit, options)).toBe(undefined); | ||
await expect(await commitChanges({ commit, commitAuthor, options })).toBe( | ||
undefined | ||
); | ||
}); | ||
|
||
describe('when commit fails due to empty message', () => { | ||
|
@@ -633,21 +637,36 @@ describe('commitChanges', () => { | |
cmdArgs: [], | ||
}); | ||
|
||
res = await commitChanges(commit, options); | ||
res = await commitChanges({ commit, commitAuthor, options }); | ||
}); | ||
|
||
it('should manually set the commit message', () => { | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
expect(spy).toHaveBeenCalledWith( | ||
expect(spy.mock.calls[0]).toEqual([ | ||
'git', | ||
['commit', '--no-edit'], | ||
expect.any(String) | ||
); | ||
expect(spy).toHaveBeenCalledWith( | ||
[ | ||
'-c', | ||
'user.name="Soren L"', | ||
'-c', | ||
'user.email="[email protected]"', | ||
'commit', | ||
'--no-edit', | ||
], | ||
expect.any(String), | ||
]); | ||
|
||
expect(spy.mock.calls[1]).toEqual([ | ||
'git', | ||
['commit', '--message=The original commit message'], | ||
expect.any(String) | ||
); | ||
[ | ||
'-c', | ||
'user.name="Soren L"', | ||
'-c', | ||
'user.email="[email protected]"', | ||
'commit', | ||
'--message=The original commit message', | ||
], | ||
expect.any(String), | ||
]); | ||
}); | ||
|
||
it('should handle the error and resolve successfully', async () => { | ||
|
@@ -661,7 +680,7 @@ describe('commitChanges', () => { | |
expect.assertions(1); | ||
|
||
await expect( | ||
commitChanges(commit, options) | ||
commitChanges({ commit, commitAuthor, options }) | ||
).rejects.toThrowErrorMatchingInlineSnapshot(`"another error"`); | ||
}); | ||
}); | ||
|
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