Skip to content

Commit

Permalink
feat: add message and ref outputs (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Mar 12, 2024
1 parent b63d6ba commit 48d2ff8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 19 deletions.
28 changes: 20 additions & 8 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,22 @@ jobs:
script: |
const assert = require('node:assert');
const message = 'Test new ref commit';
const refs = ['integration-test-playground-${{ github.run_id }}-${{ github.run_number }}', '${{ steps.commit-new-ref.outputs.sha }}'];
// Fetch the commit by both ref and sha
for (const ref of refs) {
const { data } = await github.rest.repos.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
ref,
});
assert.strictEqual('${{ steps.commit-new-ref.outputs.message }}', message, 'Expected correct message output');
assert.strictEqual('${{ steps.commit-new-ref.outputs.ref }}', refs[0], 'Expected correct ref output');
assert.strictEqual('${{ steps.commit-new-ref.outputs.ref-operation }}', 'created', 'Expected correct ref operation');
assert.strictEqual(data.sha, '${{ steps.commit-new-ref.outputs.sha }}', 'Expected sha for commit to match');
assert.strictEqual(data.commit.message, 'Test new ref commit', 'Expected commit message to match');
assert.strictEqual(data.commit.message, message, 'Expected commit message to match');
assert.strictEqual(data.commit.verification.verified, true, 'Expected commit to be verified');
}
Expand Down Expand Up @@ -107,19 +110,22 @@ jobs:
script: |
const assert = require('node:assert');
const message = 'Test updating existing ref';
const refs = ['integration-test-playground-${{ github.run_id }}-${{ github.run_number }}', '${{ steps.update-existing-ref.outputs.sha }}'];
// Fetch the commit by both ref and sha
for (const ref of refs) {
const { data } = await github.rest.repos.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
ref,
});
assert.strictEqual('${{ steps.update-existing-ref.outputs.message }}', message, 'Expected correct message output');
assert.strictEqual('${{ steps.update-existing-ref.outputs.ref }}', refs[0], 'Expected correct ref output');
assert.strictEqual('${{ steps.update-existing-ref.outputs.ref-operation }}', 'updated', 'Expected correct ref operation');
assert.strictEqual(data.sha, '${{ steps.update-existing-ref.outputs.sha }}', 'Expected sha for commit to match');
assert.strictEqual(data.commit.message, 'Test updating existing ref', 'Expected commit message to match');
assert.strictEqual(data.commit.message, message, 'Expected commit message to match');
assert.strictEqual(data.commit.verification.verified, true, 'Expected commit to be verified');
}
Expand All @@ -146,19 +152,22 @@ jobs:
script: |
const assert = require('node:assert');
const message = 'Test updating existing ref (again)';
const refs = ['integration-test-playground-${{ github.run_id }}-${{ github.run_number }}', '${{ steps.update-existing-ref-2.outputs.sha }}'];
// Fetch the commit by both ref and sha
for (const ref of refs) {
const { data } = await github.rest.repos.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
ref,
});
assert.strictEqual('${{ steps.update-existing-ref-2.outputs.message }}', message, 'Expected correct message output');
assert.strictEqual('${{ steps.update-existing-ref-2.outputs.ref }}', refs[0], 'Expected correct ref output');
assert.strictEqual('${{ steps.update-existing-ref-2.outputs.ref-operation }}', 'updated', 'Expected correct ref operation');
assert.strictEqual(data.sha, '${{ steps.update-existing-ref-2.outputs.sha }}', 'Expected sha for commit to match');
assert.strictEqual(data.commit.message, 'Test updating existing ref (again)', 'Expected commit message to match');
assert.strictEqual(data.commit.message, message, 'Expected commit message to match');
assert.strictEqual(data.commit.verification.verified, true, 'Expected commit to be verified');
}
Expand Down Expand Up @@ -200,19 +209,22 @@ jobs:
script: |
const assert = require('node:assert');
const message = 'Test updating existing ref (force)';
const refs = ['integration-test-playground-${{ github.run_id }}-${{ github.run_number }}', '${{ steps.force-update-existing-ref.outputs.sha }}'];
// Fetch the commit by both ref and sha
for (const ref of refs) {
const { data } = await github.rest.repos.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
ref,
});
assert.strictEqual('${{ steps.force-update-existing-ref.outputs.message }}', message, 'Expected correct message output');
assert.strictEqual('${{ steps.force-update-existing-ref.outputs.ref }}', refs[0], 'Expected correct ref output');
assert.strictEqual('${{ steps.force-update-existing-ref.outputs.ref-operation }}', 'updated', 'Expected correct ref operation');
assert.strictEqual(data.sha, '${{ steps.force-update-existing-ref.outputs.sha }}', 'Expected sha for commit to match');
assert.strictEqual(data.commit.message, 'Test updating existing ref (force)', 'Expected commit message to match');
assert.strictEqual(data.commit.message, message, 'Expected commit message to match');
assert.strictEqual(data.commit.verification.verified, true, 'Expected commit to be verified');
}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ jobs:

