11const { isNil, uniqBy, flatten } = require ( "lodash" ) ;
22const pFilter = require ( "p-filter" ) ;
33const issueParser = require ( "issue-parser" ) ;
4+ const debug = require ( "debug" ) ( "semantic-release:github-prerelease" ) ;
45
5- // this file will be copied two folders deeper
6- const resolveConfig = require ( "./resolve-config" ) ;
7- const getClient = require ( "./resolve-config" ) ;
8- const parseGithubUrl = require ( "./parse-github-url" ) ;
9- const getSearchQueries = require ( "./get-search-queries" ) ;
6+ const resolveConfig = require ( "@semantic-release/github/lib/resolve-config" ) ;
7+ const getClient = require ( "@semantic-release/github/lib/get-client" ) ;
8+ const parseGithubUrl = require ( "@semantic-release/github/lib/parse-github-url" ) ;
9+ const getSearchQueries = require ( "@semantic-release/github/lib/get-search-queries" ) ;
10+
11+ // these files will be copied two folders deeper
1012const success = require ( "./new-success" ) ;
1113
14+ const ghGet = async ( ghResult ) => {
15+ const res = await ghResult ;
16+ return ( res && res . data ) || res ;
17+ } ;
18+
1219module . exports = async ( pluginConfig , context ) => {
1320 const {
1421 options : { repositoryUrl } ,
@@ -22,15 +29,19 @@ module.exports = async (pluginConfig, context) => {
2229 proxy,
2330 successComment,
2431 } = resolveConfig ( pluginConfig , context ) ;
25- const github = getClient ( {
26- githubToken,
27- githubUrl,
28- githubApiPathPrefix,
29- proxy,
30- } ) ;
32+ const github = getClient (
33+ {
34+ githubToken,
35+ githubUrl,
36+ githubApiPathPrefix,
37+ proxy,
38+ } ,
39+ context
40+ ) ;
41+ const { login } = await ghGet ( github . users . getAuthenticated ( ) ) ;
3142 const [ owner , repo ] = (
32- await github . repos . get ( parseGithubUrl ( repositoryUrl ) )
33- ) . data . full_name . split ( "/" ) ;
43+ await ghGet ( github . repos . get ( parseGithubUrl ( repositoryUrl ) ) )
44+ ) . full_name . split ( "/" ) ;
3445
3546 if ( successComment === false ) {
3647 logger . log ( "Skipping old comment deletion." ) ;
@@ -42,20 +53,23 @@ module.exports = async (pluginConfig, context) => {
4253 const shas = commits . map ( ( { hash } ) => hash ) ;
4354
4455 const searchQueries = getSearchQueries (
45- `repo:${ owner } /${ repo } +type:pr+is:merged ` ,
56+ `repo:${ owner } /${ repo } +type:pr+is:open ` ,
4657 shas
4758 ) . map (
48- async ( q ) => ( await github . search . issuesAndPullRequests ( { q } ) ) . data . items
59+ async ( q ) =>
60+ ( await ghGet ( github . search . issuesAndPullRequests ( { q } ) ) ) . items
4961 ) ;
5062
5163 const prs = await pFilter (
5264 uniqBy ( flatten ( await Promise . all ( searchQueries ) ) , "number" ) ,
5365 async ( { number } ) =>
5466 (
55- await github . pulls . listCommits ( { owner, repo, pull_number : number } )
56- ) . data . find ( ( { sha } ) => shas . includes ( sha ) ) ||
67+ await ghGet (
68+ github . pulls . listCommits ( { owner, repo, pull_number : number } )
69+ )
70+ ) . find ( ( { sha } ) => shas . includes ( sha ) ) ||
5771 shas . includes (
58- ( await github . pulls . get ( { owner, repo, pull_number : number } ) ) . data
72+ ( await ghGet ( github . pulls . get ( { owner, repo, pull_number : number } ) ) )
5973 . merge_commit_sha
6074 )
6175 ) ;
@@ -77,23 +91,32 @@ module.exports = async (pluginConfig, context) => {
7791 : issues ;
7892 } , [ ] ) ;
7993
94+ const allIssues = uniqBy ( [ ...prs , ...issues ] , "number" ) ;
8095 await Promise . all (
81- uniqBy ( [ ... prs , ... issues ] , "number" ) . map ( async ( issue ) => {
96+ allIssues . map ( async ( issue ) => {
8297 try {
8398 const comments = { owner, repo, issue_number : issue . number } ;
84- const foundComments = (
85- await github . issues . listComments ( comments )
86- ) . filter ( ( comment ) => comment . user . login === "im-pipeline-bot" ) ;
99+ const allComments = await ghGet ( github . issues . listComments ( comments ) ) ;
100+ const targetComments = allComments . filter (
101+ ( comment ) => comment . user . login === login
102+ ) ;
87103
88- for ( let c in foundComments ) {
89- await github . issues . deleteComment ( {
90- owner,
91- repo,
92- comment_id : c . id ,
93- } ) ;
94- logger . log ( "Removed comment %c from issue #%d" , c . id , issue . number ) ;
95- }
104+ await Promise . all (
105+ targetComments . map ( async ( c ) => {
106+ await github . issues . deleteComment ( {
107+ owner,
108+ repo,
109+ comment_id : c . id ,
110+ } ) ;
111+ logger . log (
112+ "Removed comment %s from issue #%d" ,
113+ c . id ,
114+ issue . number
115+ ) ;
116+ } )
117+ ) ;
96118 } catch ( error ) {
119+ debug ( "got an error deleting a comment" , error ) ;
97120 if ( error . status === 403 ) {
98121 logger . error (
99122 "Not allowed to delete a comment to the issue #%d." ,
0 commit comments