#!/usr/bin/env bash # upstream-sync: merge upstream into our production branch, report failures. # # IDENTICAL COPY in meshcore-tth, meshy, openhop_core, openhop_repeater, # corescope and openhop-repeater (report only). Change one, change all; the # repo list is in MeshCore/tools/stack_status.py (REPOS). # # upstream-sync.sh merge BRANCH, UPSTREAM_URL, UPSTREAM_REFS ("main dev") # -> outputs changed=true|false, sha= # upstream-sync.sh report TOKEN; opens (or comments on) one issue # upstream-sync.sh resolve TOKEN; closes that issue after a good run # # MERGE, never rebase: Solo's cron rebased and fell over the same conflict on # every run until the branch was merged instead (3. 8. and 24. 8. 2026). A # merge resolved once stays resolved. # # Pushing is the workflow's job and happens only after its tests pass. A push # made with the job token does not start other workflows on Gitea, so anything # that has to build the result runs in the same workflow or on its own cron. set -euo pipefail out() { printf '%s=%s\n' "$1" "$2" >>"${GITHUB_OUTPUT:-/dev/null}"; } need_jq() { command -v jq >/dev/null || { apt-get update -qq && apt-get install -y -qq jq >/dev/null; } } merge() { : "${BRANCH:?}" "${UPSTREAM_URL:?}" "${UPSTREAM_REFS:?}" git config user.name "upstream-sync-bot" git config user.email "upstream-sync@rfa.cz" git remote add upstream "$UPSTREAM_URL" 2>/dev/null || git remote set-url upstream "$UPSTREAM_URL" git fetch --quiet origin "+refs/heads/$BRANCH:refs/remotes/origin/$BRANCH" git checkout --quiet -B "$BRANCH" "origin/$BRANCH" local before ref n before="$(git rev-parse HEAD)" for ref in $UPSTREAM_REFS; do git fetch --quiet --tags upstream "+refs/heads/$ref:refs/remotes/upstream/$ref" if git merge-base --is-ancestor "upstream/$ref" HEAD; then echo "upstream/$ref: nothing new" continue fi n="$(git rev-list --count "HEAD..upstream/$ref")" echo "upstream/$ref: merging $n commits" git log --no-merges --max-count=40 --format=' %h %s' "HEAD..upstream/$ref" if ! git merge --no-edit -m "Merge upstream/$ref ($n commits) [upstream-sync]" "upstream/$ref"; then echo "::error::merge conflict with upstream/$ref" { echo "Merge conflict with \`upstream/$ref\` ($n new commits). Files:" git diff --name-only --diff-filter=U | sed 's/^/- `/; s/$/`/' echo echo "Resolve locally: \`git fetch upstream && git merge upstream/$ref\` on \`$BRANCH\`, push, and the next run carries on." } >"${RUNNER_TEMP:-/tmp}/upstream-sync-reason.md" git merge --abort exit 1 fi done out changed "$([ "$before" != "$(git rev-parse HEAD)" ] && echo true || echo false)" out sha "$(git rev-parse HEAD)" out before "$before" } report() { : "${TOKEN:?}" need_jq local api title run reason num body api="${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}" title="upstream-sync failed" run="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_NUMBER}" reason="${RUNNER_TEMP:-/tmp}/upstream-sync-reason.md" if [ -s "$reason" ]; then body="$(cat "$reason")" else body="A step after the merge failed (tests or build), so nothing was pushed." fi body="$(printf '%s\n\nRun: %s\n' "$body" "$run")" num="$(curl -sf -H "Authorization: token $TOKEN" "$api/issues?state=open&type=issues&limit=50" | jq -r --arg t "$title" '[.[] | select(.title == $t)][0].number // empty')" if [ -n "$num" ]; then jq -n --arg b "$body" '{body: $b}' | curl -sf -X POST -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \ -d @- "$api/issues/$num/comments" >/dev/null echo "commented on issue #$num" else jq -n --arg t "$title" --arg b "$body" '{title: $t, body: $b}' | curl -sf -X POST -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \ -d @- "$api/issues" | jq -r '"opened issue #\(.number)"' fi } resolve() { : "${TOKEN:?}" need_jq local api num api="${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}" num="$(curl -sf -H "Authorization: token $TOKEN" "$api/issues?state=open&type=issues&limit=50" | jq -r '[.[] | select(.title == "upstream-sync failed")][0].number // empty')" || return 0 [ -n "$num" ] || return 0 jq -n --arg b "Synced again in ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_NUMBER}" '{body: $b}' | curl -sf -X POST -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \ -d @- "$api/issues/$num/comments" >/dev/null curl -sf -X PATCH -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \ -d '{"state":"closed"}' "$api/issues/$num" >/dev/null echo "closed issue #$num" } "$@"