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:
parent
b2788ae3c6
commit
a1ff787715
|
@ -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'
|
||||
);
|
||||
});
|
||||
});
|
|
@ -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,
|
||||
userEmail: string
|
||||
): Promise<void> {
|
||||
await exec.exec('git', ['config', '--global', 'gc.auto', '0']);
|
||||
|
||||
let name = '';
|
||||
if (userName) {
|
||||
name = userName;
|
||||
} else {
|
||||
name = `${process.env.GITHUB_ACTOR}`;
|
||||
if (userName && !userEmail) {
|
||||
throw new Error('user_email is undefined');
|
||||
}
|
||||
await exec.exec('git', ['config', '--global', 'user.name', name]);
|
||||
|
||||
let email = '';
|
||||
if (userName !== '' && userEmail !== '') {
|
||||
email = userEmail;
|
||||
} else {
|
||||
email = `${process.env.GITHUB_ACTOR}@users.noreply.github.com`;
|
||||
if (!userName && userEmail) {
|
||||
throw new Error('user_name is undefined');
|
||||
}
|
||||
await exec.exec('git', ['config', '--global', 'user.email', email]);
|
||||
|
||||
return;
|
||||
await exec.exec('git', ['config', 'user.name', getUserName(userName)]);
|
||||
await exec.exec('git', ['config', 'user.email', getUserEmail(userEmail)]);
|
||||
}
|
||||
|
||||
export async function commit(
|
||||
|
|
14
src/main.ts
14
src/main.ts
|
@ -3,7 +3,7 @@ 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> {
|
||||
|
@ -11,15 +11,13 @@ export async function run(): Promise<void> {
|
|||
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);
|
||||
|
@ -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');
|
||||
|
||||
|
|
Loading…
Reference in New Issue