Skip to content

Commit ab68ca3

Browse files
committed
ci: stop using pull_request event, use push
We can't access github's secrets from a pull_request event (for security reasons). Use the push event, and try not to send emails when we should not. Signed-off-by: Marc Poulhiès <[email protected]>
1 parent cd72ca8 commit ab68ca3

File tree

1 file changed

+69
-37
lines changed

1 file changed

+69
-37
lines changed

.github/workflows/send-emails.yml

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
name: Send emails for merged PR
22

33
on:
4-
pull_request:
5-
types:
6-
- closed
4+
push:
75
branches:
86
- "master"
97

108
jobs:
119
send_patches:
12-
if: github.event.pull_request.merged == true
1310
runs-on: ubuntu-latest
1411

1512
steps:
@@ -46,38 +43,43 @@ jobs:
4643
run: |
4744
echo "$GH_EVENT" > /tmp/gh_event.json
4845
49-
PR_BASE_REF=$(jq -r '.pull_request.base.sha' /tmp/gh_event.json)
50-
echo "PR_BASE_REF=$PR_BASE_REF" >> $GITHUB_ENV
46+
BEFORE_REF=$(jq -r '.before' /tmp/gh_event.json)
47+
echo "BEFORE_REF=$BEFORE_REF" >> $GITHUB_ENV
5148
52-
PR_NUMBER=$(jq -r '.pull_request.number' /tmp/gh_event.json)
53-
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
49+
AFTER_REF=$(jq -r '.after' /tmp/gh_event.json)
50+
echo "AFTER_REF=$AFTER_REF" >> $GITHUB_ENV
5451
55-
PR_TITLE=$(jq -r '.pull_request.title' /tmp/gh_event.json)
56-
echo "PR_TITLE=$PR_TITLE" >> $GITHUB_ENV
52+
# PR_NUMBER=$(jq -r '.pull_request.number' /tmp/gh_event.json)
53+
# echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
5754
58-
PR_URL=$(jq -r '.pull_request.html_url' /tmp/gh_event.json)
59-
echo "PR_URL=$PR_URL" >> $GITHUB_ENV
55+
# PR_TITLE=$(jq -r '.pull_request.title' /tmp/gh_event.json)
56+
# echo "PR_TITLE=$PR_TITLE" >> $GITHUB_ENV
6057
61-
PR_MERGE_COMMIT=$(jq -r '.pull_request.merge_commit_sha' /tmp/gh_event.json)
62-
echo "PR_MERGE_COMMIT=$PR_MERGE_COMMIT" >> $GITHUB_ENV
58+
# PR_URL=$(jq -r '.pull_request.html_url' /tmp/gh_event.json)
59+
# echo "PR_URL=$PR_URL" >> $GITHUB_ENV
6360
64-
PR_TARGET_BRANCH=$(jq -r '.pull_request.base.ref' /tmp/gh_event.json)
65-
echo "PR_TARGET_BRANCH=$PR_TARGET_BRANCH" >> $GITHUB_ENV
61+
# PR_MERGE_COMMIT=$(jq -r '.pull_request.merge_commit_sha' /tmp/gh_event.json)
62+
# echo "PR_MERGE_COMMIT=$PR_MERGE_COMMIT" >> $GITHUB_ENV
6663
67-
PR_LABELS=$(jq -r '[.pull_request.labels[].name] | join(",")' /tmp/gh_event.json)
68-
echo "PR_LABELS=$PR_LABELS" >> $GITHUB_ENV
64+
# PR_TARGET_BRANCH=$(jq -r '.pull_request.base.ref' /tmp/gh_event.json)
65+
# echo "PR_TARGET_BRANCH=$PR_TARGET_BRANCH" >> $GITHUB_ENV
6966
70-
REPO_SSH=$(jq -r '.repository.ssh_url' /tmp/gh_event.json)
71-
echo "REPO_SSH=$REPO_SSH" >> $GITHUB_ENV
67+
# PR_LABELS=$(jq -r '[.pull_request.labels[].name] | join(",")' /tmp/gh_event.json)
68+
# echo "PR_LABELS=$PR_LABELS" >> $GITHUB_ENV
69+
70+
# REPO_SSH=$(jq -r '.repository.ssh_url' /tmp/gh_event.json)
71+
# echo "REPO_SSH=$REPO_SSH" >> $GITHUB_ENV
7272
7373
echo "GH_TOKEN=${{ github.token }}" >> $GITHUB_ENV
7474
75-
echo "SERIES_DIR=/tmp/series" >> $GITHUB_ENV
75+
# echo "SERIES_DIR=/tmp/series" >> $GITHUB_ENV
7676
77-
- name: Get commit list from PR and skip the internal ones
77+
- name: Get commit list and skip the internal ones
7878
id: commits
7979
env:
8080
MAX_NUM_COMMITS: 30
81+
BEFORE_REF: ${{ github.event.before }}
82+
AFTER_REF: ${{ github.event.after }}
8183
run: |
8284
# Skip commits that touches any of these
8385
patterns=(".github/"
@@ -93,17 +95,44 @@ jobs:
9395
9496
rm -f /tmp/commits.txt
9597
98+
if [ "$BEFORE_REF" = "0000000000000000000000000000000000000000" ] ; then
99+
echo "New branch created, not sending anything" | tee $GITHUB_STEP_SUMMARY
100+
echo "has_commits=false" >> $GITHUB_OUTPUT
101+
exit 0
102+
fi
103+
104+
if [ "$AFTER_REF" = "0000000000000000000000000000000000000000" ] ; then
105+
echo "Branch is being deleted, not sending anything" | tee $GITHUB_STEP_SUMMARY
106+
echo "has_commits=false" >> $GITHUB_OUTPUT
107+
exit 0
108+
fi
109+
110+
if git merge-base --is-ancestor "$BEFORE_REF" "$AFTER_REF"; then
111+
echo "fast-forward push (not forced)"
112+
else
113+
echo "non-fast-forward push (force push or history rewrite), not sending anything" | tee $GITHUB_STEP_SUMMARY
114+
echo "has_commits=false" >> $GITHUB_OUTPUT
115+
exit 0
116+
fi
117+
118+
IS_MERGE=$(git show --pretty=%P -s HEAD | wc -w)
119+
if [ "$IS_MERGE" -gt 1 ] ; then
120+
echo "Last commit is a merge, don't send anything" | tee $GITHUB_STEP_SUMMARY
121+
echo "has_commits=false" >> $GITHUB_OUTPUT
122+
exit 0
123+
fi
124+
96125
# Fetch commits from the pull request (maybe they're from another repository)
97-
git fetch origin "pull/$PR_NUMBER/head"
126+
#git fetch origin "pull/$PR_NUMBER/head"
98127
99-
TOTAL=$(gh api repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/commits --paginate --jq '.[].sha'|wc -l)
128+
TOTAL=$(git log --oneline "$BEFORE_REF..$AFTER_REF" | wc -l)
100129
if [ "${TOTAL}" -gt "$MAX_NUM_COMMITS" ]; then
101-
echo "Pull request has too many commits"
130+
echo "Push has too many commits" | tee $GITHUB_STEP_SUMMARY
102131
echo "has_commits=false" >> $GITHUB_OUTPUT
103132
exit 0
104133
fi
105134
106-
gh api repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/commits --paginate --jq '.[].sha' | while read SHA1; do
135+
git log --reverse --format=%H "$BEFORE_REF..$AFTER_REF" | while read SHA1; do
107136
echo "Looking at $SHA1"
108137
if grep -q -E "$regex" <(git diff-tree --no-commit-id --name-only -r "$SHA1"); then
109138
echo "Touching something not to be upstreamed, skipping commit $SHA1"
@@ -127,14 +156,18 @@ jobs:
127156
- name: Check for label 'no-ml' to skip sending emails
128157
id: checklabel
129158
run: |
159+
# not sure how to do that with "push".
160+
echo "skip=false" >> $GITHUB_OUTPUT
161+
exit 0
162+
130163
# Skip if PR has label "no-ml"
131-
if echo "$PR_LABELS" | grep -qiE "(^|,)no-ml(,|$)"; then
132-
echo "Opt-out label present: skipping mailing list." | tee $GITHUB_STEP_SUMMARY
133-
echo "skip=true" >> $GITHUB_OUTPUT
134-
else
135-
echo "No opt-out label found"
136-
echo "skip=false" >> $GITHUB_OUTPUT
137-
fi
164+
# if echo "$PR_LABELS" | grep -qiE "(^|,)no-ml(,|$)"; then
165+
# echo "Opt-out label present: skipping mailing list." | tee $GITHUB_STEP_SUMMARY
166+
# echo "skip=true" >> $GITHUB_OUTPUT
167+
# else
168+
# echo "No opt-out label found"
169+
# echo "skip=false" >> $GITHUB_OUTPUT
170+
# fi
138171
139172
- name: Decide if we're sending something or not
140173
id: send_emails
@@ -149,13 +182,12 @@ jobs:
149182
set -euo pipefail
150183
151184
# Create a temporary branch that linearizes the PR commits
152-
git checkout -B ci-mail-patches "$PR_BASE_REF"
185+
git checkout -B ci-mail-patches "$BEFORE_REF"
153186
# Cherry-pick commits in the exact PR order (no-commit to batch, then commit)
154187
while read sha; do
155188
git cherry-pick "$sha"
156189
done < /tmp/commits.txt
157190
158-
159191
echo "This change was merged into the gccrs repository and is posted here for" >> /tmp/description.txt
160192
echo "upstream visibility and potential drive-by review, as requested by GCC" >> /tmp/description.txt
161193
echo "release managers." >> /tmp/description.txt
@@ -169,9 +201,9 @@ jobs:
169201
git format-patch \
170202
--subject-prefix="gccrs COMMIT" \
171203
--no-cover-letter \
172-
--base="$PR_BASE_REF" \
204+
--base="$BEFORE_REF" \
173205
--output-directory /tmp/series \
174-
"$PR_BASE_REF"..HEAD
206+
"$BEFORE_REF"..HEAD
175207
176208
echo "" >> /tmp/description.txt
177209

0 commit comments

Comments
 (0)