Skip to content

Commit 0b7411e

Browse files
authored
feat: Add full_commit_message (#275)
* feat: Add full_commit_message * ci: Add full_commit_message * docs: Add full_commit_message cf. #274
1 parent 750c807 commit 0b7411e

File tree

9 files changed

+133
-25
lines changed

9 files changed

+133
-25
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ jobs:
131131
force_orphan: true
132132
user_name: 'github-actions[bot]'
133133
user_email: 'github-actions[bot]@users.noreply.github.com'
134-
# commit_message: ${{ github.event.head_commit.message }}
134+
full_commit_message: ${{ github.event.head_commit.message }}
135135

136136
- name: Deploy
137137
if: |

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ When we create a commit with a message `docs: Update some post`, a deployment co
397397
commit_message: ${{ github.event.head_commit.message }}
398398
```
399399

400+
To set a full custom commit message without a triggered commit hash,
401+
use the `full_commit_message` option instead of the `commit_message` option.
402+
403+
```yaml
404+
- name: Deploy
405+
uses: peaceiris/actions-gh-pages@v3
406+
with:
407+
github_token: ${{ secrets.GITHUB_TOKEN }}
408+
publish_dir: ./public
409+
full_commit_message: ${{ github.event.head_commit.message }}
410+
```
411+
400412
### ⭐️ Create Git tag
401413

402414
Here is an example workflow.

__tests__/get-inputs.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function getInputsLog(authMethod: string, inps: Inputs): string {
5151
[INFO] UserName: ${inps.UserName}
5252
[INFO] UserEmail: ${inps.UserEmail}
5353
[INFO] CommitMessage: ${inps.CommitMessage}
54+
[INFO] FullCommitMessage: ${inps.FullCommitMessage}
5455
[INFO] TagName: ${inps.TagName}
5556
[INFO] TagMessage: ${inps.TagMessage}
5657
[INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll}
@@ -117,6 +118,7 @@ describe('getInputs()', () => {
117118
expect(inps.UserName).toMatch('');
118119
expect(inps.UserEmail).toMatch('');
119120
expect(inps.CommitMessage).toMatch('');
121+
expect(inps.FullCommitMessage).toMatch('');
120122
expect(inps.TagName).toMatch('');
121123
expect(inps.TagMessage).toMatch('');
122124
expect(inps.DisableNoJekyll).toBe(false);
@@ -136,6 +138,7 @@ describe('getInputs()', () => {
136138
process.env['INPUT_USER_NAME'] = 'username';
137139
process.env['INPUT_USER_EMAIL'] = '[email protected]';
138140
process.env['INPUT_COMMIT_MESSAGE'] = 'feat: Add new feature';
141+
process.env['INPUT_FULL_COMMIT_MESSAGE'] = 'feat: Add new feature';
139142
process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3';
140143
process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3';
141144
process.env['INPUT_DISABLE_NOJEKYLL'] = 'true';
@@ -155,12 +158,19 @@ describe('getInputs()', () => {
155158
expect(inps.UserName).toMatch('username');
156159
expect(inps.UserEmail).toMatch('[email protected]');
157160
expect(inps.CommitMessage).toMatch('feat: Add new feature');
161+
expect(inps.FullCommitMessage).toMatch('feat: Add new feature');
158162
expect(inps.TagName).toMatch('deploy-v1.2.3');
159163
expect(inps.TagMessage).toMatch('Deployment v1.2.3');
160164
expect(inps.DisableNoJekyll).toBe(true);
161165
expect(inps.CNAME).toMatch('github.com');
162166
});
163167

168+
test('get spec inputs enable_jekyll', () => {
169+
process.env['INPUT_ENABLE_JEKYLL'] = 'true';
170+
const inps: Inputs = getInputs();
171+
expect(inps.DisableNoJekyll).toBe(true);
172+
});
173+
164174
test('throw error enable_jekyll or disable_nojekyll', () => {
165175
process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key';
166176
process.env['INPUT_ENABLE_JEKYLL'] = 'true';

__tests__/git-utils.test.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import {getUserName, getUserEmail, setCommitAuthor} from '../src/git-utils';
1+
import {
2+
getUserName,
3+
getUserEmail,
4+
setCommitAuthor,
5+
getCommitMessage
6+
} from '../src/git-utils';
27
import {getWorkDirName, createWorkDir} from '../src/utils';
38
import {CmdResult} from '../src/interfaces';
49
import * as exec from '@actions/exec';
@@ -114,3 +119,54 @@ describe('setCommitAuthor()', () => {
114119
);
115120
});
116121
});
122+
123+
describe('getCommitMessage()', () => {
124+
test('get default message', () => {
125+
const test = getCommitMessage('', '', '', 'actions/pages', 'commit_hash');
126+
expect(test).toMatch('deploy: commit_hash');
127+
});
128+
129+
test('get default message for external repository', () => {
130+
const test = getCommitMessage(
131+
'',
132+
'',
133+
'actions/actions.github.io',
134+
'actions/pages',
135+
'commit_hash'
136+
);
137+
expect(test).toMatch('deploy: actions/pages@commit_hash');
138+
});
139+
140+
test('get custom message', () => {
141+
const test = getCommitMessage(
142+
'Custom msg',
143+
'',
144+
'',
145+
'actions/pages',
146+
'commit_hash'
147+
);
148+
expect(test).toMatch('Custom msg commit_hash');
149+
});
150+
151+
test('get custom message for external repository', () => {
152+
const test = getCommitMessage(
153+
'Custom msg',
154+
'',
155+
'actions/actions.github.io',
156+
'actions/pages',
157+
'commit_hash'
158+
);
159+
expect(test).toMatch('Custom msg actions/pages@commit_hash');
160+
});
161+
162+
test('get full custom message', () => {
163+
const test = getCommitMessage(
164+
'',
165+
'Full custom msg',
166+
'',
167+
'actions/pages',
168+
'commit_hash'
169+
);
170+
expect(test).toMatch('Full custom msg');
171+
});
172+
});

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ inputs:
4747
description: 'Set Git user.email'
4848
required: false
4949
commit_message:
50-
description: 'Set custom commit message'
50+
description: 'Set a custom commit message with a triggered commit hash'
51+
required: false
52+
full_commit_message:
53+
description: 'Set a custom full commit message without a triggered commit hash'
5154
required: false
5255
tag_name:
5356
description: 'Set tag name'

src/get-inputs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function showInputs(inps: Inputs): void {
2222
[INFO] UserName: ${inps.UserName}
2323
[INFO] UserEmail: ${inps.UserEmail}
2424
[INFO] CommitMessage: ${inps.CommitMessage}
25+
[INFO] FullCommitMessage: ${inps.FullCommitMessage}
2526
[INFO] TagName: ${inps.TagName}
2627
[INFO] TagMessage: ${inps.TagMessage}
2728
[INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll}
@@ -61,6 +62,7 @@ export function getInputs(): Inputs {
6162
UserName: core.getInput('user_name'),
6263
UserEmail: core.getInput('user_email'),
6364
CommitMessage: core.getInput('commit_message'),
65+
FullCommitMessage: core.getInput('full_commit_message'),
6466
TagName: core.getInput('tag_name'),
6567
TagMessage: core.getInput('tag_message'),
6668
DisableNoJekyll: useBuiltinJekyll,

src/git-utils.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as core from '@actions/core';
22
import * as exec from '@actions/exec';
3-
import * as github from '@actions/github';
43
import * as io from '@actions/io';
54
import path from 'path';
65
import fs from 'fs';
@@ -133,26 +132,38 @@ export async function setCommitAuthor(
133132
await exec.exec('git', ['config', 'user.email', getUserEmail(userEmail)]);
134133
}
135134

135+
export function getCommitMessage(
136+
msg: string,
137+
fullMsg: string,
138+
extRepo: string,
139+
baseRepo: string,
140+
hash: string
141+
): string {
142+
const msgHash = ((): string => {
143+
if (extRepo) {
144+
return `${baseRepo}@${hash}`;
145+
} else {
146+
return hash;
147+
}
148+
})();
149+
150+
const subject = ((): string => {
151+
if (fullMsg) {
152+
return fullMsg;
153+
} else if (msg) {
154+
return `${msg} ${msgHash}`;
155+
} else {
156+
return `deploy: ${msgHash}`;
157+
}
158+
})();
159+
160+
return subject;
161+
}
162+
136163
export async function commit(
137164
allowEmptyCommit: boolean,
138-
externalRepository: string,
139-
message: string
165+
msg: string
140166
): Promise<void> {
141-
let msg = '';
142-
if (message) {
143-
msg = message;
144-
} else {
145-
msg = 'deploy:';
146-
}
147-
148-
const hash = `${process.env.GITHUB_SHA}`;
149-
const baseRepo = `${github.context.repo.owner}/${github.context.repo.repo}`;
150-
if (externalRepository) {
151-
msg = `${msg} ${baseRepo}@${hash}`;
152-
} else {
153-
msg = `${msg} ${hash}`;
154-
}
155-
156167
try {
157168
if (allowEmptyCommit) {
158169
await exec.exec('git', ['commit', '--allow-empty', '-m', `${msg}`]);

src/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface Inputs {
1111
readonly UserName: string;
1212
readonly UserEmail: string;
1313
readonly CommitMessage: string;
14+
readonly FullCommitMessage: string;
1415
readonly TagName: string;
1516
readonly TagMessage: string;
1617
readonly DisableNoJekyll: boolean;

src/main.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import {context} from '@actions/github';
22
import * as core from '@actions/core';
33
import * as exec from '@actions/exec';
4+
import * as github from '@actions/github';
45
import {Inputs} from './interfaces';
56
import {showInputs, getInputs} from './get-inputs';
67
import {setTokens} from './set-tokens';
7-
import {setRepo, setCommitAuthor, commit, push, pushTag} from './git-utils';
8+
import {
9+
setRepo,
10+
setCommitAuthor,
11+
getCommitMessage,
12+
commit,
13+
push,
14+
pushTag
15+
} from './git-utils';
816
import {getWorkDirName, addNoJekyll, addCNAME, skipOnFork} from './utils';
917

1018
export async function run(): Promise<void> {
@@ -54,11 +62,16 @@ export async function run(): Promise<void> {
5462
await exec.exec('git', ['remote', 'add', 'origin', remoteURL]);
5563
await exec.exec('git', ['add', '--all']);
5664
await setCommitAuthor(inps.UserName, inps.UserEmail);
57-
await commit(
58-
inps.AllowEmptyCommit,
65+
const hash = `${process.env.GITHUB_SHA}`;
66+
const baseRepo = `${github.context.repo.owner}/${github.context.repo.repo}`;
67+
const commitMessage = getCommitMessage(
68+
inps.CommitMessage,
69+
inps.FullCommitMessage,
5970
inps.ExternalRepository,
60-
inps.CommitMessage
71+
baseRepo,
72+
hash
6173
);
74+
await commit(inps.AllowEmptyCommit, commitMessage);
6275
await push(inps.PublishBranch, inps.ForceOrphan);
6376
await pushTag(inps.TagName, inps.TagMessage);
6477

0 commit comments

Comments
 (0)