Skip to content

Commit

Permalink
Add support for more events - schedule, workflow_dispatch, workflow_r…
Browse files Browse the repository at this point in the history
…un (#145)

* Add support for more events

* Adding debug

* Adding debug

* Setting proper sha for new events

* Set proper sha and pr_number for workflow_run event

* Adding tests
  • Loading branch information
thsaravana authored Sep 20, 2024
1 parent 960061c commit e4bbaf0
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 33 deletions.
4 changes: 1 addition & 3 deletions __tests__/action_empty_report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,7 @@ describe('Single Empty report', function () {
it('Fail by throwing appropriate error', async () => {
initContext('pr_review', {})
core.setFailed = jest.fn(c => {
expect(c).toEqual(
'Only pull requests and pushes are supported, pr_review not supported.'
)
expect(c).toEqual('The event pr_review is not supported.')
})
core.setOutput = output

Expand Down
148 changes: 141 additions & 7 deletions __tests__/action_single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ describe('Single report', function () {
return {
rest: {
repos: {
compareCommits: jest.fn(() => {
return compareCommitsResponse
compareCommits: jest.fn(({base, head}) => {
if (base !== head) {
return compareCommitsResponse
} else {
return {data: {files: []}}
}
}),
listPullRequestsAssociatedWithCommit: jest.fn(() => {
return {data: []}
Expand All @@ -70,6 +74,7 @@ describe('Single report', function () {
})
core.summary.addRaw = addRaw
core.summary.write = write
github.context.sha = 'guasft7asdtf78asfd87as6df7y2u3'
})

const compareCommitsResponse = {
Expand Down Expand Up @@ -366,6 +371,7 @@ describe('Single report', function () {
})

describe('Pull Request Target event', function () {
const eventName = 'pull_request_target'
const payload = {
pull_request: {
number: '45',
Expand All @@ -378,8 +384,15 @@ describe('Single report', function () {
},
}

it('publish proper comment', async () => {
initContext(eventName, payload)
await action.action()

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

it('set overall coverage output', async () => {
initContext('pull_request_target', payload)
initContext(eventName, payload)
core.setOutput = output

await action.action()
Expand Down Expand Up @@ -526,13 +539,128 @@ describe('Single report', function () {
})
})

describe('Other than push or pull_request or pull_request_target event', function () {
describe('Schedule event', function () {
const eventName = 'schedule'
const payload = {}

it('publish project coverage comment', async () => {
core.getInput = jest.fn(key => {
switch (key) {
case 'comment-type':
return 'summary'
default:
return getInput(key)
}
})
initContext(eventName, payload)

await action.action()

expect(addRaw.mock.calls[0][0]).toEqual(ONLY_PROJECT_COMMENT)
expect(write).toHaveBeenCalledTimes(1)
})

it('set overall coverage output', async () => {
initContext(eventName, payload)
core.setOutput = output

await action.action()

const out = output.mock.calls[0]
expect(out).toEqual(['coverage-overall', 35.25])
})
})

describe('Workflow Dispatch event', function () {
const eventName = 'workflow_dispatch'
const payload = {}

it('publish project coverage comment', async () => {
core.getInput = jest.fn(key => {
switch (key) {
case 'comment-type':
return 'summary'
default:
return getInput(key)
}
})
initContext(eventName, payload)

await action.action()

expect(addRaw.mock.calls[0][0]).toEqual(ONLY_PROJECT_COMMENT)
expect(write).toHaveBeenCalledTimes(1)
})

it('set overall coverage output', async () => {
initContext(eventName, payload)
core.setOutput = output

await action.action()

const out = output.mock.calls[0]
expect(out).toEqual(['coverage-overall', 35.25])
})
})

describe('Workflow Run event', function () {
const eventName = 'workflow_run'
const payload = {
workflow_run: {
pull_requests: [
{
base: {
sha: 'guasft7asdtf78asfd87as6df7y2u3',
},
head: {
sha: 'aahsdflais76dfa78wrglghjkaghkj',
},
number: 45,
},
],
},
}

it('when proper payload present, publish proper comment', async () => {
initContext(eventName, payload)

await action.action()

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

it('when payload does not have pull_requests, publish project coverage comment', async () => {
initContext(eventName, {})
core.getInput = jest.fn(key => {
switch (key) {
case 'pr-number':
return 45
default:
return getInput(key)
}
})

await action.action()

expect(createComment.mock.calls[0][0].body).toEqual(ONLY_PROJECT_COMMENT)
})

it('set overall coverage output', async () => {
initContext(eventName, payload)
core.setOutput = output

await action.action()

const out = output.mock.calls[0]
expect(out).toEqual(['coverage-overall', 35.25])
})
})

describe('Unsupported events', function () {
it('Fail by throwing appropriate error', async () => {
initContext('pr_review', {})
core.setFailed = jest.fn(c => {
expect(c).toEqual(
'Only pull requests and pushes are supported, pr_review not supported.'
)
expect(c).toEqual('The event pr_review is not supported.')
})
core.setOutput = output

Expand Down Expand Up @@ -561,3 +689,9 @@ const PROPER_COMMENT = `### JaCoCo Report
|:-|:-|:-:|
|[Math.kt](https://github.com/thsaravana/jacoco-playground/blob/14a554976c0e5909d8e69bc8cce72958c49a7dc5/src/main/kotlin/com/madrapps/jacoco/Math.kt)|42% **\`-42%\`**|:x:|
|[Utility.java](https://github.com/thsaravana/jacoco-playground/blob/14a554976c0e5909d8e69bc8cce72958c49a7dc5/src/main/java/com/madrapps/jacoco/Utility.java)|18.03%|:green_apple:|`

const ONLY_PROJECT_COMMENT = `### JaCoCo Report
|Overall Project|35.25%|:x:|
|:-|:-|:-:|
> There is no coverage information present for the Files changed`
29 changes: 24 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ async function action() {
}
let prNumber = Number(core.getInput('pr-number')) || undefined;
const client = github.getOctokit(token);
let base;
let head;
const sha = github.context.sha;
let base = sha;
let head = sha;
switch (event) {
case 'pull_request':
case 'pull_request_target':
Expand All @@ -96,15 +97,33 @@ async function action() {
base = github.context.payload.before;
head = github.context.payload.after;
prNumber =
prNumber ??
(await getPrNumberAssociatedWithCommit(client, github.context.sha));
prNumber ?? (await getPrNumberAssociatedWithCommit(client, sha));
break;
case 'workflow_dispatch':
case 'schedule':
prNumber =
prNumber ?? (await getPrNumberAssociatedWithCommit(client, sha));
break;
case 'workflow_run':
const pullRequests = github.context.payload?.workflow_run?.pull_requests ?? [];
if (pullRequests.length !== 0) {
base = pullRequests[0]?.base?.sha;
head = pullRequests[0]?.head?.sha;
prNumber = prNumber ?? pullRequests[0]?.number;
}
else {
prNumber =
prNumber ?? (await getPrNumberAssociatedWithCommit(client, sha));
}
break;
default:
core.setFailed(`Only pull requests and pushes are supported, ${github.context.eventName} not supported.`);
core.setFailed(`The event ${github.context.eventName} is not supported.`);
return;
}
core.info(`base sha: ${base}`);
core.info(`head sha: ${head}`);
if (debugMode)
core.info(`context: ${(0, util_1.debug)(github.context)}`);
if (debugMode)
core.info(`reportPaths: ${reportPaths}`);
const changedFiles = await getChangedFiles(base, head, client, debugMode);
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/__tests__/action_empty_report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ describe('Single Empty report', function () {
it('Fail by throwing appropriate error', async () => {
initContext('pr_review', {});
core.setFailed = jest.fn(c => {
expect(c).toEqual('Only pull requests and pushes are supported, pr_review not supported.');
expect(c).toEqual('The event pr_review is not supported.');
});
core.setOutput = output;
await action.action();
Expand Down
Loading

0 comments on commit e4bbaf0

Please sign in to comment.