|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Usage: watch-upstream.sh <upstream-repo> <upstream-path> <local-path> |
| 3 | +# |
| 4 | +# Checks whether the given path in an upstream GitHub repo has a new commit |
| 5 | +# since the last time we created a tracking issue. If so, opens a GitHub issue |
| 6 | +# in this repo (identified by GH_REPO or inferred by gh from git context). |
| 7 | +# |
| 8 | +# Required env vars: |
| 9 | +# GH_TOKEN — GitHub token (set automatically in Actions; use `gh auth token` locally) |
| 10 | +# |
| 11 | +# Optional env vars (set automatically in GitHub Actions): |
| 12 | +# GH_REPO — target repo for issue creation, e.g. getsentry/sentry-dotnet |
| 13 | +# GITHUB_SERVER_URL — e.g. https://github.com (defaults to https://github.com) |
| 14 | +# GITHUB_RUN_ID — included in the issue footer when present |
| 15 | + |
| 16 | +set -euo pipefail |
| 17 | + |
| 18 | +if [ $# -ne 3 ]; then |
| 19 | + echo "Usage: $0 <upstream-repo> <upstream-path> <local-path>" >&2 |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +UPSTREAM_REPO="$1" |
| 24 | +UPSTREAM_PATH="$2" |
| 25 | +LOCAL_PATH="$3" |
| 26 | +UPSTREAM_URL="https://github.com/${UPSTREAM_REPO}/tree/main/${UPSTREAM_PATH}" |
| 27 | +GITHUB_SERVER_URL="${GITHUB_SERVER_URL:-https://github.com}" |
| 28 | + |
| 29 | +echo "Checking upstream: ${UPSTREAM_REPO}/${UPSTREAM_PATH}" |
| 30 | + |
| 31 | +# Get the latest commit SHA affecting the tracked path. |
| 32 | +LATEST_SHA=$(gh api "repos/${UPSTREAM_REPO}/commits?path=${UPSTREAM_PATH}&per_page=1" \ |
| 33 | + --jq '.[0].sha') |
| 34 | +LATEST_SHORT="${LATEST_SHA:0:7}" |
| 35 | +echo "Latest upstream commit: ${LATEST_SHA} (${LATEST_SHORT})" |
| 36 | + |
| 37 | +# Avoid creating duplicate issues: skip if any issue (open or closed) already |
| 38 | +# tracks this exact upstream commit SHA. The SHA in the title makes it unique. |
| 39 | +ISSUE_LABEL="upstream-watch" |
| 40 | +EXISTING_ISSUE=$(gh issue list \ |
| 41 | + --label "$ISSUE_LABEL" \ |
| 42 | + --state all \ |
| 43 | + --search "\"${UPSTREAM_REPO} ${UPSTREAM_PATH} @ ${LATEST_SHORT}\"" \ |
| 44 | + --json number,title \ |
| 45 | + --jq '.[0].number // empty') |
| 46 | + |
| 47 | +if [ -n "$EXISTING_ISSUE" ]; then |
| 48 | + echo "An issue (#${EXISTING_ISSUE}) already tracks upstream commit ${LATEST_SHORT} for ${UPSTREAM_REPO}/${UPSTREAM_PATH}. Skipping." |
| 49 | + exit 0 |
| 50 | +fi |
| 51 | + |
| 52 | +echo "No existing issue found for commit ${LATEST_SHORT}. Creating one..." |
| 53 | + |
| 54 | +# Ensure the label exists (idempotent). |
| 55 | +gh label create "$ISSUE_LABEL" \ |
| 56 | + --description "Upstream vendored code has changed — review required" \ |
| 57 | + --color "E4E669" 2>/dev/null || true |
| 58 | + |
| 59 | +COMMIT_URL="https://github.com/${UPSTREAM_REPO}/commit/${LATEST_SHA}" |
| 60 | +HISTORY_URL="https://github.com/${UPSTREAM_REPO}/commits/main/${UPSTREAM_PATH}" |
| 61 | + |
| 62 | +if [ -n "${GITHUB_RUN_ID:-}" ] && [ -n "${GITHUB_REPOSITORY:-}" ]; then |
| 63 | + FOOTER="> _Automatically opened by the [Watch Upstream Changes](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}) workflow._" |
| 64 | +else |
| 65 | + FOOTER="> _Manually triggered via watch-upstream.sh._" |
| 66 | +fi |
| 67 | + |
| 68 | +gh issue create \ |
| 69 | + --title "Upstream change detected: ${UPSTREAM_REPO} ${UPSTREAM_PATH} @ ${LATEST_SHORT}" \ |
| 70 | + --label "$ISSUE_LABEL" \ |
| 71 | + --body "## Upstream Change Detected |
| 72 | +
|
| 73 | +The code at [\`${UPSTREAM_REPO}/${UPSTREAM_PATH}\`](${UPSTREAM_URL}) has a new commit since our last review. |
| 74 | +
|
| 75 | +| | | |
| 76 | +|---|---| |
| 77 | +| **Latest commit** | [\`${LATEST_SHORT}\`](${COMMIT_URL}) | |
| 78 | +| **Path history** | [View history](${HISTORY_URL}) | |
| 79 | +
|
| 80 | +Our vendored copy lives in \`${LOCAL_PATH}\`. We modified the upstream code significantly, |
| 81 | +so a direct merge is unlikely to be appropriate — but the commit above may reveal logic |
| 82 | +changes worth porting. |
| 83 | +
|
| 84 | +### What to do |
| 85 | +
|
| 86 | +1. Review the [upstream commit](${COMMIT_URL}) and [path history](${HISTORY_URL}). |
| 87 | +2. If no action is needed, close this issue with a note explaining why. |
| 88 | +3. If changes should be ported, create a follow-up task and close this issue once the work is tracked. |
| 89 | +
|
| 90 | +${FOOTER}" |
| 91 | + |
| 92 | +echo "Issue created successfully." |
0 commit comments