Skip to content

Commit 93d02d9

Browse files
committed
fix: some krw fixes for component onboarding
- reconfigure pac if github throws error re-annotate the component to let it retry onboarding - delete old branches script protects against deleting new branches by mistake. - delete old branches only handles branches from the currently running test suite via new test env var called component_type which is now added to each test suite and checked for at run time. Signed-off-by: Scott Hebert <[email protected]>
1 parent d4f8d46 commit 93d02d9

File tree

8 files changed

+179
-35
lines changed

8 files changed

+179
-35
lines changed

integration-tests/collectors/test.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export tenant_namespace=dev-release-team-tenant
1111
export managed_namespace=managed-release-team-tenant
1212

1313
export application_name=e2eapp-${uuid}
14-
export component_name=collector-${uuid}
14+
export component_type=collector
15+
export component_name=${component_type}-${uuid}
1516
export component_branch=${component_name}
1617
## do not change this. it is a known branch created by Konflux
1718
export appstudio_component_branch="appstudio-${component_name}"

integration-tests/fbc-release/test.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export tenant_namespace=dev-release-team-tenant
1111
export managed_namespace=managed-release-team-tenant
1212

1313
export application_name=e2eapp-${uuid}
14-
export component_name=fbc-release-${uuid}
14+
export component_type=fbc-release
15+
export component_name=${component_type}-${uuid}
1516
export component_branch=${component_name}
1617
## do not change this. it is a known branch created by Konflux
1718
export appstudio_component_branch="appstudio-${component_name}"

integration-tests/lib/test-functions.sh

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ log_error() {
88
exit "${2:-1}" # Exit with provided code or 1 by default
99
}
1010

