From 09443a061a97626c29f912a5512bd1a08a617561 Mon Sep 17 00:00:00 2001 From: ozaner <56015962+ozaner@users.noreply.github.com> Date: Fri, 29 Jul 2022 02:24:05 -0400 Subject: [PATCH 1/5] Add `USERNAME` and `EMAIL` env variables Added support for a custom username and email to be used in the autogenerated commit. Just set `USERNAME` and `EMAIL` respectively in the env variables. --- action/src/index.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/action/src/index.ts b/action/src/index.ts index 217e79f..8b6189f 100644 --- a/action/src/index.ts +++ b/action/src/index.ts @@ -127,6 +127,16 @@ export interface EnvironmentVariables { */ TAG?: string; + /** + * An optional string to use as the username on the git commit. + */ + USERNAME?: string; + + /** + * An optional string to use as the email on the git commit. + */ + EMAIL?: string; + // Implicit environment variables passed by GitHub GITHUB_REPOSITORY?: string; @@ -329,9 +339,9 @@ export const main = async ({ ); const name = - event.pusher?.name || env.GITHUB_ACTOR || 'Git Publish Subdirectory'; + env.USERNAME || event.pusher?.name || env.GITHUB_ACTOR || 'Git Publish Subdirectory'; const email = - event.pusher?.email || + env.EMAIL || event.pusher?.email || (env.GITHUB_ACTOR ? `${env.GITHUB_ACTOR}@users.noreply.github.com` : 'nobody@nowhere'); From bd55e03516bf51fa58372db8a41fb7d10c7d9847 Mon Sep 17 00:00:00 2001 From: ozaner <56015962+ozaner@users.noreply.github.com> Date: Tue, 2 Aug 2022 08:34:56 -0400 Subject: [PATCH 2/5] Updated Readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 537926e..73a67b4 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,8 @@ All configuration options are passed in via `env`, as environment variables. | `MESSAGE` | A custom template to use as the commit message pushed to the target branch. See [custom commit messages](#custom-commit-messages). | No | | `TAG` | A string following the [git-check-ref-format](https://git-scm.com/docs/git-check-ref-format) that tags the commit with a lightweight git-tag. | No | | `CLEAR_GLOBS_FILE` | An optional path to a file to use as a list of globs defining which files to delete when clearing the target branch. | No | +| `USERNAME` | The username the autogenerated commit will use. If unset, uses the commit pusher's username. | No +| `EMAIL` | The username the autogenerated commit will use. If unset, uses the commit pusher's email. | No ### Custom commit messages From d441227a1c5e2a2e2d22d5f5b0c7574dec010a11 Mon Sep 17 00:00:00 2001 From: ozaner <56015962+ozaner@users.noreply.github.com> Date: Tue, 2 Aug 2022 08:35:07 -0400 Subject: [PATCH 3/5] Regenerated action code --- action/dist/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/action/dist/index.js b/action/dist/index.js index 2661cf5..32c2072 100644 --- a/action/dist/index.js +++ b/action/dist/index.js @@ -12764,8 +12764,8 @@ const main = async ({ env = process.env, log, }) => { if (!env.GITHUB_EVENT_PATH) throw new Error('Expected GITHUB_EVENT_PATH'); const event = JSON.parse((await fs_1.promises.readFile(env.GITHUB_EVENT_PATH)).toString()); - const name = ((_a = event.pusher) === null || _a === void 0 ? void 0 : _a.name) || env.GITHUB_ACTOR || 'Git Publish Subdirectory'; - const email = ((_b = event.pusher) === null || _b === void 0 ? void 0 : _b.email) || + const name = env.USERNAME || ((_a = event.pusher) === null || _a === void 0 ? void 0 : _a.name) || env.GITHUB_ACTOR || 'Git Publish Subdirectory'; + const email = env.EMAIL || ((_b = event.pusher) === null || _b === void 0 ? void 0 : _b.email) || (env.GITHUB_ACTOR ? `${env.GITHUB_ACTOR}@users.noreply.github.com` : 'nobody@nowhere'); From 31090042717dd5df6027b2505ad8993861612a95 Mon Sep 17 00:00:00 2001 From: ozaner <56015962+ozaner@users.noreply.github.com> Date: Tue, 2 Aug 2022 09:34:43 -0400 Subject: [PATCH 4/5] Add unit test --- .../ssh-custom-username-email.spec.ts.snap | 7 +++ .../test/specs/ssh-custom-username-email.ts | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 action/test/specs/__snapshots__/ssh-custom-username-email.spec.ts.snap create mode 100644 action/test/specs/ssh-custom-username-email.ts diff --git a/action/test/specs/__snapshots__/ssh-custom-username-email.spec.ts.snap b/action/test/specs/__snapshots__/ssh-custom-username-email.spec.ts.snap new file mode 100644 index 0000000..950b739 --- /dev/null +++ b/action/test/specs/__snapshots__/ssh-custom-username-email.spec.ts.snap @@ -0,0 +1,7 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Test custom username and email 1`] = ` +"msg:Update branch-a to output generated at +tree:8bf87c66655949e66937b11593cc4ae732d1f610 +author:tester " +`; diff --git a/action/test/specs/ssh-custom-username-email.ts b/action/test/specs/ssh-custom-username-email.ts new file mode 100644 index 0000000..219f75a --- /dev/null +++ b/action/test/specs/ssh-custom-username-email.ts @@ -0,0 +1,49 @@ +import { promises as fs } from 'fs'; +import * as path from 'path'; +import { mkdirP } from '@actions/io'; + +import * as util from '../util'; +import { prepareTestFolders } from '../util/io'; + +it('Test custom username and email', async () => { + const folders = await prepareTestFolders({ __filename }); + + // Create empty repo + await util.wrappedExec('git init --bare', { cwd: folders.repoDir }); + + // Create dummy data + await mkdirP(path.join(folders.dataDir, 'dummy')); + await fs.writeFile(path.join(folders.dataDir, 'dummy', 'baz'), 'foobar'); + await fs.writeFile(path.join(folders.dataDir, 'dummy', '.bat'), 'foobar'); + + // Run Action + await util.runWithGithubEnv( + path.basename(__filename), + { + REPO: folders.repoUrl, + BRANCH: 'branch-a', + FOLDER: folders.dataDir, + SSH_PRIVATE_KEY: (await fs.readFile(util.SSH_PRIVATE_KEY)).toString(), + KNOWN_HOSTS_FILE: util.KNOWN_HOSTS, + USERNAME: 'tester', + EMAIL: 'tester@test.com' + }, + 's0/test', + {}, + 's0' + ); + + // Check that the log of the repo is as expected + // (check tree-hash, commit message, and author) + const log = ( + await util.exec( + 'git log --pretty="format:msg:%s%ntree:%T%nauthor:%an <%ae>" branch-a', + { + cwd: folders.repoDir, + } + ) + ).stdout; + const sha = await util.getRepoSha(); + const cleanedLog = log.replace(sha, ''); + expect(cleanedLog).toMatchSnapshot(); +}); From ad3c5083e119cd8bab34c37b3fc0c5146b954e39 Mon Sep 17 00:00:00 2001 From: ozzaner <56015962+ozaner@users.noreply.github.com> Date: Thu, 25 Aug 2022 09:10:27 -0400 Subject: [PATCH 5/5] Renamed `COMMIT_EMAIL` and `COMMIT_NAME` env vars. - The `USERNAME` and `EMAIL` env variables have been renamed `COMMIT_NAME` and `COMMIT_EMAIL` respectively. - Updated readme to reflect this. - Fixed the file name of the username-email spec. --- README.md | 4 ++-- action/dist/index.js | 4 ++-- action/src/index.ts | 12 ++++++------ ...me-email.ts => ssh-custom-username-email.spec.ts} | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) rename action/test/specs/{ssh-custom-username-email.ts => ssh-custom-username-email.spec.ts} (95%) diff --git a/README.md b/README.md index 73a67b4..020aa69 100644 --- a/README.md +++ b/README.md @@ -172,8 +172,8 @@ All configuration options are passed in via `env`, as environment variables. | `MESSAGE` | A custom template to use as the commit message pushed to the target branch. See [custom commit messages](#custom-commit-messages). | No | | `TAG` | A string following the [git-check-ref-format](https://git-scm.com/docs/git-check-ref-format) that tags the commit with a lightweight git-tag. | No | | `CLEAR_GLOBS_FILE` | An optional path to a file to use as a list of globs defining which files to delete when clearing the target branch. | No | -| `USERNAME` | The username the autogenerated commit will use. If unset, uses the commit pusher's username. | No -| `EMAIL` | The username the autogenerated commit will use. If unset, uses the commit pusher's email. | No +| `COMMIT_NAME` | The username the autogenerated commit will use. If unset, uses the commit pusher's username. | No +| `COMMIT_EMAIL` | The email the autogenerated commit will use. If unset, uses the commit pusher's email. | No ### Custom commit messages diff --git a/action/dist/index.js b/action/dist/index.js index 32c2072..2a8c710 100644 --- a/action/dist/index.js +++ b/action/dist/index.js @@ -12764,8 +12764,8 @@ const main = async ({ env = process.env, log, }) => { if (!env.GITHUB_EVENT_PATH) throw new Error('Expected GITHUB_EVENT_PATH'); const event = JSON.parse((await fs_1.promises.readFile(env.GITHUB_EVENT_PATH)).toString()); - const name = env.USERNAME || ((_a = event.pusher) === null || _a === void 0 ? void 0 : _a.name) || env.GITHUB_ACTOR || 'Git Publish Subdirectory'; - const email = env.EMAIL || ((_b = event.pusher) === null || _b === void 0 ? void 0 : _b.email) || + const name = env.COMMIT_NAME || ((_a = event.pusher) === null || _a === void 0 ? void 0 : _a.name) || env.GITHUB_ACTOR || 'Git Publish Subdirectory'; + const email = env.COMMIT_EMAIL || ((_b = event.pusher) === null || _b === void 0 ? void 0 : _b.email) || (env.GITHUB_ACTOR ? `${env.GITHUB_ACTOR}@users.noreply.github.com` : 'nobody@nowhere'); diff --git a/action/src/index.ts b/action/src/index.ts index 8b6189f..9a1e95f 100644 --- a/action/src/index.ts +++ b/action/src/index.ts @@ -128,14 +128,14 @@ export interface EnvironmentVariables { TAG?: string; /** - * An optional string to use as the username on the git commit. + * An optional string to use as the commiter name on the git commit. */ - USERNAME?: string; + COMMIT_NAME?: string; /** - * An optional string to use as the email on the git commit. + * An optional string to use as the commiter email on the git commit. */ - EMAIL?: string; + COMMIT_EMAIL?: string; // Implicit environment variables passed by GitHub @@ -339,9 +339,9 @@ export const main = async ({ ); const name = - env.USERNAME || event.pusher?.name || env.GITHUB_ACTOR || 'Git Publish Subdirectory'; + env.COMMIT_NAME || event.pusher?.name || env.GITHUB_ACTOR || 'Git Publish Subdirectory'; const email = - env.EMAIL || event.pusher?.email || + env.COMMIT_EMAIL || event.pusher?.email || (env.GITHUB_ACTOR ? `${env.GITHUB_ACTOR}@users.noreply.github.com` : 'nobody@nowhere'); diff --git a/action/test/specs/ssh-custom-username-email.ts b/action/test/specs/ssh-custom-username-email.spec.ts similarity index 95% rename from action/test/specs/ssh-custom-username-email.ts rename to action/test/specs/ssh-custom-username-email.spec.ts index 219f75a..23174c0 100644 --- a/action/test/specs/ssh-custom-username-email.ts +++ b/action/test/specs/ssh-custom-username-email.spec.ts @@ -25,8 +25,8 @@ it('Test custom username and email', async () => { FOLDER: folders.dataDir, SSH_PRIVATE_KEY: (await fs.readFile(util.SSH_PRIVATE_KEY)).toString(), KNOWN_HOSTS_FILE: util.KNOWN_HOSTS, - USERNAME: 'tester', - EMAIL: 'tester@test.com' + COMMIT_NAME: 'tester', + COMMIT_EMAIL: 'tester@test.com' }, 's0/test', {},