Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add USERNAME and EMAIL env variables #75

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| `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

Expand Down
4 changes: 2 additions & 2 deletions action/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.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');
Expand Down
14 changes: 12 additions & 2 deletions action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ export interface EnvironmentVariables {
*/
TAG?: string;

/**
* An optional string to use as the commiter name on the git commit.
*/
COMMIT_NAME?: string;

/**
* An optional string to use as the commiter email on the git commit.
*/
COMMIT_EMAIL?: string;

// Implicit environment variables passed by GitHub

GITHUB_REPOSITORY?: string;
Expand Down Expand Up @@ -329,9 +339,9 @@ export const main = async ({
);

const name =
event.pusher?.name || env.GITHUB_ACTOR || 'Git Publish Subdirectory';
env.COMMIT_NAME || event.pusher?.name || env.GITHUB_ACTOR || 'Git Publish Subdirectory';
const email =
event.pusher?.email ||
env.COMMIT_EMAIL || event.pusher?.email ||
(env.GITHUB_ACTOR
? `${env.GITHUB_ACTOR}@users.noreply.github.com`
: 'nobody@nowhere');
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <sha>
tree:8bf87c66655949e66937b11593cc4ae732d1f610
author:tester <tester@test.com>"
`;
49 changes: 49 additions & 0 deletions action/test/specs/ssh-custom-username-email.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
COMMIT_NAME: 'tester',
COMMIT_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, '<sha>');
expect(cleanedLog).toMatchSnapshot();
});