Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coverage report to PR for push event #144

Merged
merged 7 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions __tests__/action_aggregate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ describe('Aggregate report', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return {data: []}
}),
},
issues: {
createComment,
Expand Down
3 changes: 3 additions & 0 deletions __tests__/action_empty_aggregate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ describe('Aggregate Empty report', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return {data: []}
}),
},
issues: {
createComment,
Expand Down
3 changes: 3 additions & 0 deletions __tests__/action_empty_multiple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ describe('Multiple Empty reports', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return {data: []}
}),
},
issues: {
createComment: comment,
Expand Down
3 changes: 3 additions & 0 deletions __tests__/action_empty_report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ describe('Single Empty report', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return {data: []}
}),
},
issues: {
createComment,
Expand Down
3 changes: 3 additions & 0 deletions __tests__/action_multiple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ describe('Multiple reports', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return {data: []}
}),
},
issues: {
createComment: comment,
Expand Down
51 changes: 51 additions & 0 deletions __tests__/action_single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ describe('Single report', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return {data: []}
}),
},
issues: {
createComment,
Expand Down Expand Up @@ -473,6 +476,54 @@ describe('Single report', function () {
expect(createComment).toHaveBeenCalledTimes(0)
})
})

it('when pr-number present, add the comment in pr', async () => {
core.getInput = jest.fn(c => {
switch (c) {
case 'pr-number':
return '45'
default:
return getInput(c)
}
})
initContext(eventName, payload)

await action.action()
expect(createComment.mock.calls[0][0].body).toEqual(PROPER_COMMENT)
})

it('when pr-number not present and associated PR available from commit, add the comment in pr', async () => {
core.getInput = jest.fn(c => {
switch (c) {
case 'pr-number':
return ''
default:
return getInput(c)
}
})
github.getOctokit = jest.fn(() => {
return {
rest: {
repos: {
compareCommits: jest.fn(() => {
return compareCommitsResponse
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return {data: [{number: 45}]}
}),
},
issues: {
createComment,
listComments,
updateComment,
},
},
}
})
initContext(eventName, payload)
await action.action()
expect(createComment.mock.calls[0][0].body).toEqual(PROPER_COMMENT)
})
})

