Skip to content

Commit ee8d122

Browse files
committed
Add rebase CI
1 parent 01217ac commit ee8d122

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

.github/scripts/push.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
set -eux
4+
5+
upstream_repo=$1
6+
base_repo=$2
7+
output_branches=$3
8+
9+
git remote add upstream "${upstream_repo}" || true
10+
git fetch upstream master
11+
12+
# push each branch
13+
while IFS= read -r branch || [[ -n $branch ]]; do
14+
git checkout "${branch}"
15+
echo "git push -f ${base_repo} ${branch}"
16+
# git push -f "${base_repo}" "${branch}"
17+
done < <(echo "${output_branches}")
18+

.github/scripts/rebase.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
set -eux
4+
5+
upstream_repo=$1
6+
7+
{
8+
9+
git remote add upstream "${upstream_repo}" || true
10+
git fetch upstream
11+
12+
output_branches=()
13+
14+
# rebase each branch
15+
mkdir -p rebase
16+
while IFS= read -r branch || [[ -n $branch ]]; do
17+
(
18+
git checkout "${branch}"
19+
common_ancestor=$(git merge-base "${branch}" "origin/master")
20+
[ -e rebase/"${branch}" ] && exit 1
21+
mkdir -p rebase/"${branch}"
22+
cd rebase/"${branch}"
23+
echo "${common_ancestor}" > BASE_COMMIT
24+
git format-patch "${common_ancestor}".."${branch}"
25+
if compgen -G "*.patch" > /dev/null; then
26+
git reset --hard "upstream/master"
27+
git am --3way *.patch
28+
fi
29+
)
30+
output_branches+=( "${branch}" )
31+
done < <(gh pr list --label rebase --state open --json headRefName --template '{{range .}}{{tablerow .headRefName}}{{end}}')
32+
33+
# sync master with upstream
34+
git checkout master
35+
git reset --hard upstream/master
36+
output_branches+=( "master" )
37+
38+
} >&2
39+
40+
(
41+
IFS=$'\n'
42+
echo "${output_branches[*]}"
43+
)
44+

.github/workflows/rebase.yaml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Rebase against upstream
2+
3+
on:
4+
schedule:
5+
- cron: '0 0 * * *'
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: write
10+
issues: write
11+
pull-requests: read
12+
13+
jobs:
14+
rebase:
15+
name: Rebase now!
16+
runs-on: ubuntu-latest
17+
env:
18+
UPSTREAM_REPO: https://github.com/haskell/cabal.git
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
with:
23+
fetch-depth: 0
24+
persist-credentials: false
25+
26+
- run: |
27+
set -eux
28+
29+
gh repo set-default stable-haskell/cabal
30+
git config checkout.defaultRemote origin
31+
32+
# required to apply patches
33+
git config user.email "[email protected]"
34+
git config user.name "GitHub CI"
35+
36+
# this does not push
37+
branches=$(bash .github/scripts/rebase.sh ${{ env.UPSTREAM_REPO }})
38+
39+
# now push
40+
git checkout ${{ github.ref_name }}
41+
bash .github/scripts/push.sh ${{ env.UPSTREAM_REPO }} https://${{ secrets.REBASE_PAT }}@github.com/${{ github.repository }}.git "${branches}"
42+
shell: bash
43+
env:
44+
GH_TOKEN: ${{ github.token }}
45+
46+
47+
# create an issue with a link to the workflow run on failure
48+
- if: failure()
49+
run: |
50+
set -eux
51+
gh repo set-default stable-haskell/cabal
52+
for issue in $(gh issue list --label rebase-failure --json url -q '.[] | .url') ; do
53+
gh issue close "${issue}"
54+
done
55+
gh issue create --title "Rebase failed on $(date -u +"%Y-%m-%d")" --label rebase-failure --body "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
56+
env:
57+
GH_TOKEN: ${{ github.token }}
58+
59+
# we always want the original repo and the patches that were applied
60+
# for debugging and backup reasons
61+
- if: always()
62+
run: |
63+
git checkout -f master || true
64+
git archive master > backup.tar
65+
tar -rf backup.tar .git rebase
66+
- if: always()
67+
name: Upload backup
68+
uses: actions/upload-artifact@v4
69+
with:
70+
if-no-files-found: error
71+
retention-days: 7
72+
name: backup
73+
path: |
74+
./backup.tar

0 commit comments

Comments
 (0)