Skip to content

Commit fa34ffc

Browse files
committed
fix: check org membership before labeling contributions
1 parent 43c6106 commit fa34ffc

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,34 @@ 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:
2534
GH_TOKEN: ${{ github.token }}
35+
MEMBERS_TOKEN: ${{ steps.membership-token.outputs.token }}
36+
ORG: ${{ github.repository_owner }}
2637
REPO: ${{ github.repository }}
2738
run: |
2839
set -euo pipefail
2940
3041
label="external-contribution"
42+
# shellcheck disable=SC2016
43+
membership_query='
44+
query($login: String!, $org: String!) {
45+
user(login: $login) {
46+
organization(login: $org) {
47+
login
48+
}
49+
}
50+
}'
3151
updated_cutoff=$(date -u -d "1 hour ago" "+%Y-%m-%dT%H:%M:%SZ")
3252
3353
while IFS= read -r pr_number; do
@@ -46,13 +66,13 @@ jobs:
4666
(.head.repo.full_name | type == "string") and
4767
.head.repo.full_name != $repo and
4868
.user.type == "User" and
49-
.author_association != "MEMBER" and
50-
.author_association != "OWNER" and
69+
(.user.login | type == "string" and length > 0) and
5170
(any(.labels[]?; .name == $label) | not)' \
5271
>/dev/null <<<"$pr_json"; then
5372
continue
5473
fi
5574
75+
author=$(jq -r '.user.login' <<<"$pr_json")
5676
events=$(gh api --paginate \
5777
"repos/$REPO/issues/$pr_number/events?per_page=100" |
5878
jq -cs 'add')
@@ -63,6 +83,17 @@ jobs:
6383
continue
6484
fi
6585
86+
is_member=$(GH_TOKEN="$MEMBERS_TOKEN" gh api graphql \
87+
-f query="$membership_query" \
88+
-f login="$author" \
89+
-f org="$ORG" \
90+
--jq '.data.user.organization != null')
91+
92+
if [ "$is_member" = true ]; then
93+
echo "Pull request #$pr_number was opened by an organization member; skipping."
94+
continue
95+
fi
96+
6697
jq -n --arg label "$label" '{labels: [$label]}' |
6798
gh api --method POST \
6899
"repos/$REPO/issues/$pr_number/labels" \

0 commit comments

Comments
 (0)