7777
7878 PR_COMMIT_MESSAGE=$(echo "$PR_COMMIT_DETAILS_JSON" | jq -r '.commit.message')
7979
80- PR_COMMENT_BODY_ACCUMULATOR+="PR Commit Message:\n"
81- PR_COMMENT_BODY_ACCUMULATOR+="$(echo "$PR_COMMIT_MESSAGE" | sed 's/^/ /')\n"
82-
8380 # Extract the upstream Linux commit hash from the PR commit message.
8481 UPSTREAM_LINUX_HASH=$(echo "$PR_COMMIT_MESSAGE" | grep -Eo "^commit [0-9a-f]{40}$" | awk '{print $2}')
8582
9996
10097 FOUND_FIXING_COMMITS=()
10198 SEARCH_PAGE=1
102- MAX_SEARCH_PAGES=3 # Limit the search to prevent excessive API calls, e.g., up to 300 commits
99+ MAX_SEARCH_PAGES=3 # Limit the search to prevent excessive API calls (up to 300 commits)
100+ ALL_SEARCH_RESULTS_JSON="[]" # Initialize as an empty JSON array
103101
104102 while [ "$SEARCH_PAGE" -le "$MAX_SEARCH_PAGES" ]; do
105103 PR_COMMENT_BODY_ACCUMULATOR+=" Fetching search results (page $SEARCH_PAGE)...\n"
@@ -108,47 +106,52 @@ jobs:
108106 # URL-encode the query string for safe API calls
109107 ENCODED_SEARCH_QUERY=$(echo -n "$SEARCH_QUERY" | jq -sRr @uri)
110108
111- SEARCH_RESULTS_JSON =$(curl -sS -H "Accept: application/vnd.github.v3+json" \
109+ CURRENT_PAGE_RESULTS_JSON =$(curl -sS -H "Accept: application/vnd.github.v3+json" \
112110 -H "Authorization: token $GITHUB_TOKEN" \
113111 "https://api.github.com/search/commits?q=${ENCODED_SEARCH_QUERY}&per_page=100&page=$SEARCH_PAGE")
114112
115113 # Robustly check for an API error message from the search endpoint.
116- API_ERROR=$(echo "$SEARCH_RESULTS_JSON " | jq -r 'if type == "object" and .message then .message else null end')
114+ API_ERROR=$(echo "$CURRENT_PAGE_RESULTS_JSON " | jq -r 'if type == "object" and .message then .message else null end')
117115 if [ "$API_ERROR" != "null" ] && [ -n "$API_ERROR" ]; then
118116 PR_COMMENT_BODY_ACCUMULATOR+=" ERROR: Failed to retrieve search results (page $SEARCH_PAGE) from GitHub Search API: $API_ERROR\n"
119- PR_COMMENT_BODY_ACCUMULATOR+=" API Response snippet: $(echo "$SEARCH_RESULTS_JSON " | head -n 5)\n"
117+ PR_COMMENT_BODY_ACCUMULATOR+=" API Response snippet: $(echo "$CURRENT_PAGE_RESULTS_JSON " | head -n 5)\n"
120118 exit 1
121119 fi
122120
123121 # Check if there are any items on this page (search results are in the 'items' array)
124- CURRENT_PAGE_ITEMS=$(echo "$SEARCH_RESULTS_JSON " | jq '.items | length')
122+ CURRENT_PAGE_ITEMS=$(echo "$CURRENT_PAGE_RESULTS_JSON " | jq '.items | length')
125123 if [ "$CURRENT_PAGE_ITEMS" -eq 0 ]; then
126124 PR_COMMENT_BODY_ACCUMULATOR+=" No more search results found on this page. Stopping search.\n"
127125 break # No more commits to process
128126 fi
129127
130- # Iterate through each item (commit object) in the search results
131- echo "$SEARCH_RESULTS_JSON" | jq -c '.items[]' | while read -r commit_json_line; do
132- COMMIT_SHA=$(echo "$commit_json_line" | jq -r '.sha')
133- COMMIT_MESSAGE=$(echo "$commit_json_line" | jq -r '.commit.message') # Get the full message for reporting
134-
135- # Double-check the grep match for robustness to ensure it's a legitimate "Fixes:" tag
136- # and matches the FULL upstream hash.
137- if echo "$COMMIT_MESSAGE" | grep -Eq "^Fixes: [0-9a-fA-F]*${UPSTREAM_LINUX_HASH}.*"; then
138- PR_COMMENT_BODY_ACCUMULATOR+=" Found upstream commit \`$COMMIT_SHA\` that fixes \`$UPSTREAM_LINUX_HASH\`.\n"
139- PR_COMMENT_BODY_ACCUMULATOR+=" Fixing commit message snippet:\n"
140- PR_COMMENT_BODY_ACCUMULATOR+="$(echo "$COMMIT_MESSAGE" | grep -Ei "^Fixes:.*" | sed 's/^/ /')\n"
141- FOUND_FIXING_COMMITS+=("$COMMIT_SHA")
142- fi
143- done # End of while read loop for search results
128+ # Append current page's items to the accumulated results
129+ ALL_SEARCH_RESULTS_JSON=$(echo "$ALL_SEARCH_RESULTS_JSON" "$CURRENT_PAGE_RESULTS_JSON" | jq -s '.[0] + .[1].items')
144130
145131 SEARCH_PAGE=$((SEARCH_PAGE + 1))
146132 done # End of while loop for search pages
147133
134+ # Process all accumulated search results with a single jq command for filtering
135+ # This filters for the exact "Fixes: <FULL_SHA>" and extracts relevant info.
136+ FILTERED_COMMITS=$(echo "$ALL_SEARCH_RESULTS_JSON" | jq -r --arg full_hash "$UPSTREAM_LINUX_HASH" '
137+ .[] |
138+ select(.commit.message | test("Fixes: [0-9a-fA-F]*\\Q" + $full_hash + "\\E.*"; "i")) | # "i" for case-insensitive
139+ "\(.sha):\(.commit.message | split("\n")[] | select(test("Fixes: [0-9a-fA-F]*\\Q" + $full_hash + "\\E.*"; "i")))"
140+ ')
141+
142+ if [ -n "$FILTERED_COMMITS" ]; then
143+ IFS=$'\n' read -r -d '' -a FOUND_FIXING_COMMITS <<< "$FILTERED_COMMITS"
144+ else
145+ FOUND_FIXING_COMMITS=() # Ensure it's an empty array if no matches
146+ fi
147+
148148 if [ ${#FOUND_FIXING_COMMITS[@]} -gt 0 ]; then
149149 PR_COMMENT_BODY_ACCUMULATOR+=" SUCCESS: Found ${#FOUND_FIXING_COMMITS[@]} upstream Linux commit(s) that fix \`$UPSTREAM_LINUX_HASH\`:\n"
150- for FIX_COMMIT_SHA in "${FOUND_FIXING_COMMITS[@]}"; do
151- PR_COMMENT_BODY_ACCUMULATOR+=" - \`$FIX_COMMIT_SHA\`\n"
150+ for FIX_COMMIT_ENTRY in "${FOUND_FIXING_COMMITS[@]}"; do
151+ # Split SHA and message part for display
152+ FIX_COMMIT_SHA=$(echo "$FIX_COMMIT_ENTRY" | cut -d ':' -f 1)
153+ FIX_MESSAGE_SNIPPET=$(echo "$FIX_COMMIT_ENTRY" | cut -d ':' -f 2-)
154+ PR_COMMENT_BODY_ACCUMULATOR+=" - \`$FIX_COMMIT_SHA\` (Fixes tag: \`$FIX_MESSAGE_SNIPPET\`)\n"
152155 done
153156 else
154157 PR_COMMENT_BODY_ACCUMULATOR+=" No upstream fixes found for \`$UPSTREAM_LINUX_HASH\`.\n"
0 commit comments