Skip to content

Commit 960061c

Browse files
authored
Add coverage report to PR for push event (#144)
* Get PR number for push event * Setting prNumber from 0 to undefined * Setting prNumber from 0 to undefined * Adapt tests * Add tests for push event to publish pr comment * Update version
1 parent ad47d82 commit 960061c

19 files changed

+188
-14
lines changed

__tests__/action_aggregate.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ describe('Aggregate report', function () {
4949
compareCommits: jest.fn(() => {
5050
return compareCommitsResponse
5151
}),
52+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
53+
return {data: []}
54+
}),
5255
},
5356
issues: {
5457
createComment,

__tests__/action_empty_aggregate.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ describe('Aggregate Empty report', function () {
4949
compareCommits: jest.fn(() => {
5050
return compareCommitsResponse
5151
}),
52+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
53+
return {data: []}
54+
}),
5255
},
5356
issues: {
5457
createComment,

__tests__/action_empty_multiple.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ describe('Multiple Empty reports', function () {
7878
compareCommits: jest.fn(() => {
7979
return compareCommitsResponse
8080
}),
81+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
82+
return {data: []}
83+
}),
8184
},
8285
issues: {
8386
createComment: comment,

__tests__/action_empty_report.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ describe('Single Empty report', function () {
4949
compareCommits: jest.fn(() => {
5050
return compareCommitsResponse
5151
}),
52+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
53+
return {data: []}
54+
}),
5255
},
5356
issues: {
5457
createComment,

__tests__/action_multiple.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ describe('Multiple reports', function () {
7878
compareCommits: jest.fn(() => {
7979
return compareCommitsResponse
8080
}),
81+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
82+
return {data: []}
83+
}),
8184
},
8285
issues: {
8386
createComment: comment,

__tests__/action_single.test.ts

+51
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ describe('Single report', function () {
5353
compareCommits: jest.fn(() => {
5454
return compareCommitsResponse
5555
}),
56+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
57+
return {data: []}
58+
}),
5659
},
5760
issues: {
5861
createComment,
@@ -473,6 +476,54 @@ describe('Single report', function () {
473476
expect(createComment).toHaveBeenCalledTimes(0)
474477
})
475478
})
479+
480+
it('when pr-number present, add the comment in pr', async () => {
481+
core.getInput = jest.fn(c => {
482+
switch (c) {
483+
case 'pr-number':
484+
return '45'
485+
default:
486+
return getInput(c)
487+
}
488+
})
489+
initContext(eventName, payload)
490+
491+
await action.action()
492+
expect(createComment.mock.calls[0][0].body).toEqual(PROPER_COMMENT)
493+
})
494+
495+
it('when pr-number not present and associated PR available from commit, add the comment in pr', async () => {
496+
core.getInput = jest.fn(c => {
497+
switch (c) {
498+
case 'pr-number':
499+
return ''
500+
default:
501+
return getInput(c)
502+
}
503+
})
504+
github.getOctokit = jest.fn(() => {
505+
return {
506+
rest: {
507+
repos: {
508+
compareCommits: jest.fn(() => {
509+
return compareCommitsResponse
510+
}),
511+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
512+
return {data: [{number: 45}]}
513+
}),
514+
},
515+
issues: {
516+
createComment,
517+
listComments,
518+
updateComment,
519+
},
520+
},
521+
}
522+
})
523+
initContext(eventName, payload)
524+
await action.action()
525+
expect(createComment.mock.calls[0][0].body).toEqual(PROPER_COMMENT)
526+
})
476527
})
477528

