@@ -30,7 +30,22 @@ export default class GitHub {
30
30
this . commonParams = { owner, repo } ;
31
31
32
32
this . issuesCache = new Map ( ) ;
33
- this . cacheLoadedPromise = null ;
33
+ this . _issuesPromise = null ;
34
+ }
35
+
36
+ get issues ( ) {
37
+ if ( ! this . _issuesPromise ) {
38
+ logger . info ( 'Loading issues from GitHub…' ) ;
39
+ this . _issuesPromise = this . loadAllIssues ( ) ;
40
+ }
41
+
42
+ return this . _issuesPromise ;
43
+ }
44
+
45
+ clearCache ( ) {
46
+ this . issuesCache . clear ( ) ;
47
+ this . _issuesPromise = null ;
48
+ logger . info ( 'Issues cache cleared' ) ;
34
49
}
35
50
36
51
async initialize ( ) {
@@ -74,29 +89,13 @@ export default class GitHub {
74
89
}
75
90
} ) ;
76
91
77
- logger . info ( `Cached ${ onlyIssues . length } issues from the repository` ) ;
78
- } catch ( error ) {
79
- logger . error ( `Failed to load issues: ${ error . message } ` ) ;
80
- }
81
- }
92
+ logger . info ( `Cached ${ onlyIssues . length } issues from the GitHub repository` ) ;
82
93
83
- async refreshIssuesCache ( ) {
84
- try {
85
- logger . info ( 'Refreshing issues cache from GitHub…' ) ;
86
- this . issuesCache . clear ( ) ;
87
- this . cacheLoadedPromise = this . loadAllIssues ( ) ;
88
- await this . cacheLoadedPromise ;
89
- logger . info ( 'Issues cache refreshed successfully' ) ;
94
+ return this . issuesCache ;
90
95
} catch ( error ) {
91
- logger . error ( `Failed to refresh issues cache: ${ error . message } ` ) ;
92
- }
93
- }
94
-
95
- async ensureCacheLoaded ( ) {
96
- if ( ! this . cacheLoadedPromise ) {
97
- this . cacheLoadedPromise = this . loadAllIssues ( ) ;
96
+ logger . error ( `Failed to load issues: ${ error . message } ` ) ;
97
+ throw error ;
98
98
}
99
- await this . cacheLoadedPromise ;
100
99
}
101
100
102
101
async getRepositoryLabels ( ) {
@@ -114,9 +113,11 @@ export default class GitHub {
114
113
} ) ;
115
114
}
116
115
117
- async createIssue ( { title, description : body , labels } ) {
118
- await this . ensureCacheLoaded ( ) ;
116
+ async getIssue ( title ) {
117
+ return ( await this . issues ) . get ( title ) ;
118
+ }
119
119
120
+ async createIssue ( { title, description : body , labels } ) {
120
121
const { data : issue } = await this . octokit . request ( 'POST /repos/{owner}/{repo}/issues' , {
121
122
...this . commonParams ,
122
123
title,
@@ -130,8 +131,6 @@ export default class GitHub {
130
131
}
131
132
132
133
async updateIssue ( issue , { state, labels } ) {
133
- await this . ensureCacheLoaded ( ) ;
134
-
135
134
const { data : updatedIssue } = await this . octokit . request ( 'PATCH /repos/{owner}/{repo}/issues/{issue_number}' , {
136
135
...this . commonParams ,
137
136
issue_number : issue . number ,
@@ -144,13 +143,7 @@ export default class GitHub {
144
143
return updatedIssue ;
145
144
}
146
145
147
- getIssue ( title ) {
148
- return this . issuesCache . get ( title ) || null ;
149
- }
150
-
151
146
async addCommentToIssue ( { issue, comment : body } ) {
152
- await this . ensureCacheLoaded ( ) ;
153
-
154
147
const { data : comment } = await this . octokit . request ( 'POST /repos/{owner}/{repo}/issues/{issue_number}/comments' , {
155
148
...this . commonParams ,
156
149
issue_number : issue . number ,
@@ -162,9 +155,7 @@ export default class GitHub {
162
155
163
156
async closeIssueWithCommentIfExists ( { title, comment } ) {
164
157
try {
165
- await this . ensureCacheLoaded ( ) ;
166
-
167
- const issue = this . getIssue ( title ) ;
158
+ const issue = await this . getIssue ( title ) ;
168
159
169
160
if ( ! issue || issue . state == GitHub . ISSUE_STATE_CLOSED ) {
170
161
return ;
@@ -183,9 +174,7 @@ export default class GitHub {
183
174
184
175
async createOrUpdateIssue ( { title, description, label } ) {
185
176
try {
186
- await this . ensureCacheLoaded ( ) ;
187
-
188
- const issue = this . getIssue ( title ) ;
177
+ const issue = await this . getIssue ( title ) ;
189
178
190
179
if ( ! issue ) {
191
180
const createdIssue = await this . createIssue ( { title, description, labels : [ label ] } ) ;
@@ -194,7 +183,6 @@ export default class GitHub {
194
183
}
195
184
196
185
const managedLabelsNames = this . MANAGED_LABELS . map ( label => label . name ) ;
197
-
198
186
const labelsNotManagedToKeep = issue . labels . map ( label => label . name ) . filter ( label => ! managedLabelsNames . includes ( label ) ) ;
199
187
const [ managedLabel ] = issue . labels . filter ( label => managedLabelsNames . includes ( label . name ) ) ; // It is assumed that only one specific reason for failure is possible at a time, making managed labels mutually exclusive
200
188
0 commit comments