@@ -20,12 +20,19 @@ function freshRequire() {
2020function createGithubMock ( ) {
2121 const removedLabels = [ ] ;
2222 const graphqlCalls = [ ] ;
23+ let graphqlError ;
2324
2425 return {
2526 removedLabels,
2627 graphqlCalls,
28+ failGraphql ( error ) {
29+ graphqlError = error ;
30+ } ,
2731 graphql : async ( query , variables ) => {
2832 graphqlCalls . push ( { query, variables } ) ;
33+ if ( graphqlError ) {
34+ throw graphqlError ;
35+ }
2936 return {
3037 convertPullRequestToDraft : {
3138 pullRequest : {
@@ -69,22 +76,22 @@ function createContext(overrides = {}) {
6976describe ( 'revision-guard index' , ( ) => {
7077 beforeEach ( ( ) => {
7178 delete process . env . REVISION_GUARD_MANAGED_LABELS ;
79+ delete process . env . REVIEWBOT_TOKEN ;
7280 } ) ;
7381
7482 afterEach ( ( ) => {
7583 delete process . env . REVISION_GUARD_MANAGED_LABELS ;
84+ delete process . env . REVIEWBOT_TOKEN ;
7685 } ) ;
7786
78- it ( 'converts a ready PR to draft and removes only managed labels ' , async ( ) => {
87+ it ( 'removes only managed labels without draft conversion when no review bot token is configured ' , async ( ) => {
7988 const handler = freshRequire ( ) ;
8089 const github = createGithubMock ( ) ;
8190 const context = createContext ( ) ;
8291
8392 await handler ( { github, context, core : { info ( ) { } } } ) ;
8493
85- assert . equal ( github . graphqlCalls . length , 1 ) ;
86- assert . match ( github . graphqlCalls [ 0 ] . query , / c o n v e r t P u l l R e q u e s t T o D r a f t / ) ;
87- assert . deepEqual ( github . graphqlCalls [ 0 ] . variables , { pullRequestId : 'PR_node_42' } ) ;
94+ assert . equal ( github . graphqlCalls . length , 0 ) ;
8895 assert . deepEqual ( github . removedLabels , [
8996 'queue:committers' ,
9097 'status: ready-to-merge' ,
@@ -110,7 +117,7 @@ describe('revision-guard index', () => {
110117 assert . equal ( github . removedLabels . length , 0 ) ;
111118 } ) ;
112119
113- it ( 'skips already-draft PRs' , async ( ) => {
120+ it ( 'removes managed labels from already-draft PRs without converting again ' , async ( ) => {
114121 const handler = freshRequire ( ) ;
115122 const github = createGithubMock ( ) ;
116123 const context = createContext ( {
@@ -126,7 +133,40 @@ describe('revision-guard index', () => {
126133 await handler ( { github, context, core : { info ( ) { } } } ) ;
127134
128135 assert . equal ( github . graphqlCalls . length , 0 ) ;
129- assert . equal ( github . removedLabels . length , 0 ) ;
136+ assert . deepEqual ( github . removedLabels , [ 'queue:committers' ] ) ;
137+ } ) ;
138+
139+ it ( 'converts to draft when a review bot token is configured' , async ( ) => {
140+ process . env . REVIEWBOT_TOKEN = 'token' ;
141+ const handler = freshRequire ( ) ;
142+ const github = createGithubMock ( ) ;
143+ const context = createContext ( ) ;
144+
145+ await handler ( { github, context, core : { info ( ) { } } } ) ;
146+
147+ assert . equal ( github . graphqlCalls . length , 1 ) ;
148+ assert . match ( github . graphqlCalls [ 0 ] . query , / c o n v e r t P u l l R e q u e s t T o D r a f t / ) ;
149+ assert . deepEqual ( github . graphqlCalls [ 0 ] . variables , { pullRequestId : 'PR_node_42' } ) ;
150+ assert . deepEqual ( github . removedLabels , [
151+ 'queue:committers' ,
152+ 'status: ready-to-merge' ,
153+ ] ) ;
154+ } ) ;
155+
156+ it ( 'still removes managed labels when draft conversion fails' , async ( ) => {
157+ process . env . REVIEWBOT_TOKEN = 'token' ;
158+ const handler = freshRequire ( ) ;
159+ const github = createGithubMock ( ) ;
160+ github . failGraphql ( new Error ( 'Resource not accessible by integration' ) ) ;
161+ const context = createContext ( ) ;
162+
163+ await handler ( { github, context, core : { error ( ) { } , info ( ) { } } } ) ;
164+
165+ assert . equal ( github . graphqlCalls . length , 1 ) ;
166+ assert . deepEqual ( github . removedLabels , [
167+ 'queue:committers' ,
168+ 'status: ready-to-merge' ,
169+ ] ) ;
130170 } ) ;
131171
132172 it ( 'uses configurable managed labels and still removes defaults' , async ( ) => {
@@ -149,9 +189,7 @@ describe('revision-guard index', () => {
149189
150190 await handler ( { github, context, core : { info ( ) { } } } ) ;
151191
152- // Draft conversion must also fire for configurable-label scenarios.
153- assert . equal ( github . graphqlCalls . length , 1 ) ;
154- assert . deepEqual ( github . graphqlCalls [ 0 ] . variables , { pullRequestId : 'PR_node_45' } ) ;
192+ assert . equal ( github . graphqlCalls . length , 0 ) ;
155193 // Custom labels AND the matching default (queue:committers) must both be removed.
156194 assert . deepEqual ( github . removedLabels , [ 'queue:committers' , 'custom: one' , 'custom: two' ] ) ;
157195 } ) ;
0 commit comments