Skip to content

Commit e4bbaf0

Browse files
authored
Add support for more events - schedule, workflow_dispatch, workflow_run (#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
1 parent 960061c commit e4bbaf0

8 files changed

+331
-33
lines changed

__tests__/action_empty_report.test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,7 @@ describe('Single Empty report', function () {
350350
it('Fail by throwing appropriate error', async () => {
351351
initContext('pr_review', {})
352352
core.setFailed = jest.fn(c => {
353-
expect(c).toEqual(
354-
'Only pull requests and pushes are supported, pr_review not supported.'
355-
)
353+
expect(c).toEqual('The event pr_review is not supported.')
356354
})
357355
core.setOutput = output
358356

__tests__/action_single.test.ts

+141-7
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ describe('Single report', function () {
5050
return {
5151
rest: {
5252
repos: {
53-
compareCommits: jest.fn(() => {
54-
return compareCommitsResponse
53+
compareCommits: jest.fn(({base, head}) => {
54+
if (base !== head) {
55+
return compareCommitsResponse
56+
} else {
57+
return {data: {files: []}}
58+
}
5559
}),
5660
listPullRequestsAssociatedWithCommit: jest.fn(() => {
5761
return {data: []}
@@ -70,6 +74,7 @@ describe('Single report', function () {
7074
})
7175
core.summary.addRaw = addRaw
7276
core.summary.write = write
77+
github.context.sha = 'guasft7asdtf78asfd87as6df7y2u3'
7378
})
7479

7580
const compareCommitsResponse = {
@@ -366,6 +371,7 @@ describe('Single report', function () {
366371
})
367372

368373
describe('Pull Request Target event', function () {
374+
const eventName = 'pull_request_target'
369375
const payload = {
370376
pull_request: {
371377
number: '45',
@@ -378,8 +384,15 @@ describe('Single report', function () {
378384
},
379385
}
380386

387+
it('publish proper comment', async () => {
388+
initContext(eventName, payload)
389+
await action.action()
390+
391+
expect(createComment.mock.calls[0][0].body).toEqual(PROPER_COMMENT)
392+
})
393+
381394
it('set overall coverage output', async () => {
382-
initContext('pull_request_target', payload)
395+
initContext(eventName, payload)
383396
core.setOutput = output
384397

385398
await action.action()
@@ -526,13 +539,128 @@ describe('Single report', function () {
526539
})
527540
})
528541

529-
describe('Other than push or pull_request or pull_request_target event', function () {
542+
describe('Schedule event', function () {
543+
const eventName = 'schedule'
544+
const payload = {}
545+
546+
it('publish project coverage comment', async () => {
547+
core.getInput = jest.fn(key => {
548+
switch (key) {
549+
case 'comment-type':
550+
return 'summary'
551+
default:
552+
return getInput(key)
553+
}
554+
})
555+
initContext(eventName, payload)
556+
557+
await action.action()
558+
559+
expect(addRaw.mock.calls[0][0]).toEqual(ONLY_PROJECT_COMMENT)
560+
expect(write).toHaveBeenCalledTimes(1)
561+
})
562+
563+
it('set overall coverage output', async () => {
564+
initContext(eventName, payload)
565+
core.setOutput = output
566+
567+
await action.action()
568+
569+
const out = output.mock.calls[0]
570+
expect(out).toEqual(['coverage-overall', 35.25])
571+
})
572+
})
573+
574+
describe('Workflow Dispatch event', function () {
575+
const eventName = 'workflow_dispatch'
576+
const payload = {}
577+
578+
it('publish project coverage comment', async () => {
579+
core.getInput = jest.fn(key => {
580+
switch (key) {
581+
case 'comment-type':
582+
return 'summary'
583+
default:
584+
return getInput(key)
585+
}
586+
})
587+
initContext(eventName, payload)
588+
589+
await action.action()
590+
591+
expect(addRaw.mock.calls[0][0]).toEqual(ONLY_PROJECT_COMMENT)
592+
expect(write).toHaveBeenCalledTimes(1)
593+
})
594+
595+
it('set overall coverage output', async () => {
596+
initContext(eventName, payload)
597+
core.setOutput = output
598+
599+
await action.action()
600+
601+
const out = output.mock.calls[0]
602+
expect(out).toEqual(['coverage-overall', 35.25])
603+
})
604+
})
605+
606+
describe('Workflow Run event', function () {
607+
const eventName = 'workflow_run'
608+
const payload = {
609+
workflow_run: {
610+
pull_requests: [
611+
{
612+
base: {
613+
sha: 'guasft7asdtf78asfd87as6df7y2u3',
614+
},
615+
head: {
616+
sha: 'aahsdflais76dfa78wrglghjkaghkj',
617+
},
618+
number: 45,
619+
},
620+
],
621+
},
622+
}
623+
624+
it('when proper payload present, publish proper comment', async () => {
625+
initContext(eventName, payload)
626+
627+
await action.action()
628+
629+
expect(createComment.mock.calls[0][0].body).toEqual(PROPER_COMMENT)
630+
})
631+
632+
it('when payload does not have pull_requests, publish project coverage comment', async () => {
633+
initContext(eventName, {})
634+
core.getInput = jest.fn(key => {
635+
switch (key) {
636+
case 'pr-number':
637+
return 45
638+
default:
639+
return getInput(key)
640+
}
641+
})
642+
643+
await action.action()
644+
645+
expect(createComment.mock.calls[0][0].body).toEqual(ONLY_PROJECT_COMMENT)
646+
})
647+
648+
it('set overall coverage output', async () => {
649+
initContext(eventName, payload)
650+
core.setOutput = output
651+
652+
await action.action()
653+
654+
const out = output.mock.calls[0]
655+
expect(out).toEqual(['coverage-overall', 35.25])
656+
})
657+
})
658+
659+
describe('Unsupported events', function () {
530660
it('Fail by throwing appropriate error', async () => {
531661
initContext('pr_review', {})
532662
core.setFailed = jest.fn(c => {
533-
expect(c).toEqual(
534-
'Only pull requests and pushes are supported, pr_review not supported.'
535-
)
663+
expect(c).toEqual('The event pr_review is not supported.')
536664
})
537665
core.setOutput = output
538666

@@ -561,3 +689,9 @@ const PROPER_COMMENT = `### JaCoCo Report
561689
|:-|:-|:-:|
562690
|[Math.kt](https://github.com/thsaravana/jacoco-playground/blob/14a554976c0e5909d8e69bc8cce72958c49a7dc5/src/main/kotlin/com/madrapps/jacoco/Math.kt)|42% **\`-42%\`**|:x:|
563691
|[Utility.java](https://github.com/thsaravana/jacoco-playground/blob/14a554976c0e5909d8e69bc8cce72958c49a7dc5/src/main/java/com/madrapps/jacoco/Utility.java)|18.03%|:green_apple:|`
692+
693+
const ONLY_PROJECT_COMMENT = `### JaCoCo Report
694+
|Overall Project|35.25%|:x:|
695+
|:-|:-|:-:|
696+
697+
> There is no coverage information present for the Files changed`

dist/index.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ async function action() {
8383
}
8484
let prNumber = Number(core.getInput('pr-number')) || undefined;
8585
const client = github.getOctokit(token);
86-
let base;
87-
let head;
86+
const sha = github.context.sha;
87+
let base = sha;
88+
let head = sha;
8889
switch (event) {
8990
case 'pull_request':
9091
case 'pull_request_target':
@@ -96,15 +97,33 @@ async function action() {
9697
base = github.context.payload.before;
9798
head = github.context.payload.after;
9899
prNumber =
99-
prNumber ??
100-
(await getPrNumberAssociatedWithCommit(client, github.context.sha));
100+
prNumber ?? (await getPrNumberAssociatedWithCommit(client, sha));
101+
break;
102+
case 'workflow_dispatch':
103+
case 'schedule':
104+
prNumber =
105+
prNumber ?? (await getPrNumberAssociatedWithCommit(client, sha));
106+
break;
107+
case 'workflow_run':
108+
const pullRequests = github.context.payload?.workflow_run?.pull_requests ?? [];
109+
if (pullRequests.length !== 0) {
110+
base = pullRequests[0]?.base?.sha;
111+
head = pullRequests[0]?.head?.sha;
112+
prNumber = prNumber ?? pullRequests[0]?.number;
113+
}
114+
else {
115+
prNumber =
116+
prNumber ?? (await getPrNumberAssociatedWithCommit(client, sha));
117+
}
101118
break;
102119
default:
103-
core.setFailed(`Only pull requests and pushes are supported, ${github.context.eventName} not supported.`);
120+
core.setFailed(`The event ${github.context.eventName} is not supported.`);
104121
return;
105122
}
106123
core.info(`base sha: ${base}`);
107124
core.info(`head sha: ${head}`);
125+
if (debugMode)
126+
core.info(`context: ${(0, util_1.debug)(github.context)}`);
108127
if (debugMode)
109128
core.info(`reportPaths: ${reportPaths}`);
110129
const changedFiles = await getChangedFiles(base, head, client, debugMode);

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_empty_report.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ describe('Single Empty report', function () {
316316
it('Fail by throwing appropriate error', async () => {
317317
initContext('pr_review', {});
318318
core.setFailed = jest.fn(c => {
319-
expect(c).toEqual('Only pull requests and pushes are supported, pr_review not supported.');
319+
expect(c).toEqual('The event pr_review is not supported.');
320320
});
321321
core.setOutput = output;
322322
await action.action();

0 commit comments

Comments
 (0)