Skip to content

Commit 56f54b4

Browse files
committed
feat: output for ref operation performed
1 parent c6971fc commit 56f54b4

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

.github/workflows/integration-tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ jobs:
7272
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
7373
});
7474
75+
assert.strictEqual('${{ steps.commit-new-ref.outputs.ref-operation }}', 'created', 'Expected correct ref operation');
7576
assert.strictEqual(data.sha, '${{ steps.commit-new-ref.outputs.sha }}', 'Expected sha for commit to match');
7677
assert.strictEqual(data.commit.message, 'Test new ref commit', 'Expected commit message to match');
7778
assert.strictEqual(data.commit.verification.verified, true, 'Expected commit to be verified');
@@ -116,6 +117,7 @@ jobs:
116117
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
117118
});
118119
120+
assert.strictEqual('${{ steps.update-existing-ref.outputs.ref-operation }}', 'updated', 'Expected correct ref operation');
119121
assert.strictEqual(data.sha, '${{ steps.update-existing-ref.outputs.sha }}', 'Expected sha for commit to match');
120122
assert.strictEqual(data.commit.message, 'Test updating existing ref', 'Expected commit message to match');
121123
assert.strictEqual(data.commit.verification.verified, true, 'Expected commit to be verified');
@@ -154,6 +156,7 @@ jobs:
154156
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
155157
});
156158
159+
assert.strictEqual('${{ steps.update-existing-ref-2.outputs.ref-operation }}', 'updated', 'Expected correct ref operation');
157160
assert.strictEqual(data.sha, '${{ steps.update-existing-ref-2.outputs.sha }}', 'Expected sha for commit to match');
158161
assert.strictEqual(data.commit.message, 'Test updating existing ref (again)', 'Expected commit message to match');
159162
assert.strictEqual(data.commit.verification.verified, true, 'Expected commit to be verified');
@@ -207,6 +210,7 @@ jobs:
207210
ref: 'integration-test-playground-${{ github.run_id }}-${{ github.run_number }}',
208211
});
209212
213+
assert.strictEqual('${{ steps.force-update-existing-ref.outputs.ref-operation }}', 'updated', 'Expected correct ref operation');
210214
assert.strictEqual(data.sha, '${{ steps.force-update-existing-ref.outputs.sha }}', 'Expected sha for commit to match');
211215
assert.strictEqual(data.commit.message, 'Test updating existing ref (force)', 'Expected commit message to match');
212216
assert.strictEqual(data.commit.verification.verified, true, 'Expected commit to be verified');

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ jobs:
6868

6969
### Outputs
7070

71+
- `ref-operation` - Which operation was performed on the ref: `created` or `updated`. Has no value if there were no changes to commit.
7172
- `sha` - SHA for the commit
7273

7374
## License

__tests__/main.test.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ describe('action', () => {
7777
expect(core.setFailed).toHaveBeenLastCalledWith(
7878
'Input required and not supplied: message'
7979
);
80+
expect(core.setOutput).not.toHaveBeenCalled();
8081
});
8182

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

9496
it('defaults to HEAD ref', async () => {
@@ -129,8 +131,9 @@ describe('action', () => {
129131
})
130132
);
131133

132-
expect(core.setOutput).toHaveBeenCalledTimes(1);
133-
expect(core.setOutput).toHaveBeenLastCalledWith('sha', commitSha);
134+
expect(core.setOutput).toHaveBeenCalledTimes(2);
135+
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
136+
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
134137
});
135138

136139
it('uses user-supplied ref', async () => {
@@ -170,8 +173,9 @@ describe('action', () => {
170173
})
171174
);
172175

173-
expect(core.setOutput).toHaveBeenCalledTimes(1);
174-
expect(core.setOutput).toHaveBeenLastCalledWith('sha', commitSha);
176+
expect(core.setOutput).toHaveBeenCalledTimes(2);
177+
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
178+
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
175179
});
176180

177181
it('updates existing ref', async () => {
@@ -193,6 +197,10 @@ describe('action', () => {
193197
expect(lib.getHeadRef).toHaveBeenCalled();
194198
expect(updateRef).toHaveBeenCalled();
195199
expect(createRef).not.toHaveBeenCalled();
200+
201+
expect(core.setOutput).toHaveBeenCalledTimes(2);
202+
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
203+
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
196204
});
197205

198206
it('creates new ref', async () => {
@@ -232,6 +240,10 @@ describe('action', () => {
232240
ref: `refs/heads/${ref}`
233241
})
234242
);
243+
244+
expect(core.setOutput).toHaveBeenCalledTimes(2);
245+
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'created');
246+
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
235247
});
236248

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

257269
expect(core.setFailed).toHaveBeenCalledTimes(1);
258270
expect(core.setFailed).toHaveBeenLastCalledWith('Server error');
271+
expect(core.setOutput).not.toHaveBeenCalled();
259272
});
260273

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

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

290305
it('can force an update', async () => {
@@ -311,6 +326,10 @@ describe('action', () => {
311326
})
312327
);
313328
expect(createRef).not.toHaveBeenCalled();
329+
330+
expect(core.setOutput).toHaveBeenCalledTimes(2);
331+
expect(core.setOutput).toHaveBeenCalledWith('ref-operation', 'updated');
332+
expect(core.setOutput).toHaveBeenCalledWith('sha', commitSha);
314333
});
315334

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

325344
expect(core.setFailed).toHaveBeenCalledTimes(1);
326345
expect(core.setFailed).toHaveBeenLastCalledWith('Server error');
346+
expect(core.setOutput).not.toHaveBeenCalled();
327347
});
328348

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

338358
expect(core.setFailed).toHaveBeenCalledTimes(1);
339359
expect(core.setFailed).toHaveBeenLastCalledWith('42');
360+
expect(core.setOutput).not.toHaveBeenCalled();
340361
});
341362
});
342363

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ inputs:
2626
required: true
2727

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

dist/index.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export async function run(): Promise<void> {
109109
sha: newCommit.data.sha,
110110
force
111111
});
112+
core.setOutput('ref-operation', 'updated');
112113
core.debug(`Updated ref: ${ref} to ${newCommit.data.sha}`);
113114
} catch (err) {
114115
if (
@@ -122,6 +123,7 @@ export async function run(): Promise<void> {
122123
ref: `refs/${ref}`,
123124
sha: newCommit.data.sha
124125
});
126+
core.setOutput('ref-operation', 'created');
125127
core.debug(`Created ref: ${ref} at ${newCommit.data.sha}`);
126128
} else {
127129
throw err;

0 commit comments

Comments
 (0)