Skip to content

Commit c2c4f53

Browse files
authored
Fix inconsistent PR Number extraction (#1416)
Inconsistent PR Number extraction causing Sonar workflow to fail occasionally.
1 parent c82d713 commit c2c4f53

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

.github/actions/test/action.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ runs:
5757
pwd
5858
ls -lh coverage.xml
5959
head -3 coverage.xml
60-
60+
61+
# Add PR number to coverage file for SonarCloud analysis
62+
- name: Inject PR number into coverage file
63+
if: github.event_name == 'pull_request'
64+
shell: bash
65+
run: |
66+
sed -i '2i <!-- PR ${{ github.event.number }} -->' coverage.xml
67+
echo "PR Number: ${{ github.event.number }} injected into coverage file"
68+
6169
# Upload coverage artifact to Github for SonarCloud (only from Python 3.11)
6270
- name: Upload coverage as artifact
6371
if: inputs.python-version == '3.11'

.github/workflows/sonar-pr.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,28 @@ jobs:
5252
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5353

5454
run: |
55-
# Extract PR number directly from workflow_run context
56-
PR_NUM='${{ github.event.workflow_run.pull_requests[0].number }}'
55+
# Extract PR number from coverage.xml file
56+
if [ -f coverage.xml ] && [ -s coverage.xml ]; then
57+
PR_NUMBER=$(grep -m 1 '<!-- PR' coverage.xml | awk '{print $3}' || echo "")
58+
else
59+
echo "❌ Coverage.xml file not found or empty."
60+
PR_NUMBER="0"
61+
fi
5762
5863
# Check if PR number is valid
59-
if [[ -n "$PR_NUM" ]] && [[ "$PR_NUM" =~ ^[0-9]+$ ]]; then
64+
if [[ -n "$PR_NUMBER" ]] && [[ "$PR_NUMBER" =~ ^[0-9]+$ ]]; then
6065
# Valid PR number found
61-
echo "├── PR Number: ✅ ${PR_NUM}"
62-
echo "PR_NUMBER=$PR_NUM" >> $GITHUB_ENV
66+
echo "├── PR Number: ✅ ${PR_NUMBER}"
67+
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
6368
else
64-
echo "├── PR Number: ${PR_NUM} - ❌ Invalid (not found in workflow_run context)"
69+
echo "├── PR Number: ${PR_NUMBER} - ❌ Invalid (not found in coverage.xml file)"
6570
echo "├── Result: ⏭️ Skip - \"No valid PR number available\""
6671
echo "├── This job requires a PR number to run PR analysis."
6772
exit 1
6873
fi
6974
7075
# Get PR metadata from GitHub API
71-
PR_DATA=$(gh api "repos/$WORKFLOW_REPO_NAME/pulls/$PR_NUM")
76+
PR_DATA=$(gh api "repos/$WORKFLOW_REPO_NAME/pulls/$PR_NUMBER")
7277
PR_BASE=$(echo "$PR_DATA" | jq -r '.base.ref')
7378
PR_HEAD=$(echo "$PR_DATA" | jq -r '.head.ref')
7479

0 commit comments

Comments
 (0)