Skip to content

Commit

Permalink
Merge pull request #5 from technote-space/release/v0.0.4
Browse files Browse the repository at this point in the history
Release/v0.0.4
  • Loading branch information
technote-space authored Aug 25, 2019
2 parents ad8209b + e4c28a7 commit 93d8dfe
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
50 changes: 48 additions & 2 deletions __tests__/utils/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import path from 'path';
import nock from 'nock';
import tmp from 'tmp';
import {encodeContent} from '../util';
import {isTargetEvent, parseConfig, getCommitMessage, getCloneDepth, getWorkspace, getBuildCommands, isGitCloned, getGitUrl} from '../../src/utils/misc';
import {DEFAULT_COMMIT_MESSAGE, DEFAULT_CLONE_DEPTH} from '../../src/constant';
import {isTargetEvent, parseConfig, getCommitMessage, getCommitName, getCommitEmail, getCloneDepth, getWorkspace, getBuildCommands, isGitCloned, getGitUrl} from '../../src/utils/misc';
import {DEFAULT_COMMIT_MESSAGE, DEFAULT_COMMIT_NAME, DEFAULT_COMMIT_EMAIL, DEFAULT_CLONE_DEPTH} from '../../src/constant';

nock.disableNetConnect();

Expand Down Expand Up @@ -110,6 +110,52 @@ describe('getCommitMessage', () => {
});
});

describe('getCommitName', () => {
const OLD_ENV = process.env;

beforeEach(() => {
jest.resetModules();
process.env = {...OLD_ENV};
delete process.env.NODE_ENV;
});

afterEach(() => {
process.env = OLD_ENV;
});

it('should get commit name', () => {
process.env.INPUT_COMMIT_NAME = 'test';
expect(getCommitName()).toBe('test');
});

it('should get commit default name', () => {
expect(getCommitName()).toBe(DEFAULT_COMMIT_NAME);
});
});

describe('getCommitEmail', () => {
const OLD_ENV = process.env;

beforeEach(() => {
jest.resetModules();
process.env = {...OLD_ENV};
delete process.env.NODE_ENV;
});

afterEach(() => {
process.env = OLD_ENV;
});

it('should get commit email', () => {
process.env.INPUT_COMMIT_EMAIL = 'test';
expect(getCommitEmail()).toBe('test');
});

it('should get commit default email', () => {
expect(getCommitEmail()).toBe(DEFAULT_COMMIT_EMAIL);
});
});

describe('getCloneDepth', () => {
const OLD_ENV = process.env;

Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ inputs:
CLONE_DEPTH:
description: Git clone depth.
default: '50'
COMMIT_NAME:
description: Git commit name.
default: 'GitHub Actions'
COMMIT_EMAIL:
description: Git commit email.
default: 'example@example.com'
runs:
using: node12
main: lib/main.js
2 changes: 2 additions & 0 deletions src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const DEFAULT_COMMIT_MESSAGE = 'feat: Build for release';
export const DEFAULT_COMMIT_NAME = 'GitHub Actions';
export const DEFAULT_COMMIT_EMAIL = 'example@example.com';
export const DEFAULT_CLONE_DEPTH = '50';
export const TARGET_EVENT_NAME = 'release';
export const TARGET_EVENT_ACTION = 'published';
7 changes: 5 additions & 2 deletions src/utils/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import signale from 'signale';
import {exec} from 'child_process';
import {Context} from '@actions/github/lib/context';
import {isGitCloned, getGitUrl, getBuildCommands, getCloneDepth, getWorkspace, getCommitMessage} from './misc';
import {isGitCloned, getGitUrl, getBuildCommands, getCloneDepth, getWorkspace, getCommitMessage, getCommitName, getCommitEmail} from './misc';

export const clone = async (context: Context) => {
if (isGitCloned()) return;
Expand Down Expand Up @@ -31,7 +31,10 @@ export const getDiffFiles = async () => {
export const commit = async () => {
const workspace = getWorkspace();
const message = getCommitMessage();
await execAsync(`git -C ${workspace} commit -m "${message}"`);
const name = getCommitName();
const email = getCommitEmail();

await execAsync(`git -C ${workspace} -c user.name="${name}" -c user.email="${email}" commit -m "${message}"`);
return execAsync(`git -C ${workspace} rev-parse HEAD`);
};

Expand Down
6 changes: 5 additions & 1 deletion src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import yaml from 'js-yaml';
import {getInput} from '@actions/core' ;
import {Context} from '@actions/github/lib/context';
import {TARGET_EVENT_NAME, TARGET_EVENT_ACTION, DEFAULT_COMMIT_MESSAGE, DEFAULT_CLONE_DEPTH} from '../constant';
import {TARGET_EVENT_NAME, TARGET_EVENT_ACTION, DEFAULT_COMMIT_MESSAGE, DEFAULT_CLONE_DEPTH, DEFAULT_COMMIT_NAME, DEFAULT_COMMIT_EMAIL} from '../constant';

export const isTargetEvent = (context: Context) => TARGET_EVENT_NAME === context.eventName && TARGET_EVENT_ACTION === context.payload.action;

Expand All @@ -21,6 +21,10 @@ export const getBuildCommands = () => {

export const getCommitMessage = () => getInput('COMMIT_MESSAGE') || DEFAULT_COMMIT_MESSAGE;

export const getCommitName = () => getInput('COMMIT_NAME') || DEFAULT_COMMIT_NAME;

export const getCommitEmail = () => getInput('COMMIT_EMAIL') || DEFAULT_COMMIT_EMAIL;

export const getCloneDepth = () => getInput('CLONE_DEPTH') || DEFAULT_CLONE_DEPTH;

export const getWorkspace = () => process.env.GITHUB_WORKSPACE || '';

0 comments on commit 93d8dfe

Please sign in to comment.