Skip to content

Commit

Permalink
feat: output for ref operation performed
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 committed Feb 28, 2024
1 parent c6971fc commit 56f54b4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ jobs:
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
});
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.verification.verified, true, 'Expected commit to be verified');
Expand Down Expand Up @@ -116,6 +117,7 @@ jobs:
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
});
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.verification.verified, true, 'Expected commit to be verified');
Expand Down Expand Up @@ -154,6 +156,7 @@ jobs:
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
});
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.verification.verified, true, 'Expected commit to be verified');
Expand Down Expand Up @@ -207,6 +210,7 @@ jobs:
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
});
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.verification.verified, true, 'Expected commit to be verified');
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jobs:

### Outputs

- `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

## License
Expand Down
29 changes: 25 additions & 4 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('action', () => {
expect(core.setFailed).toHaveBeenLastCalledWith(
'Input required and not supplied: message'
);
expect(core.setOutput).not.toHaveBeenCalled();
});

it('requires the token input', async () => {
Expand All @@ -89,6 +90,7 @@ describe('action', () => {
expect(core.setFailed).toHaveBeenLastCalledWith(
'Input required and not supplied: token'
);
expect(core.setOutput).not.toHaveBeenCalled();
});

it('defaults to HEAD ref', async () => {
Expand Down Expand Up @@ -129,8 +131,9 @@ describe('action', () => {
})
);

expect(core.setOutput).toHaveBeenCalledTimes(1);
expect(core.setOutput).toHaveBeenLastCalledWith('sha', commitSha);
expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
});

it('uses user-supplied ref', async () => {
Expand Down Expand Up @@ -170,8 +173,9 @@ describe('action', () => {
})
);

expect(core.setOutput).toHaveBeenCalledTimes(1);
expect(core.setOutput).toHaveBeenLastCalledWith('sha', commitSha);
expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
});

it('updates existing ref', async () => {
Expand All @@ -193,6 +197,10 @@ describe('action', () => {
expect(lib.getHeadRef).toHaveBeenCalled();
expect(updateRef).toHaveBeenCalled();
expect(createRef).not.toHaveBeenCalled();

expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
});

it('creates new ref', async () => {
Expand Down Expand Up @@ -232,6 +240,10 @@ describe('action', () => {
ref: `refs/heads/${ref}`
})
);

expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'created');
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
});

it('rethrows other errors on updateRef', async () => {
Expand All @@ -256,6 +268,7 @@ describe('action', () => {

expect(core.setFailed).toHaveBeenCalledTimes(1);
expect(core.setFailed).toHaveBeenLastCalledWith('Server error');
expect(core.setOutput).not.toHaveBeenCalled();
});

it('errors if no changes to commit', async () => {
Expand All @@ -270,6 +283,7 @@ describe('action', () => {
expect(core.setFailed).toHaveBeenLastCalledWith(
'No changes found to commit'
);
expect(core.setOutput).not.toHaveBeenCalled();
});

it('does not error if fail-on-no-changes is false', async () => {
Expand All @@ -285,6 +299,7 @@ describe('action', () => {
expect(core.notice).toHaveBeenLastCalledWith(
'No changes found to commit - skipping'
);
expect(core.setOutput).not.toHaveBeenCalled();
});

it('can force an update', async () => {
Expand All @@ -311,6 +326,10 @@ describe('action', () => {
})
);
expect(createRef).not.toHaveBeenCalled();

expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
});

it('handles generic errors', async () => {
Expand All @@ -324,6 +343,7 @@ describe('action', () => {

expect(core.setFailed).toHaveBeenCalledTimes(1);
expect(core.setFailed).toHaveBeenLastCalledWith('Server error');
expect(core.setOutput).not.toHaveBeenCalled();
});

it('stringifies non-errors', async () => {
Expand All @@ -337,6 +357,7 @@ describe('action', () => {

expect(core.setFailed).toHaveBeenCalledTimes(1);
expect(core.setFailed).toHaveBeenLastCalledWith('42');
expect(core.setOutput).not.toHaveBeenCalled();
});
});

Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ inputs:
required: true

outputs:
ref-operation:
description: 'Which operation was performed on the ref: `created` or `updated`. Has no value if there were no changes to commit.'
sha:
description: SHA for the commit

Expand Down
2 changes: 2 additions & 0 deletions dist/index.js

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

2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export async function run(): Promise<void> {
sha: newCommit.data.sha,
force
});
core.setOutput('ref-operation', 'updated');
core.debug(`Updated ref: ${ref} to ${newCommit.data.sha}`);
} catch (err) {
if (
Expand All @@ -122,6 +123,7 @@ export async function run(): Promise<void> {
ref: `refs/${ref}`,
sha: newCommit.data.sha
});
core.setOutput('ref-operation', 'created');
core.debug(`Created ref: ${ref} at ${newCommit.data.sha}`);
} else {
throw err;
Expand Down

0 comments on commit 56f54b4

Please sign in to comment.