From 2dd88a7b898c08106eb81dfc508cfb96b33f980e Mon Sep 17 00:00:00 2001 From: robojerk Date: Mon, 21 Jul 2025 19:50:43 +0000 Subject: [PATCH] Fix HTTP code extraction and implement dynamic package ID lookup for assignment --- .../workflows/build-libostree-backport.yml | 97 ++++++++++++------- 1 file changed, 61 insertions(+), 36 deletions(-) diff --git a/.forgejo/workflows/build-libostree-backport.yml b/.forgejo/workflows/build-libostree-backport.yml index e9e964c..f04bcfb 100644 --- a/.forgejo/workflows/build-libostree-backport.yml +++ b/.forgejo/workflows/build-libostree-backport.yml @@ -304,26 +304,22 @@ jobs: echo "$response_and_code" echo "=== End curl output ===" - # Extract HTTP code (from headers) - http_code=$(echo "$response_and_code" | head -n 1 | grep -oP 'HTTP/\d.\d \K\d{3}') - # Extract JSON body (after headers) + # Extract HTTP code from verbose output (look for the actual HTTP response line) + http_code=$(echo "$response_and_code" | grep -oP '^< HTTP/\d.\d \K\d{3}' | tail -1 || echo "N/A") + # Extract JSON body (after headers) - but 201 responses typically have no body response_body=$(echo "$response_and_code" | awk '/^\r?$/{p=1;next}p') echo "HTTP Response Code: $http_code" if [ "$http_code" = "201" ]; then echo "✅ Debian Package Registry upload SUCCESS for $deb_file" - # Parse JSON body to get the 'id' - package_id=$(echo "$response_body" | jq -r '.id' 2>/dev/null) - if [ -n "$package_id" ] && [ "$package_id" != "null" ]; then - echo "Captured Package ID: $package_id" - uploaded_debian_ids+=("$package_id") - else - echo "Could not extract Package ID from response for $deb_file." - fi + # Note: 201 responses typically have no body, so no package ID to extract + # We'll need to find the package ID later by listing packages + echo "Package uploaded successfully (no ID returned in response)" else echo "❌ Debian Package Registry upload FAILED for $deb_file (HTTP $http_code)" echo "Response body: $response_body" + exit 1 # Fail the step if upload fails fi fi done @@ -429,8 +425,6 @@ jobs: echo "=== Assigning uploaded packages to repository ===" REPO_ID="${{ steps.get_repo_id.outputs.REPO_ID }}" - # Convert space-separated strings back to arrays - read -ra DEBIAN_PACKAGE_IDS <<< "${{ steps.debian_upload.outputs.debian_package_ids }}" if [ -z "$REPO_ID" ]; then echo "Repository ID not found. Cannot assign packages." @@ -438,29 +432,60 @@ jobs: fi echo "Repository ID: $REPO_ID" - echo "Debian Package IDs: ${DEBIAN_PACKAGE_IDS[*]}" - # Process Debian packages - if [ ${#DEBIAN_PACKAGE_IDS[@]} -gt 0 ]; then - for package_id in "${DEBIAN_PACKAGE_IDS[@]}"; do - echo "Assigning Debian package ID $package_id to repo $REPO_ID..." - response=$(curl -s -w "%{http_code}" -X POST \ - -H "Authorization: Bearer ${{ secrets.ACCESS_TOKEN }}" \ - -H "Content-Type: application/json" \ - -d "{\"id\": $package_id}" \ - "https://git.raines.xyz/api/v1/packages/robojerk/libostree-dev/attach") - - http_code=$(echo "$response" | tail -c 4) - echo "Assignment HTTP Response Code for Debian package $package_id: $http_code" - if [ "$http_code" = "200" ] || [ "$http_code" = "204" ]; then - echo "✅ Successfully assigned Debian package ID $package_id" - else - echo "❌ Failed to assign Debian package ID $package_id (HTTP $http_code)" - echo "Response: $response" - fi - done - else - echo "No Debian package IDs captured for assignment." + # Since we can't get package IDs from upload responses, we need to find them dynamically + echo "Listing all packages to find recently uploaded ones..." + all_packages_json=$(curl -s -H "Authorization: Bearer ${{ secrets.ACCESS_TOKEN }}" \ + "https://git.raines.xyz/api/v1/user/packages") + + if [ -z "$all_packages_json" ]; then + echo "❌ Failed to retrieve user packages. Cannot assign." + exit 1 fi - # Generic package processing removed - focusing only on Debian Package Registry \ No newline at end of file + echo "Found packages:" + echo "$all_packages_json" | jq -r '.[] | "\(.id): \(.name) \(.version) (\(.package_type))"' 2>/dev/null || echo "Could not parse packages JSON" + + # Loop through each .deb file we attempted to upload + for deb_file in release-assets/*.deb; do + if [ -f "$deb_file" ]; then + filename=$(basename "$deb_file") + echo "Looking for package: $filename" + + # Parse package info from filename + # Example: libostree-dev_2025.2-1~noble1_amd64.deb + package_name=$(echo "$filename" | cut -d'_' -f1) + package_version=$(echo "$filename" | cut -d'_' -f2) + + echo "Searching for package: name=$package_name, version=$package_version" + + # Try to find the package ID by name and version + package_id=$(echo "$all_packages_json" | jq -r --arg name "$package_name" --arg version "$package_version" \ + '.[] | select(.name == $name and .version == $version and .package_type == "debian") | .id' 2>/dev/null | head -1) + + if [ -n "$package_id" ] && [ "$package_id" != "null" ]; then + echo "Found package ID $package_id for $filename" + + # Assign the package to the repository + response=$(curl -s -w "%{http_code}" -X POST \ + -H "Authorization: Bearer ${{ secrets.ACCESS_TOKEN }}" \ + -H "Content-Type: application/json" \ + -d "{\"id\": $package_id}" \ + "https://git.raines.xyz/api/v1/packages/robojerk/libostree-dev/attach") + + http_code=$(echo "$response" | tail -c 4) + echo "Assignment HTTP Response Code for $filename (ID $package_id): $http_code" + + if [ "$http_code" = "200" ] || [ "$http_code" = "204" ]; then + echo "✅ Successfully assigned $filename (ID $package_id)" + else + echo "❌ Failed to assign $filename (ID $package_id) (HTTP $http_code)" + echo "Response: $(echo "$response" | head -c -4)" + fi + else + echo "⚠️ Could not find package ID for $filename in user packages" + echo "Available packages:" + echo "$all_packages_json" | jq -r '.[] | " \(.name) \(.version) (\(.package_type))"' 2>/dev/null || echo "Could not parse packages" + fi + fi + done \ No newline at end of file