Skip to content

Commit 8519651

Browse files
committed
Sync overlay from auroralabs-loci/mirror-template
1 parent 1ba641e commit 8519651

File tree

9 files changed

+139
-136
lines changed

9 files changed

+139
-136
lines changed

.github/scripts/fetch-upstream-prs.sh

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,19 @@ while true; do
138138
if loci_main_branch=$(bash "$SCRIPT_DIR/sync-loci-main.sh" "$merge_base"); then
139139
: # Branch already up-to-date
140140
else
141-
# Branch was created/updated - in scheduled mode, skip PR until next run
141+
# Branch was created/updated — push pending branch and skip PR creation
142142
if [ "$manual_mode" -eq 0 ]; then
143-
echo " PR #${pull_num}: created/updated ${loci_main_branch}. Skipping PR until next run."
143+
pending_branch="loci/pending-pr-${pull_num}-${sanitized_branch}"
144+
echo " PR #${pull_num}: ${loci_main_branch} just triggered to create/update. Pushing pending branch: ${pending_branch}."
145+
146+
if git show-ref --verify --quiet "refs/remotes/upstream/pr/${pull_num}"; then
147+
git branch --no-track -f "${pending_branch}" "refs/remotes/upstream/pr/${pull_num}"
148+
else
149+
git fetch upstream "refs/pull/${pull_num}/head:refs/heads/${pending_branch}" || \
150+
git fetch upstream "${pull_head_sha}:refs/heads/${pending_branch}"
151+
fi
152+
153+
git push origin "refs/heads/${pending_branch}:refs/heads/${pending_branch}" --force
144154
continue
145155
else
146156
echo " PR #${pull_num}: created/updated ${loci_main_branch}. Continuing with PR."
@@ -221,8 +231,14 @@ while true; do
221231
done
222232

223233
if [ "$selected_pulls_count" -eq 0 ]; then
234+
latest_upstream_sha=$(git rev-parse "refs/remotes/upstream/${UPSTREAM_DEFAULT}")
235+
echo "No PRs found. Syncing latest upstream ${UPSTREAM_DEFAULT} (${latest_upstream_sha})."
236+
if loci_main_branch=$(bash "$SCRIPT_DIR/sync-loci-main.sh" "$latest_upstream_sha"); then
237+
echo "${loci_main_branch} already up-to-date."
238+
else
239+
echo "Created/updated ${loci_main_branch}."
240+
fi
224241
echo "prs_to_sync=no" >> "$GITHUB_OUTPUT"
225-
echo "No valid upstream PRs to sync"
226242
else
227243
echo "prs_to_sync=yes" >> "$GITHUB_OUTPUT"
228244
echo "Selected ${selected_pulls_count} upstream PRs to process"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
SCRIPT_DIR="$(dirname "$0")"
5+
source "$SCRIPT_DIR/utils.sh"
6+
7+
# List pending-pr branches, rename to pr, create mirror PRs
8+
pending_branches=$(git ls-remote --heads origin 'refs/heads/loci/pending-pr-*' | awk '{print $2}' | sed 's|refs/heads/||')
9+
if [ -z "$pending_branches" ]; then
10+
echo "No pending PR branches found."
11+
exit 0
12+
fi
13+
14+
while read -r pending_branch; do
15+
[[ "$pending_branch" =~ ^loci/pending-pr-([0-9]+)-(.+)$ ]] || continue
16+
num="${BASH_REMATCH[1]}"
17+
rest="${BASH_REMATCH[2]}"
18+
pr_branch="loci/pr-${num}-${rest}"
19+
20+
echo "::group::Promoting ${pending_branch}${pr_branch}"
21+
22+
# Rename: push as loci/pr-*, delete loci/pending-pr-*
23+
git fetch origin "${pending_branch}:refs/heads/${pending_branch}"
24+
git push origin "refs/heads/${pending_branch}:refs/heads/${pr_branch}" --force
25+
git push origin --delete "${pending_branch}"
26+
27+
# Create mirror PR (lib fetches upstream metadata internally)
28+
upsert_mirror_pr "$pr_branch" "main" "$num"
29+
30+
echo "::endgroup::"
31+
done <<<"$pending_branches"
Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ set -euo pipefail
88
# GH_TOKEN - GitHub token for API calls
99
# GITHUB_REPOSITORY - current repo
1010

11+
SCRIPT_DIR="$(dirname "$0")"
12+
source "$SCRIPT_DIR/utils.sh"
13+
1114
git remote add upstream "https://github.com/${UPSTREAM_REPO}.git" 2>/dev/null || true
1215

1316
while read -r line; do
@@ -39,20 +42,7 @@ while read -r line; do
3942
fi
4043
git push origin "refs/heads/${loci_pr_branch}:refs/heads/${loci_pr_branch}" --force
4144

42-
# Check for existing PR with same head targeting the base branch
43-
existing=$(gh pr list --repo "$GITHUB_REPOSITORY" --state open --head "$loci_pr_branch" --base "$target_base" --json number --jq '.[0].number // empty' 2>/dev/null || true)
44-
45-
if [ -n "${existing}" ]; then
46-
echo "Mirrored PR #${existing} already exists. Branch updated."
47-
else
48-
echo "Creating mirrored PR targeting ${target_base}."
49-
PR_BODY=$(printf '> [!NOTE]\n> Source pull request: [%s#%s](https://github.com/%s/pull/%s)\n\n%s' "$UPSTREAM_REPO" "$num" "$UPSTREAM_REPO" "$num" "$body")
50-
gh pr create --repo "$GITHUB_REPOSITORY" \
51-
--head "${loci_pr_branch}" \
52-
--base "$target_base" \
53-
--title "UPSTREAM PR #${num}: ${title}" \
54-
--body "$PR_BODY"
55-
fi
45+
upsert_mirror_pr "$loci_pr_branch" "$target_base" "$num"
5646

