|
| 1 | +import 'dotenv/config'; |
| 2 | +import config from 'config'; |
| 3 | +import { Octokit } from 'octokit'; |
| 4 | + |
| 5 | +async function removeDuplicateIssues() { |
| 6 | + const repository = config.get('@opentermsarchive/engine.reporter.githubIssues.repositories.declarations'); |
| 7 | + |
| 8 | + if (!repository.includes('/') || repository.includes('https://')) { |
| 9 | + throw new Error(`Configuration entry "reporter.githubIssues.repositories.declarations" is expected to be a string in the format <owner>/<repo>, but received: "${repository}"`); |
| 10 | + } |
| 11 | + |
| 12 | + const [ owner, repo ] = repository.split('/'); |
| 13 | + |
| 14 | + const octokit = new Octokit({ auth: process.env.OTA_ENGINE_GITHUB_TOKEN }); |
| 15 | + |
| 16 | + console.log(`Getting issues from repository ${repository}…`); |
| 17 | + |
| 18 | + const issues = await octokit.paginate('GET /repos/{owner}/{repo}/issues', { |
| 19 | + owner, |
| 20 | + repo, |
| 21 | + state: 'open', |
| 22 | + per_page: 100, |
| 23 | + }); |
| 24 | + |
| 25 | + const onlyIssues = issues.filter(issue => !issue.pull_request); |
| 26 | + const issuesByTitle = new Map(); |
| 27 | + let counter = 0; |
| 28 | + |
| 29 | + console.log(`Found ${onlyIssues.length} issues`); |
| 30 | + |
| 31 | + for (const issue of onlyIssues) { |
| 32 | + if (!issuesByTitle.has(issue.title)) { |
| 33 | + issuesByTitle.set(issue.title, [issue]); |
| 34 | + } else { |
| 35 | + issuesByTitle.get(issue.title).push(issue); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + for (const [ title, duplicateIssues ] of issuesByTitle) { |
| 40 | + if (duplicateIssues.length === 1) continue; |
| 41 | + |
| 42 | + const originalIssue = duplicateIssues.reduce((oldest, current) => (new Date(current.created_at) < new Date(oldest.created_at) ? current : oldest)); |
| 43 | + |
| 44 | + console.log(`\nFound ${duplicateIssues.length - 1} duplicates for issue #${originalIssue.number} "${title}"`); |
| 45 | + |
| 46 | + for (const issue of duplicateIssues) { |
| 47 | + if (issue.number === originalIssue.number) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', { /* eslint-disable-line no-await-in-loop */ |
| 52 | + owner, |
| 53 | + repo, |
| 54 | + issue_number: issue.number, |
| 55 | + state: 'closed', |
| 56 | + }); |
| 57 | + |
| 58 | + await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', { /* eslint-disable-line no-await-in-loop */ |
| 59 | + owner, |
| 60 | + repo, |
| 61 | + issue_number: issue.number, |
| 62 | + body: `This issue is detected as duplicate as it has the same title as #${originalIssue.number}. It most likely was created accidentally by an engine older than [v2.3.2](https://github.com/OpenTermsArchive/engine/releases/tag/v2.3.2). Closing automatically.`, |
| 63 | + }); |
| 64 | + |
| 65 | + counter++; |
| 66 | + console.log(`Closed issue #${issue.number}: ${issue.html_url}`); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + console.log(`\nDuplicate removal process completed; ${counter} issues closed`); |
| 71 | +} |
| 72 | + |
| 73 | +removeDuplicateIssues(); |
0 commit comments