@@ -403,6 +403,133 @@ describe('Environments Plugin test suite', () => {
403403 } )
404404 } )
405405
406+ // update variable
407+ describe ( 'When there is an existing variable and config calls for a different value' , ( ) => {
408+ it ( 'detect divergence and update the variable' , async ( ) => {
409+ // arrange
410+ environmentName = 'variables_environment'
411+ // represent config with a reviewers being a user and a team
412+ const plugin = new Environments ( undefined , github , { owner : org , repo } , [
413+ {
414+ name : environmentName ,
415+ variables : [
416+ {
417+ name : 'TEST' ,
418+ value : 'test-updated'
419+ }
420+ ]
421+ }
422+ ] , log , errors )
423+
424+ // model an existing environment with a variable that has a different value
425+ when ( github . request )
426+ . calledWith ( 'GET /repos/:org/:repo/environments' , { org, repo } )
427+ . mockResolvedValue ( {
428+ data : {
429+ environments : [
430+ fillEnvironment ( {
431+ name : environmentName
432+ } )
433+ ]
434+ }
435+ } )
436+
437+ // model an existing environment with a variable that has a different value
438+ when ( github . request )
439+ . calledWith ( 'GET /repos/:org/:repo/environments/:environment_name/variables' , { org, repo, environment_name : environmentName } )
440+ . mockResolvedValue ( {
441+ data : {
442+ variables : [
443+ {
444+ name : 'TEST' ,
445+ value : 'test'
446+ }
447+ ]
448+ }
449+ } )
450+
451+ // act - run sync() in environments.js
452+ await plugin . sync ( ) . then ( ( ) => {
453+ // assert - update the variables
454+ expect ( github . request ) . toHaveBeenCalledWith ( 'GET /repos/:org/:repo/environments' , { org, repo } )
455+ expect ( github . request ) . toHaveBeenCalledWith ( 'GET /repos/:org/:repo/environments/:environment_name/variables' , { org, repo, environment_name : environmentName } )
456+ expect ( github . request ) . toHaveBeenCalledWith ( 'GET /repos/:org/:repo/environments/:environment_name/deployment_protection_rules' , { org, repo, environment_name : environmentName } )
457+ expect ( github . request ) . toHaveBeenCalledWith ( 'PATCH /repos/:org/:repo/environments/:environment_name/variables/:variable_name' , expect . objectContaining ( {
458+ org,
459+ repo,
460+ environment_name : environmentName ,
461+ variable_name : 'test' ,
462+ value : 'test-updated'
463+ } ) )
464+ } )
465+ } )
466+ } )
467+
468+ // delete variable
469+ describe ( 'When there are multiple variables and config calls for one to be deleted' , ( ) => {
470+ it ( 'detect divergence and delete the variable' , async ( ) => {
471+ // arrange
472+ environmentName = 'variables_environment'
473+ // represent config with a reviewers being a user and a team
474+ const plugin = new Environments ( undefined , github , { owner : org , repo } , [
475+ {
476+ name : environmentName ,
477+ variables : [
478+ {
479+ name : 'TEST' ,
480+ value : 'test'
481+ }
482+ ]
483+ }
484+ ] , log , errors )
485+
486+ // model an existing environment with a variable that has a different value
487+ when ( github . request )
488+ . calledWith ( 'GET /repos/:org/:repo/environments' , { org, repo } )
489+ . mockResolvedValue ( {
490+ data : {
491+ environments : [
492+ fillEnvironment ( {
493+ name : environmentName
494+ } )
495+ ]
496+ }
497+ } )
498+
499+ // model an existing environment with a variable that has a different value
500+ when ( github . request )
501+ . calledWith ( 'GET /repos/:org/:repo/environments/:environment_name/variables' , { org, repo, environment_name : environmentName } )
502+ . mockResolvedValue ( {
503+ data : {
504+ variables : [
505+ {
506+ name : 'TEST' ,
507+ value : 'test'
508+ } ,
509+ {
510+ name : 'TEST2' ,
511+ value : 'test2'
512+ }
513+ ]
514+ }
515+ } )
516+
517+ // act - run sync() in environments.js
518+ await plugin . sync ( ) . then ( ( ) => {
519+ // assert - update the variables
520+ expect ( github . request ) . toHaveBeenCalledWith ( 'GET /repos/:org/:repo/environments' , { org, repo } )
521+ expect ( github . request ) . toHaveBeenCalledWith ( 'GET /repos/:org/:repo/environments/:environment_name/variables' , { org, repo, environment_name : environmentName } )
522+ expect ( github . request ) . toHaveBeenCalledWith ( 'GET /repos/:org/:repo/environments/:environment_name/deployment_protection_rules' , { org, repo, environment_name : environmentName } )
523+ expect ( github . request ) . toHaveBeenCalledWith ( 'DELETE /repos/:org/:repo/environments/:environment_name/variables/:variable_name' , expect . objectContaining ( {
524+ org,
525+ repo,
526+ environment_name : environmentName ,
527+ variable_name : 'test2'
528+ } ) )
529+ } )
530+ } )
531+ } )
532+
406533 // add deployment protection rules
407534 describe ( 'When there are no existing deployment protection rules, but config calls for one' , ( ) => {
408535 it ( 'detect divergence and add the deployment protection rule' , async ( ) => {
0 commit comments