describe('Other than push or pull_request or pull_request_target event', function () {
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ inputs:
- 'pr_comment'
- 'summary'
- 'both'
pr-number:
description: 'The PR number to add the comment to. If not provided, the action will try to get it from the environment.'
required: false
skip-if-no-changes:
description: "Comment won't be added if there is no coverage information present for the files changed"
required: false
Expand Down
17 changes: 14 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,30 @@ async function action() {
if (!isValidCommentType(commentType)) {
core.setFailed(`'comment-type' ${commentType} is invalid`);
}
let prNumber = Number(core.getInput('pr-number')) || undefined;
const client = github.getOctokit(token);
let base;
let head;
let prNumber;
switch (event) {
case 'pull_request':
case 'pull_request_target':
base = github.context.payload.pull_request?.base.sha;
head = github.context.payload.pull_request?.head.sha;
prNumber = github.context.payload.pull_request?.number;
prNumber = prNumber ?? github.context.payload.pull_request?.number;
break;
case 'push':
base = github.context.payload.before;
head = github.context.payload.after;
prNumber =
prNumber ??
(await getPrNumberAssociatedWithCommit(client, github.context.sha));
break;
default:
core.setFailed(`Only pull requests and pushes are supported, ${github.context.eventName} not supported.`);
return;
}
core.info(`base sha: ${base}`);
core.info(`head sha: ${head}`);
const client = github.getOctokit(token);
if (debugMode)
core.info(`reportPaths: ${reportPaths}`);
const changedFiles = await getChangedFiles(base, head, client, debugMode);
Expand Down Expand Up @@ -234,6 +237,14 @@ const validCommentTypes = ['pr_comment', 'summary', 'both'];
const isValidCommentType = (value) => {
return validCommentTypes.includes(value);
};
async function getPrNumberAssociatedWithCommit(client, commitSha) {
const response = await client.rest.repos.listPullRequestsAssociatedWithCommit({
commit_sha: commitSha,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
});
return response.data.length > 0 ? response.data[0].number : undefined;
}


/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/__tests__/action_aggregate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ describe('Aggregate report', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse;
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return { data: [] };
}),
},
issues: {
createComment,
Expand Down
3 changes: 3 additions & 0 deletions lib/__tests__/action_empty_aggregate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ describe('Aggregate Empty report', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse;
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return { data: [] };
}),
},
issues: {
createComment,
Expand Down
3 changes: 3 additions & 0 deletions lib/__tests__/action_empty_multiple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ describe('Multiple Empty reports', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse;
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return { data: [] };
}),
},
issues: {
createComment: comment,
Expand Down
3 changes: 3 additions & 0 deletions lib/__tests__/action_empty_report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ describe('Single Empty report', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse;
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return { data: [] };
}),
},
issues: {
createComment,
Expand Down
3 changes: 3 additions & 0 deletions lib/__tests__/action_multiple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ describe('Multiple reports', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse;
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return { data: [] };
}),
},
issues: {
createComment: comment,
Expand Down
48 changes: 48 additions & 0 deletions lib/__tests__/action_single.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ describe('Single report', function () {
compareCommits: jest.fn(() => {
return compareCommitsResponse;
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return { data: [] };
}),
},
issues: {
createComment,
Expand Down Expand Up @@ -429,6 +432,51 @@ describe('Single report', function () {
expect(createComment).toHaveBeenCalledTimes(0);
});
});
it('when pr-number present, add the comment in pr', async () => {
core.getInput = jest.fn(c => {
switch (c) {
case 'pr-number':
return '45';
default:
return getInput(c);
}
});
initContext(eventName, payload);
await action.action();
expect(createComment.mock.calls[0][0].body).toEqual(PROPER_COMMENT);
});
it('when pr-number not present and associated PR available from commit, add the comment in pr', async () => {
core.getInput = jest.fn(c => {
switch (c) {
case 'pr-number':
return '';
default:
return getInput(c);
}
});
github.getOctokit = jest.fn(() => {
return {
rest: {
repos: {
compareCommits: jest.fn(() => {
return compareCommitsResponse;
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return { data: [{ number: 45 }] };
}),
},
issues: {
createComment,
listComments,
updateComment,
},
},
};
});
initContext(eventName, payload);
await action.action();
expect(createComment.mock.calls[0][0].body).toEqual(PROPER_COMMENT);
});
});
describe('Other than push or pull_request or pull_request_target event', function () {
it('Fail by throwing appropriate error', async () => {
Expand Down
17 changes: 14 additions & 3 deletions lib/src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,30 @@ async function action() {
if (!isValidCommentType(commentType)) {
core.setFailed(`'comment-type' ${commentType} is invalid`);
}
let prNumber = Number(core.getInput('pr-number')) || undefined;
const client = github.getOctokit(token);
let base;
let head;
let prNumber;
switch (event) {
case 'pull_request':
case 'pull_request_target':
base = github.context.payload.pull_request?.base.sha;
head = github.context.payload.pull_request?.head.sha;
prNumber = github.context.payload.pull_request?.number;
prNumber = prNumber ?? github.context.payload.pull_request?.number;
break;
case 'push':
base = github.context.payload.before;
head = github.context.payload.after;
prNumber =
prNumber ??
(await getPrNumberAssociatedWithCommit(client, github.context.sha));
break;
default:
core.setFailed(`Only pull requests and pushes are supported, ${github.context.eventName} not supported.`);
return;
}
core.info(`base sha: ${base}`);
core.info(`head sha: ${head}`);
const client = github.getOctokit(token);
if (debugMode)
core.info(`reportPaths: ${reportPaths}`);
const changedFiles = await getChangedFiles(base, head, client, debugMode);
Expand Down Expand Up @@ -227,3 +230,11 @@ const validCommentTypes = ['pr_comment', 'summary', 'both'];
const isValidCommentType = (value) => {
return validCommentTypes.includes(value);
};
async function getPrNumberAssociatedWithCommit(client, commitSha) {
const response = await client.rest.repos.listPullRequestsAssociatedWithCommit({
commit_sha: commitSha,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
});
return response.data.length > 0 ? response.data[0].number : undefined;
}
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jacoco-report",
"version": "1.7.1",
"version": "1.7.2",
"description": "Github action that publishes the JaCoCo report as a comment in the Pull Request",
"main": "lib/src/index.js",
"scripts": {
Expand Down
Loading