5747
echo "::endgroup::"
5848
done < pulls.ndjson

.github/scripts/sync-loci-main.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,26 @@ base_sha="$1"
2121
short_sha="${base_sha:0:7}"
2222
loci_main_branch="loci/main-${short_sha}"
2323

24+
# Update main branch if base_sha is newer than what's already there
25+
if ! git merge-base --is-ancestor "$base_sha" "refs/remotes/origin/main" 2>/dev/null; then
26+
echo "Commit ${base_sha} not in main. Updating main branch." >&2
27+
git checkout -B main "$base_sha"
28+
git restore --source refs/remotes/origin/overlay -- .github/workflows/loci-analysis.yml || true
29+
if [ -n "$(git status --porcelain)" ]; then
30+
git add -A
31+
git commit -m "Add loci-analysis workflow from overlay"
32+
fi
33+
git push origin "main:refs/heads/main" --force
34+
fi
35+
2436
if git ls-remote --exit-code --heads origin "refs/heads/${loci_main_branch}" &>/dev/null; then
2537
git fetch origin "${loci_main_branch}:refs/remotes/origin/${loci_main_branch}" 2>/dev/null || true
2638

2739
overlay_hash=$(git rev-parse "refs/remotes/origin/overlay:.github/workflows/loci-analysis.yml" 2>/dev/null || true)
2840
branch_hash=$(git rev-parse "refs/remotes/origin/${loci_main_branch}:.github/workflows/loci-analysis.yml" 2>/dev/null || true)
2941

3042
if [ "$overlay_hash" = "$branch_hash" ]; then
43+
git checkout origin/overlay -- .
3144
echo "$loci_main_branch"
3245
exit 0 # branch is up-to-date
3346
fi

.github/scripts/sync-upstream.sh

Lines changed: 0 additions & 57 deletions
This file was deleted.

.github/scripts/utils.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
# Shared functionality for mirror PR operations. Source this file, don't execute it.
3+
#
4+
# Requires env: UPSTREAM_REPO, GITHUB_REPOSITORY, GH_TOKEN
5+
6+
# upsert_mirror_pr <pr_branch> <target_base> <upstream_pr_number>
7+
#
8+
# Checks if a mirror PR already exists for the given branch+base.
9+
# If not, fetches upstream PR metadata and creates the mirror PR.
10+
upsert_mirror_pr() {
11+
local pr_branch="$1" target_base="$2" num="$3"
12+
13+
local existing
14+
existing=$(gh pr list --repo "$GITHUB_REPOSITORY" --state open \
15+
--head "$pr_branch" --base "$target_base" \
16+
--json number --jq '.[0].number // empty' 2>/dev/null || true)
17+
18+
if [ -n "${existing}" ]; then
19+
echo "Mirrored PR #${existing} already exists. Branch updated."
20+
return 0
21+
fi
22+
23+
# Fetch upstream PR metadata
24+
local pr_data title body
25+
pr_data=$(gh api "repos/${UPSTREAM_REPO}/pulls/${num}" 2>/dev/null || echo '{}')
26+
title=$(jq -r '.title // "Unknown"' <<<"$pr_data")
27+
body=$(jq -r '.body // ""' <<<"$pr_data")
28+
29+
echo "Creating mirrored PR targeting ${target_base}."
30+
local PR_BODY
31+
PR_BODY=$(printf '> [!NOTE]\n> Source pull request: [%s#%s](https://github.com/%s/pull/%s)\n\n%s' \
32+
"$UPSTREAM_REPO" "$num" "$UPSTREAM_REPO" "$num" "$body")
33+
34+
gh pr create --repo "$GITHUB_REPOSITORY" \
35+
--head "${pr_branch}" \
36+
--base "$target_base" \
37+
--title "UPSTREAM PR #${num}: ${title}" \
38+
--body "$PR_BODY"
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Process Pending PRs
2+
on:
3+
schedule:
4+
- cron: '00 * * * *' # hourly at :00
5+
workflow_dispatch: {}
6+
7+
permissions:
8+
actions: write
9+
contents: write
10+
pull-requests: write
11+
12+
concurrency:
13+
group: process-pending-prs
14+
cancel-in-progress: false
15+
16+
jobs:
17+
process:
18+
if: vars.UPSTREAM_REPO != '' && (github.event_name != 'workflow_dispatch' || github.ref == 'refs/heads/overlay')
19+
runs-on: ubuntu-latest
20+
21+
env:
22+
GH_TOKEN: ${{ secrets.MIRROR_REPOS_WRITE_PAT }}
23+
UPSTREAM_REPO: '${{ vars.UPSTREAM_REPO }}'
24+
25+
steps:
26+
- uses: actions/checkout@v4
27+
with:
28+
fetch-depth: 0
29+
token: ${{ secrets.MIRROR_REPOS_WRITE_PAT }}
30+
31+
- name: Process pending PRs
32+
run: bash .github/scripts/process-pending-prs.sh

.github/workflows/sync-upstream-prs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ jobs:
7777

7878
- name: Upsert mirror branches & PRs
7979
if: steps.schedule.outputs.skip != 'true' && steps.refresh.outputs.prs_to_sync == 'yes'
80-
run: bash .github/scripts/upsert-mirror-prs.sh
80+
run: bash .github/scripts/process-prs.sh

.github/workflows/sync-upstream.yml

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)