@@ -181,20 +181,19 @@ describe('Branches', () => {
181181 )
182182
183183 return plugin . sync ( ) . then ( ( ) => {
184- expect ( github . repos . updateBranchProtection ) . toHaveBeenCalledWith ( {
184+ expect ( github . repos . updateBranchProtection ) . toHaveBeenCalledWith ( expect . objectContaining ( {
185185 owner : 'bkeepers' ,
186186 repo : 'test' ,
187187 branch : 'main' ,
188188 required_status_checks : {
189189 strict : true ,
190190 contexts : [ ]
191191 } ,
192- // When override processing clears {{EXTERNALLY_DEFINED}} contexts,
193- // enforce_admins defaults to null since config doesn't specify it
194- enforce_admins : null ,
192+ // Existing enforce_admins should be preserved from GitHub
193+ enforce_admins : false ,
195194 restrictions : null ,
196195 headers : { accept : 'application/vnd.github.hellcat-preview+json,application/vnd.github.luke-cage-preview+json,application/vnd.github.zzzax-preview+json' }
197- } )
196+ } ) )
198197 } )
199198 } )
200199 } )
@@ -249,17 +248,44 @@ describe('Branches', () => {
249248 )
250249 } )
251250 } )
251+
252+ it ( 'normalizes restrictions and defaults missing arrays when preserving from GitHub' , ( ) => {
253+ github . repos . getBranchProtection = jest . fn ( ) . mockResolvedValue ( {
254+ data : {
255+ enforce_admins : { enabled : true } ,
256+ restrictions : {
257+ url : 'https://api.github.com/...' ,
258+ users : [ { login : 'user1' } ]
259+ }
260+ }
261+ } )
262+
263+ const plugin = configure ( [ {
264+ name : 'main' ,
265+ protection : {
266+ enforce_admins : false
267+ }
268+ } ] )
269+
270+ return plugin . sync ( ) . then ( ( ) => {
271+ const payload = github . repos . updateBranchProtection . mock . calls [ 0 ] [ 0 ]
272+ expect ( payload . restrictions ) . toEqual ( {
273+ users : [ 'user1' ] ,
274+ teams : [ ] ,
275+ apps : [ ]
276+ } )
277+ expect ( payload . restrictions . url ) . toBeUndefined ( )
278+ } )
279+ } )
252280 } )
253281
254282 describe ( 'when {{EXTERNALLY_DEFINED}} is present in "required_status_checks" and status checks exist in GitHub' , ( ) => {
255283 it ( 'it retains the status checks from GitHub' , ( ) => {
256284 github . repos . getBranchProtection = jest . fn ( ) . mockResolvedValue ( {
257285 data : {
258286 enforce_admins : { enabled : false } ,
259- protection : {
260- required_status_checks : {
261- contexts : [ 'check-1' , 'check-2' ]
262- }
287+ required_status_checks : {
288+ contexts : [ 'check-1' , 'check-2' ]
263289 }
264290 }
265291 } )
@@ -276,18 +302,18 @@ describe('Branches', () => {
276302 )
277303
278304 return plugin . sync ( ) . then ( ( ) => {
279- expect ( github . repos . updateBranchProtection ) . toHaveBeenCalledWith ( {
305+ expect ( github . repos . updateBranchProtection ) . toHaveBeenCalledWith ( expect . objectContaining ( {
280306 owner : 'bkeepers' ,
281307 repo : 'test' ,
282308 branch : 'main' ,
283309 required_status_checks : {
284310 strict : true ,
285311 contexts : [ 'check-1' , 'check-2' ]
286312 } ,
287- enforce_admins : null ,
313+ enforce_admins : false ,
288314 restrictions : null ,
289315 headers : { accept : 'application/vnd.github.hellcat-preview+json,application/vnd.github.luke-cage-preview+json,application/vnd.github.zzzax-preview+json' }
290- } )
316+ } ) )
291317 } )
292318 } )
293319 } )
0 commit comments