Skip to content

Commit af34d15

Browse files
committed
fix: Correct private forking implementation
Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com>
1 parent d5f775e commit af34d15

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

github/resource_github_repository.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func resourceGithubRepository() *schema.Resource {
249249
"allow_forking": {
250250
Type: schema.TypeBool,
251251
Optional: true,
252-
Default: false,
252+
Computed: true,
253253
Description: "Set to 'true' to allow private forking on the repository; this is only relevant if the repository is owned by an organization and is private or internal.",
254254
},
255255
"squash_merge_commit_title": {
@@ -580,11 +580,13 @@ func calculateSecurityAndAnalysis(d *schema.ResourceData) *github.SecurityAndAna
580580
}
581581

582582
func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
583+
visibility := calculateVisibility(d)
584+
583585
repository := &github.Repository{
584586
Name: github.Ptr(d.Get("name").(string)),
585587
Description: github.Ptr(d.Get("description").(string)),
586588
Homepage: github.Ptr(d.Get("homepage_url").(string)),
587-
Visibility: github.Ptr(calculateVisibility(d)),
589+
Visibility: github.Ptr(visibility),
588590
HasDownloads: github.Ptr(d.Get("has_downloads").(bool)),
589591
HasIssues: github.Ptr(d.Get("has_issues").(bool)),
590592
HasDiscussions: github.Ptr(d.Get("has_discussions").(bool)),
@@ -595,7 +597,6 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
595597
AllowSquashMerge: github.Ptr(d.Get("allow_squash_merge").(bool)),
596598
AllowRebaseMerge: github.Ptr(d.Get("allow_rebase_merge").(bool)),
597599
AllowAutoMerge: github.Ptr(d.Get("allow_auto_merge").(bool)),
598-
AllowForking: github.Ptr(d.Get("allow_forking").(bool)),
599600
DeleteBranchOnMerge: github.Ptr(d.Get("delete_branch_on_merge").(bool)),
600601
WebCommitSignoffRequired: github.Ptr(d.Get("web_commit_signoff_required").(bool)),
601602
AutoInit: github.Ptr(d.Get("auto_init").(bool)),
@@ -625,6 +626,12 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
625626
}
626627
}
627628

629+
// only configure allow forking if repository is not public
630+
allowForking, ok := d.Get("allow_forking").(bool)
631+
if ok && visibility != "public" {
632+
repository.AllowForking = github.Ptr(allowForking)
633+
}
634+
628635
return repository
629636
}
630637

@@ -637,27 +644,10 @@ func resourceGithubRepositoryCreate(ctx context.Context, d *schema.ResourceData,
637644

638645
repoReq := resourceGithubRepositoryObject(d)
639646
owner := meta.(*Owner).name
640-
641647
repoName := repoReq.GetName()
642648

643-
// determine if repository should be private. assume public to start
644-
isPrivate := false
645-
646-
// prefer visibility to private flag since private flag is deprecated
647-
privateKeyword, ok := d.Get("private").(bool)
648-
if ok {
649-
isPrivate = privateKeyword
650-
}
651-
652-
visibility, ok := d.Get("visibility").(string)
653-
if ok {
654-
if visibility == "private" || visibility == "internal" {
655-
isPrivate = true
656-
}
657-
}
658-
649+
isPrivate := repoReq.GetVisibility() == "private"
659650
repoReq.Private = github.Ptr(isPrivate)
660-
661651
if template, ok := d.GetOk("template"); ok {
662652
templateConfigBlocks := template.([]any)
663653

@@ -930,6 +920,12 @@ func resourceGithubRepositoryUpdate(ctx context.Context, d *schema.ResourceData,
930920
// handle visibility updates separately from other fields
931921
repoReq.Visibility = nil
932922

923+
// This change needs to be made with the correct visibility
924+
allowForking := repoReq.AllowForking
925+
if d.HasChanges("visibility", "private") {
926+
repoReq.AllowForking = nil
927+
}
928+
933929
if !d.HasChange("security_and_analysis") {
934930
repoReq.SecurityAndAnalysis = nil
935931
log.Print("[DEBUG] No security_and_analysis update required. Removing this field from the payload.")
@@ -1016,6 +1012,7 @@ func resourceGithubRepositoryUpdate(ctx context.Context, d *schema.ResourceData,
10161012
}
10171013

10181014
if d.HasChange("visibility") {
1015+
repoReq.AllowForking = allowForking
10191016
o, n := d.GetChange("visibility")
10201017
repoReq.Visibility = github.Ptr(n.(string))
10211018
log.Printf("[DEBUG] Updating repository visibility from %s to %s", o, n)
@@ -1030,6 +1027,7 @@ func resourceGithubRepositoryUpdate(ctx context.Context, d *schema.ResourceData,
10301027
}
10311028

10321029
if d.HasChange("private") {
1030+
repoReq.AllowForking = allowForking
10331031
o, n := d.GetChange("private")
10341032
repoReq.Private = github.Ptr(n.(bool))
10351033
log.Printf("[DEBUG] Updating repository privacy from %v to %v", o, n)

0 commit comments

Comments
 (0)