generated from im-open/javascript-action-template
-
Notifications
You must be signed in to change notification settings - Fork 2
850 lines (716 loc) · 45 KB
/
build-and-review-pr.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
name: Build and Review PR
run-name: 'Build and Review PR #${{ github.event.pull_request.number }}'
on:
# https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token
#
# This workflow uses the pull_request trigger which prevents write permissions on the
# GH_TOKEN and secrets access from public forks. This should remain as a pull_request
# trigger to minimize the access public forks have in the repository. The reduced
# permissions are adequate but do mean that re-compiles and readme changes will have to be
# made manually by the PR author. These auto-updates could be done by this workflow
# for branches but in order to re-trigger a PR build (which is needed for status checks),
# we would make the commits with a different user and their PAT. To minimize exposure
# and complication we will request those changes be manually made by the PR author.
pull_request:
types: [opened, synchronize, reopened]
# paths:
# Do not include specific paths here. We always want this build to run and produce a
# status check which are branch protection rules can use. If this is skipped because of
# path filtering, a status check will not be created and we won't be able to merge the PR
# without disabling that requirement. If we have a status check that is always produced,
# we can also use that to require all branches be up to date before they are merged.
env:
EMPTY_JSON_RESULTS_FILE: './test/input-files/empty-results.json'
FAILING_JSON_RESULTS_FILE: './test/input-files/failing.json'
NO_TESTS_JSON_RESULTS_FILE: './test/input-files/no-tests.json'
PASSING_JSON_RESULTS_FILE: './test/input-files/passing.json'
TRUNCATE_JSON_RESULTS_FILE: './test/input-files/truncate.json'
jobs:
build-and-review-pr:
# This reusable workflow will check to see if an action's source code has changed based on
# whether the PR includes files that match the files-with-code arg or are in one of the
# dirs-with-code directories. If there are source code changes, this reusable workflow
# will then run the action's build (if one was provided) and update the README.md with the
# the latest version of the action. If those two steps result in any changes that need to
# be committed, the workflow will fail because the PR needs some updates. Instructions for
# updating the PR will be available in the build log, the workflow summary and as a PR
# comment if the PR came from a branch (not a fork).
# This workflow assumes:
# - The main README.md is at the root of the repo
# - The README contains a contribution guidelines and usage examples section
uses: im-open/.github/.github/workflows/reusable-build-and-review-pr.yml@v1
with:
action-name: ${{ github.repository }}
default-branch: main
readme-name: 'README.md'
# The id of the contribution guidelines section of the README.md
readme-contribution-id: '#contributing'
# The id of the usage examples section of the README.md
readme-examples-id: '#usage-examples'
# The files that contain source code for the action. Only files that affect the action's execution
# should be included like action.yml or package.json. Do not include files like README.md or .gitignore.
# Files do not need to be explicitly provided here if they fall under one of the dirs in dirs-with-code.
# ** This value must match the same files-with-code argument specified in increment-version-on-merge.yml.
files-with-code: 'action.yml,package.json,package-lock.json'
# The directories that contain source code for the action. Only dirs with files that affect the action's
# execution should be included like src or dist. Do not include dirs like .github or node_modules.
# ** This value must match the same dirs-with-code argument specified in increment-version-on-merge.yml.
dirs-with-code: 'src,dist'
# The npm script to run to build the action. This is typically 'npm run build' if the
# action needs to be compiled. For composite-run-steps actions this is typically empty.
build-command: 'npm run build'
unit-tests:
runs-on: ubuntu-latest
env:
PASSING_MD_FILE: './test/expected-markdown/unit-tests/passing-tests.md'
FAILING_MD_FILE: './test/expected-markdown/unit-tests/failing-tests.md'
NO_TESTS_MD_FILE: './test/expected-markdown/unit-tests/no-tests.md'
steps:
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' SETUP '
run: echo ""
- name: Setup - Checkout the action
uses: actions/checkout@v4
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 1 - MISSING TOKEN INPUT '
run: echo ""
- name: 1 - When process-jest-test-results is called with a missing github-token input
id: missing-github-token
if: always()
continue-on-error: true # This is needed because we expect the step to fail but we need it to "pass" in order for the test job to succeed.
uses: ./
with:
github-token: ''
results-file: '${{ env.EMPTY_JSON_RESULTS_FILE }}'
- name: 1 - Then the action outcome should be failure
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "failure" --actual "${{ steps.missing-github-token.outcome }}"
- name: 1 - And each of the outputs should be empty
if: always()
run: |
./test/assert-value-is-empty.sh --name "test-outcome output" --value "${{ steps.missing-github-token.outputs.test-outcome }}"
./test/assert-value-is-empty.sh --name "test-results-truncated output" --value "${{ steps.missing-github-token.outputs.test-results-truncated }}"
./test/assert-value-is-empty.sh --name "test-results-file-path output" --value "${{ steps.missing-github-token.outputs.test-results-file-path }}"
./test/assert-value-is-empty.sh --name "status-check-id output" --value "${{ steps.missing-github-token.outputs.status-check-id }}"
./test/assert-value-is-empty.sh --name "pr-comment-id output" --value "${{ steps.missing-github-token.outputs.pr-comment-id }}"
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 2 - MISSING RESULTS_FILE INPUT '
run: echo ""
- name: 2 - When process-jest-test-results is called with a missing results-file input
id: missing-results-file
if: always()
continue-on-error: true # This is needed because we expect the step to fail but we need it to "pass" in order for the test job to succeed.
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: ''
- name: 2 - Then the action outcome should be failure
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "failure" --actual "${{ steps.missing-results-file.outcome }}"
- name: 2 - And each of the outputs should be empty
if: always()
run: |
./test/assert-value-is-empty.sh --name "test-outcome output" --value "${{ steps.missing-results-file.outputs.test-outcome }}"
./test/assert-value-is-empty.sh --name "test-results-truncated output" --value "${{ steps.missing-results-file.outputs.test-results-truncated }}"
./test/assert-value-is-empty.sh --name "test-results-file-path output" --value "${{ steps.missing-results-file.outputs.test-results-file-path }}"
./test/assert-value-is-empty.sh --name "status-check-id output" --value "${{ steps.missing-results-file.outputs.status-check-id }}"
./test/assert-value-is-empty.sh --name "pr-comment-id output" --value "${{ steps.missing-results-file.outputs.pr-comment-id }}"
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 3 - RESULTS_FILE DOES NOT EXIST '
run: echo ""
- name: 3 - When process-jest-test-results is called with a results file that does not exist
id: file-does-not-exist
if: always()
continue-on-error: true # This is needed because we expect the step to fail but we need it to "pass" in order for the test job to succeed.
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: './test/expected-markdown/input-files/file-that-does-not-exist.json'
create-status-check: false
create-pr-comment: false
- name: 3 - Then the action outcome should be failure
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "failure" --actual "${{ steps.file-does-not-exist.outcome }}"
- name: 3 - And the 'test-outcome' output should be Failed
if: always()
run: |
./test/assert-values-match.sh --name "test-outcome output" --expected 'Failed' --actual "${{ steps.file-does-not-exist.outputs.test-outcome }}"
- name: 3 - And the remaining outputs should be empty
if: always()
run: |
./test/assert-value-is-empty.sh --name "test-results-truncated output" --value "${{ steps.file-does-not-exist.outputs.test-results-truncated }}"
./test/assert-value-is-empty.sh --name "test-results-file-path output" --value "${{ steps.file-does-not-exist.outputs.test-results-file-path }}"
./test/assert-value-is-empty.sh --name "status-check-id output" --value "${{ steps.file-does-not-exist.outputs.status-check-id }}"
./test/assert-value-is-empty.sh --name "pr-comment-id output" --value "${{ steps.file-does-not-exist.outputs.pr-comment-id }}"
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 4 - RESULTS_FILE IS EMPTY '
run: echo ""
- name: 4 - When process-jest-test-results is called with a results file that is empty
id: empty-file
if: always()
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: '${{ env.EMPTY_JSON_RESULTS_FILE }}'
create-status-check: false
create-pr-comment: false
- name: 4 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.empty-file.outcome }}"
- name: 4 - And the 'test-outcome' output should be Failed
if: always()
run: |
./test/assert-values-match.sh --name "test-outcome output" --expected 'Failed' --actual "${{ steps.empty-file.outputs.test-outcome }}"
- name: 4 - And the remaining outputs should be empty
if: always()
run: |
./test/assert-value-is-empty.sh --name "test-results-truncated output" --value "${{ steps.empty-file.outputs.test-results-truncated }}"
./test/assert-value-is-empty.sh --name "test-results-file-path output" --value "${{ steps.empty-file.outputs.test-results-file-path }}"
./test/assert-value-is-empty.sh --name "status-check-id output" --value "${{ steps.empty-file.outputs.status-check-id }}"
./test/assert-value-is-empty.sh --name "pr-comment-id output" --value "${{ steps.empty-file.outputs.pr-comment-id }}"
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 5 - PASSING TESTS '
run: echo ""
- name: 5 - When process-jest-test-results is called with a results file that has all passing tests
id: passing-tests
if: always()
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: '${{ env.PASSING_JSON_RESULTS_FILE }}'
create-status-check: false
create-pr-comment: false
timezone: 'America/Denver'
report-name: 'Passing Test Results'
- name: 5 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.passing-tests.outcome }}"
- name: 5 - And the 'test-outcome' output should be Passed
if: always()
run: |
./test/assert-values-match.sh --name "test-outcome output" --expected 'Passed' --actual "${{ steps.passing-tests.outputs.test-outcome }}"
- name: 5 - And the 'test-results-file-path output' should be populated
if: always()
run: ./test/assert-value-is-not-empty.sh --name "test-results-file-path output" --value "${{ steps.passing-tests.outputs.test-results-file-path }}"
- name: 5 - And the remaining outputs should be empty since status checks and pr comments were not created
if: always()
run: |
./test/assert-value-is-empty.sh --name "status-check-id output" --value "${{ steps.passing-tests.outputs.status-check-id }}"
./test/assert-value-is-empty.sh --name "pr-comment-id output" --value "${{ steps.passing-tests.outputs.pr-comment-id }}"
./test/assert-value-is-empty.sh --name "test-results-truncated output" --value "${{ steps.passing-tests.outputs.test-results-truncated }}"
- name: 5 - And the contents of test-results.md file should match the contents of ${{ env.PASSING_MD_FILE }} file
if: always()
run: |
# Comparing the test-results.md file will ensure that:
# - The provided timezone (MST/MDT) is used
# - The provided report name is used
# - The badge has the right count/status/color
# - The Duration stats are included in the report
# - The Counter stats are included in the report
expectedFileName="${{ env.PASSING_MD_FILE }}"
actualFileName="${{ steps.passing-tests.outputs.test-results-file-path }}"
./test/assert-file-contents-match.sh --expectedFileName $expectedFileName --actualFileName $actualFileName
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 6 - FAILING TESTS '
run: echo ""
- name: 6 - When process-jest-test-results is called with a results file that has failing tests
id: failing-tests
if: always()
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: '${{ env.FAILING_JSON_RESULTS_FILE }}'
create-status-check: false
create-pr-comment: false
# timezone: 'UTC' # Test the default
# report-name: 'Jest Test Results' # Test the default
- name: 6 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.failing-tests.outcome }}"
- name: 6 - And the 'test-outcome' output should be Failed
if: always()
run: |
./test/assert-values-match.sh --name "test-outcome output" --expected 'Failed' --actual "${{ steps.failing-tests.outputs.test-outcome }}"
- name: 6 - And the 'test-results-file-path output' should be populated
if: always()
run: ./test/assert-value-is-not-empty.sh --name "test-results-file-path output" --value "${{ steps.failing-tests.outputs.test-results-file-path }}"
- name: 6 - And the remaining outputs should be empty since status checks and pr comments were not created
if: always()
run: |
./test/assert-value-is-empty.sh --name "status-check-id output" --value "${{ steps.failing-tests.outputs.status-check-id }}"
./test/assert-value-is-empty.sh --name "pr-comment-id output" --value "${{ steps.failing-tests.outputs.pr-comment-id }}"
./test/assert-value-is-empty.sh --name "test-results-truncated output" --value "${{ steps.failing-tests.outputs.test-results-truncated }}"
- name: 6 - And the contents of test-results.md file should match the contents of ${{ env.FAILING_MD_FILE }} file
if: always()
run: |
# Comparing the test-results.md file will ensure that:
# - The default timezone (UTC) is used
# - The default report name (Jest Test Results) is used
# - The badge has the right count/status/color
# - The Duration stats are included in the report
# - The Counter stats are included in the report
# - The failing test details are included in the report
expectedFileName="${{ env.FAILING_MD_FILE }}"
actualFileName="${{ steps.failing-tests.outputs.test-results-file-path }}"
./test/assert-file-contents-match.sh --expectedFileName $expectedFileName --actualFileName $actualFileName
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 7 - NO TESTS REPORTED '
run: echo ""
- name: 7 - When process-jest-test-results is called with a results file that has no tests reported
id: no-tests
if: always()
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: '${{ env.NO_TESTS_JSON_RESULTS_FILE }}'
report-name: 'Missing Test Results'
create-status-check: false
create-pr-comment: false
- name: 7 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.no-tests.outcome }}"
- name: 7 - And the 'test-outcome' output should be Passed
if: always()
run: |
./test/assert-values-match.sh --name "test-outcome output" --expected 'Passed' --actual "${{ steps.no-tests.outputs.test-outcome }}"
- name: 7 - And the 'test-results-file-path output' should be populated
if: always()
run: ./test/assert-value-is-not-empty.sh --name "test-results-file-path output" --value "${{ steps.no-tests.outputs.test-results-file-path }}"
- name: 7 - And the remaining outputs should be empty since status checks and pr comments were not created
if: always()
run: |
./test/assert-value-is-empty.sh --name "status-check-id output" --value "${{ steps.no-tests.outputs.status-check-id }}"
./test/assert-value-is-empty.sh --name "pr-comment-id output" --value "${{ steps.no-tests.outputs.pr-comment-id }}"
./test/assert-value-is-empty.sh --name "test-results-truncated output" --value "${{ steps.no-tests.outputs.test-results-truncated }}"
- name: 7 - And the contents of test-results.md file should match the contents of ${{ env.NO_TESTS_MD_FILE }} file
if: always()
run: |
# Comparing the test-results.md file will ensure that:
# - The default timezone (UTC) is used
# - The default report name (Missing Test Results) is used
# - The badge has the right count/status/color
# - The Duration section should not be included in the report
# - The Counter stats are included in the report
# - The no-test details are included in the report
expectedFileName="${{ env.NO_TESTS_MD_FILE }}"
actualFileName="${{ steps.no-tests.outputs.test-results-file-path }}"
./test/assert-file-contents-match.sh --expectedFileName $expectedFileName --actualFileName $actualFileName
test-status-checks:
runs-on: ubuntu-latest
env:
NO_FAILURES_MD_FILE: './test/expected-markdown/status-checks/no-failures.md'
IGNORE_FAILURES_MD_FILE: './test/expected-markdown/status-checks/ignore-failures.md'
ALLOW_FAILURES_MD_FILE: './test/expected-markdown/status-checks/allow-failures.md'
NO_FAILURES_REPORT_NAME: 'No Failures Scenario'
IGNORE_FAILURES_REPORT_NAME: 'Ignore Failures Scenario'
ALLOW_FAILURES_REPORT_NAME: 'Allow Failures Scenario'
steps:
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' SETUP '
run: echo ""
- name: Setup - Fail test job if fork
run: |
if [ "${{ github.event.pull_request.head.repo.fork }}" == "true" ]; then
echo "This test job requires the `checks: write` scope on the GITHUB_TOKEN which PRs from forks do not have. Before this PR can be merged, the tests should be run on an intermediate branch created by repository owners."
exit 1
fi
- name: Setup - Checkout the action
uses: actions/checkout@v4
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 8 - STATUS CHECK - NO FAILURES '
run: echo ""
- name: 8 - When process-jest-test-results is called with no failures
if: always()
id: no-failures
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: ${{ env.PASSING_JSON_RESULTS_FILE }}
report-name: ${{ env.NO_FAILURES_REPORT_NAME }}
create-status-check: true
create-pr-comment: false
- name: 8 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.no-failures.outcome }}"
- name: 8 - And the status-check-id output should be populated
if: always()
run: ./test/assert-value-is-not-empty.sh --name "status-check-id output" --value "${{ steps.no-failures.outputs.status-check-id }}"
- name: 8 - And the test-outcome output should be Passed
if: always()
run: ./test/assert-values-match.sh --name "test-outcome output" --expected "Passed" --actual "${{ steps.no-failures.outputs.test-outcome }}"
- name: 8 - And the status check should match the inputs
if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const assertStatusCheckExists = require('./test/assert-status-check-exists.js');
const assertStatusCheckMatchesExpectations = require('./test/assert-status-check-matches-expectations.js');
const checkId = '${{ steps.no-failures.outputs.status-check-id }}';
const actualCheck = await assertStatusCheckExists(github, context, core, checkId);
const expectedBody = fs.readFileSync('${{ env.NO_FAILURES_MD_FILE }}', 'utf8');
const expectedValues = {
name: 'status check - ${{ env.NO_FAILURES_REPORT_NAME }}'.toLowerCase(),
status: 'completed',
conclusion: 'success',
title: '${{ env.NO_FAILURES_REPORT_NAME }}',
text: expectedBody
};
assertStatusCheckMatchesExpectations(core, actualCheck, expectedValues);
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 9 - STATUS CHECK - IGNORE FAILURES '
run: echo ""
- name: 9 - When process-jest-test-results is called with test failures & ignore-test-failures=true
if: always()
id: ignore-failures
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: ${{ env.FAILING_JSON_RESULTS_FILE }}
report-name: ${{ env.IGNORE_FAILURES_REPORT_NAME }}
create-status-check: true
ignore-test-failures: true
create-pr-comment: false
- name: 9 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.ignore-failures.outcome }}"
- name: 9 - And the status-check-id output should be populated
if: always()
run: ./test/assert-value-is-not-empty.sh --name "status-check-id output" --value "${{ steps.ignore-failures.outputs.status-check-id }}"
- name: 9 - And the test-outcome output should be Failed
if: always()
run: ./test/assert-values-match.sh --name "test-outcome output" --expected "Failed" --actual "${{ steps.ignore-failures.outputs.test-outcome }}"
- name: 9 - And the status check should match the inputs
if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const assertStatusCheckExists = require('./test/assert-status-check-exists.js');
const assertStatusCheckMatchesExpectations = require('./test/assert-status-check-matches-expectations.js');
const checkId = '${{ steps.ignore-failures.outputs.status-check-id }}';
const actualCheck = await assertStatusCheckExists(github, context, core, checkId);
const expectedBody = fs.readFileSync('${{ env.IGNORE_FAILURES_MD_FILE }}', 'utf8');
const expectedValues = {
name: 'status check - ${{ env.IGNORE_FAILURES_REPORT_NAME }}'.toLowerCase(),
status: 'completed',
conclusion: 'neutral',
title: '${{ env.IGNORE_FAILURES_REPORT_NAME }}',
text: expectedBody
};
assertStatusCheckMatchesExpectations(core, actualCheck, expectedValues);
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 10 - STATUS CHECK - ALLOW FAILURES '
run: echo ""
- name: 10 - When process-jest-test-results is called with test failures & ignore-test-failures=false
if: always()
id: allow-failures
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: ${{ env.FAILING_JSON_RESULTS_FILE }}
report-name: ${{ env.ALLOW_FAILURES_REPORT_NAME }}
create-status-check: true
ignore-test-failures: false
create-pr-comment: false
- name: 10 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.allow-failures.outcome }}"
- name: 10 - And the status-check-id output should be populated
if: always()
run: ./test/assert-value-is-not-empty.sh --name "status-check-id output" --value "${{ steps.allow-failures.outputs.status-check-id }}"
- name: 10 - And the test-outcome output should be Failed
if: always()
run: ./test/assert-values-match.sh --name "test-outcome output" --expected "Failed" --actual "${{ steps.allow-failures.outputs.test-outcome }}"
- name: 10 - And the status check should match the inputs
if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const assertStatusCheckExists = require('./test/assert-status-check-exists.js');
const assertStatusCheckMatchesExpectations = require('./test/assert-status-check-matches-expectations.js');
const checkId = '${{ steps.allow-failures.outputs.status-check-id }}';
const actualCheck = await assertStatusCheckExists(github, context, core, checkId);
const expectedBody = fs.readFileSync('${{ env.ALLOW_FAILURES_MD_FILE }}', 'utf8');
const expectedValues = {
name: 'status check - ${{ env.ALLOW_FAILURES_REPORT_NAME }}'.toLowerCase(),
status: 'completed',
conclusion: 'failure',
title: '${{ env.ALLOW_FAILURES_REPORT_NAME }}',
text: expectedBody
};
assertStatusCheckMatchesExpectations(core, actualCheck, expectedValues);
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEARDOWN '
run: echo ""
- name: Teardown - Modify failing Status Check conclusion
if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const updateFailingStatusCheck = require('./test/update-failing-status-check.js');
await updateFailingStatusCheck(github, context, core, '${{ steps.allow-failures.outputs.status-check-id }}');
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
test-pr-comments:
runs-on: ubuntu-latest
env:
EXISTING_COMMENT_ID: ''
COMMENT_IDENTIFIER: 'existing-comment-${{ github.run_id }}'
UPDATE_WITH_MATCHING_PREFIX_MD_FILE: './test/expected-markdown/pr-comments/update-matching-prefix.md'
UPDATE_WITHOUT_MATCHING_PREFIX_MD_FILE: './test/expected-markdown/pr-comments/update-without-matching-prefix.md'
NO_UPDATE_MD_FILE: './test/expected-markdown/pr-comments/no-update.md'
TRUNCATE_FULL_MD_FILE: './test/expected-markdown/pr-comments/truncate-full-markdown.md'
TRUNCATE_TRUNCATED_MD_FILE: './test/expected-markdown/pr-comments/truncate-truncated-markdown.md'
UPDATE_WITH_MATCHING_PREFIX_REPORT_NAME: 'Update Comment with Matching Prefix Scenario'
UPDATE_WITHOUT_MATCHING_PREFIX_REPORT_NAME: 'Update Comment but no Matching Prefix Scenario'
NO_UPDATE_REPORT_NAME: 'Do Not Update Comment Scenario'
TRUNCATE_FAILURES_REPORT_NAME: 'Truncated PR Comment Scenario'
steps:
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' SETUP '
run: echo ""
- name: Setup - Fail test job if fork
run: |
if [ "${{ github.event.pull_request.head.repo.fork }}" == "true" ]; then
echo "This test job requires the `pull_request: write` scope on the GITHUB_TOKEN which PRs from forks do not have. Before this PR can be merged, the tests should be run on an intermediate branch created by repository owners."
exit 1
fi
- name: Setup - Checkout the action
uses: actions/checkout@v4
- name: Setup - Delete pre-existing process-jest-test-results PR Comments
if: always()
uses: actions/github-script@v7
with:
script: |
const deletePrComments = require('./test/delete-pre-existing-comments.js');
await deletePrComments(github, context, core);
- name: Setup - Create a process-jest-test-results comment that can be updated
if: always()
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `<!-- im-open/process-jest-test-results ${{ env.COMMENT_IDENTIFIER }} -->\nThis comment will be replaced soon.`
})
.then(response => {
core.info(`The 'existing' process-jest-test-results comment has id: ${response.data.id}`);
core.exportVariable('EXISTING_COMMENT_ID', response.data.id);
})
.catch(error => {
core.setFailed(`An error occurred in the setup step while creating a comment: ${error.message}`);
});
await new Promise(r => setTimeout(r, 5 * 1000));
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 11 - PR COMMENT - UPDATE W/ MATCHING PREFIX '
run: echo ""
- name: 11 - When process-jest-test-results is called with updateComment=true and there is a comment with matching prefix
if: always()
id: update-with-matching-prefix
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: ${{ env.PASSING_JSON_RESULTS_FILE }}
report-name: ${{ env.UPDATE_WITH_MATCHING_PREFIX_REPORT_NAME }}
create-status-check: false
create-pr-comment: true
update-comment-if-one-exists: true
comment-identifier: ${{ env.COMMENT_IDENTIFIER }}
- name: 11 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.update-with-matching-prefix.outcome }}"
- name: 11 - And the pr-comment-id output should match the existing comment id
if: always()
run: ./test/assert-values-match.sh --name "pr-comment-id output" --expected "${{ env.EXISTING_COMMENT_ID }}" --actual "${{ steps.update-with-matching-prefix.outputs.pr-comment-id }}"
- name: 11 - And the test-results-truncated output should be false
if: always()
run: ./test/assert-values-match.sh --name "test-results-truncated output" --expected "false" --actual "${{ steps.update-with-matching-prefix.outputs.test-results-truncated }}"
- name: 11 - And the pr-comment should match the match the expected values
if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const assertCommentExists = require('./test/assert-pr-comment-exists.js');
const assertCommentMatchesExpectations = require('./test/assert-pr-comment-matches-expectations.js');
const commentId = '${{ steps.update-with-matching-prefix.outputs.pr-comment-id }}';
const actualComment = await assertCommentExists(github, context, core, commentId);
const expectedMarkdown = fs.readFileSync('${{ env.UPDATE_WITH_MATCHING_PREFIX_MD_FILE }}', 'utf8');
const actualTestResults = fs.readFileSync('${{ steps.update-with-matching-prefix.outputs.test-results-file-path }}', 'utf8');
const expectedComment = {
prefix: '<!-- im-open/process-jest-test-results ${{ env.COMMENT_IDENTIFIER }} -->',
fullMarkdown: expectedMarkdown,
action: 'updated',
truncated: false
};
assertCommentMatchesExpectations(core, actualComment, actualTestResults, expectedComment);
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 12 - PR COMMENT - UPDATE W/O MATCHING PREFIX '
run: echo ""
- name: 12 - When process-jest-test-results is called with updateComment=true but there is no comment with matching prefix
if: always()
id: update-without-matching-prefix
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: ${{ env.PASSING_JSON_RESULTS_FILE }}
report-name: ${{ env.UPDATE_WITHOUT_MATCHING_PREFIX_REPORT_NAME }}
create-status-check: false
create-pr-comment: true
update-comment-if-one-exists: true
comment-identifier: 'different-identifier-${{ github.run_id }}'
- name: 12 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.update-without-matching-prefix.outcome }}"
- name: 12 - And the pr-comment-id output should be different than the existing comment id
if: always()
run: ./test/assert-values-do-not-match.sh --name "pr-comment-id output" --value1 "${{ env.EXISTING_COMMENT_ID }}" --value2 "${{ steps.update-without-matching-prefix.outputs.pr-comment-id }}"
- name: 12 - And the test-results-truncated output should be false
if: always()
run: ./test/assert-values-match.sh --name "test-results-truncated output" --expected "false" --actual "${{ steps.update-without-matching-prefix.outputs.test-results-truncated }}"
- name: 12 - And the pr-comment should match the expected values
if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const assertCommentExists = require('./test/assert-pr-comment-exists.js');
const assertCommentMatchesExpectations = require('./test/assert-pr-comment-matches-expectations.js');
const commentId = '${{ steps.update-without-matching-prefix.outputs.pr-comment-id }}';
const actualComment = await assertCommentExists(github, context, core, commentId);
const expectedMarkdown = fs.readFileSync('${{ env.UPDATE_WITHOUT_MATCHING_PREFIX_MD_FILE }}', 'utf8');
const actualTestResults = fs.readFileSync('${{ steps.update-without-matching-prefix.outputs.test-results-file-path }}', 'utf8');
const expectedComment = {
prefix: '<!-- im-open/process-jest-test-results different-identifier-${{ github.run_id }} -->',
fullMarkdown: expectedMarkdown,
action: 'created',
truncated: false
};
assertCommentMatchesExpectations(core, actualComment, actualTestResults, expectedComment);
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 13 - PR COMMENT - NO UPDATE '
run: echo ""
- name: 13 - When process-jest-test-results is called with updateComment=false
if: always()
id: matching-prefix-no-update
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: ${{ env.PASSING_JSON_RESULTS_FILE }}
report-name: ${{ env.NO_UPDATE_REPORT_NAME }}
create-status-check: false
create-pr-comment: true
update-comment-if-one-exists: false
- name: 13 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.matching-prefix-no-update.outcome }}"
- name: 13 - And the pr-comment-id output should be different than the existing comment id
if: always()
run: ./test/assert-values-do-not-match.sh --name "pr-comment-id output" --value1 "${{ env.EXISTING_COMMENT_ID }}" --value2 "${{ steps.matching-prefix-no-update.outputs.pr-comment-id }}"
- name: 13 - And the test-results-truncated output should be false
if: always()
run: ./test/assert-values-match.sh --name "test-results-truncated output" --expected "false" --actual "${{ steps.matching-prefix-no-update.outputs.test-results-truncated }}"
- name: 13 - And the pr-comment should match the expected values
if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const assertCommentExists = require('./test/assert-pr-comment-exists.js');
const assertCommentMatchesExpectations = require('./test/assert-pr-comment-matches-expectations.js');
const commentId = '${{ steps.matching-prefix-no-update.outputs.pr-comment-id }}';
const actualComment = await assertCommentExists(github, context, core, commentId);
const expectedMarkdown = fs.readFileSync('${{ env.NO_UPDATE_MD_FILE }}', 'utf8');
const actualTestResults = fs.readFileSync('${{ steps.matching-prefix-no-update.outputs.test-results-file-path }}', 'utf8');
const expectedComment = {
prefix: `<!-- im-open/process-jest-test-results test-pr-comments_matching-prefix-no-update -->`,
fullMarkdown: expectedMarkdown,
action: 'created',
truncated: false
};
assertCommentMatchesExpectations(core, actualComment, actualTestResults, expectedComment);
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 14 - PR COMMENT - TRUNCATE '
run: echo ""
- name: 14 - When process-jest-test-results is called with a large comment that needs to be truncated
if: always()
id: truncate
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
results-file: ${{ env.TRUNCATE_JSON_RESULTS_FILE }}
report-name: ${{ env.TRUNCATE_FAILURES_REPORT_NAME }}
create-status-check: false
create-pr-comment: true
update-comment-if-one-exists: true
comment-identifier: ${{ env.COMMENT_IDENTIFIER }}
- name: 14 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.truncate.outcome }}"
- name: 14 - And the pr-comment-id output should match the existing comment id
if: always()
run: ./test/assert-values-match.sh --name "pr-comment-id output" --expected "${{ env.EXISTING_COMMENT_ID }}" --actual "${{ steps.truncate.outputs.pr-comment-id }}"
- name: 14 - And the test-results-truncated output should be true
if: always()
run: ./test/assert-values-match.sh --name "test-results-truncated output" --expected "true" --actual "${{ steps.truncate.outputs.test-results-truncated }}"
- name: 14 - And the pr-comment should match the expected values
if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const assertCommentExists = require('./test/assert-pr-comment-exists.js');
const assertCommentMatchesExpectations = require('./test/assert-pr-comment-matches-expectations.js');
const commentId = '${{ steps.truncate.outputs.pr-comment-id }}';
const actualComment = await assertCommentExists(github, context, core, commentId);
const expectedMarkdown = fs.readFileSync('${{ env.TRUNCATE_FULL_MD_FILE }}', 'utf8');
const expectedTruncatedMarkdown = fs.readFileSync('${{ env.TRUNCATE_TRUNCATED_MD_FILE }}', 'utf8');
const actualTestResults = fs.readFileSync('${{ steps.truncate.outputs.test-results-file-path }}', 'utf8');
const truncateMessage = 'Test results truncated due to character limit. See full report in output.';
const expectedComment = {
prefix: `<!-- im-open/process-jest-test-results ${{ env.COMMENT_IDENTIFIER }} -->`,
fullMarkdown: expectedMarkdown,
action: 'updated',
truncated: true,
truncatedMarkdown: expectedTruncatedMarkdown,
};
assertCommentMatchesExpectations(core, actualComment, actualTestResults, expectedComment);
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEARDOWN '
run: echo ""
- name: Teardown - Delete PR Comments
if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const deletePrComment = require('./test/delete-pr-comment.js');
await deletePrComment(github, context, core, '${{ env.EXISTING_COMMENT_ID }}');
await deletePrComment(github, context, core, '${{ steps.matching-prefix-no-update.outputs.pr-comment-id }}');
await deletePrComment(github, context, core, '${{ steps.update-without-matching-prefix.outputs.pr-comment-id }}');
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""