-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add script to remove duplicate issues on declarations repository #1115
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
636c49b
Add script to remove duplicate issues
Ndpnt 4899a46
Close duplicates with link to original issue
Ndpnt 24a8c01
Add changelog entry
Ndpnt 1eace8e
Update scripts/reporter/duplicate/index.js
Ndpnt 5f02583
Improve configuration checks
Ndpnt 8ea50a7
Improve copywriting
MattiSG 317eaf6
Improve readme
Ndpnt 903f399
Remove obsolete try/catch block
Ndpnt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Duplicate issues removal script | ||
|
||
This script helps remove duplicate issues from a GitHub repository by closing issues that have the same title as any older issue. | ||
|
||
## Prerequisites | ||
|
||
1. Set up environment variables: | ||
- Create a `.env` file in the root directory | ||
- Add the GitHub personal access token of the bot that manages issues on your collection, with `repo` permissions: | ||
|
||
```shell | ||
OTA_ENGINE_GITHUB_TOKEN=your_github_token | ||
``` | ||
|
||
2. Configure the target repository in your chosen configuration file within the `config` folder: | ||
|
||
```json | ||
{ | ||
"@opentermsarchive/engine": { | ||
"reporter": { | ||
"githubIssues": { | ||
"repositories": { | ||
"declarations": "owner/repository" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
Run the script using: | ||
|
||
```shell | ||
node scripts/reporter/duplicate/index.js | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'dotenv/config'; | ||
import config from 'config'; | ||
import { Octokit } from 'octokit'; | ||
|
||
async function removeDuplicateIssues() { | ||
const repository = config.get('@opentermsarchive/engine.reporter.githubIssues.repositories.declarations'); | ||
|
||
if (!repository.includes('/') || repository.includes('https://')) { | ||
throw new Error(`Configuration entry "reporter.githubIssues.repositories.declarations" is expected to be a string in the format <owner>/<repo>, but received: "${repository}"`); | ||
} | ||
|
||
const [ owner, repo ] = repository.split('/'); | ||
|
||
const octokit = new Octokit({ auth: process.env.OTA_ENGINE_GITHUB_TOKEN }); | ||
|
||
console.log(`Getting issues from repository ${repository}…`); | ||
|
||
const issues = await octokit.paginate('GET /repos/{owner}/{repo}/issues', { | ||
owner, | ||
repo, | ||
state: 'open', | ||
per_page: 100, | ||
}); | ||
|
||
const onlyIssues = issues.filter(issue => !issue.pull_request); | ||
const issuesByTitle = new Map(); | ||
let counter = 0; | ||
|
||
console.log(`Found ${onlyIssues.length} issues`); | ||
|
||
for (const issue of onlyIssues) { | ||
if (!issuesByTitle.has(issue.title)) { | ||
issuesByTitle.set(issue.title, [issue]); | ||
} else { | ||
issuesByTitle.get(issue.title).push(issue); | ||
} | ||
} | ||
|
||
for (const [ title, duplicateIssues ] of issuesByTitle) { | ||
if (duplicateIssues.length === 1) continue; | ||
|
||
const originalIssue = duplicateIssues.reduce((oldest, current) => (new Date(current.created_at) < new Date(oldest.created_at) ? current : oldest)); | ||
|
||
console.log(`\nFound ${duplicateIssues.length - 1} duplicates for issue #${originalIssue.number} "${title}"`); | ||
|
||
for (const issue of duplicateIssues) { | ||
if (issue.number === originalIssue.number) { | ||
continue; | ||
} | ||
|
||
await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', { /* eslint-disable-line no-await-in-loop */ | ||
owner, | ||
repo, | ||
issue_number: issue.number, | ||
state: 'closed', | ||
}); | ||
|
||
await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', { /* eslint-disable-line no-await-in-loop */ | ||
owner, | ||
repo, | ||
issue_number: issue.number, | ||
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.`, | ||
}); | ||
|
||
counter++; | ||
console.log(`Closed issue #${issue.number}: ${issue.html_url}`); | ||
} | ||
} | ||
|
||
console.log(`\nDuplicate removal process completed; ${counter} issues closed`); | ||
} | ||
|
||
removeDuplicateIssues(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say this is a
no-release
considering there is strictly no change in behavior exposed to reusers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered that initially, but if we ask our partners to update to the latest version to access this script, they won’t be able to do so if there’s no official release available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, true 😅