Skip to content

Commit 7170b5b

Browse files
committed
feat: inputs to set commit author
1 parent 0f8e2b6 commit 7170b5b

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

.github/workflows/integration-tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
id: generate-token
3838
with:
3939
creds: ${{ secrets.GH_APP_CREDS }}
40+
export-git-user: true
4041

4142
- name: Make changes to commit and stage them
4243
run: |
@@ -55,6 +56,8 @@ jobs:
5556
uses: ./
5657
id: commit-new-ref
5758
with:
59+
author-email: 161413468+gh-app-commit-action-integration[bot]@users.noreply.github.com
60+
author-name: gh-app-commit-action-integration[bot]
5861
message: Test new ref commit
5962
ref: integration-test-playground-${{ github.run_id }}-${{ github.run_number }}
6063
token: ${{ steps.generate-token.outputs.token }}

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ branding:
77
color: green
88

99
inputs:
10+
author-email:
11+
description: The email address to use for the commit author
12+
required: false
13+
author-name:
14+
description: The name to use for the commit author
15+
required: false
1016
fail-on-no-changes:
1117
description: Whether or not to set action failure if there are no changes to commit
1218
required: false

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from 'node:fs';
22
import * as path from 'node:path';
33
import * as process from 'node:process';
4+
import { inspect } from 'node:util';
45

56
import * as core from '@actions/core';
67
import * as github from '@actions/github';
@@ -69,13 +70,25 @@ export async function run(): Promise<void> {
6970
const token = core.getInput('token', { required: true });
7071

7172
// Optional inputs
73+
const authorEmail = core.getInput('author-email');
74+
const authorName = core.getInput('author-name');
7275
const ref = core.getInput('ref') || (await getHeadRef());
7376
const failOnNoChanges = core.getBooleanInput('fail-on-no-changes');
7477
const force = core.getBooleanInput('force');
7578
const owner = core.getInput('owner') || github.context.repo.owner;
7679
const repo = core.getInput('repository') || github.context.repo.repo;
7780
const workingDirectory = core.getInput('working-directory');
7881

82+
if (authorEmail && !authorName) {
83+
core.setFailed('Input required and not supplied: author-name');
84+
return;
85+
}
86+
87+
if (!authorEmail && authorName) {
88+
core.setFailed('Input required and not supplied: author-email');
89+
return;
90+
}
91+
7992
if (workingDirectory) {
8093
process.chdir(workingDirectory);
8194
}
@@ -117,13 +130,28 @@ export async function run(): Promise<void> {
117130
});
118131
core.debug(`New tree SHA: ${newTree.data.sha}`);
119132

120-
const newCommit = await octokit.rest.git.createCommit({
133+
core.debug(`Creating commit with committer: ${process.env.GIT_COMMITTER_NAME} <${process.env.GIT_COMMITTER_EMAIL}>`);
134+
135+
const createCommitParams: Endpoints['POST /repos/{owner}/{repo}/git/commits']['parameters'] = {
121136
owner,
122137
repo,
138+
// committer: {
139+
// name: process.env.GIT_COMMITTER_NAME,
140+
// email: process.env.GIT_COMMITTER_EMAIL
141+
// },
123142
parents: [await getHeadSha()],
124143
message,
125144
tree: newTree.data.sha
126-
});
145+
};
146+
if (authorEmail && authorName) {
147+
core.debug(`Creating commit with author: ${authorName} <${authorEmail}>`);
148+
createCommitParams.author = {
149+
name: authorName,
150+
email: authorEmail
151+
};
152+
}
153+
const newCommit = await octokit.rest.git.createCommit(createCommitParams);
154+
core.debug(`New commit author: ${newCommit.data.author?.email}`);
127155
core.debug(`New commit SHA: ${newCommit.data.sha}`);
128156

129157
try {
@@ -160,6 +188,7 @@ export async function run(): Promise<void> {
160188
core.setOutput('sha', newCommit.data.sha);
161189
} catch (error) {
162190
// Fail the workflow run if an error occurs
191+
core.debug(inspect(error));
163192
if (error instanceof Error && error.stack) core.debug(error.stack);
164193
core.setFailed(
165194
error instanceof Error ? error.message : JSON.stringify(error)

0 commit comments

Comments
 (0)