From 56f54b482f8019a16d8f8b0bfbbc9fa3ee9802e8 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Wed, 28 Feb 2024 13:52:31 -0800 Subject: [PATCH] feat: output for ref operation performed --- .github/workflows/integration-tests.yml | 4 ++++ README.md | 1 + __tests__/main.test.ts | 29 +++++++++++++++++++++---- action.yml | 2 ++ dist/index.js | 2 ++ src/main.ts | 2 ++ 6 files changed, 36 insertions(+), 4 deletions(-) diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index 0a6d5a2..e007048 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -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'); @@ -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'); @@ -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'); @@ -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'); diff --git a/README.md b/README.md index ee431ad..8f7ae54 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 5881d81..79070c9 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -337,6 +357,7 @@ describe('action', () => { expect(core.setFailed).toHaveBeenCalledTimes(1); expect(core.setFailed).toHaveBeenLastCalledWith('42'); + expect(core.setOutput).not.toHaveBeenCalled(); }); }); diff --git a/action.yml b/action.yml index 97639ed..affb49c 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/dist/index.js b/dist/index.js index 40e571d..ad3ea0e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -30403,6 +30403,7 @@ async function run() { sha: newCommit.data.sha, force }); + core.setOutput('ref-operation', 'updated'); core.debug(`Updated ref: ${ref} to ${newCommit.data.sha}`); } catch (err) { @@ -30415,6 +30416,7 @@ async function run() { ref: `refs/${ref}`, sha: newCommit.data.sha }); + core.setOutput('ref-operation', 'created'); core.debug(`Created ref: ${ref} at ${newCommit.data.sha}`); } else { diff --git a/src/main.ts b/src/main.ts index 0bf63fa..c2cbc53 100644 --- a/src/main.ts +++ b/src/main.ts @@ -109,6 +109,7 @@ export async function run(): Promise { sha: newCommit.data.sha, force }); + core.setOutput('ref-operation', 'updated'); core.debug(`Updated ref: ${ref} to ${newCommit.data.sha}`); } catch (err) { if ( @@ -122,6 +123,7 @@ export async function run(): Promise { ref: `refs/${ref}`, sha: newCommit.data.sha }); + core.setOutput('ref-operation', 'created'); core.debug(`Created ref: ${ref} at ${newCommit.data.sha}`); } else { throw err;