@@ -4,6 +4,9 @@ name: Codebuild
44on :
55 push :
66 branches : [main]
7+ # This event can use aws credentials, but runs against upstream code instead of PR code.
8+ # https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request_target
9+ # https://securitylab.github.com/resources/github-actions-preventing-pwn-requests/
710 pull_request_target :
811 branches : [main]
912
@@ -14,22 +17,44 @@ jobs:
1417 id-token : write
1518 contents : read
1619 env :
17- event_name : ${{ github.event_name }}
1820 source_pr : pr/${{ github.event.pull_request.number }}
1921 source_sha : ${{ github.sha }}
22+ pr_author : ${{ github.event.pull_request.user.login }}
2023 steps :
2124 - uses : actions/checkout@v4
25+
26+ - name : Get permissions
27+ id : get_permission
28+ if : github.event_name == 'pull_request_target'
29+ uses : octokit/request-action@v2.x
30+ with :
31+ route : GET /repos/{repo}/collaborators/{author}/permission
32+ repo : ${{ github.repository }}
33+ author : ${{ github.event.pull_request.user.login }}
34+ env :
35+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
36+
2237 - name : Get credentials
2338 uses : aws-actions/configure-aws-credentials@v4.2.1
2439 with :
2540 role-to-assume : arn:aws:iam::003495580562:role/GitHubOIDCRole
2641 role-session-name : ${{ github.run_id }}
2742 aws-region : us-west-2
28- - name : Start Codebuild
43+
44+ - name : Start Codebuild for SHA
45+ # This version runs when PRs are merged to main
46+ if : github.event_name != 'pull_request_target'
47+ run : ./codebuild/bin/start_codebuild.sh $source_sha
48+
49+ - name : Start Codebuild for PR
50+ # This version runs when PRs are created or updated
51+ if : github.event_name == 'pull_request_target'
2952 run : |
30- if [[ "$event_name" == "pull_request_target" ]]; then
31- source=$source_pr
53+ permission=$(jq -r '.permission' <<< '${{ steps.get_permission.outputs.data }}')
54+ echo "$pr_author has permission '$permission'".
55+ if [[ "$permission" == "admin" || "$permission" == "write" ]]; then
56+ ./codebuild/bin/start_codebuild.sh $source_pr
3257 else
33- source=$source_sha
58+ echo "$pr_author does not have write permissions."
59+ echo "A maintainer will need to manually run start_codebuild.sh."
3460 fi
35- ./codebuild/bin/start_codebuild.sh $source
0 commit comments