11+
# Helper function to log errors
12+
log_warning() {
13+
echo "⚠️ Warning: $1"
14+
}
15+
1116
# Function to check for required environment variables
1217
check_env_vars() {
1318
echo "Checking required environment variables..."
@@ -19,13 +24,42 @@ check_env_vars() {
1924
["RELEASE_CATALOG_GIT_REVISION"]="Missing RELEASE_CATALOG_GIT_REVISION"
2025
)
2126

27+
# Check core required variables
2228
for var_name in "${!required_vars[@]}"; do
2329
if [ -z "${!var_name}" ]; then
2430
echo "❌ error: ${required_vars[$var_name]}"
2531
missing_vars=$((missing_vars + 1))
2632
fi
2733
done
2834

35+
# Check variables from test.env files (static list of all variables found in test.env files)
36+
echo "Checking test environment variables..."
37+
local -a test_env_vars=(
38+
"application_name"
39+
"appstudio_component_branch"
40+
"component_base_branch"
41+
"component_branch"
42+
"component_git_url"
43+
"component_name"
44+
"component_repo_name"
45+
"component_type"
46+
"managed_namespace"
47+
"managed_sa_name"
48+
"originating_tool"
49+
"tenant_namespace"
50+
"tenant_sa_name"
51+
)
52+
for var_name in "${test_env_vars[@]}"; do
53+
# Check if variable is set
54+
if [ -z "${!var_name}" ]; then
55+
echo "❌ error: Missing test environment variable: $var_name"
56+
missing_vars=$((missing_vars + 1))
57+
else
58+
echo "$var_name is set"
59+
fi
60+
done
61+
62+
# Special file validation
2963
if [ -n "$VAULT_PASSWORD_FILE" ] && [ ! -f "$VAULT_PASSWORD_FILE" ]; then
3064
echo "❌ error: env var VAULT_PASSWORD_FILE points to a non-existent file: $VAULT_PASSWORD_FILE"
3165
missing_vars=$((missing_vars + 1))
@@ -38,7 +72,7 @@ check_env_vars() {
3872
if [ -n "$KUBECONFIG" ] ; then
3973
echo "Using provided KUBECONFIG"
4074
else
41-
echo "⚠️ Warning: KUBECONFIG is not set. Assuming kubectl is configured correctly."
75+
log_warning "KUBECONFIG is not set. Assuming kubectl is configured correctly."
4276
fi
4377
echo "Environment variable check complete."
4478
}
@@ -92,7 +126,7 @@ get_build_pipeline_run_url() { # args are ns, app, name
92126
console_url=${console_url%/}
93127

94128
if [ -z "$console_url" ]; then
95-
echo "⚠️ Warning: Could not retrieve custom-console-url. URL might be incomplete."
129+
log_warning "Could not retrieve custom-console-url. URL might be incomplete."
96130
echo "kubectl get cm/pipelines-as-code -n openshift-pipelines -ojson" # Add command for easier debugging
97131
echo "${ns}/applications/${app}/pipelineruns/${name}" # Fallback or partial URL
98132
else
@@ -150,6 +184,7 @@ cleanup_resources() {
150184
echo "Killing any child processes..." >> "${cleanup_log_file}"
151185
pkill -e -P $$
152186

187+
cat "${cleanup_log_file}"
153188
if [ "$err" -ne 0 ]; then
154189
exit "$err"
155190
fi
@@ -239,21 +274,54 @@ create_kubernetes_resources() {
239274
# Modifies global variables: component_pr, pr_number
240275
# Relies on global variables: component_name, tenant_namespace
241276
wait_for_component_initialization() {
242-
echo -n "Waiting for component ${component_name} in namespace ${tenant_namespace} to be initialized: "
277+
echo "Waiting for component ${component_name} in namespace ${tenant_namespace} to be initialized..."
278+
279+
local max_attempts=60 # 10 minutes with 10-second intervals
280+
local attempt=1
243281
local component_annotations=""
244-
while [ -z "${component_annotations}" ]; do
245-
sleep 1
246-
echo -n "."
282+
local initialization_success=false
283+
284+
while [ $attempt -le $max_attempts ]; do
285+
echo "Initialization check attempt ${attempt}/${max_attempts}..."
286+
287+
# Try to get component annotations
247288
component_annotations=$(kubectl get component/"${component_name}" -n "${tenant_namespace}" -ojson 2>/dev/null | \
248289
jq -r --arg k "build.appstudio.openshift.io/status" '.metadata.annotations[$k] // ""')
290+
291+
if [ -n "${component_annotations}" ]; then
292+
# component_pr is made global by not declaring it local
293+
component_pr=$(jq -r '.pac."merge-url" // ""' <<< "${component_annotations}")
294+
if [ -n "${component_pr}" ]; then
295+
echo "✅ Component initialized successfully"
296+
initialization_success=true
297+
break
298+
else
299+
log_warning "Could not get component PR from annotations: ${component_annotations}"
300+
echo "Requesting a new configure-pac..."
301+
kubectl annotate components/${component_name} build.appstudio.openshift.io/request=configure-pac -n "${tenant_namespace}"
302+
echo "Waiting 10 seconds before retry..."
303+
sleep 10
304+
fi
305+
306+
else
307+
log_warning "Component not yet initialized (attempt ${attempt}/${max_attempts})"
308+
309+
# Wait before retrying (except on the last attempt)
310+
if [ $attempt -lt $max_attempts ]; then
311+
echo "Waiting 10 seconds before retry..."
312+
sleep 10
313+
fi
314+
fi
315+
316+
attempt=$((attempt + 1))
249317
done
250-
echo ""
251-
echo "️✅️ Initialized."
252318

253-
# component_pr is made global by not declaring it local
254-
component_pr=$(jq -r '.pac."merge-url" // ""' <<< "${component_annotations}")
255-
if [ -z "${component_pr}" ]; then
256-
log_error "Could not get component PR from annotations: ${component_annotations}"
319+
# Check if initialization ultimately succeeded
320+
if [ "$initialization_success" = false ]; then
321+
echo "🔴 error: component ${component_name} failed to initialize after ${max_attempts} attempts ($(($max_attempts * 10 / 60)) minutes)"
322+
echo " - Component may not exist in namespace ${tenant_namespace}"
323+
echo " - Component creation may have failed"
324+
exit 1
257325
fi
258326

259327
# pr_number is made global by not declaring it local
@@ -286,7 +354,7 @@ merge_github_pr() {
286354
# Retry loop for PR merge
287355
while [ $attempt -le $max_attempts ] && [ "$success" = false ]; do
288356
echo "Merge attempt ${attempt}/${max_attempts}..."
289-
357+
290358
set +e
291359
merge_result=$(curl -L \
292360
-X PUT \
@@ -511,7 +579,8 @@ cleanup_old_resources() {
511579
# - GITHUB_TOKEN environment variable must be set
512580
delete_old_branches() {
513581
local repo_name="$1"
514-
local cutoff_days="${2:-1}"
582+
local branch_prefix="$2"
583+
local cutoff_days="${3:-1}"
515584

516585
if [ -z "$repo_name" ]; then
517586
echo "🔴 Error: Repository name is required (format: owner/repo)"
@@ -531,5 +600,5 @@ delete_old_branches() {
531600
fi
532601

533602
echo "🔍 Deleting branches in ${repo_name} older than ${cutoff_days} day(s)..."
534-
CUTOFF_DATE="${cutoff_days} day" bash "$script_path" "$repo_name"
603+
CUTOFF_DATE="${cutoff_days} day" bash "$script_path" "$repo_name" "$branch_prefix"
535604
}

integration-tests/push-to-addons-registry/test.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export tenant_namespace=dev-release-team-tenant
1111
export managed_namespace=managed-release-team-tenant
1212

1313
export application_name=e2eapp-${uuid}
14-
export component_name=push-to-addons-registry-${uuid}
14+
export component_type=push-to-addons-registry
15+
export component_name=${component_type}-${uuid}
1516
export component_branch=${component_name}
1617
## do not change this. it is a known branch created by Konflux
1718
export appstudio_component_branch="appstudio-${component_name}"

integration-tests/rhtap-service-push/test.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export tenant_namespace=dev-release-team-tenant
1111
export managed_namespace=managed-release-team-tenant
1212

1313
export application_name=e2eapp-${uuid}
14-
export component_name=rhtap-service-push-${uuid}
14+
export component_type=rhtap-service-push
15+
export component_name=${component_type}-${uuid}
1516
export component_branch=${component_name}
1617
## do not change this. it is a known branch created by Konflux
1718
export appstudio_component_branch="appstudio-${component_name}"

integration-tests/run-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ check_env_vars "$@" # Pass all args for consistency, though check_env_vars doesn
125125
parse_options "$@" # Parses options and sets CLEANUP, NO_CVE
126126

127127
decrypt_secrets
128-
delete_old_branches "${component_repo_name}" 2
128+
delete_old_branches "${component_repo_name}" "${component_branch_prefix}" 2
129129
create_github_branch
130130
setup_namespaces # Ensures correct context before resource creation
131131
cleanup_old_resources "${originating_tool}"

integration-tests/scripts/delete-old-branches.sh

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,30 @@
77
# --------
88
# This script automatically cleans up old branches in a GitHub repository by
99
# deleting branches that haven't had any commits within a specified timeframe.
10+
# It only considers branches that start with a specified prefix.
1011
# It skips branches ending in "-base" and the "main" branch for safety.
1112
#
13+
# Branch deletion logic:
14+
# - Branches with commits after creation: deleted if last commit is older than cutoff
15+
# - Branches with no commits after creation: deleted only if branch is older than 2 days
16+
#
1217
# Features:
1318
# --------
1419
# - Identifies branches with no recent commits
1520
# - Configurable cutoff period (default: 1 day)
1621
# - Safe deletion with base branch protection
22+
# - Branch prefix filtering to target specific branch types
23+
# - Smart branch deletion: preserves new branches without commits, removes old unused branches
1724
# - Handles pagination for repositories with many branches
1825
# - Provides detailed output of deleted branches
1926
#
2027
# Usage:
2128
# -----
22-
# ./delete-old-branches.sh <owner/repository>
29+
# ./delete-old-branches.sh <owner/repository> <branch_prefix>
2330
#
2431
# Example:
25-
# ./delete-old-branches.sh redhat/release-service
32+
# ./delete-old-branches.sh redhat/release-service e2e-test
33+
# ./delete-old-branches.sh konflux-ci/release-service-catalog integration-test
2634
#
2735
# Required Environment Variables:
2836
# ----------------------------
@@ -57,10 +65,21 @@ if [ -z "$repo_name" ] ; then
5765
exit 1
5866
fi
5967

68+
branch_prefix=$2
69+
if [ -z "$branch_prefix" ] ; then
70+
echo "🔴 error: missing parameter branch_prefix"
71+
echo "Usage: $0 <owner/repository> <branch_prefix>"
72+
exit 1
73+
fi
74+
6075
CUTOFF_DATE="${CUTOFF_DATE:-1 day}"
6176
CUTOFF_SECONDS=$(date -d "$CUTOFF_DATE ago" +%s)
6277

63-
echo "Finding branches in $repo_name with no commits for $CUTOFF_DATE..."
78+
# Additional cutoff for branches with no commits after creation
79+
BRANCH_AGE_CUTOFF_SECONDS=$(date -d "2 days ago" +%s)
80+
81+
echo "Finding branches in $repo_name starting with '$branch_prefix' with no commits for $CUTOFF_DATE..."
82+
echo "Also checking for unused branches older than 2 days..."
6483
echo "------------------------------------------------------------------"
6584

6685
# --- Main Logic ---
@@ -84,8 +103,32 @@ echo "$branches_data" | jq -r '.[] | .name + " " + .commit.sha' | while read -r
84103
if [[ "${branch_name}" == *"-base" ]] || [[ "${branch_name}" == "main" ]] ; then
85104
continue
86105
fi
87-
88-
# 2. For each branch, get the date of its last commit
106+
107+
# Only consider branches that start with the specified prefix
108+
if [[ "${branch_name}" != "${branch_prefix}"* ]] ; then
109+
continue
110+
fi
111+
112+
# 2. Get the branch creation date from git ref
113+
branch_ref_data=$(curl -s -L \
114+
-H "Accept: application/vnd.github+json" \
115+
-H "Authorization: Bearer $GITHUB_TOKEN" \
116+
-H "X-GitHub-Api-Version: 2022-11-28" \
117+
"https://api.github.com/repos/$repo_name/git/refs/heads/$branch_name")
118+
119+
# Get the commit that created the branch
120+
branch_creation_sha=$(echo "$branch_ref_data" | jq -r '.object.sha')
121+
# Get the branch creation commit details
122+
branch_creation_commit_data=$(curl -s -L \
123+
-H "Accept: application/vnd.github+json" \
124+
-H "Authorization: Bearer $GITHUB_TOKEN" \
125+
-H "X-GitHub-Api-Version: 2022-11-28" \
126+
"https://api.github.com/repos/$repo_name/commits/$branch_creation_sha")
127+
128+
branch_creation_date_str=$(echo "$branch_creation_commit_data" | jq -r '.commit.committer.date')
129+
branch_creation_date_seconds=$(date -d "$branch_creation_date_str" +%s)
130+
131+
# 3. For each branch, get the date of its last commit
89132
commit_data=$(curl -s -L \
90133
-H "Accept: application/vnd.github+json" \
91134
-H "Authorization: Bearer $GITHUB_TOKEN" \
@@ -95,18 +138,45 @@ echo "$branches_data" | jq -r '.[] | .name + " " + .commit.sha' | while read -r
95138
commit_date_str=$(echo "$commit_data" | jq -r '.commit.committer.date')
96139
commit_date_seconds=$(date -d "$commit_date_str" +%s)
97140

98-
# 3. Compare the commit date with the cutoff date
99-
if [ "$commit_date_seconds" -lt "$CUTOFF_SECONDS" ]; then
100-
printf "Branch: %-40s Last Commit Date: %s\n" "$branch_name" "$commit_date_str"
141+
# 4. Check if branch has commits after creation
142+
if [ "$commit_date_seconds" -le "$branch_creation_date_seconds" ]; then
143+
# No commits after branch creation - check if branch is older than 2 days
144+
if [ "$branch_creation_date_seconds" -lt "$BRANCH_AGE_CUTOFF_SECONDS" ]; then
145+
echo "Branch: $branch_name - No commits after creation but branch is older than 2 days"
146+
echo " Branch created: $branch_creation_date_str"
147+
echo " Last commit: $commit_date_str"
101148

102-
echo "- Deleting $branch_name in $repo_name"
103-
curl -L \
104-
-X DELETE \
105-
-H "Accept: application/vnd.github+json" \
106-
-H "Authorization: Bearer $GITHUB_TOKEN" \
107-
-H "X-GitHub-Api-Version: 2022-11-28" \
108-
https://api.github.com/repos/$repo_name/git/refs/heads/$branch_name
149+
echo "- Deleting $branch_name in $repo_name"
150+
curl -L \
151+
-X DELETE \
152+
-H "Accept: application/vnd.github+json" \
153+
-H "Authorization: Bearer $GITHUB_TOKEN" \
154+
-H "X-GitHub-Api-Version: 2022-11-28" \
155+
https://api.github.com/repos/$repo_name/git/refs/heads/$branch_name
156+
else
157+
echo "Branch: $branch_name SKIPPED - No commits after branch creation (branch too new)"
158+
echo " Branch created: $branch_creation_date_str"
159+
echo " Last commit: $commit_date_str"
160+
fi
161+
else
162+
# 5. Branch has commits after creation - use regular cutoff logic
163+
if [ "$commit_date_seconds" -lt "$CUTOFF_SECONDS" ]; then
164+
echo "Branch: $branch_name Last Commit Date: $commit_date_str"
165+
echo " Branch created: $branch_creation_date_str"
166+
echo " Has commits after branch creation: YES"
167+
168+
echo "- Deleting $branch_name in $repo_name"
169+
curl -L \
170+
-X DELETE \
171+
-H "Accept: application/vnd.github+json" \
172+
-H "Authorization: Bearer $GITHUB_TOKEN" \
173+
-H "X-GitHub-Api-Version: 2022-11-28" \
174+
https://api.github.com/repos/$repo_name/git/refs/heads/$branch_name
109175

176+
else
177+
echo "Branch: $branch_name SKIPPED - Recent commits (within cutoff)"
178+
echo " Last commit: $commit_date_str"
179+
fi
110180
fi
111181
done
112182

tasks/managed/collect-data/collect-data.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ metadata:
66
annotations:
77
tekton.dev/pipelines.minVersion: "0.12.1"
88
tekton.dev/tags: release
9+
test: "scott"
910
spec:
1011
description: >-
1112
Tekton task to collect data from release resources and verify that they do not contain any

0 commit comments

Comments
 (0)