@@ -14,8 +14,8 @@ import { getMessageHandler, MessageHandler } from './message';
14
14
15
15
export class PRContext {
16
16
constructor (
17
- public pr : PullRequest = getState ( ) ,
18
- public onchange : ( ( ctx : PullRequest ) => void ) | null = null ,
17
+ public pr : PullRequest | undefined = getState ( ) ,
18
+ public onchange : ( ( ctx : PullRequest | undefined ) => void ) | null = null ,
19
19
private _handler : MessageHandler | null = null ,
20
20
) {
21
21
if ( ! _handler ) {
@@ -54,10 +54,14 @@ export class PRContext {
54
54
public gotoChangesSinceReview = ( ) => this . postMessage ( { command : 'pr.gotoChangesSinceReview' } ) ;
55
55
56
56
public refresh = async ( ) => {
57
- this . pr . busy = true ;
57
+ if ( this . pr ) {
58
+ this . pr . busy = true ;
59
+ }
58
60
this . updatePR ( this . pr ) ;
59
61
await this . postMessage ( { command : 'pr.refresh' } ) ;
60
- this . pr . busy = false ;
62
+ if ( this . pr ) {
63
+ this . pr . busy = false ;
64
+ }
61
65
this . updatePR ( this . pr ) ;
62
66
} ;
63
67
@@ -99,6 +103,9 @@ export class PRContext {
99
103
public deleteComment = async ( args : { id : number ; pullRequestReviewId ?: number } ) => {
100
104
await this . postMessage ( { command : 'pr.delete-comment' , args } ) ;
101
105
const { pr } = this ;
106
+ if ( ! pr ) {
107
+ throw new Error ( 'Unexpectedly no PR when trying to delete comment' ) ;
108
+ }
102
109
const { id, pullRequestReviewId } = args ;
103
110
if ( ! pullRequestReviewId ) {
104
111
this . updatePR ( {
@@ -116,11 +123,11 @@ export class PRContext {
116
123
console . error ( 'No comments to delete for review:' , pullRequestReviewId , review ) ;
117
124
return ;
118
125
}
119
- this . pr . events . splice ( index , 1 , {
126
+ pr . events . splice ( index , 1 , {
120
127
...review ,
121
128
comments : review . comments . filter ( c => c . id !== id ) ,
122
129
} ) ;
123
- this . updatePR ( this . pr ) ;
130
+ this . updatePR ( pr ) ;
124
131
} ;
125
132
126
133
public editComment = ( args : { comment : IComment ; text : string } ) =>
@@ -152,9 +159,13 @@ export class PRContext {
152
159
public submit = ( body : string ) => this . submitReviewCommand ( 'pr.submit' , body ) ;
153
160
154
161
public close = async ( body ?: string ) => {
162
+ const { pr } = this ;
163
+ if ( ! pr ) {
164
+ throw new Error ( 'Unexpectedly no PR when trying to close' ) ;
165
+ }
155
166
try {
156
167
const result : CloseResult = await this . postMessage ( { command : 'pr.close' , args : body } ) ;
157
- let events : TimelineEvent [ ] = [ ...this . pr . events ] ;
168
+ let events : TimelineEvent [ ] = [ ...pr . events ] ;
158
169
if ( result . commentEvent ) {
159
170
events . push ( result . commentEvent ) ;
160
171
}
@@ -172,8 +183,12 @@ export class PRContext {
172
183
} ;
173
184
174
185
public removeLabel = async ( label : string ) => {
186
+ const { pr } = this ;
187
+ if ( ! pr ) {
188
+ throw new Error ( 'Unexpectedly no PR when trying to remove label' ) ;
189
+ }
175
190
await this . postMessage ( { command : 'pr.remove-label' , args : label } ) ;
176
- const labels = this . pr . labels . filter ( r => r . name !== label ) ;
191
+ const labels = pr . labels . filter ( r => r . name !== label ) ;
177
192
this . updatePR ( { labels } ) ;
178
193
} ;
179
194
@@ -182,8 +197,11 @@ export class PRContext {
182
197
} ;
183
198
184
199
private appendReview ( reply : SubmitReviewReply ) {
200
+ const { pr : state } = this ;
201
+ if ( ! state ) {
202
+ throw new Error ( 'Unexpectedly no PR when trying to append review' ) ;
203
+ }
185
204
const { events, reviewers, reviewedEvent } = reply ;
186
- const state = this . pr ;
187
205
state . busy = false ;
188
206
if ( ! events ) {
189
207
this . updatePR ( state ) ;
@@ -202,40 +220,55 @@ export class PRContext {
202
220
}
203
221
204
222
public reRequestReview = async ( reviewerId : string ) => {
223
+ const { pr : state } = this ;
224
+ if ( ! state ) {
225
+ throw new Error ( 'Unexpectedly no PR when trying to re-request review' ) ;
226
+ }
205
227
const { reviewers } = await this . postMessage ( { command : 'pr.re-request-review' , args : reviewerId } ) ;
206
- const state = this . pr ;
207
228
state . reviewers = reviewers ;
208
229
this . updatePR ( state ) ;
209
230
}
210
231
211
232
public async updateAutoMerge ( { autoMerge, autoMergeMethod } : { autoMerge ?: boolean , autoMergeMethod ?: MergeMethod } ) {
233
+ const { pr : state } = this ;
234
+ if ( ! state ) {
235
+ throw new Error ( 'Unexpectedly no PR when trying to update auto merge' ) ;
236
+ }
212
237
const response : { autoMerge : boolean , autoMergeMethod ?: MergeMethod } = await this . postMessage ( { command : 'pr.update-automerge' , args : { autoMerge, autoMergeMethod } } ) ;
213
- const state = this . pr ;
214
238
state . autoMerge = response . autoMerge ;
215
239
state . autoMergeMethod = response . autoMergeMethod ;
216
240
this . updatePR ( state ) ;
217
241
}
218
242
219
243
public updateBranch = async ( ) => {
244
+ const { pr : state } = this ;
245
+ if ( ! state ) {
246
+ throw new Error ( 'Unexpectedly no PR when trying to update branch' ) ;
247
+ }
220
248
const result : Partial < PullRequest > = await this . postMessage ( { command : 'pr.update-branch' } ) ;
221
- const state = this . pr ;
222
249
state . events = result . events ?? state . events ;
223
250
state . mergeable = result . mergeable ?? state . mergeable ;
224
251
this . updatePR ( state ) ;
225
252
}
226
253
227
254
public dequeue = async ( ) => {
255
+ const { pr : state } = this ;
256
+ if ( ! state ) {
257
+ throw new Error ( 'Unexpectedly no PR when trying to dequeue' ) ;
258
+ }
228
259
const isDequeued = await this . postMessage ( { command : 'pr.dequeue' } ) ;
229
- const state = this . pr ;
230
260
if ( isDequeued ) {
231
261
state . mergeQueueEntry = undefined ;
232
262
}
233
263
this . updatePR ( state ) ;
234
264
}
235
265
236
266
public enqueue = async ( ) => {
267
+ const { pr : state } = this ;
268
+ if ( ! state ) {
269
+ throw new Error ( 'Unexpectedly no PR when trying to enqueue' ) ;
270
+ }
237
271
const result = await this . postMessage ( { command : 'pr.enqueue' } ) ;
238
- const state = this . pr ;
239
272
if ( result . mergeQueueEntry ) {
240
273
state . mergeQueueEntry = result . mergeQueueEntry ;
241
274
}
@@ -262,7 +295,7 @@ export class PRContext {
262
295
263
296
public openCommitChanges = ( commitSha : string ) => this . postMessage ( { command : 'pr.openCommitChanges' , args : { commitSha } as OpenCommitChangesArgs } ) ;
264
297
265
- setPR = ( pr : PullRequest ) => {
298
+ setPR = ( pr : PullRequest | undefined ) => {
266
299
this . pr = pr ;
267
300
setState ( this . pr ) ;
268
301
if ( this . onchange ) {
@@ -271,9 +304,9 @@ export class PRContext {
271
304
return this ;
272
305
} ;
273
306
274
- updatePR = ( pr : Partial < PullRequest > ) => {
307
+ updatePR = ( pr : Partial < PullRequest > | undefined ) => {
275
308
updateState ( pr ) ;
276
- this . pr = { ...this . pr , ...pr } ;
309
+ this . pr = this . pr ? { ...this . pr , ...pr } : pr as PullRequest ;
277
310
if ( this . onchange ) {
278
311
this . onchange ( this . pr ) ;
279
312
}
@@ -286,6 +319,9 @@ export class PRContext {
286
319
287
320
handleMessage = ( message : any ) => {
288
321
switch ( message . command ) {
322
+ case 'pr.clear' :
323
+ this . setPR ( undefined ) ;
324
+ return ;
289
325
case 'pr.initialize' :
290
326
return this . setPR ( message . pullrequest ) ;
291
327
case 'update-state' :
0 commit comments