Skip to content

Commit 4d7e5f9

Browse files
committed
Add rebase CI
1 parent 01217ac commit 4d7e5f9

File tree

3 files changed

+135
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)