|
1 | 1 | #!/bin/sh |
2 | | -# Merge open PRs from sagemath/sage labeled "blocker". |
3 | | -REPO="sagemath/sage" |
4 | | -GH="gh -R $REPO" |
5 | | -PRs="$($GH pr list --label "p: blocker / 1" --json number --jq '.[].number')" |
6 | | -if [ -z "$PRs" ]; then |
7 | | - echo 'Nothing to do: Found no open PRs with "blocker" status.' |
8 | | -else |
9 | | - echo "Found PRs: " $PRs |
10 | | - export GIT_AUTHOR_NAME="ci-sage workflow" |
11 | | - export GIT_AUTHOR_EMAIL= "[email protected]" |
12 | | - export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME" |
13 | | - export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL" |
14 | | - git tag -f test_base |
15 | | - git commit -q -m "Uncommitted changes" --no-allow-empty -a |
16 | | - for a in $PRs; do |
17 | | - git fetch --unshallow --all > /dev/null 2>&1 && echo "Unshallowed." |
18 | | - echo "::group::Merging PR https://github.com/$REPO/pull/$a" |
19 | | - git tag -f test_head |
20 | | - $GH pr checkout -b pr-$a $a |
21 | | - git checkout -q test_head |
22 | | - if git merge --no-edit --squash -q pr-$a; then |
23 | | - echo "::endgroup::" |
24 | | - if git commit -q -m "Merge https://github.com/$REPO/pull/$a" -a --no-allow-empty; then |
25 | | - echo "Merged #$a" |
| 2 | +# Apply open PRs labeled "blocker" from sagemath/sage as patches. |
| 3 | +# This script is invoked by various workflows in .github/workflows |
| 4 | +# |
| 5 | +# The repository variable SAGE_CI_FIXES_FROM_REPOS can be set |
| 6 | +# to customize this for CI runs in forks: |
| 7 | +# |
| 8 | +# - If set to a whitespace-separated list of repositories, use them instead of sagemath/sage. |
| 9 | +# - If set to "none", do not apply any PRs. |
| 10 | +# |
| 11 | +# https://docs.github.com/en/actions/learn-github-actions/variables#creating-configuration-variables-for-a-repository |
| 12 | +export GIT_AUTHOR_NAME="ci-sage workflow" |
| 13 | +export GIT_AUTHOR_EMAIL= "[email protected]" |
| 14 | +export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME" |
| 15 | +export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL" |
| 16 | +mkdir -p upstream |
| 17 | +for REPO in ${SAGE_CI_FIXES_FROM_REPOSITORIES:-sagemath/sage}; do |
| 18 | + case $REPO in |
| 19 | + none) |
| 20 | + echo "Nothing to do for 'none' in SAGE_CI_FIXES_FROM_REPOSITORIES" |
| 21 | + ;; |
| 22 | + */*) |
| 23 | + echo "Getting open PRs with 'blocker' status from https://github.com/$REPO/pulls?q=is%3Aopen+label%3A%22p%3A+blocker+%2F+1%22" |
| 24 | + GH="gh -R $REPO" |
| 25 | + REPO_FILE="upstream/ci-fixes-${REPO%%/*}-${REPO##*/}" |
| 26 | + PRs="$($GH pr list --label "p: blocker / 1" --json number --jq '.[].number' | tee $REPO_FILE)" |
| 27 | + date -u +"%Y-%m-%dT%H:%M:%SZ" > $REPO_FILE.date # Record the date, for future reference |
| 28 | + if [ -z "$PRs" ]; then |
| 29 | + echo "Nothing to do: Found no open PRs with 'blocker' status in $REPO." |
26 | 30 | else |
27 | | - echo "Empty, skipped" |
| 31 | + echo "Found open PRs with 'blocker' status in $REPO: $(echo $PRs)" |
| 32 | + git tag -f test_base |
| 33 | + git commit -q -m "Uncommitted changes" --no-allow-empty -a |
| 34 | + for a in $PRs; do |
| 35 | + # We used to pull the branch and merge it (after unshallowing), but when run on PRs that are |
| 36 | + # based on older releases, it has the side effect of updating to this release, |
| 37 | + # which may be confusing. |
| 38 | + # |
| 39 | + # Here, we obtain the "git am"-able patch of the PR branch. |
| 40 | + # This also makes it unnecessary to unshallow the repository. |
| 41 | + # |
| 42 | + # Considered alternative: Use https://github.com/$REPO/pull/$a.diff, |
| 43 | + # which squashes everything into one diff without commit metadata. |
| 44 | + PULL_URL="https://github.com/$REPO/pull/$a" |
| 45 | + PULL_FILE="$REPO_FILE-$a" |
| 46 | + PATH=build/bin:$PATH build/bin/sage-download-file --quiet "$PULL_URL.patch" $PULL_FILE.patch |
| 47 | + date -u +"%Y-%m-%dT%H:%M:%SZ" > $PULL_FILE.date # Record the date, for future reference |
| 48 | + LAST_SHA=$(sed -n -E '/^From [0-9a-f]{40}/s/^From ([0-9a-f]{40}).*/\1/p' $PULL_FILE.patch | tail -n 1) |
| 49 | + COMMITS_URL="https://github.com/$REPO/commits/$LAST_SHA" |
| 50 | + echo "::group::Applying PR $PULL_URL @ $COMMITS_URL as a patch" |
| 51 | + export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME applying $PULL_URL @ $COMMITS_URL" |
| 52 | + if git am --signoff --empty=keep < $PULL_FILE.patch; then |
| 53 | + echo "---- Applied patch --------------------------------------------------------------------------------" |
| 54 | + cat $PULL_FILE.patch |
| 55 | + echo "--------------------------------------------------------------------8<-----------------------------" |
| 56 | + echo "::endgroup::" |
| 57 | + elif git am --abort \ |
| 58 | + && if git fetch --unshallow --all > /dev/null 2>&1; then echo "Unshallowed"; fi \ |
| 59 | + && echo "Retrying with 3-way merge" \ |
| 60 | + && git am --empty=keep --3way < $PULL_FILE.patch; then |
| 61 | + echo "---- Applied patch --------------------------------------------------------------------------------" |
| 62 | + cat $PULL_FILE.patch |
| 63 | + echo "--------------------------------------------------------------------8<-----------------------------" |
| 64 | + echo "::endgroup::" |
| 65 | + else |
| 66 | + echo "---- Failure applying patch -----------------------------------------------------------------------" |
| 67 | + git am --signoff --show-current-patch=diff |
| 68 | + echo "--------------------------------------------------------------------8<-----------------------------" |
| 69 | + echo "::endgroup::" |
| 70 | + echo "Failure applying $PULL_URL as a patch, resetting" |
| 71 | + git am --signoff --abort |
| 72 | + fi |
| 73 | + done |
28 | 74 | fi |
29 | | - else |
30 | | - echo "::endgroup::" |
31 | | - echo "Failure merging #$a, resetting" |
32 | | - git reset --hard |
33 | | - fi |
34 | | - done |
35 | | - git log test_base..HEAD |
36 | | -fi |
| 75 | + ;; |
| 76 | + *) |
| 77 | + echo "Repository variable SAGE_CI_FIXES_FROM_REPOSITORIES, if set, should be a whitespace-separated list of repositories or 'none'" |
| 78 | + echo "Got: $SAGE_CI_FIXES_FROM_REPOSITORIES" |
| 79 | + exit 1 |
| 80 | + ;; |
| 81 | + esac |
| 82 | +done |
0 commit comments