Skip to content

Commit b4a471d

Browse files
authored
feat: Adds outputs for base_sha and head_sha (#8)
1 parent 56b4b02 commit b4a471d

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

.github/workflows/pr-test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ jobs:
3333
STATE=success
3434
if [[ "${{ steps.refs_in_test.outputs.base_ref }}" == "" ]]; then
3535
STATE=failure
36+
elif [[ "${{ steps.refs_in_test.outputs.base_sha }}" == "" ]]; then
37+
STATE=failure
3638
elif [[ "${{ steps.refs_in_test.outputs.head_ref }}" == "" ]]; then
3739
STATE=failure
40+
elif [[ "${{ steps.refs_in_test.outputs.head_sha }}" == "" ]]; then
41+
STATE=failure
3842
elif [[ "${{ steps.refs_in_test.outcome }}" != "success" ]]; then
3943
STATE=failure
4044
fi

dist/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ const resolver_1 = __nccwpck_require__(7409);
4040
(() => __awaiter(void 0, void 0, void 0, function* () {
4141
try {
4242
const githubToken = core.getInput('token');
43-
const { headRef, baseRef } = yield (0, resolver_1.resolveRefs)(githubToken);
43+
const { headRef, headSha, baseRef, baseSha } = yield (0, resolver_1.resolveRefs)(githubToken);
4444
core.setOutput('base_ref', baseRef);
45+
core.setOutput('base_sha', baseSha);
4546
core.setOutput('head_ref', headRef);
47+
core.setOutput('head_sha', headSha);
4648
}
4749
catch (error) {
4850
core.setFailed(error.message);
@@ -101,8 +103,10 @@ const resolveRefs = (token) => __awaiter(void 0, void 0, void 0, function* () {
101103
const prDetails = yield octokit.request(`GET ${prUrl}`);
102104
const status = prDetails.status;
103105
const headRef = prDetails.data.head.ref;
106+
const headSha = prDetails.data.head.sha;
104107
const baseRef = prDetails.data.base.ref;
105-
return { status, headRef, baseRef };
108+
const baseSha = prDetails.data.base.sha;
109+
return { status, headRef, headSha, baseRef, baseSha };
106110
});
107111
exports.resolveRefs = resolveRefs;
108112

index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import { resolveRefs } from './src/resolver';
66
try {
77
const githubToken = core.getInput('token');
88

9-
const { headRef, baseRef } = await resolveRefs(githubToken);
9+
const { headRef, headSha, baseRef, baseSha } = await resolveRefs(githubToken);
1010

1111
core.setOutput('base_ref', baseRef);
12+
core.setOutput('base_sha', baseSha);
1213
core.setOutput('head_ref', headRef);
14+
core.setOutput('head_sha', headSha);
1315
} catch (error: any) {
1416
core.setFailed(error.message);
1517
}

src/resolver.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export const resolveRefs = async (token: string) => {
1818

1919
const status = prDetails.status;
2020
const headRef = prDetails.data.head.ref;
21+
const headSha = prDetails.data.head.sha;
2122
const baseRef = prDetails.data.base.ref;
23+
const baseSha = prDetails.data.base.sha;
2224

23-
return { status, headRef, baseRef };
25+
return { status, headRef, headSha, baseRef, baseSha };
2426
};

test/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,21 @@ describe('resolveRefs', () => {
1111
expect(headRef).toEqual(mockPayload.octokitPayload.data.head.ref);
1212
});
1313

14+
it(`should return head_sha: ${mockPayload.getHeadSha()}`, async () => {
15+
const { headSha } = await resolveRefs(token);
16+
17+
expect(headSha).toEqual(mockPayload.octokitPayload.data.head.sha);
18+
});
19+
1420
it(`should return base_ref: ${mockPayload.getBaseRef()}`, async () => {
1521
const { baseRef } = await resolveRefs(token);
1622

1723
expect(baseRef).toEqual(mockPayload.octokitPayload.data.base.ref);
1824
});
25+
26+
it(`should return base_sha: ${mockPayload.getBaseSha()}`, async () => {
27+
const { baseSha } = await resolveRefs(token);
28+
29+
expect(baseSha).toEqual(mockPayload.octokitPayload.data.base.sha);
30+
});
1931
});

test/payload-provider.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class PayloadProvider {
22
contextPayload: any;
3-
octokitPayload: { status: string; data: { head: { ref: string; }; base: { ref: string; }; }; };
3+
octokitPayload: { status: string; data: { head: { ref: string; sha: string; }; base: { ref: string; sha: string; }; }; };
44

55
constructor() {
66
this.contextPayload = {
@@ -12,8 +12,8 @@ class PayloadProvider {
1212
this.octokitPayload = {
1313
status: '200',
1414
data: {
15-
head: { ref: 'refs/heads/pr' },
16-
base: { ref: 'refs/heads/main' },
15+
head: { ref: 'refs/heads/pr', sha: 'shaHead' },
16+
base: { ref: 'refs/heads/main', sha: 'shaBase' },
1717
},
1818
};
1919
}
@@ -27,7 +27,9 @@ class PayloadProvider {
2727
}
2828

2929
getHeadRef() { return this.octokitPayload.data.head.ref; }
30+
getHeadSha() { return this.octokitPayload.data.head.sha; }
3031
getBaseRef() { return this.octokitPayload.data.base.ref; }
32+
getBaseSha() { return this.octokitPayload.data.base.sha; }
3133
}
3234

3335
export default new PayloadProvider();

0 commit comments

Comments
 (0)