@@ -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 merge_group :
@@ -17,22 +20,44 @@ jobs:
1720 id-token : write
1821 contents : read
1922 env :
20- event_name : ${{ github.event_name }}
2123 source_pr : pr/${{ github.event.pull_request.number }}
2224 source_sha : ${{ github.sha }}
25+ pr_author : ${{ github.event.pull_request.user.login }}
2326 steps :
2427 - uses : actions/checkout@v4
28+
29+ - name : Get permissions
30+ id : get_permission
31+ if : github.event_name == 'pull_request_target'
32+ uses : octokit/request-action@v2.x
33+ with :
34+ route : GET /repos/{repo}/collaborators/{author}/permission
35+ repo : ${{ github.repository }}
36+ author : ${{ github.event.pull_request.user.login }}
37+ env :
38+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
39+
2540 - name : Get credentials
2641 uses : aws-actions/configure-aws-credentials@v4.2.1
2742 with :
2843 role-to-assume : arn:aws:iam::024603541914:role/GitHubOIDCRole
2944 role-session-name : ${{ github.run_id }}
3045 aws-region : us-west-2
31- - name : Start Codebuild
46+
47+ - name : Start Codebuild for SHA
48+ # This version runs when PRs are added to the merge queue or merged to main
49+ if : github.event_name != 'pull_request_target'
50+ run : ./codebuild/bin/start_codebuild.sh $source_sha
51+
52+ - name : Start Codebuild for PR
53+ # This version runs when PRs are created or updated
54+ if : github.event_name == 'pull_request_target'
3255 run : |
33- if [[ "$event_name" == "pull_request_target" ]]; then
34- source=$source_pr
56+ permission=$(jq -r '.permission' <<< '${{ steps.get_permission.outputs.data }}')
57+ echo "$pr_author has permission '$permission'".
58+ if [[ "$permission" == "admin" || "$permission" == "write" ]]; then
59+ ./codebuild/bin/start_codebuild.sh $source_pr
3560 else
36- source=$source_sha
61+ echo "$pr_author does not have write permissions."
62+ echo "A maintainer will need to manually run start_codebuild.sh."
3763 fi
38- ./codebuild/bin/start_codebuild.sh $source
0 commit comments