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