Skip to content

Commit 5e5bf3b

Browse files
committed
Modernize run-test workflow
1 parent b56cedd commit 5e5bf3b

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed

.github/workflows/run-tests.yml

+59-14
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,72 @@ name: Run tests
22

33
on:
44
pull_request_target:
5+
types: [opened, synchronize, labeled]
56
schedule:
67
- cron: '0 0 * * *'
78

89
jobs:
9-
php-tests:
10-
runs-on: ${{ matrix.os }}
10+
access_check:
11+
runs-on: ubuntu-latest
12+
name: Access check
13+
steps:
14+
- name: Ensure pull-request is safe to run
15+
uses: actions/github-script@v7
16+
with:
17+
github-token: ${{secrets.GITHUB_TOKEN}}
18+
script: |
19+
if (context.eventName === 'schedule') {
20+
return
21+
}
22+
23+
// If the user that pushed the commit is a maintainer, skip the check
24+
const collaborators = await github.rest.repos.listCollaborators({
25+
owner: context.repo.owner,
26+
repo: context.repo.repo
27+
});
28+
29+
if (collaborators.data.some(c => c.login === context.actor)) {
30+
console.log(`User ${context.actor} is allowed to run tests because they are a collaborator.`);
31+
return
32+
}
33+
34+
const issue_number = context.issue.number;
35+
const repository = context.repo.repo;
36+
const owner = context.repo.owner;
37+
38+
const response = await github.rest.issues.listLabelsOnIssue({
39+
owner,
40+
repo: repository,
41+
issue_number
42+
});
43+
const labels = response.data.map(label => label.name);
44+
let hasLabel = labels.includes('safe-to-test')
45+
46+
if (context.payload.action === 'synchronize' && hasLabel) {
47+
hasLabel = false
48+
await github.rest.issues.removeLabel({
49+
owner,
50+
repo: repository,
51+
issue_number,
52+
name: 'safe-to-test'
53+
});
54+
}
55+
56+
if (!hasLabel) {
57+
throw "Action was not authorized. Exiting now."
58+
}
1159
60+
php-tests:
61+
runs-on: ubuntu-latest
62+
needs: access_check
1263
strategy:
1364
matrix:
14-
os: [ubuntu-latest]
1565
payload:
1666
- { laravel: '11.*', php: '8.3', 'testbench': '9.*' }
1767
- { laravel: '11.*', php: '8.2', 'testbench': '9.*' }
18-
- { laravel: '10.*', php: '8.3', 'testbench': '8.*'}
19-
- { laravel: '10.*', php: '8.2', 'testbench': '8.*'}
20-
- { laravel: '10.*', php: '8.1', 'testbench': '8.*'}
68+
- { laravel: '10.*', php: '8.3', 'testbench': '8.*' }
69+
- { laravel: '10.*', php: '8.2', 'testbench': '8.*' }
70+
- { laravel: '10.*', php: '8.1', 'testbench': '8.*' }
2171

2272
name: PHP ${{ matrix.payload.php }} - Laravel ${{ matrix.payload.laravel }}
2373

@@ -36,7 +86,9 @@ jobs:
3686

3787
steps:
3888
- name: Checkout code
39-
uses: actions/checkout@v3
89+
uses: actions/checkout@v4
90+
with:
91+
ref: ${{ github.event.pull_request.head.sha }}
4092

4193
- name: Setup PHP
4294
uses: shivammathur/setup-php@v2
@@ -50,11 +102,4 @@ jobs:
50102
composer require "laravel/framework:${{ matrix.payload.laravel }}" "orchestra/testbench:${{ matrix.payload.testbench }}" --no-interaction --no-update
51103
composer update --prefer-stable --prefer-dist --no-interaction
52104
- name: Execute tests
53-
env:
54-
CI_DB_DRIVER: mysql
55-
CI_DB_HOST: 127.0.0.1
56-
CI_DB_PORT: 3307
57-
CI_DB_DATABASE: test
58-
CI_DB_USERNAME: root
59-
CI_DB_PASSWORD: root
60105
run: composer test

0 commit comments

Comments
 (0)