@@ -8,8 +8,13 @@ import EventEmitter from 'events'
88import _ from 'lodash-es'
99import logger from '../logger/index.js'
1010
11+ function shootKey ( projectName , name ) {
12+ return JSON . stringify ( [ projectName , name ] )
13+ }
14+
1115function init ( ) {
1216 const issues = new Map ( )
17+ const issuesByShoot = new Map ( ) // Map<shootKey, Map<issueNumber, issue>>
1318 const commentsForIssues = new Map ( ) // we could also think of getting rid of the comments cache
1419 const emitter = new EventEmitter ( )
1520
@@ -55,11 +60,19 @@ function init () {
5560 }
5661
5762 function getIssueNumbersForNameAndProjectName ( { name, projectName } ) {
58- return _
59- . chain ( getIssues ( ) )
60- . filter ( _ . matches ( { metadata : { name, projectName } } ) )
61- . map ( 'metadata.number' )
62- . value ( )
63+ const map = issuesByShoot . get ( shootKey ( projectName , name ) )
64+ if ( ! map ) {
65+ return [ ]
66+ }
67+ return Array . from ( map . keys ( ) )
68+ }
69+
70+ function getIssuesForShoot ( { name, projectName } ) {
71+ const map = issuesByShoot . get ( shootKey ( projectName , name ) )
72+ if ( ! map ) {
73+ return [ ]
74+ }
75+ return Array . from ( map . values ( ) )
6376 }
6477
6578 function getCommentsForIssueCache ( { issueNumber } ) {
@@ -76,7 +89,14 @@ function init () {
7689 }
7790
7891 function addOrUpdateIssue ( { issue } ) {
92+ const number = issue . metadata . number
93+ const oldIssue = issues . get ( number )
94+ if ( oldIssue ) {
95+ removeFromShootIndex ( oldIssue )
96+ }
7997 updateIfNewer ( 'issue' , issues , issue )
98+ // Re-read from issues map — updateIfNewer may have kept the old item if it was newer
99+ addToShootIndex ( issues . get ( number ) )
80100 }
81101
82102 function addOrUpdateComment ( { issueNumber, comment } ) {
@@ -88,6 +108,11 @@ function init () {
88108 const issueNumber = issue . metadata . number
89109 logger . trace ( 'removing issue' , issueNumber , 'and comments' )
90110
111+ const cachedIssue = issues . get ( issueNumber )
112+ if ( cachedIssue ) {
113+ removeFromShootIndex ( cachedIssue )
114+ }
115+
91116 const comments = getCommentsForIssueCache ( { issueNumber } )
92117
93118 issues . delete ( issueNumber )
@@ -107,6 +132,36 @@ function init () {
107132 emitCommmentDeleted ( comment )
108133 }
109134
135+ function addToShootIndex ( issue ) {
136+ const { projectName, name, number } = issue ?. metadata ?? { }
137+ if ( ! projectName || ! name ) {
138+ return
139+ }
140+ const key = shootKey ( projectName , name )
141+ let map = issuesByShoot . get ( key )
142+ if ( ! map ) {
143+ map = new Map ( )
144+ issuesByShoot . set ( key , map )
145+ }
146+ map . set ( number , issue )
147+ }
148+
149+ function removeFromShootIndex ( issue ) {
150+ const { projectName, name, number } = issue ?. metadata ?? { }
151+ if ( ! projectName || ! name ) {
152+ return
153+ }
154+ const key = shootKey ( projectName , name )
155+ const map = issuesByShoot . get ( key )
156+ if ( ! map ) {
157+ return
158+ }
159+ map . delete ( number )
160+ if ( map . size === 0 ) {
161+ issuesByShoot . delete ( key )
162+ }
163+ }
164+
110165 function updateIfNewer ( kind , cachedMap , item ) {
111166 const identifier = kind === 'issue'
112167 ? item . metadata . number
@@ -142,6 +197,7 @@ function init () {
142197 getCommentsForIssue,
143198 getIssueNumbers,
144199 getIssueNumbersForNameAndProjectName,
200+ getIssuesForShoot,
145201 addOrUpdateIssues,
146202 addOrUpdateIssue,
147203 addOrUpdateComment,
0 commit comments