@@ -14,8 +14,8 @@ import { getMessageHandler, MessageHandler } from './message';
1414
1515export class PRContext {
1616 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 ,
1919 private _handler : MessageHandler | null = null ,
2020 ) {
2121 if ( ! _handler ) {
@@ -54,10 +54,14 @@ export class PRContext {
5454 public gotoChangesSinceReview = ( ) => this . postMessage ( { command : 'pr.gotoChangesSinceReview' } ) ;
5555
5656 public refresh = async ( ) => {
57- this . pr . busy = true ;
57+ if ( this . pr ) {
58+ this . pr . busy = true ;
59+ }
5860 this . updatePR ( this . pr ) ;
5961 await this . postMessage ( { command : 'pr.refresh' } ) ;
60- this . pr . busy = false ;
62+ if ( this . pr ) {
63+ this . pr . busy = false ;
64+ }
6165 this . updatePR ( this . pr ) ;
6266 } ;
6367
@@ -99,6 +103,9 @@ export class PRContext {
99103 public deleteComment = async ( args : { id : number ; pullRequestReviewId ?: number } ) => {
100104 await this . postMessage ( { command : 'pr.delete-comment' , args } ) ;
101105 const { pr } = this ;
106+ if ( ! pr ) {
107+ throw new Error ( 'Unexpectedly no PR when trying to delete comment' ) ;
108+ }
102109 const { id, pullRequestReviewId } = args ;
103110 if ( ! pullRequestReviewId ) {
104111 this . updatePR ( {
@@ -116,11 +123,11 @@ export class PRContext {
116123 console . error ( 'No comments to delete for review:' , pullRequestReviewId , review ) ;
117124 return ;
118125 }
119- this . pr . events . splice ( index , 1 , {
126+ pr . events . splice ( index , 1 , {
120127 ...review ,
121128 comments : review . comments . filter ( c => c . id !== id ) ,
122129 } ) ;
123- this . updatePR ( this . pr ) ;
130+ this . updatePR ( pr ) ;
124131 } ;
125132
126133 public editComment = ( args : { comment : IComment ; text : string } ) =>
@@ -152,9 +159,13 @@ export class PRContext {
152159 public submit = ( body : string ) => this . submitReviewCommand ( 'pr.submit' , body ) ;
153160
154161 public close = async ( body ?: string ) => {
162+ const { pr } = this ;
163+ if ( ! pr ) {
164+ throw new Error ( 'Unexpectedly no PR when trying to close' ) ;
165+ }
155166 try {
156167 const result : CloseResult = await this . postMessage ( { command : 'pr.close' , args : body } ) ;
157- let events : TimelineEvent [ ] = [ ...this . pr . events ] ;
168+ let events : TimelineEvent [ ] = [ ...pr . events ] ;
158169 if ( result . commentEvent ) {
159170 events . push ( result . commentEvent ) ;
160171 }
@@ -172,8 +183,12 @@ export class PRContext {
172183 } ;
173184
174185 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+ }
175190 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 ) ;
177192 this . updatePR ( { labels } ) ;
178193 } ;
179194
@@ -182,8 +197,11 @@ export class PRContext {
182197 } ;
183198
184199 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+ }
185204 const { events, reviewers, reviewedEvent } = reply ;
186- const state = this . pr ;
187205 state . busy = false ;
188206 if ( ! events ) {
189207 this . updatePR ( state ) ;
@@ -202,40 +220,55 @@ export class PRContext {
202220 }
203221
204222 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+ }
205227 const { reviewers } = await this . postMessage ( { command : 'pr.re-request-review' , args : reviewerId } ) ;
206- const state = this . pr ;
207228 state . reviewers = reviewers ;
208229 this . updatePR ( state ) ;
209230 }
210231
211232 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+ }
212237 const response : { autoMerge : boolean , autoMergeMethod ?: MergeMethod } = await this . postMessage ( { command : 'pr.update-automerge' , args : { autoMerge, autoMergeMethod } } ) ;
213- const state = this . pr ;
214238 state . autoMerge = response . autoMerge ;
215239 state . autoMergeMethod = response . autoMergeMethod ;
216240 this . updatePR ( state ) ;
217241 }
218242
219243 public updateBranch = async ( ) => {
244+ const { pr : state } = this ;
245+ if ( ! state ) {
246+ throw new Error ( 'Unexpectedly no PR when trying to update branch' ) ;
247+ }
220248 const result : Partial < PullRequest > = await this . postMessage ( { command : 'pr.update-branch' } ) ;
221- const state = this . pr ;
222249 state . events = result . events ?? state . events ;
223250 state . mergeable = result . mergeable ?? state . mergeable ;
224251 this . updatePR ( state ) ;
225252 }
226253
227254 public dequeue = async ( ) => {
255+ const { pr : state } = this ;
256+ if ( ! state ) {
257+ throw new Error ( 'Unexpectedly no PR when trying to dequeue' ) ;
258+ }
228259 const isDequeued = await this . postMessage ( { command : 'pr.dequeue' } ) ;
229- const state = this . pr ;
230260 if ( isDequeued ) {
231261 state . mergeQueueEntry = undefined ;
232262 }
233263 this . updatePR ( state ) ;
234264 }
235265
236266 public enqueue = async ( ) => {
267+ const { pr : state } = this ;
268+ if ( ! state ) {
269+ throw new Error ( 'Unexpectedly no PR when trying to enqueue' ) ;
270+ }
237271 const result = await this . postMessage ( { command : 'pr.enqueue' } ) ;
238- const state = this . pr ;
239272 if ( result . mergeQueueEntry ) {
240273 state . mergeQueueEntry = result . mergeQueueEntry ;
241274 }
@@ -262,7 +295,7 @@ export class PRContext {
262295
263296 public openCommitChanges = ( commitSha : string ) => this . postMessage ( { command : 'pr.openCommitChanges' , args : { commitSha } as OpenCommitChangesArgs } ) ;
264297
265- setPR = ( pr : PullRequest ) => {
298+ setPR = ( pr : PullRequest | undefined ) => {
266299 this . pr = pr ;
267300 setState ( this . pr ) ;
268301 if ( this . onchange ) {
@@ -271,9 +304,9 @@ export class PRContext {
271304 return this ;
272305 } ;
273306
274- updatePR = ( pr : Partial < PullRequest > ) => {
307+ updatePR = ( pr : Partial < PullRequest > | undefined ) => {
275308 updateState ( pr ) ;
276- this . pr = { ...this . pr , ...pr } ;
309+ this . pr = this . pr ? { ...this . pr , ...pr } : pr as PullRequest ;
277310 if ( this . onchange ) {
278311 this . onchange ( this . pr ) ;
279312 }
@@ -286,6 +319,9 @@ export class PRContext {
286319
287320 handleMessage = ( message : any ) => {
288321 switch ( message . command ) {
322+ case 'pr.clear' :
323+ this . setPR ( undefined ) ;
324+ return ;
289325 case 'pr.initialize' :
290326 return this . setPR ( message . pullrequest ) ;
291327 case 'update-state' :
0 commit comments