Skip to content

Sync Codacy Issues to GitHub #9

Sync Codacy Issues to GitHub

Sync Codacy Issues to GitHub #9

Workflow file for this run

name: Sync Codacy Issues to GitHub
on:
check_run:
types: [completed]
jobs:
sync-issues:
if: github.event.check_run.name == 'Codacy Static Code Analysis' &&
github.event.check_run.pull_requests != null &&
github.event.check_run.pull_requests != ''
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set PR number
run: echo "PR_NUMBER=${{ github.event.check_run.pull_requests[0].number }}" >> $GITHUB_ENV
- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y jq
- name: Fetch Codacy issues (v3 API)
env:
CODACY_TOKEN: ${{ secrets.CODACY_API_TOKEN }}
PROJECT_UUID: ${{ secrets.CODACY_PROJECT_UUID }}
PR_NUMBER: ${{ env.PR_NUMBER }}
OWNER: ${{ github.repository_owner }}
REPO_NAME: ${{ github.event.repository.name }}
run: |
curl -s -H "api-token: $CODACY_TOKEN" \
"https://api.codacy.com/api/v3/analysis/organizations/gh/$OWNER/repositories/$REPO_NAME/pull-requests/$PR_NUMBER/issues?severity=MEDIUM,CRITICAL" \
-o codacy_issues.json
echo "Found $(jq '.data.issues | length' codacy_issues.json) issues"
- name: Authenticate gh CLI
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token
- name: Sync Codacy issues to GitHub
run: |
# Extract current Codacy issues (Warning + Error severities only)
issues=$(jq -c '.data[]
| select(.commitIssue.patternInfo.severityLevel == "Warning"
or .commitIssue.patternInfo.severityLevel == "Error")
| {
id: .commitIssue.issueId,
title: .commitIssue.message,
body: ("**File:** " + .commitIssue.filePath
+ "\n**Line:** " + (.commitIssue.lineNumber|tostring)
+ "\n**Severity:** " + .commitIssue.patternInfo.severityLevel
+ "\n\n" + .commitIssue.lineText),
labels: ["codacy", .commitIssue.patternInfo.severityLevel]
}' codacy.json)
# Track Codacy issue IDs from this run
codacy_ids=$(echo "$issues" | jq -r .id)
# Process each Codacy issue
echo "$issues" | while read -r issue; do
issue_id=$(echo "$issue" | jq -r .id)
title=$(echo "$issue" | jq -r .title)
body=$(echo "$issue" | jq -r .body)
labels=$(echo "$issue" | jq -r '.labels | join(",")')
# Search for an existing GitHub issue with this Codacy issueId
existing=$(gh issue list \
--search "in:body $issue_id" \
--json number \
--jq '.[0].number')
if [ -n "$existing" ]; then
echo "Updating issue #$existing for Codacy issue $issue_id"
gh issue edit "$existing" --title "$title" --body "$body\n\n_Codacy issueId: $issue_id_" --add-label "$labels"
else
echo "Creating new issue for Codacy issue $issue_id"
gh issue create \
--title "$title" \
--body "$body\n\n_Codacy issueId: $issue_id_" \
--label "$labels"
fi
done
# Now close any GitHub issues that no longer appear in Codacy
open_codacy_issues=$(gh issue list \
--label codacy \
--state open \
--json number,body \
--jq '.[] | {number, id: ( .body | capture("(?<id>[a-f0-9]{32})").id )}')
echo "$open_codacy_issues" | while read -r gi; do
gi_number=$(echo "$gi" | jq -r .number)
gi_id=$(echo "$gi" | jq -r .id)
if ! echo "$codacy_ids" | grep -q "$gi_id"; then
echo "Closing GitHub issue #$gi_number (Codacy issue $gi_id resolved)"
gh issue close "$gi_number" --comment "Resolved in latest Codacy scan ✅"
fi
done
env:
GH_TOKEN: ${{ github.token }}