|
| 1 | + |
1 | 2 | name: Sync Codacy Issues |
2 | 3 |
|
3 | 4 | on: |
|
10 | 11 | push: |
11 | 12 | # branches to consider in the event; optional, defaults to all |
12 | 13 | branches: |
13 | | - - main |
| 14 | + - maion |
14 | 15 | jobs: |
15 | 16 | sync-codacy-issues: |
16 | 17 | runs-on: ubuntu-latest |
@@ -166,4 +167,69 @@ jobs: |
166 | 167 | } |
167 | 168 | } |
168 | 169 |
|
| 170 | + - name: Close Duplicate Codacy Issues |
| 171 | + uses: actions/github-script@v7 |
| 172 | + with: |
| 173 | + script: | |
| 174 | + const dryRun = "${{ github.event.inputs.dry_run }}" === "true"; |
| 175 | +
|
| 176 | + // Fetch all issues with the codacy label (open + closed) |
| 177 | + const allIssues = await github.paginate( |
| 178 | + github.rest.issues.listForRepo, |
| 179 | + { |
| 180 | + owner: context.repo.owner, |
| 181 | + repo: context.repo.repo, |
| 182 | + state: "all", |
| 183 | + labels: ["codacy"] |
| 184 | + } |
| 185 | + ); |
| 186 | +
|
| 187 | + const grouped = {}; |
| 188 | +
|
| 189 | + for (const issue of allIssues) { |
| 190 | + const matches = issue.body?.match(/codacy-issue-([a-f0-9]+)/g); |
| 191 | + if (!matches) continue; |
| 192 | +
|
| 193 | + for (const match of matches) { |
| 194 | + const issueId = match.replace("codacy-issue-", ""); |
| 195 | + if (!grouped[issueId]) grouped[issueId] = []; |
| 196 | + grouped[issueId].push(issue); |
| 197 | + } |
| 198 | + } |
| 199 | +
|
| 200 | + for (const [issueId, issues] of Object.entries(grouped)) { |
| 201 | + if (issues.length <= 1) continue; // No duplicates |
| 202 | +
|
| 203 | + // Sort by creation date descending (newest first) |
| 204 | + issues.sort((a, b) => new Date(b.created_at) - new Date(a.created_at)); |
| 205 | +
|
| 206 | + const [latest, ...duplicates] = issues; |
| 207 | + console.log(`Found ${issues.length} duplicates for Codacy issueId ${issueId}. Keeping #${latest.number}.`); |
| 208 | +
|
| 209 | + for (const dup of duplicates) { |
| 210 | + if (dup.state === "closed") continue; |
| 211 | +
|
| 212 | + if (dryRun) { |
| 213 | + console.log(`[DRY RUN] Would close duplicate issue #${dup.number} (Codacy issueId ${issueId})`); |
| 214 | + } else { |
| 215 | + await github.rest.issues.createComment({ |
| 216 | + owner: context.repo.owner, |
| 217 | + repo: context.repo.repo, |
| 218 | + issue_number: dup.number, |
| 219 | + body: `Auto-closing as duplicate of newer Codacy issue #${latest.number} for ID \`${issueId}\`.` |
| 220 | + }); |
| 221 | +
|
| 222 | + await github.rest.issues.update({ |
| 223 | + owner: context.repo.owner, |
| 224 | + repo: context.repo.repo, |
| 225 | + issue_number: dup.number, |
| 226 | + state: "closed" |
| 227 | + }); |
| 228 | +
|
| 229 | + console.log(`❌ Closed duplicate issue #${dup.number} (Codacy issueId ${issueId})`); |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | +
|
169 | 235 | |
0 commit comments