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