478529
describe('Other than push or pull_request or pull_request_target event', function () {

action.yml

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ inputs:
3030
- 'pr_comment'
3131
- 'summary'
3232
- 'both'
33+
pr-number:
34+
description: 'The PR number to add the comment to. If not provided, the action will try to get it from the environment.'
35+
required: false
3336
skip-if-no-changes:
3437
description: "Comment won't be added if there is no coverage information present for the files changed"
3538
required: false

dist/index.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,30 @@ async function action() {
8181
if (!isValidCommentType(commentType)) {
8282
core.setFailed(`'comment-type' ${commentType} is invalid`);
8383
}
84+
let prNumber = Number(core.getInput('pr-number')) || undefined;
85+
const client = github.getOctokit(token);
8486
let base;
8587
let head;
86-
let prNumber;
8788
switch (event) {
8889
case 'pull_request':
8990
case 'pull_request_target':
9091
base = github.context.payload.pull_request?.base.sha;
9192
head = github.context.payload.pull_request?.head.sha;
92-
prNumber = github.context.payload.pull_request?.number;
93+
prNumber = prNumber ?? github.context.payload.pull_request?.number;
9394
break;
9495
case 'push':
9596
base = github.context.payload.before;
9697
head = github.context.payload.after;
98+
prNumber =
99+
prNumber ??
100+
(await getPrNumberAssociatedWithCommit(client, github.context.sha));
97101
break;
98102
default:
99103
core.setFailed(`Only pull requests and pushes are supported, ${github.context.eventName} not supported.`);
100104
return;
101105
}
102106
core.info(`base sha: ${base}`);
103107
core.info(`head sha: ${head}`);
104-
const client = github.getOctokit(token);
105108
if (debugMode)
106109
core.info(`reportPaths: ${reportPaths}`);
107110
const changedFiles = await getChangedFiles(base, head, client, debugMode);
@@ -234,6 +237,14 @@ const validCommentTypes = ['pr_comment', 'summary', 'both'];
234237
const isValidCommentType = (value) => {
235238
return validCommentTypes.includes(value);
236239
};
240+
async function getPrNumberAssociatedWithCommit(client, commitSha) {
241+
const response = await client.rest.repos.listPullRequestsAssociatedWithCommit({
242+
commit_sha: commitSha,
243+
owner: github.context.repo.owner,
244+
repo: github.context.repo.repo,
245+
});
246+
return response.data.length > 0 ? response.data[0].number : undefined;
247+
}
237248

238249

239250
/***/ }),

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/__tests__/action_aggregate.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ describe('Aggregate report', function () {
6969
compareCommits: jest.fn(() => {
7070
return compareCommitsResponse;
7171
}),
72+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
73+
return { data: [] };
74+
}),
7275
},
7376
issues: {
7477
createComment,

lib/__tests__/action_empty_aggregate.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ describe('Aggregate Empty report', function () {
6969
compareCommits: jest.fn(() => {
7070
return compareCommitsResponse;
7171
}),
72+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
73+
return { data: [] };
74+
}),
7275
},
7376
issues: {
7477
createComment,

lib/__tests__/action_empty_multiple.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ describe('Multiple Empty reports', function () {
9292
compareCommits: jest.fn(() => {
9393
return compareCommitsResponse;
9494
}),
95+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
96+
return { data: [] };
97+
}),
9598
},
9699
issues: {
97100
createComment: comment,

lib/__tests__/action_empty_report.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ describe('Single Empty report', function () {
6969
compareCommits: jest.fn(() => {
7070
return compareCommitsResponse;
7171
}),
72+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
73+
return { data: [] };
74+
}),
7275
},
7376
issues: {
7477
createComment,

lib/__tests__/action_multiple.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ describe('Multiple reports', function () {
9292
compareCommits: jest.fn(() => {
9393
return compareCommitsResponse;
9494
}),
95+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
96+
return { data: [] };
97+
}),
9598
},
9699
issues: {
97100
createComment: comment,

lib/__tests__/action_single.test.js

+48
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ describe('Single report', function () {
7373
compareCommits: jest.fn(() => {
7474
return compareCommitsResponse;
7575
}),
76+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
77+
return { data: [] };
78+
}),
7679
},
7780
issues: {
7881
createComment,
@@ -429,6 +432,51 @@ describe('Single report', function () {
429432
expect(createComment).toHaveBeenCalledTimes(0);
430433
});
431434
});
435+
it('when pr-number present, add the comment in pr', async () => {
436+
core.getInput = jest.fn(c => {
437+
switch (c) {
438+
case 'pr-number':
439+
return '45';
440+
default:
441+
return getInput(c);
442+
}
443+
});
444+
initContext(eventName, payload);
445+
await action.action();
446+
expect(createComment.mock.calls[0][0].body).toEqual(PROPER_COMMENT);
447+
});
448+
it('when pr-number not present and associated PR available from commit, add the comment in pr', async () => {
449+
core.getInput = jest.fn(c => {
450+
switch (c) {
451+
case 'pr-number':
452+
return '';
453+
default:
454+
return getInput(c);
455+
}
456+
});
457+
github.getOctokit = jest.fn(() => {
458+
return {
459+
rest: {
460+
repos: {
461+
compareCommits: jest.fn(() => {
462+
return compareCommitsResponse;
463+
}),
464+
listPullRequestsAssociatedWithCommit: jest.fn(() => {
465+
return { data: [{ number: 45 }] };
466+
}),
467+
},
468+
issues: {
469+
createComment,
470+
listComments,
471+
updateComment,
472+
},
473+
},
474+
};
475+
});
476+
initContext(eventName, payload);
477+
await action.action();
478+
expect(createComment.mock.calls[0][0].body).toEqual(PROPER_COMMENT);
479+
});
432480
});
433481
describe('Other than push or pull_request or pull_request_target event', function () {
434482
it('Fail by throwing appropriate error', async () => {

lib/src/action.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,30 @@ async function action() {
7474
if (!isValidCommentType(commentType)) {
7575
core.setFailed(`'comment-type' ${commentType} is invalid`);
7676
}
77+
let prNumber = Number(core.getInput('pr-number')) || undefined;
78+
const client = github.getOctokit(token);
7779
let base;
7880
let head;
79-
let prNumber;
8081
switch (event) {
8182
case 'pull_request':
8283
case 'pull_request_target':
8384
base = github.context.payload.pull_request?.base.sha;
8485
head = github.context.payload.pull_request?.head.sha;
85-
prNumber = github.context.payload.pull_request?.number;
86+
prNumber = prNumber ?? github.context.payload.pull_request?.number;
8687
break;
8788
case 'push':
8889
base = github.context.payload.before;
8990
head = github.context.payload.after;
91+
prNumber =
92+
prNumber ??
93+
(await getPrNumberAssociatedWithCommit(client, github.context.sha));
9094
break;
9195
default:
9296
core.setFailed(`Only pull requests and pushes are supported, ${github.context.eventName} not supported.`);
9397
return;
9498
}
9599
core.info(`base sha: ${base}`);
96100
core.info(`head sha: ${head}`);
97-
const client = github.getOctokit(token);
98101
if (debugMode)
99102
core.info(`reportPaths: ${reportPaths}`);
100103
const changedFiles = await getChangedFiles(base, head, client, debugMode);
@@ -227,3 +230,11 @@ const validCommentTypes = ['pr_comment', 'summary', 'both'];
227230
const isValidCommentType = (value) => {
228231
return validCommentTypes.includes(value);
229232
};
233+
async function getPrNumberAssociatedWithCommit(client, commitSha) {
234+
const response = await client.rest.repos.listPullRequestsAssociatedWithCommit({
235+
commit_sha: commitSha,
236+
owner: github.context.repo.owner,
237+
repo: github.context.repo.repo,
238+
});
239+
return response.data.length > 0 ? response.data[0].number : undefined;
240+
}

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jacoco-report",
3-
"version": "1.7.1",
3+
"version": "1.7.2",
44
"description": "Github action that publishes the JaCoCo report as a comment in the Pull Request",
55
"main": "lib/src/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)