Skip to content

Commit 6ff2221

Browse files
committed
fix: allow test suite to run via PR workflow.
1 parent 820feaa commit 6ff2221

File tree

6 files changed

+157
-151
lines changed

6 files changed

+157
-151
lines changed

.github/workflows/test-exclude.yml

Lines changed: 0 additions & 66 deletions
This file was deleted.

.github/workflows/test-include.yml

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Test Negative
2+
on:
3+
pull_request: {}
4+
5+
jobs:
6+
setup:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Setup
10+
run: echo "Do setup"
11+
12+
create-release:
13+
runs-on: ubuntu-latest
14+
continue-on-error: true
15+
needs: [setup]
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v3
19+
20+
- id: create-release
21+
run: |
22+
release_name="${PREFIX}$(git rev-parse --short HEAD)"
23+
gh release create ${release_name} --prerelease --target ${{ github.sha }} --notes "Created by ${{ github.workflow }} (will be automatically deleted)."
24+
echo "name=${release_name}" > $GITHUB_OUTPUT
25+
echo "id=$(gh release view v0.0.0-test.exclude.${{ steps.sha-short.outputs.result }} --json id -q '.id')" >> $GITHUB_OUTPUT
26+
env:
27+
# Below is the main difference between this workflow's positive counterpart.
28+
# The action will not pick up a comment because the provided 'include_regex' will not be able to match it.
29+
PREFIX: "v0.0.0-test.exclude."
30+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
32+
outputs:
33+
name: ${{ steps.create-release.outputs.name }}
34+
id: ${{ steps.create-release.outputs.id }}
35+
36+
assert:
37+
runs-on: ubuntu-latest
38+
needs: [create-release]
39+
timeout-minutes: 5 # Timeout for test release workflow to run
40+
steps:
41+
- uses: actions/checkout@v4
42+
43+
- uses: ./
44+
with:
45+
release_id: ${{ needs.create-release.outputs.name}}
46+
include_regex: "/v0\\.0\\.0-test\\.include\\.*./g" # Should not find a comment
47+
48+
- name: Find Comment (should fail)
49+
uses: peter-evans/find-comment@v3
50+
id: fc
51+
continue-on-error: true
52+
with:
53+
issue-number: ${{ github.event.pull_request.number }}
54+
body-regex: "^These changes were released in .*${{ needs.create-release.outputs.name}}.*$"
55+
56+
- name: Assert Comment Not Found
57+
if: steps.fc.outcome != 'success'
58+
run: exit 0
59+
60+
teardown:
61+
runs-on: ubuntu-latest
62+
needs: [create-release, assert]
63+
if: ${{ always() }}
64+
steps:
65+
- run: gh release delete ${{ needs.test.outputs.release }}
66+
env:
67+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Test Positive
2+
on:
3+
pull_request: {}
4+
5+
jobs:
6+
setup:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Setup
10+
run: echo "Do setup"
11+
12+
create-release:
13+
runs-on: ubuntu-latest
14+
continue-on-error: true
15+
needs: [setup]
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v3
19+
20+
- id: create-release
21+
run: |
22+
release_name="${PREFIX}$(git rev-parse --short HEAD)"
23+
gh release create ${release_name} --prerelease --target ${{ github.sha }} --notes "Created by ${{ github.workflow }} (will be automatically deleted)."
24+
echo "name=${release_name}" > $GITHUB_OUTPUT
25+
echo "id=$(gh release view v0.0.0-test.exclude.${{ steps.sha-short.outputs.result }} --json id -q '.id')" >> $GITHUB_OUTPUT
26+
env:
27+
# Below is the main difference between this workflow's negative counterpart.
28+
# The action _will_ pick up a comment because the provided 'include_regex' will be able to match it.
29+
PREFIX: "v0.0.0-test.include."
30+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
32+
outputs:
33+
name: ${{ steps.create-release.outputs.name }}
34+
id: ${{ steps.create-release.outputs.id }}
35+
36+
assert:
37+
runs-on: ubuntu-latest
38+
needs: [create-release]
39+
timeout-minutes: 5 # Timeout for test release workflow to run
40+
steps:
41+
- uses: actions/checkout@v4
42+
43+
- uses: ./
44+
with:
45+
release_id: ${{ needs.create-release.outputs.name}}
46+
include_regex: "/v0\\.0\\.0-test\\.include\\.*./g" # Should not find a comment
47+
48+
- name: Find Comment (should pass)
49+
uses: peter-evans/find-comment@v3
50+
id: fc
51+
with:
52+
issue-number: ${{ github.event.pull_request.number }}
53+
body-regex: "^These changes were released in .*${{ needs.create-release.outputs.name}}.*$"
54+
55+
teardown:
56+
runs-on: ubuntu-latest
57+
needs: [create-release, assert]
58+
if: ${{ always() }}
59+
steps:
60+
- run: gh release delete ${{ needs.test.outputs.release }}
61+
env:
62+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test-release.yml

Lines changed: 0 additions & 25 deletions
This file was deleted.

action.yml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ inputs:
1313
required: false
1414
description: "JavaScript regex used to filter releases."
1515
default: "/.*/g"
16+
release_id:
17+
required: false
18+
description: "The ID of the release. Defaults to `github.event.release.id`."
19+
default: ""
1620
runs:
1721
using: "composite"
1822
steps:
@@ -21,6 +25,25 @@ runs:
2125
result-encoding: string
2226
retries: ${{ inputs.retries }}
2327
script: |
28+
isReleaseEvent = false;
29+
switch(context.eventName) {
30+
case ('release'):
31+
isReleaseEvent = true;
32+
break;
33+
case ('workflow_run'):
34+
console.log("warning::This action is designed to run on 'release', not 'workflow_run'. Even when corresponding to a workflow triggered by 'release', 'workflow_run' may not work due to its event context schema.");
35+
break;
36+
case ('pull_request'):
37+
console.log("warning::This action is meant to only run on 'release'. 'pull_request' is reserved for testing.");
38+
break;
39+
default:
40+
throw new Error(`Unsupported event: ${context.eventName}`);
41+
}
42+
43+
if (context.eventName === 'pull_request' && length("${{ inputs.release_id }}") === 0) {
44+
throw new Error(`Event is ${context.eventName} but inputs.release_id is not provided.`);
45+
}
46+
2447
// Function to check if a value is unique in an array
2548
function onlyUnique(value, index, array) {
2649
return array.indexOf(value) === index;
@@ -53,7 +76,11 @@ runs:
5376
}
5477
5578
// Retrieving the ID of the current release
56-
release_id = context.payload.release.id;
79+
if (length("${{ inputs.release_id }}") > 0) {
80+
release_id = "${{ inputs.release_id }}";
81+
} else {
82+
release_id = context.payload.release.id;
83+
}
5784
5885
// Fetching details of the current release
5986
currentReleaseResponse = await github.rest.repos.getRelease({

0 commit comments

Comments
 (0)