Skip to content

Commit

Permalink
Merge pull request #78 from promise/feat/author-env-variables
Browse files Browse the repository at this point in the history
Add author environment variables
  • Loading branch information
s0 authored Nov 17, 2022
2 parents faeb9af + a5c652c commit a1245bc
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 19 deletions.
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Simply include the action `s0/git-publish-subdir-action@develop` in the appropri
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
runs-on: ubuntu-latest
steps:

# Any prerequisite steps
Expand Down Expand Up @@ -53,14 +53,14 @@ jobs:
```yml
name: Deploy to GitHub Pages
on:
push:
branches:
push:
branches:
- master
jobs:
deploy:
name: Deploy to GitHub Pages
runs-on: ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
Expand All @@ -78,14 +78,14 @@ jobs:
```yml
name: Deploy to GitHub Pages
on:
push:
branches:
push:
branches:
- master
jobs:
deploy:
name: Deploy to GitHub Pages
runs-on: ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Use Node.js
Expand Down Expand Up @@ -115,14 +115,14 @@ and store the SSH private key as as Secret in the repository settings.
```yml
name: Deploy to GitHub Pages
on:
push:
branches:
push:
branches:
- master
jobs:
deploy:
name: Deploy to GitHub Pages
runs-on: ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Use Node.js
Expand Down Expand Up @@ -172,6 +172,9 @@ 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 All @@ -182,10 +185,10 @@ relevant values:

| Placeholder | Description |
| ------------------ | ----------------------------------------------------- |
| `{target-branch}` | The name of the target branch being updated |
| `{sha}` | The 7-character sha of the HEAD of the current branch |
| `{long-sha}` | The full sha of the HEAD of the current branch |
| `{msg}` | The commit message for the HEAD of the current branch |
| `{target-branch}` | The name of the target branch being updated |
| `{sha}` | The 7-character sha of the HEAD of the current branch |
| `{long-sha}` | The full sha of the HEAD of the current branch |
| `{msg}` | The commit message for the HEAD of the current branch |

Example Usage:

Expand Down Expand Up @@ -298,8 +301,8 @@ Use `ssh-keygen` to create a new ssh key, add these to GitHub in the relevant re
> ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/sam/.ssh/id_ed25519): temp-deploy-key
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in temp-deploy-key.
Your public key has been saved in temp-deploy-key.pub.
The key fingerprint is:
Expand Down
8 changes: 6 additions & 2 deletions action/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12091,8 +12091,12 @@ 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
16 changes: 15 additions & 1 deletion 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,8 +339,12 @@ 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 =
env.COMMIT_EMAIL ||
event.pusher?.email ||
(env.GITHUB_ACTOR
? `${env.GITHUB_ACTOR}@users.noreply.github.com`
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 <[email protected]>"
`;
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: '[email protected]',
},
'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();
});

0 comments on commit a1245bc

Please sign in to comment.