Skip to content

Commit 88df73a

Browse files
authored
Merge pull request #22673 from github/sam-robson/use-org-membership-labeler-app
Use org membership app for external contribution labels
2 parents d92e57c + 385947c commit 88df73a

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

‎.github/workflows/label-external-contributions.yml‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,21 @@ jobs:
2020
pull-requests: write
2121

2222
steps:
23+
- name: Create organization membership token
24+
id: membership-token
25+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
26+
with:
27+
client-id: ${{ vars.CODEQL_ORG_MEMBERS_APP_CLIENT_ID }}
28+
private-key: ${{ secrets.CODEQL_ORG_MEMBERS_APP_PRIVATE_KEY }}
29+
owner: ${{ github.repository_owner }}
30+
permission-members: read
31+
2332
- name: Label external contributions
2433
env:
34+
API_URL: ${{ github.api_url }}
2535
GH_TOKEN: ${{ github.token }}
36+
MEMBERS_TOKEN: ${{ steps.membership-token.outputs.token }}
37+
ORG: ${{ github.repository_owner }}
2638
REPO: ${{ github.repository }}
2739
run: |
2840
set -euo pipefail
@@ -46,13 +58,13 @@ jobs:
4658
(.head.repo.full_name | type == "string") and
4759
.head.repo.full_name != $repo and
4860
.user.type == "User" and
49-
.author_association != "MEMBER" and
50-
.author_association != "OWNER" and
61+
(.user.login | type == "string" and length > 0) and
5162
(any(.labels[]?; .name == $label) | not)' \
5263
>/dev/null <<<"$pr_json"; then
5364
continue
5465
fi
5566
67+
author=$(jq -r '.user.login' <<<"$pr_json")
5668
events=$(gh api --paginate \
5769
"repos/$REPO/issues/$pr_number/events?per_page=100" |
5870
jq -cs 'add')
@@ -63,6 +75,34 @@ jobs:
6375
continue
6476
fi
6577
78+
if ! membership_status=$(curl \
79+
--silent \
80+
--show-error \
81+
--output /dev/null \
82+
--write-out '%{http_code}' \
83+
--connect-timeout 10 \
84+
--max-time 30 \
85+
--header "Accept: application/vnd.github+json" \
86+
--header "Authorization: Bearer $MEMBERS_TOKEN" \
87+
--header "X-GitHub-Api-Version: 2022-11-28" \
88+
"$API_URL/orgs/$ORG/members/$author"); then
89+
echo "::error::Membership check failed for pull request #$pr_number."
90+
exit 1
91+
fi
92+
93+
case "$membership_status" in
94+
204)
95+
echo "Pull request #$pr_number was opened by an organization member; skipping."
96+
continue
97+
;;
98+
404)
99+
;;
100+
*)
101+
echo "::error::Membership check for pull request #$pr_number returned HTTP $membership_status."
102+
exit 1
103+
;;
104+
esac
105+
66106
jq -n --arg label "$label" '{labels: [$label]}' |
67107
gh api --method POST \
68108
"repos/$REPO/issues/$pr_number/labels" \

0 commit comments

Comments
 (0)