### Outputs

- `message` - The commit message
- `ref` - The associated Git reference
- `ref-operation` - Which operation was performed on the ref: `created` or `updated`. Has no value if there were no changes to commit.
- `sha` - SHA for the commit

Expand Down
20 changes: 15 additions & 5 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ describe('action', () => {
})
);

expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledTimes(4);
expect(core.setOutput).toHaveBeenCalledWith('message', message);
expect(core.setOutput).toHaveBeenCalledWith('ref', ref);
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
});
Expand Down Expand Up @@ -173,7 +175,9 @@ describe('action', () => {
})
);

expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledTimes(4);
expect(core.setOutput).toHaveBeenCalledWith('message', message);
expect(core.setOutput).toHaveBeenCalledWith('ref', ref);
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
});
Expand All @@ -198,7 +202,9 @@ describe('action', () => {
expect(updateRef).toHaveBeenCalled();
expect(createRef).not.toHaveBeenCalled();

expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledTimes(4);
expect(core.setOutput).toHaveBeenCalledWith('message', message);
expect(core.setOutput).toHaveBeenCalledWith('ref', ref);
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
});
Expand Down Expand Up @@ -241,7 +247,9 @@ describe('action', () => {
})
);

expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledTimes(4);
expect(core.setOutput).toHaveBeenCalledWith('message', message);
expect(core.setOutput).toHaveBeenCalledWith('ref', ref);
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'created');
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
});
Expand Down Expand Up @@ -327,7 +335,9 @@ describe('action', () => {
);
expect(createRef).not.toHaveBeenCalled();

expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledTimes(4);
expect(core.setOutput).toHaveBeenCalledWith('message', message);
expect(core.setOutput).toHaveBeenCalledWith('ref', ref);
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
});
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ inputs:
required: true

outputs:
message:
description: The commit message
ref:
description: The associated Git reference
ref-operation:
description: 'Which operation was performed on the ref: `created` or `updated`. Has no value if there were no changes to commit.'
sha:
Expand Down
8 changes: 5 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export async function run(): Promise<void> {
const token = core.getInput('token', { required: true });

// Optional inputs
const ref = `heads/${core.getInput('ref') || (await getHeadRef())}`;
const ref = core.getInput('ref') || (await getHeadRef());
const failOnNoChanges = core.getBooleanInput('fail-on-no-changes');
const force = core.getBooleanInput('force');

Expand Down Expand Up @@ -105,7 +105,7 @@ export async function run(): Promise<void> {
await octokit.rest.git.updateRef({
owner,
repo,
ref,
ref: `heads/${ref}`,
sha: newCommit.data.sha,
force
});
Expand All @@ -120,7 +120,7 @@ export async function run(): Promise<void> {
await octokit.rest.git.createRef({
owner,
repo,
ref: `refs/${ref}`,
ref: `refs/heads/${ref}`,
sha: newCommit.data.sha
});
core.setOutput('ref-operation', 'created');
Expand All @@ -130,6 +130,8 @@ export async function run(): Promise<void> {
}
}

core.setOutput('message', message);
core.setOutput('ref', ref);
core.setOutput('sha', newCommit.data.sha);
} catch (error) {
// Fail the workflow run if an error occurs
Expand Down

0 comments on commit 48d2ff8

Please sign in to comment.