-
Notifications
You must be signed in to change notification settings - Fork 0
127 lines (113 loc) · 5.08 KB
/
Copy pathsync-upstream.yml
File metadata and controls
127 lines (113 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
name: Sync with upstream
# Opens a PR in this repo whenever the upstream AIM template has new commits.
# Merge the PR to pull the updates in; Netlify will auto-rebuild.
#
# === Prerequisite (one-time, per repo) ===
# GitHub disables Actions-created PRs by default. To let this workflow open
# PRs automatically, go to:
# Settings → Actions → General → Workflow permissions
# ✅ Allow GitHub Actions to create and approve pull requests
# (then click Save)
#
# Without that toggle, the workflow still pushes the sync branch and prints a
# clickable compare URL — you just have to open the PR yourself from the UI.
#
# To disable: delete this file.
# To customize: change UPSTREAM_REPO below.
on:
schedule:
# Daily at 06:00 UTC. Adjust if you want a different cadence.
- cron: "0 6 * * *"
workflow_dispatch: {}
permissions:
contents: write
pull-requests: write
env:
UPSTREAM_REPO: kitty4D/aim
UPSTREAM_BRANCH: main
SYNC_BRANCH: sync/upstream
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout this repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Configure git identity
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Add upstream remote and fetch
run: |
git remote add upstream "https://github.com/${UPSTREAM_REPO}.git"
git fetch upstream "${UPSTREAM_BRANCH}"
- name: Compute divergence
id: diff
run: |
BEHIND=$(git rev-list --count HEAD.."upstream/${UPSTREAM_BRANCH}")
AHEAD=$(git rev-list --count "upstream/${UPSTREAM_BRANCH}"..HEAD)
echo "behind=$BEHIND" >> "$GITHUB_OUTPUT"
echo "ahead=$AHEAD" >> "$GITHUB_OUTPUT"
echo "Behind upstream by $BEHIND, ahead by $AHEAD."
- name: Already up to date — nothing to do
if: steps.diff.outputs.behind == '0'
run: echo "::notice::Already up to date with ${UPSTREAM_REPO}@${UPSTREAM_BRANCH}"
- name: Update sync branch from upstream
if: steps.diff.outputs.behind != '0'
run: |
git checkout -B "${SYNC_BRANCH}" "upstream/${UPSTREAM_BRANCH}"
git push origin "${SYNC_BRANCH}" --force
- name: Open or update sync PR
if: steps.diff.outputs.behind != '0'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BEHIND: ${{ steps.diff.outputs.behind }}
AHEAD: ${{ steps.diff.outputs.ahead }}
run: |
TITLE="Sync with upstream ${UPSTREAM_REPO} (${BEHIND} new commit(s))"
BODY_FILE="$(mktemp)"
{
echo "## 🔄 Upstream sync"
echo
echo "Your repo is **${BEHIND} commit(s) behind** [\`${UPSTREAM_REPO}@${UPSTREAM_BRANCH}\`](https://github.com/${UPSTREAM_REPO}/tree/${UPSTREAM_BRANCH})."
echo
if [ "${AHEAD}" != "0" ]; then
echo "> ⚠️ You also have **${AHEAD} local commit(s)** that aren't in upstream."
echo "> This sync branch is reset to the upstream state, so merging will overwrite those local commits with the upstream version. Review the diff carefully before merging."
echo
fi
echo "### What this PR does"
echo
echo "Brings your repo up to date with the latest [\`${UPSTREAM_REPO}\`](https://github.com/${UPSTREAM_REPO}) template."
echo
echo "### How to apply"
echo
echo "Use **Squash and merge** or **Create a merge commit** below. Netlify will auto-rebuild your site within ~1 minute."
echo
echo "---"
echo
echo "_This PR was opened automatically by the \`.github/workflows/sync-upstream.yml\` workflow. Delete that file to disable auto-syncing._"
} > "${BODY_FILE}"
COMPARE_URL="https://github.com/${GITHUB_REPOSITORY}/compare/${UPSTREAM_BRANCH}...${SYNC_BRANCH}"
EXISTING=$(gh pr list --head "${SYNC_BRANCH}" --state open --json number --jq '.[0].number // empty' || echo "")
if [ -n "${EXISTING}" ]; then
if gh pr edit "${EXISTING}" --title "${TITLE}" --body-file "${BODY_FILE}"; then
echo "::notice::Updated existing PR #${EXISTING}"
else
echo "::warning::Couldn't update PR #${EXISTING} via API — see compare URL: ${COMPARE_URL}"
fi
else
if gh pr create \
--base "${UPSTREAM_BRANCH}" \
--head "${SYNC_BRANCH}" \
--title "${TITLE}" \
--body-file "${BODY_FILE}"; then
echo "::notice::Opened sync PR"
else
echo "::warning::PR API rejected (likely 'Resource not accessible by integration')."
echo "::warning::Fix: Settings → Actions → General → Workflow permissions → enable 'Allow GitHub Actions to create and approve pull requests'."
echo "::notice::Sync branch is pushed; open the PR manually here:"
echo "::notice::${COMPARE_URL}"
fi
fi