fix: set commit author as local config (#152)

* fix: set commit author as local config

Close #151

Co-authored-by: Ryo Ota <nwtgck@nwtgck.org>
This commit is contained in:
Shohei Ueda 2020-03-12 22:58:12 +09:00 committed by GitHub
parent b2788ae3c6
commit a1ff787715
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 145 additions and 26 deletions

116
__tests__/git-utils.test.ts Normal file
View File

@ -0,0 +1,116 @@
import {getUserName, getUserEmail, setCommitAuthor} from '../src/git-utils';
import {getWorkDirName, createWorkDir} from '../src/utils';
import {CmdResult} from '../src/interfaces';
import * as exec from '@actions/exec';
beforeEach(() => {
jest.resetModules();
process.env['GITHUB_ACTOR'] = 'default-octocat';
process.env['GITHUB_REPOSITORY'] = 'owner/repo';
});
afterEach(() => {
delete process.env['GITHUB_ACTOR'];
delete process.env['GITHUB_REPOSITORY'];
});
describe('getUserName()', () => {
test('get default git user name', () => {
const userName = '';
const test = getUserName(userName);
expect(test).toMatch('default-octocat');
});
test('get custom git user name', () => {
const userName = 'custom-octocat';
const test = getUserName(userName);
expect(test).toMatch(userName);
});
});
describe('getUserEmail()', () => {
test('get default git user email', () => {
const userEmail = '';
const test = getUserEmail(userEmail);
expect(test).toMatch('default-octocat@users.noreply.github.com');
});
test('get custom git user email', () => {
const userEmail = 'custom-octocat@github.com';
const test = getUserEmail(userEmail);
expect(test).toMatch(userEmail);
});
});
describe('setCommitAuthor()', () => {
let workDirName = '';
(async (): Promise<void> => {
const date = new Date();
const unixTime = date.getTime();
workDirName = await getWorkDirName(`${unixTime}`);
})();
beforeEach(async () => {
await createWorkDir(workDirName);
process.chdir(workDirName);
await exec.exec('git', ['init']);
});
test('get default commit author', async () => {
const userName = '';
const userEmail = '';
const result: CmdResult = {
exitcode: 0,
output: ''
};
const options = {
listeners: {
stdout: (data: Buffer): void => {
result.output += data.toString();
}
}
};
await setCommitAuthor(userName, userEmail);
result.exitcode = await exec.exec('git', ['config', 'user.name'], options);
expect(result.output).toMatch('default-octocat');
result.exitcode = await exec.exec('git', ['config', 'user.email'], options);
expect(result.output).toMatch('default-octocat@users.noreply.github.com');
});
test('get custom commit author', async () => {
const userName = 'custom-octocat';
const userEmail = 'custom-octocat@github.com';
const result: CmdResult = {
exitcode: 0,
output: ''
};
const options = {
listeners: {
stdout: (data: Buffer): void => {
result.output += data.toString();
}
}
};
await setCommitAuthor(userName, userEmail);
result.exitcode = await exec.exec('git', ['config', 'user.name'], options);
expect(result.output).toMatch(userName);
result.exitcode = await exec.exec('git', ['config', 'user.email'], options);
expect(result.output).toMatch(userEmail);
});
test('throw error user_email is undefined', async () => {
const userName = 'custom-octocat';
const userEmail = '';
await expect(setCommitAuthor(userName, userEmail)).rejects.toThrowError(
'user_email is undefined'
);
});
test('throw error user_name is undefined', async () => {
const userName = '';
const userEmail = 'custom-octocat@github.com';
await expect(setCommitAuthor(userName, userEmail)).rejects.toThrowError(
'user_name is undefined'
);
});
});

View File

@ -103,29 +103,34 @@ export async function setRepo(
} }
} }
export async function setConfig( export function getUserName(userName: string): string {
if (userName) {
return userName;
} else {
return `${process.env.GITHUB_ACTOR}`;
}
}
export function getUserEmail(userEmail: string): string {
if (userEmail) {
return userEmail;
} else {
return `${process.env.GITHUB_ACTOR}@users.noreply.github.com`;
}
}
export async function setCommitAuthor(
userName: string, userName: string,
userEmail: string userEmail: string
): Promise<void> { ): Promise<void> {
await exec.exec('git', ['config', '--global', 'gc.auto', '0']); if (userName && !userEmail) {
throw new Error('user_email is undefined');
let name = '';
if (userName) {
name = userName;
} else {
name = `${process.env.GITHUB_ACTOR}`;
} }
await exec.exec('git', ['config', '--global', 'user.name', name]); if (!userName && userEmail) {
throw new Error('user_name is undefined');
let email = '';
if (userName !== '' && userEmail !== '') {
email = userEmail;
} else {
email = `${process.env.GITHUB_ACTOR}@users.noreply.github.com`;
} }
await exec.exec('git', ['config', '--global', 'user.email', email]); await exec.exec('git', ['config', 'user.name', getUserName(userName)]);
await exec.exec('git', ['config', 'user.email', getUserEmail(userEmail)]);
return;
} }
export async function commit( export async function commit(

View File

@ -3,7 +3,7 @@ import * as exec from '@actions/exec';
import {Inputs} from './interfaces'; import {Inputs} from './interfaces';
import {showInputs, getInputs} from './get-inputs'; import {showInputs, getInputs} from './get-inputs';
import {setTokens} from './set-tokens'; import {setTokens} from './set-tokens';
import * as git from './git-utils'; import {setRepo, setCommitAuthor, commit, push, pushTag} from './git-utils';
import {getWorkDirName, addNoJekyll, addCNAME} from './utils'; import {getWorkDirName, addNoJekyll, addCNAME} from './utils';
export async function run(): Promise<void> { export async function run(): Promise<void> {
@ -11,15 +11,13 @@ export async function run(): Promise<void> {
const inps: Inputs = getInputs(); const inps: Inputs = getInputs();
showInputs(inps); showInputs(inps);
await git.setConfig(inps.UserName, inps.UserEmail);
const remoteURL = await setTokens(inps); const remoteURL = await setTokens(inps);
core.debug(`[INFO] remoteURL: ${remoteURL}`); core.debug(`[INFO] remoteURL: ${remoteURL}`);
const date = new Date(); const date = new Date();
const unixTime = date.getTime(); const unixTime = date.getTime();
const workDir = await getWorkDirName(`${unixTime}`); const workDir = await getWorkDirName(`${unixTime}`);
await git.setRepo(inps, remoteURL, workDir); await setRepo(inps, remoteURL, workDir);
await addNoJekyll(workDir, inps.DisableNoJekyll, inps.PublishBranch); await addNoJekyll(workDir, inps.DisableNoJekyll, inps.PublishBranch);
await addCNAME(workDir, inps.CNAME); await addCNAME(workDir, inps.CNAME);
@ -31,14 +29,14 @@ export async function run(): Promise<void> {
} }
await exec.exec('git', ['remote', 'add', 'origin', remoteURL]); await exec.exec('git', ['remote', 'add', 'origin', remoteURL]);
await exec.exec('git', ['add', '--all']); await exec.exec('git', ['add', '--all']);
await setCommitAuthor(inps.UserName, inps.UserEmail);
await git.commit( await commit(
inps.AllowEmptyCommit, inps.AllowEmptyCommit,
inps.ExternalRepository, inps.ExternalRepository,
inps.CommitMessage inps.CommitMessage
); );
await git.push(inps.PublishBranch, inps.ForceOrphan); await push(inps.PublishBranch, inps.ForceOrphan);
await git.pushTag(inps.TagName, inps.TagMessage); await pushTag(inps.TagName, inps.TagMessage);
core.info('[INFO] Action successfully completed'); core.info('[INFO] Action successfully completed');