|
| 1 | +#!/bin/bash |
| 2 | +# Script to set or reset the repository reference in workflow files |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# ./set-workflow-repo.sh # Reset to default (k8snetworkplumbingwg/ptp-operator) |
| 6 | +# ./set-workflow-repo.sh owner/repo # Set to custom repository |
| 7 | +# ./set-workflow-repo.sh --current # Show current repository reference |
| 8 | + |
| 9 | +set -e |
| 10 | + |
| 11 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 12 | +WORKFLOWS_DIR="$SCRIPT_DIR/../workflows" |
| 13 | +DEFAULT_REPO="redhat-cne/upstream-ptp-operator-monorepo" |
| 14 | + |
| 15 | +get_current_repo() { |
| 16 | + grep -h "uses:.*/_reusable-" "$WORKFLOWS_DIR"/*.y*ml 2>/dev/null | \ |
| 17 | + head -1 | \ |
| 18 | + sed 's/.*uses: \([^/]*\/[^/]*\)\/.*/\1/' || echo "unknown" |
| 19 | +} |
| 20 | + |
| 21 | +show_current() { |
| 22 | + local current=$(get_current_repo) |
| 23 | + echo "Current repository reference: $current" |
| 24 | + [ "$current" = "$DEFAULT_REPO" ] && echo "Status: Using default" || echo "Status: Custom (default: $DEFAULT_REPO)" |
| 25 | +} |
| 26 | + |
| 27 | +set_repo() { |
| 28 | + local new_repo="$1" |
| 29 | + local current=$(get_current_repo) |
| 30 | + |
| 31 | + [ "$current" = "$new_repo" ] && echo "Already set to: $new_repo" && return 0 |
| 32 | + |
| 33 | + echo "Updating: $current -> $new_repo" |
| 34 | + |
| 35 | + local count=0 |
| 36 | + for f in "$WORKFLOWS_DIR"/*.y*ml; do |
| 37 | + [[ "$(basename "$f")" == _reusable-* ]] && continue |
| 38 | + if grep -q "uses:.*/_reusable-" "$f"; then |
| 39 | + perl -i -pe "s|uses: [^/]+/[^/]+/\.github/workflows/(_reusable-)|uses: $new_repo/.github/workflows/\$1|g" "$f" |
| 40 | + ((count++)) |
| 41 | + fi |
| 42 | + done |
| 43 | + |
| 44 | + echo "Updated $count files" |
| 45 | + show_current |
| 46 | +} |
| 47 | + |
| 48 | +case "${1:-}" in |
| 49 | + --current|-c) show_current ;; |
| 50 | + --help|-h) echo "Usage: $0 [owner/repo | --current]" ;; |
| 51 | + "") set_repo "$DEFAULT_REPO" ;; |
| 52 | + *) [[ "$1" =~ ^[a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+$ ]] && set_repo "$1" || echo "Error: Invalid format (expected: owner/repo)" ;; |
| 53 | +esac |
0 commit comments