@@ -60,13 +60,14 @@ func resourceGithubRepository() *schema.Resource {
60
60
"security_and_analysis" : {
61
61
Type : schema .TypeList ,
62
62
Optional : true ,
63
+ Computed : true ,
63
64
MaxItems : 1 ,
64
65
Description : "Security and analysis settings for the repository. To use this parameter you must have admin permissions for the repository or be an owner or security manager for the organization that owns the repository." ,
65
66
Elem : & schema.Resource {
66
67
Schema : map [string ]* schema.Schema {
67
68
"advanced_security" : {
68
69
Type : schema .TypeList ,
69
- Required : true ,
70
+ Optional : true ,
70
71
MaxItems : 1 ,
71
72
Elem : & schema.Resource {
72
73
Schema : map [string ]* schema.Schema {
@@ -80,7 +81,7 @@ func resourceGithubRepository() *schema.Resource {
80
81
},
81
82
"secret_scanning" : {
82
83
Type : schema .TypeList ,
83
- Required : true ,
84
+ Optional : true ,
84
85
MaxItems : 1 ,
85
86
Elem : & schema.Resource {
86
87
Schema : map [string ]* schema.Schema {
@@ -94,7 +95,7 @@ func resourceGithubRepository() *schema.Resource {
94
95
},
95
96
"secret_scanning_push_protection" : {
96
97
Type : schema .TypeList ,
97
- Required : true ,
98
+ Optional : true ,
98
99
MaxItems : 1 ,
99
100
Elem : & schema.Resource {
100
101
Schema : map [string ]* schema.Schema {
@@ -352,6 +353,54 @@ func calculateVisibility(d *schema.ResourceData) string {
352
353
return "public"
353
354
}
354
355
356
+ func tryGetSecurityAndAnalysisSettingStatus (securityAndAnalysis map [string ]interface {}, setting string ) (bool , string ) {
357
+ value , ok := securityAndAnalysis [setting ]
358
+ if ! ok {
359
+ return false , ""
360
+ }
361
+
362
+ asList := value .([]interface {})
363
+ if len (asList ) == 0 || asList [0 ] == nil {
364
+ return false , ""
365
+ }
366
+
367
+ return true , asList [0 ].(map [string ]interface {})["status" ].(string )
368
+ }
369
+
370
+ func calculateSecurityAndAnalysis (d * schema.ResourceData ) * github.SecurityAndAnalysis {
371
+ value , ok := d .GetOk ("security_and_analysis" )
372
+ if ! ok {
373
+ return nil
374
+ }
375
+
376
+ asList := value .([]interface {})
377
+ if len (asList ) == 0 || asList [0 ] == nil {
378
+ return nil
379
+ }
380
+
381
+ lookup := asList [0 ].(map [string ]interface {})
382
+
383
+ var securityAndAnalysis github.SecurityAndAnalysis
384
+
385
+ if ok , status := tryGetSecurityAndAnalysisSettingStatus (lookup , "advanced_security" ); ok {
386
+ securityAndAnalysis .AdvancedSecurity = & github.AdvancedSecurity {
387
+ Status : github .String (status ),
388
+ }
389
+ }
390
+ if ok , status := tryGetSecurityAndAnalysisSettingStatus (lookup , "secret_scanning" ); ok {
391
+ securityAndAnalysis .SecretScanning = & github.SecretScanning {
392
+ Status : github .String (status ),
393
+ }
394
+ }
395
+ if ok , status := tryGetSecurityAndAnalysisSettingStatus (lookup , "secret_scanning_push_protection" ); ok {
396
+ securityAndAnalysis .SecretScanningPushProtection = & github.SecretScanningPushProtection {
397
+ Status : github .String (status ),
398
+ }
399
+ }
400
+
401
+ return & securityAndAnalysis
402
+ }
403
+
355
404
func resourceGithubRepositoryObject (d * schema.ResourceData ) * github.Repository {
356
405
return & github.Repository {
357
406
Name : github .String (d .Get ("name" ).(string )),
@@ -379,6 +428,7 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
379
428
Archived : github .Bool (d .Get ("archived" ).(bool )),
380
429
Topics : expandStringList (d .Get ("topics" ).(* schema.Set ).List ()),
381
430
AllowUpdateBranch : github .Bool (d .Get ("allow_update_branch" ).(bool )),
431
+ SecurityAndAnalysis : calculateSecurityAndAnalysis (d ),
382
432
}
383
433
}
384
434
@@ -477,14 +527,6 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er
477
527
}
478
528
}
479
529
480
- securityAndAnalysis := expandSecurityAndAnalysis (d .Get ("security_and_analysis" ).([]interface {}))
481
- if securityAndAnalysis != nil {
482
- _ , _ , err := client .Repositories .Edit (ctx , owner , repoName , securityAndAnalysis )
483
- if err != nil {
484
- return err
485
- }
486
- }
487
-
488
530
return resourceGithubRepositoryUpdate (d , meta )
489
531
}
490
532
@@ -634,29 +676,6 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
634
676
}
635
677
}
636
678
637
- if d .HasChange ("security_and_analysis" ) && ! d .IsNewResource () {
638
- opts := expandSecurityAndAnalysis (d .Get ("security_and_analysis" ).([]interface {}))
639
- if opts != nil {
640
- _ , _ , err := client .Repositories .Edit (ctx , owner , repoName , opts )
641
- if err != nil {
642
- return err
643
- }
644
- } else { // disable security and analysis
645
- _ , _ , err := client .Repositories .Edit (ctx , owner , repoName , & github.Repository {
646
- SecurityAndAnalysis : & github.SecurityAndAnalysis {
647
- AdvancedSecurity : & github.AdvancedSecurity {
648
- Status : github .String ("disabled" )},
649
- SecretScanning : & github.SecretScanning {
650
- Status : github .String ("disabled" )},
651
- SecretScanningPushProtection : & github.SecretScanningPushProtection {
652
- Status : github .String ("disabled" )}},
653
- })
654
- if err != nil {
655
- return err
656
- }
657
- }
658
- }
659
-
660
679
if d .HasChange ("topics" ) {
661
680
topics := repoReq .Topics
662
681
_ , _ , err = client .Repositories .ReplaceAllTopics (ctx , owner , * repo .Name , topics )
@@ -815,45 +834,22 @@ func flattenSecurityAndAnalysis(securityAndAnalysis *github.SecurityAndAnalysis)
815
834
return []interface {}{}
816
835
}
817
836
818
- advancedSecurityMap := make (map [string ]interface {})
819
- advancedSecurityMap ["status" ] = securityAndAnalysis .GetAdvancedSecurity ().GetStatus ()
820
-
821
- secretScanningMap := make (map [string ]interface {})
822
- secretScanningMap ["status" ] = securityAndAnalysis .GetSecretScanning ().GetStatus ()
823
-
824
- secretScanningPushProtectionMap := make (map [string ]interface {})
825
- secretScanningPushProtectionMap ["status" ] = securityAndAnalysis .GetSecretScanningPushProtection ().GetStatus ()
826
-
827
837
securityAndAnalysisMap := make (map [string ]interface {})
828
- securityAndAnalysisMap ["advanced_security" ] = []interface {}{advancedSecurityMap }
829
- securityAndAnalysisMap ["secret_scanning" ] = []interface {}{secretScanningMap }
830
- securityAndAnalysisMap ["secret_scanning_push_protection" ] = []interface {}{secretScanningPushProtectionMap }
831
-
832
- return []interface {}{securityAndAnalysisMap }
833
- }
834
-
835
- func expandSecurityAndAnalysis (input []interface {}) * github.Repository {
836
- if len (input ) == 0 || input [0 ] == nil {
837
- return nil
838
- }
839
-
840
- securityAndAnalysis := input [0 ].(map [string ]interface {})
841
- update := & github.SecurityAndAnalysis {}
842
838
843
- advancedSecurity := securityAndAnalysis ["advanced_security" ].([]interface {})[0 ].(map [string ]interface {})
844
- update .AdvancedSecurity = & github.AdvancedSecurity {
845
- Status : github .String (advancedSecurity ["status" ].(string )),
839
+ advancedSecurity := securityAndAnalysis .GetAdvancedSecurity ()
840
+ if advancedSecurity != nil {
841
+ securityAndAnalysisMap ["advanced_security" ] = []interface {}{map [string ]interface {}{
842
+ "status" : advancedSecurity .GetStatus (),
843
+ }}
846
844
}
847
845
848
- secretScanning := securityAndAnalysis ["secret_scanning" ].([]interface {})[0 ].(map [string ]interface {})
849
- update .SecretScanning = & github.SecretScanning {
850
- Status : github .String (secretScanning ["status" ].(string )),
851
- }
846
+ securityAndAnalysisMap ["secret_scanning" ] = []interface {}{map [string ]interface {}{
847
+ "status" : securityAndAnalysis .GetSecretScanning ().GetStatus (),
848
+ }}
852
849
853
- secretScanningPushProtection := securityAndAnalysis ["secret_scanning_push_protection" ].([]interface {})[0 ].(map [string ]interface {})
854
- update .SecretScanningPushProtection = & github.SecretScanningPushProtection {
855
- Status : github .String (secretScanningPushProtection ["status" ].(string )),
856
- }
850
+ securityAndAnalysisMap ["secret_scanning_push_protection" ] = []interface {}{map [string ]interface {}{
851
+ "status" : securityAndAnalysis .GetSecretScanningPushProtection ().GetStatus (),
852
+ }}
857
853
858
- return & github. Repository { SecurityAndAnalysis : update }
854
+ return [] interface {}{ securityAndAnalysisMap }
859
855
}
0 commit comments