Przeglądaj źródła

chore: Improve error handling and debugging in Jira sync workflow

David Höck 8 miesięcy temu
rodzic
commit
bf777b96e9
1 zmienionych plików z 64 dodań i 16 usunięć
  1. 64 16
      .github/workflows/sync-jira.yml

+ 64 - 16
.github/workflows/sync-jira.yml

@@ -22,41 +22,89 @@ jobs:
           JIRA_BASE:   ${{ secrets.JIRA_BASE }}    
           PROJECT_KEY: VEN
         run: |
+          set -e  # Exit on any error
+          set -x  # Print each command before execution
+          
+          # Verify environment variables
+          if [ -z "$JIRA_EMAIL" ] || [ -z "$JIRA_TOKEN" ] || [ -z "$JIRA_BASE" ]; then
+            echo "Error: Required Jira environment variables are not set"
+            exit 1
+          fi
+          
           GH_URL="https://github.com/${{ github.repository }}/issues/${{ github.event.issue.number }}"
-          SUMMARY=$(jq -Rs . <<<"${{ github.event.issue.title }}")
-          DESC=$(jq -Rs . <<<"Imported from GitHub: $GH_URL")
-          # look for an existing ticket whose description contains the GH URL
-          JQL=$(jq -Rs . <<<"description ~ \"$GH_URL\"")
-          SEARCH="$JIRA_BASE/rest/api/3/search?jql=$JQL"
+          echo "GitHub URL: $GH_URL"
+          
+          # Test jq installation
+          if ! command -v jq &> /dev/null; then
+            echo "Error: jq is not installed"
+            exit 1
+          fi
+          
+          # Prepare the data with error checking
+          SUMMARY=$(jq -Rs . <<<"${{ github.event.issue.title }}" || { echo "Error processing summary"; exit 1; })
+          DESC=$(jq -Rs . <<<"Imported from GitHub: $GH_URL" || { echo "Error processing description"; exit 1; })
+          JQL=$(jq -Rs . <<<"description ~ \"$GH_URL\"" || { echo "Error processing JQL"; exit 1; })
+          
+          echo "Summary: $SUMMARY"
+          echo "Description: $DESC"
+          echo "JQL: $JQL"
+          
+          # URL encode the JQL query
+          ENCODED_JQL=$(echo "$JQL" | jq -sRr @uri)
+          SEARCH="$JIRA_BASE/rest/api/3/search?jql=$ENCODED_JQL"
           
-          # Add debug output
-          echo "Searching Jira with URL: $GH_URL"
-          echo "JQL query: $JQL"
+          echo "Searching Jira with URL: $SEARCH"
+          
+          # Search with error checking
+          SEARCH_RESULT=$(curl -f -s -u "$JIRA_EMAIL:$JIRA_TOKEN" "$SEARCH" || { 
+            echo "Error: Jira search failed with status $?"
+            exit 1
+          })
           
-          SEARCH_RESULT=$(curl -s -u "$JIRA_EMAIL:$JIRA_TOKEN" "$SEARCH")
           echo "Search result: $SEARCH_RESULT"
           
-          KEY=$(echo "$SEARCH_RESULT" | jq -r '.issues[0].key // empty')
+          # Parse the key with error checking
+          KEY=$(echo "$SEARCH_RESULT" | jq -r '.issues[0].key // empty' || { 
+            echo "Error parsing search result"
+            exit 1
+          })
+          
           echo "Found existing key: $KEY"
           
           if [ -z "$KEY" ]; then
             echo "No existing ticket found, creating new one"
             BODY=$(jq -n --arg summary "$SUMMARY" --arg desc "$DESC" --arg proj "$PROJECT_KEY" '
               {fields:{project:{key:$proj},summary:$summary,description:$desc,
-              issuetype:{name:"Task"},labels:["core-team"]}}')
+              issuetype:{name:"Task"},labels:["core-team"]}}' || {
+                echo "Error creating request body"
+                exit 1
+              })
             
-            # Add debug output for creation
             echo "Creating ticket with body: $BODY"
-            CREATE_RESULT=$(curl -s -u "$JIRA_EMAIL:$JIRA_TOKEN" \
+            
+            # Create with error checking
+            CREATE_RESULT=$(curl -f -s -u "$JIRA_EMAIL:$JIRA_TOKEN" \
                    -H "Content-Type: application/json" \
-                   --data "$BODY" "$JIRA_BASE/rest/api/3/issue")
+                   --data "$BODY" "$JIRA_BASE/rest/api/3/issue" || {
+                     echo "Error: Jira create failed with status $?"
+                     exit 1
+                   })
+            
             echo "Create result: $CREATE_RESULT"
             
-            KEY=$(echo "$CREATE_RESULT" | jq -r .key)
+            KEY=$(echo "$CREATE_RESULT" | jq -r .key || {
+              echo "Error parsing create result"
+              exit 1
+            })
+            
             echo "Created new key: $KEY"
           fi
           
-          # Final debug output
+          if [ -z "$KEY" ]; then
+            echo "Error: Failed to get a valid Jira key"
+            exit 1
+          fi
+          
           echo "Final key value: $KEY"
           echo "key=$KEY" >> $GITHUB_OUTPUT
       - name: Record the mapping on the GH issue