Sync Codacy Issues to GitHub #32
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: ${{ secrets.CODACY_API_TOKEN }}" \ | |
| "https://api.codacy.com/api/v3/analysis/organizations/gh/$OWNER/repositories/$REPO_NAME/pull-requests/$PR_NUMBER/issues" \ | |
| | jq '.data | |
| | map(select(.commitIssue.patternInfo.severityLevel == "Warning" | |
| or .commitIssue.patternInfo.severityLevel == "Error"))' \ | |
| > codacy_pr_issues.json | |
| - name: Authenticate gh CLI | |
| run: | | |
| echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token | |
| - name: Sync Codacy issues to GitHub | |
| run: | | |
| # 2️⃣ Get the list of Codacy issue IDs for this PR | |
| codacy_ids=$(jq -r '.[].commitIssue.issueId' codacy_pr_issues.json) | |
| # 3️⃣ Create or update GitHub issues for new/remaining PR issues | |
| jq -c '.[]' codacy_pr_issues.json | while read -r issue; do | |
| title=$(echo "$issue" | jq -r '.commitIssue.message') | |
| body=$(echo "$issue" | jq -r ' | |
| "**File:** " + .commitIssue.filePath + | |
| "\n**Line:** " + (.commitIssue.lineNumber|tostring) + | |
| "\n**Severity:** " + .commitIssue.patternInfo.severityLevel + | |
| "\n\n" + .commitIssue.lineText + | |
| "\n\n_Codacy issueId: " + .commitIssue.issueId + "_" | |
| ') | |
| labels="codacy" | |
| existing=$(gh issue list --search "in:body $(echo "$issue" | jq -r '.commitIssue.issueId')" --json number --jq '.[0].number') | |
| if [ -n "$existing" ]; then | |
| gh issue edit "$existing" --title "$title" --body "$body" --add-label "$labels" | |
| else | |
| gh issue create --title "$title" --body "$body" --label "$labels" | |
| fi | |
| done | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |