Skip to content

Commit

Permalink
fix: set commit author as local config (#152)
Browse files Browse the repository at this point in the history
* fix: set commit author as local config

Close #151

Co-authored-by: Ryo Ota <nwtgck@nwtgck.org>
  • Loading branch information
peaceiris and nwtgck authored Mar 12, 2020
1 parent b2788ae commit a1ff787
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 24 deletions.
116 changes: 116 additions & 0 deletions __tests__/git-utils.test.ts
Original file line number Diff line number Diff line change
@@ -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'
);
});
});
37 changes: 21 additions & 16 deletions src/git-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,34 @@ export async function setRepo(
}
}

export async function setConfig(
userName: string,
userEmail: string
): Promise<void> {
await exec.exec('git', ['config', '--global', 'gc.auto', '0']);

let name = '';
export function getUserName(userName: string): string {
if (userName) {
name = userName;
return userName;
} else {
name = `${process.env.GITHUB_ACTOR}`;
return `${process.env.GITHUB_ACTOR}`;
}
await exec.exec('git', ['config', '--global', 'user.name', name]);
}

let email = '';
if (userName !== '' && userEmail !== '') {
email = userEmail;
export function getUserEmail(userEmail: string): string {
if (userEmail) {
return userEmail;
} else {
email = `${process.env.GITHUB_ACTOR}@users.noreply.github.com`;
return `${process.env.GITHUB_ACTOR}@users.noreply.github.com`;
}
await exec.exec('git', ['config', '--global', 'user.email', email]);
}

return;
export async function setCommitAuthor(
userName: string,
userEmail: string
): Promise<void> {
if (userName && !userEmail) {
throw new Error('user_email is undefined');
}
if (!userName && userEmail) {
throw new Error('user_name is undefined');
}
await exec.exec('git', ['config', 'user.name', getUserName(userName)]);
await exec.exec('git', ['config', 'user.email', getUserEmail(userEmail)]);
}

export async function commit(
Expand Down
14 changes: 6 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ import * as exec from '@actions/exec';
import {Inputs} from './interfaces';
import {showInputs, getInputs} from './get-inputs';
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';

export async function run(): Promise<void> {
try {
const inps: Inputs = getInputs();
showInputs(inps);

await git.setConfig(inps.UserName, inps.UserEmail);

const remoteURL = await setTokens(inps);
core.debug(`[INFO] remoteURL: ${remoteURL}`);

const date = new Date();
const unixTime = date.getTime();
const workDir = await getWorkDirName(`${unixTime}`);
await git.setRepo(inps, remoteURL, workDir);
await setRepo(inps, remoteURL, workDir);

await addNoJekyll(workDir, inps.DisableNoJekyll, inps.PublishBranch);
await addCNAME(workDir, inps.CNAME);
Expand All @@ -31,14 +29,14 @@ export async function run(): Promise<void> {
}
await exec.exec('git', ['remote', 'add', 'origin', remoteURL]);
await exec.exec('git', ['add', '--all']);

await git.commit(
await setCommitAuthor(inps.UserName, inps.UserEmail);
await commit(
inps.AllowEmptyCommit,
inps.ExternalRepository,
inps.CommitMessage
);
await git.push(inps.PublishBranch, inps.ForceOrphan);
await git.pushTag(inps.TagName, inps.TagMessage);
await push(inps.PublishBranch, inps.ForceOrphan);
await pushTag(inps.TagName, inps.TagMessage);

core.info('[INFO] Action successfully completed');

Expand Down

0 comments on commit a1ff787

Please sign in to comment.