-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtest-gerrit-local.sh
More file actions
executable file
·129 lines (119 loc) · 4.54 KB
/
test-gerrit-local.sh
File metadata and controls
executable file
·129 lines (119 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$SCRIPT_DIR"
# WORKFLOWS_DIR="$REPO_ROOT/.github/workflows" # Currently unused
EVENT_JSON="$REPO_ROOT/gerrit-verify-event.json"
TARGET_WORKFLOW_REL=".github/workflows/gerrit-verify.yaml"
JOB=""
GERRIT_URL=""
PATCHSET_NUMBER="latest"
BRANCH="master"
DRY_RUN=false
VERBOSE=false
LOCAL_CHANGES=false
usage(){ cat <<EOF
Usage: $0 [options]
--job <name> Run only a specific job.
--gerrit-url <url> Gerrit change URL (e.g., https://git.opendaylight.org/gerrit/c/docs/+/117503).
--patchset-number <num> Gerrit patchset number (default: latest).
--branch <name> Target branch (default master).
--local-changes Test current local changes instead of fetching from Gerrit.
--dry-run Pass -n to act.
--verbose Echo act command.
-h|--help This help.
EOF
}
while [[ $# -gt 0 ]]; do case "$1" in
--job) JOB="$2"; shift 2;;
--gerrit-url) GERRIT_URL="$2"; shift 2;;
--patchset-number) PATCHSET_NUMBER="$2"; shift 2;;
--branch) BRANCH="$2"; shift 2;;
--local-changes) LOCAL_CHANGES=true; shift;;
--dry-run) DRY_RUN=true; shift;;
--verbose) VERBOSE=true; shift;;
-h|--help) usage; exit 0;;
*) echo "Unknown arg $1" >&2; usage; exit 1;; esac; done
# Function to extract change number from Gerrit URL
extract_change_number() {
local url="$1"
# Extract change number from URLs like:
# https://git.opendaylight.org/gerrit/c/docs/+/117503
# https://git.opendaylight.org/gerrit/c/docs/+/117503/1
echo "$url" | grep -oE '/[0-9]+(/[0-9]+)?$' | grep -oE '[0-9]+' | head -1
}
# Function to get latest patchset number for a change
get_latest_patchset() {
local change_number="$1"
local gerrit_host="git.opendaylight.org"
# Query Gerrit REST API to get change info with detailed revisions
local api_url="https://${gerrit_host}/gerrit/changes/${change_number}?o=ALL_REVISIONS"
if command -v curl >/dev/null 2>&1 && command -v jq >/dev/null 2>&1; then
local patchset
patchset=$(curl -s "${api_url}" 2>/dev/null | sed '1d' | jq -r '
.revisions |
to_entries |
max_by(.value._number) |
.value._number
' 2>/dev/null)
if [[ -n "$patchset" ]] && [[ "$patchset" != "null" ]] && [[ "$patchset" =~ ^[0-9]+$ ]]; then
echo "$patchset"
return 0
fi
fi
# Fallback to patchset 1 if API query fails
echo "1"
}
# Parse Gerrit URL or generate synthetic data
if [[ -n "$GERRIT_URL" ]]; then
CHANGE_NUMBER=$(extract_change_number "$GERRIT_URL")
if [[ -z "$CHANGE_NUMBER" ]]; then
echo "Error: Could not extract change number from URL: $GERRIT_URL" >&2
exit 1
fi
# Extract project from URL (e.g., docs)
GERRIT_PROJECT=$(echo "$GERRIT_URL" | sed -n 's|.*/gerrit/c/\([^/]*\)/.*|\1|p')
[[ -z "$GERRIT_PROJECT" ]] && GERRIT_PROJECT="docs"
# Resolve latest patchset if needed
if [[ "$PATCHSET_NUMBER" == "latest" ]]; then
echo "Fetching latest patchset for change $CHANGE_NUMBER..." >&2
PATCHSET_NUMBER=$(get_latest_patchset "$CHANGE_NUMBER")
echo "Using patchset $PATCHSET_NUMBER" >&2
fi
else
# Generate synthetic data if no URL provided
CHANGE_NUMBER="$(shuf -i 1000-9999 -n1)"
GERRIT_URL="https://git.opendaylight.org/gerrit/c/docs/+/$CHANGE_NUMBER"
GERRIT_PROJECT="docs"
# For synthetic data, use patchset 1
[[ "$PATCHSET_NUMBER" == "latest" ]] && PATCHSET_NUMBER="1"
fi
rand_hex(){ head -c16 /dev/urandom | sha1sum | cut -c1-40; }
# Set up event data
# CHANGE_ID="I$(rand_hex)" # Currently unused
# REVISION="$(rand_hex)" # Currently unused
REFSPEC="refs/changes/${CHANGE_NUMBER: -2}/$CHANGE_NUMBER/$PATCHSET_NUMBER"
# For local changes, use current HEAD instead of Gerrit refspec
if [[ "$LOCAL_CHANGES" == "true" ]]; then
REF="refs/heads/$BRANCH"
echo "Testing local changes on branch $BRANCH"
else
REF="$REFSPEC"
echo "Testing Gerrit change $CHANGE_NUMBER patchset $PATCHSET_NUMBER"
fi
cat > "$EVENT_JSON" <<JSON
{
"ref": "$REF",
"inputs": {
"GERRIT_CHANGE_URL": "$GERRIT_URL",
"GERRIT_PATCHSET_NUMBER": "$PATCHSET_NUMBER"
}
}
JSON
WORKFLOW_PATH="$REPO_ROOT/$TARGET_WORKFLOW_REL"
[[ -f "$WORKFLOW_PATH" ]] || { echo "Workflow not found: $WORKFLOW_PATH" >&2; exit 2; }
CMD=(act --workflows "$WORKFLOW_PATH" --eventpath "$EVENT_JSON" workflow_dispatch)
[[ -n "$JOB" ]] && CMD+=(--job "$JOB")
$DRY_RUN && CMD+=(-n)
$VERBOSE && echo "Running: ${CMD[*]}" >&2
"${CMD[@]}"