Skip to content

Commit

Permalink
feat: Add full_commit_message (#275)
Browse files Browse the repository at this point in the history
* feat: Add full_commit_message
* ci: Add full_commit_message
* docs: Add full_commit_message

cf. #274
  • Loading branch information
peaceiris committed May 4, 2020
1 parent 750c807 commit 0b7411e
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ jobs:
force_orphan: true
user_name: 'github-actions[bot]'
user_email: 'github-actions[bot]@users.noreply.github.com'
# commit_message: ${{ github.event.head_commit.message }}
full_commit_message: ${{ github.event.head_commit.message }}

- name: Deploy
if: |
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,18 @@ When we create a commit with a message `docs: Update some post`, a deployment co
commit_message: ${{ github.event.head_commit.message }}
```

To set a full custom commit message without a triggered commit hash,
use the `full_commit_message` option instead of the `commit_message` option.

```yaml
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public
full_commit_message: ${{ github.event.head_commit.message }}
```

### ⭐️ Create Git tag

Here is an example workflow.
Expand Down
10 changes: 10 additions & 0 deletions __tests__/get-inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function getInputsLog(authMethod: string, inps: Inputs): string {
[INFO] UserName: ${inps.UserName}
[INFO] UserEmail: ${inps.UserEmail}
[INFO] CommitMessage: ${inps.CommitMessage}
[INFO] FullCommitMessage: ${inps.FullCommitMessage}
[INFO] TagName: ${inps.TagName}
[INFO] TagMessage: ${inps.TagMessage}
[INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll}
Expand Down Expand Up @@ -117,6 +118,7 @@ describe('getInputs()', () => {
expect(inps.UserName).toMatch('');
expect(inps.UserEmail).toMatch('');
expect(inps.CommitMessage).toMatch('');
expect(inps.FullCommitMessage).toMatch('');
expect(inps.TagName).toMatch('');
expect(inps.TagMessage).toMatch('');
expect(inps.DisableNoJekyll).toBe(false);
Expand All @@ -136,6 +138,7 @@ describe('getInputs()', () => {
process.env['INPUT_USER_NAME'] = 'username';
process.env['INPUT_USER_EMAIL'] = 'github@github.com';
process.env['INPUT_COMMIT_MESSAGE'] = 'feat: Add new feature';
process.env['INPUT_FULL_COMMIT_MESSAGE'] = 'feat: Add new feature';
process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3';
process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3';
process.env['INPUT_DISABLE_NOJEKYLL'] = 'true';
Expand All @@ -155,12 +158,19 @@ describe('getInputs()', () => {
expect(inps.UserName).toMatch('username');
expect(inps.UserEmail).toMatch('github@github.com');
expect(inps.CommitMessage).toMatch('feat: Add new feature');
expect(inps.FullCommitMessage).toMatch('feat: Add new feature');
expect(inps.TagName).toMatch('deploy-v1.2.3');
expect(inps.TagMessage).toMatch('Deployment v1.2.3');
expect(inps.DisableNoJekyll).toBe(true);
expect(inps.CNAME).toMatch('github.com');
});

test('get spec inputs enable_jekyll', () => {
process.env['INPUT_ENABLE_JEKYLL'] = 'true';
const inps: Inputs = getInputs();
expect(inps.DisableNoJekyll).toBe(true);
});

test('throw error enable_jekyll or disable_nojekyll', () => {
process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key';
process.env['INPUT_ENABLE_JEKYLL'] = 'true';
Expand Down
58 changes: 57 additions & 1 deletion __tests__/git-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {getUserName, getUserEmail, setCommitAuthor} from '../src/git-utils';
import {
getUserName,
getUserEmail,
setCommitAuthor,
getCommitMessage
} from '../src/git-utils';
import {getWorkDirName, createWorkDir} from '../src/utils';
import {CmdResult} from '../src/interfaces';
import * as exec from '@actions/exec';
Expand Down Expand Up @@ -114,3 +119,54 @@ describe('setCommitAuthor()', () => {
);
});
});

describe('getCommitMessage()', () => {
test('get default message', () => {
const test = getCommitMessage('', '', '', 'actions/pages', 'commit_hash');
expect(test).toMatch('deploy: commit_hash');
});

test('get default message for external repository', () => {
const test = getCommitMessage(
'',
'',
'actions/actions.github.io',
'actions/pages',
'commit_hash'
);
expect(test).toMatch('deploy: actions/pages@commit_hash');
});

test('get custom message', () => {
const test = getCommitMessage(
'Custom msg',
'',
'',
'actions/pages',
'commit_hash'
);
expect(test).toMatch('Custom msg commit_hash');
});

test('get custom message for external repository', () => {
const test = getCommitMessage(
'Custom msg',
'',
'actions/actions.github.io',
'actions/pages',
'commit_hash'
);
expect(test).toMatch('Custom msg actions/pages@commit_hash');
});

test('get full custom message', () => {
const test = getCommitMessage(
'',
'Full custom msg',
'',
'actions/pages',
'commit_hash'
);
expect(test).toMatch('Full custom msg');
});
});
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ inputs:
description: 'Set Git user.email'
required: false
commit_message:
description: 'Set custom commit message'
description: 'Set a custom commit message with a triggered commit hash'
required: false
full_commit_message:
description: 'Set a custom full commit message without a triggered commit hash'
required: false
tag_name:
description: 'Set tag name'
Expand Down
2 changes: 2 additions & 0 deletions src/get-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function showInputs(inps: Inputs): void {
[INFO] UserName: ${inps.UserName}
[INFO] UserEmail: ${inps.UserEmail}
[INFO] CommitMessage: ${inps.CommitMessage}
[INFO] FullCommitMessage: ${inps.FullCommitMessage}
[INFO] TagName: ${inps.TagName}
[INFO] TagMessage: ${inps.TagMessage}
[INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll}
Expand Down Expand Up @@ -61,6 +62,7 @@ export function getInputs(): Inputs {
UserName: core.getInput('user_name'),
UserEmail: core.getInput('user_email'),
CommitMessage: core.getInput('commit_message'),
FullCommitMessage: core.getInput('full_commit_message'),
TagName: core.getInput('tag_name'),
TagMessage: core.getInput('tag_message'),
DisableNoJekyll: useBuiltinJekyll,
Expand Down
47 changes: 29 additions & 18 deletions src/git-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as github from '@actions/github';
import * as io from '@actions/io';
import path from 'path';
import fs from 'fs';
Expand Down Expand Up @@ -133,26 +132,38 @@ export async function setCommitAuthor(
await exec.exec('git', ['config', 'user.email', getUserEmail(userEmail)]);
}

export function getCommitMessage(
msg: string,
fullMsg: string,
extRepo: string,
baseRepo: string,
hash: string
): string {
const msgHash = ((): string => {
if (extRepo) {
return `${baseRepo}@${hash}`;
} else {
return hash;
}
})();

const subject = ((): string => {
if (fullMsg) {
return fullMsg;
} else if (msg) {
return `${msg} ${msgHash}`;
} else {
return `deploy: ${msgHash}`;
}
})();

return subject;
}

export async function commit(
allowEmptyCommit: boolean,
externalRepository: string,
message: string
msg: string
): Promise<void> {
let msg = '';
if (message) {
msg = message;
} else {
msg = 'deploy:';
}

const hash = `${process.env.GITHUB_SHA}`;
const baseRepo = `${github.context.repo.owner}/${github.context.repo.repo}`;
if (externalRepository) {
msg = `${msg} ${baseRepo}@${hash}`;
} else {
msg = `${msg} ${hash}`;
}

try {
if (allowEmptyCommit) {
await exec.exec('git', ['commit', '--allow-empty', '-m', `${msg}`]);
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Inputs {
readonly UserName: string;
readonly UserEmail: string;
readonly CommitMessage: string;
readonly FullCommitMessage: string;
readonly TagName: string;
readonly TagMessage: string;
readonly DisableNoJekyll: boolean;
Expand Down
21 changes: 17 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import {context} from '@actions/github';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as github from '@actions/github';
import {Inputs} from './interfaces';
import {showInputs, getInputs} from './get-inputs';
import {setTokens} from './set-tokens';
import {setRepo, setCommitAuthor, commit, push, pushTag} from './git-utils';
import {
setRepo,
setCommitAuthor,
getCommitMessage,
commit,
push,
pushTag
} from './git-utils';
import {getWorkDirName, addNoJekyll, addCNAME, skipOnFork} from './utils';

export async function run(): Promise<void> {
Expand Down Expand Up @@ -54,11 +62,16 @@ export async function run(): Promise<void> {
await exec.exec('git', ['remote', 'add', 'origin', remoteURL]);
await exec.exec('git', ['add', '--all']);
await setCommitAuthor(inps.UserName, inps.UserEmail);
await commit(
inps.AllowEmptyCommit,
const hash = `${process.env.GITHUB_SHA}`;
const baseRepo = `${github.context.repo.owner}/${github.context.repo.repo}`;
const commitMessage = getCommitMessage(
inps.CommitMessage,
inps.FullCommitMessage,
inps.ExternalRepository,
inps.CommitMessage
baseRepo,
hash
);
await commit(inps.AllowEmptyCommit, commitMessage);
await push(inps.PublishBranch, inps.ForceOrphan);
await pushTag(inps.TagName, inps.TagMessage);

Expand Down

0 comments on commit 0b7411e

Please sign in to comment.