Skip to content

Commit 6fb0937

Browse files
authored
Add upstream push protection to go-upgrade script (#4659)
* Add upstream push protection to go-upgrade script Signed-off-by: Aravindhan Ayyanathan <aravindhan.a@est.tech> * Address review comments Signed-off-by: Aravindhan Ayyanathan <aravindhan.a@est.tech> --------- Signed-off-by: Aravindhan Ayyanathan <aravindhan.a@est.tech>
1 parent 2f83fc1 commit 6fb0937

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

scripts/go-upgrade/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ krm-functions-sdk (leaf — no upstream deps)
5858
| `--repo=NAME` | Scope to a single repository |
5959
| `--continue` | Don't fail-fast; accumulate errors and report at end |
6060
| `--push` | After successful operations, create branch, commit, push, and raise draft PR |
61+
| `--force`, `-f` | Override upstream push protection (allows push to `kptdev`) |
6162
| `--for=CMD` | With `push` subcommand: specify which upgrade was done (default: `all`) |
6263

6364
## Configuration
@@ -68,12 +69,18 @@ Edit `config.env` to change target versions, repositories, or exclusions.
6869

6970
The `FORK_OWNER` variable controls which GitHub org/user to clone from. All repo URLs are derived from it. Defaults to `Nordix` (shared development forks). PRs always target upstream `kptdev/*` regardless of fork owner.
7071

72+
If `FORK_OWNER` is set to `kptdev`, the script will refuse to push branches to prevent accidental upstream modifications. The check also inspects the actual `origin` remote URL of the cloned workspace, so it catches stale workspaces previously cloned from upstream even if `FORK_OWNER` has since changed. Use `--force` to override this protection.
73+
7174
```bash
7275
# Use your personal fork
7376
FORK_OWNER=myuser ./upgrade.sh go-version
7477

7578
# Default: uses Nordix forks
7679
./upgrade.sh go-version
80+
81+
# Push to kptdev (blocked by default)
82+
FORK_OWNER=kptdev ./upgrade.sh all --push # ← blocked
83+
FORK_OWNER=kptdev ./upgrade.sh all --push --force # ← allowed
7784
```
7885

7986
### Target Versions

scripts/go-upgrade/lib/push.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,16 @@ Automated via go-upgrade script (AI-assisted development)."
137137

138138
log " committed: ${branch_name}"
139139

140-
# Push
140+
# Push — protect against accidental pushes to upstream
141+
local origin_url origin_owner
142+
origin_url=$(cd "$dir" && (git remote get-url --push origin 2>/dev/null || git remote get-url origin))
143+
origin_owner=$(echo "$origin_url" | sed -E 's|.*[:/]([^/]+)/[^/]+(\.git)?$|\1|')
144+
if [[ ("$FORK_OWNER" == "kptdev" || "$origin_owner" == "kptdev") && "${FORCE:-false}" != true ]]; then
145+
err " ${name}: refusing to push to upstream org 'kptdev' (FORK_OWNER=${FORK_OWNER}, origin owner=${origin_owner}). Use --force to override."
146+
record_failure "push blocked: ${name} (upstream protection)"
147+
continue
148+
fi
149+
141150
if ! (cd "$dir" && git push -u origin "$branch_name" 2>&1); then
142151
record_failure "push: ${name}"
143152
continue
@@ -152,8 +161,7 @@ Automated via go-upgrade script (AI-assisted development)."
152161
fi
153162

154163
# Determine head ref and repository IDs for cross-fork PR
155-
local origin_repo
156-
origin_repo=$(cd "$dir" && git remote get-url origin | sed -E 's|.*[:/]([^/]+/[^/]+)\.git$|\1|')
164+
local origin_repo="${origin_owner}/$(echo "$origin_url" | sed -E 's|.*[:/][^/]+/||; s|\.git$||')"
157165

158166
log " PR: ${origin_repo}:${branch_name}${target}:${base_branch}"
159167
log " commits ahead: $(cd "$dir" && git log --oneline "origin/${base_branch}..HEAD" | wc -l)"
@@ -164,7 +172,6 @@ Automated via go-upgrade script (AI-assisted development)."
164172
# Use GraphQL mutation with headRepositoryId for reliable cross-fork PRs
165173
local target_owner="${target%%/*}"
166174
local target_name="${target##*/}"
167-
local origin_owner="${origin_repo%%/*}"
168175
local origin_name="${origin_repo##*/}"
169176

170177
local target_repo_id origin_repo_id

scripts/go-upgrade/upgrade.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ source "${SCRIPT_DIR}/lib/push.sh"
4141
# --- Options ---
4242
FAIL_FAST=true
4343
GIT_PUSH=false
44+
FORCE=false
4445
FILTER_REPO=""
4546
SUBCOMMAND=""
4647
PUSH_FOR=""
@@ -62,6 +63,7 @@ Options:
6263
--repo=NAME Run only against the specified repository
6364
--continue Don't fail-fast; accumulate errors and report at end
6465
--push After operations, create branch, commit, push, and raise PR
66+
--force, -f Override upstream push protection (allows push to kptdev)
6567
--for=CMD With 'push' subcommand: specify which upgrade was done
6668
(go-version, lint-version, cross-deps, generate-docs, all). Default: all
6769
@@ -90,6 +92,8 @@ parse_args() {
9092
FAIL_FAST=false ;;
9193
--push)
9294
GIT_PUSH=true ;;
95+
--force|-f)
96+
FORCE=true ;;
9397
--for=*)
9498
PUSH_FOR="${arg#--for=}" ;;
9599
--repo=*)
@@ -143,6 +147,7 @@ main() {
143147
log "Target golangci-lint: ${TARGET_GOLANGCI_LINT_VERSION}"
144148
log "Fork owner: ${FORK_OWNER}"
145149
if [[ "$GIT_PUSH" == true ]]; then log "Mode: push enabled"; fi
150+
if [[ "$FORCE" == true ]]; then log "Mode: force (upstream push protection overridden)"; fi
146151
if [[ -n "$FILTER_REPO" ]]; then log "Repo filter: ${FILTER_REPO}"; fi
147152
echo ""
148153

0 commit comments

Comments
 (0)