Skip to content

Commit c2a1941

Browse files
authored
feat: Add support for repo private forking (#3056)
Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com>
1 parent 0596d98 commit c2a1941

File tree

6 files changed

+309
-264
lines changed

6 files changed

+309
-264
lines changed

github/data_source_github_repository.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99
"strings"
1010

1111
"github.com/google/go-github/v81/github"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1314
)
1415

1516
func dataSourceGithubRepository() *schema.Resource {
1617
return &schema.Resource{
17-
Read: dataSourceGithubRepositoryRead,
18+
ReadContext: dataSourceGithubRepositoryRead,
1819

1920
Schema: map[string]*schema.Schema{
2021
"full_name": {
@@ -95,6 +96,10 @@ func dataSourceGithubRepository() *schema.Resource {
9596
Type: schema.TypeBool,
9697
Computed: true,
9798
},
99+
"allow_forking": {
100+
Type: schema.TypeBool,
101+
Computed: true,
102+
},
98103
"squash_merge_commit_title": {
99104
Type: schema.TypeString,
100105
Computed: true,
@@ -339,8 +344,7 @@ func dataSourceGithubRepository() *schema.Resource {
339344
}
340345
}
341346

342-
func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta any) error {
343-
ctx := context.Background()
347+
func dataSourceGithubRepositoryRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
344348
client := meta.(*Owner).v3client
345349
owner := meta.(*Owner).name
346350
var repoName string
@@ -349,15 +353,15 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta any) error {
349353
var err error
350354
owner, repoName, err = splitRepoFullName(fullName.(string))
351355
if err != nil {
352-
return err
356+
return diag.FromErr(err)
353357
}
354358
}
355359
if name, ok := d.GetOk("name"); ok {
356360
repoName = name.(string)
357361
}
358362

359363
if repoName == "" {
360-
return fmt.Errorf("one of %q or %q has to be provided", "full_name", "name")
364+
return diag.Errorf("one of %q or %q has to be provided", "full_name", "name")
361365
}
362366

363367
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
@@ -370,7 +374,7 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta any) error {
370374
return nil
371375
}
372376
}
373-
return err
377+
return diag.FromErr(err)
374378
}
375379

376380
d.SetId(repoName)
@@ -389,6 +393,7 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta any) error {
389393
_ = d.Set("allow_squash_merge", repo.GetAllowSquashMerge())
390394
_ = d.Set("allow_rebase_merge", repo.GetAllowRebaseMerge())
391395
_ = d.Set("allow_auto_merge", repo.GetAllowAutoMerge())
396+
_ = d.Set("allow_forking", repo.GetAllowForking())
392397
_ = d.Set("squash_merge_commit_title", repo.GetSquashMergeCommitTitle())
393398
_ = d.Set("squash_merge_commit_message", repo.GetSquashMergeCommitMessage())
394399
_ = d.Set("merge_commit_title", repo.GetMergeCommitTitle())
@@ -412,25 +417,25 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta any) error {
412417
if repo.GetHasPages() {
413418
pages, _, err := client.Repositories.GetPagesInfo(ctx, owner, repoName)
414419
if err != nil {
415-
return err
420+
return diag.FromErr(err)
416421
}
417422
if err := d.Set("pages", flattenPages(pages)); err != nil {
418-
return fmt.Errorf("error setting pages: %w", err)
423+
return diag.Errorf("error setting pages: %v", err)
419424
}
420425
} else {
421426
err = d.Set("pages", flattenPages(nil))
422427
if err != nil {
423-
return err
428+
return diag.FromErr(err)
424429
}
425430
}
426431

427432
if repo.License != nil {
428433
repository_license, _, err := client.Repositories.License(ctx, owner, repoName)
429434
if err != nil {
430-
return err
435+
return diag.FromErr(err)
431436
}
432437
if err := d.Set("repository_license", flattenRepositoryLicense(repository_license)); err != nil {
433-
return fmt.Errorf("error setting repository_license: %w", err)
438+
return diag.Errorf("error setting repository_license: %v", err)
434439
}
435440
} else {
436441
_ = d.Set("repository_license", flattenRepositoryLicense(nil))
@@ -444,18 +449,18 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta any) error {
444449
},
445450
})
446451
if err != nil {
447-
return err
452+
return diag.FromErr(err)
448453
}
449454
} else {
450455
err = d.Set("template", []any{})
451456
if err != nil {
452-
return err
457+
return diag.FromErr(err)
453458
}
454459
}
455460

456461
err = d.Set("topics", flattenStringList(repo.Topics))
457462
if err != nil {
458-
return err
463+
return diag.FromErr(err)
459464
}
460465

461466
return nil

github/data_source_github_repository_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
99
)
1010

11-
func TestAccGithubRepositoryDataSource(t *testing.T) {
11+
func TestAccDataSourceGithubRepository(t *testing.T) {
1212
t.Run("queries a public repository without error", func(t *testing.T) {
1313
config := fmt.Sprintf(`
1414
data "github_repository" "test" {

github/resource_github_repository.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ func resourceGithubRepository() *schema.Resource {
246246
Default: false,
247247
Description: "Set to 'true' to allow auto-merging pull requests on the repository.",
248248
},
249+
"allow_forking": {
250+
Type: schema.TypeBool,
251+
Optional: true,
252+
Default: false,
253+
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.",
254+
},
249255
"squash_merge_commit_title": {
250256
Type: schema.TypeString,
251257
Optional: true,
@@ -589,6 +595,7 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
589595
AllowSquashMerge: github.Ptr(d.Get("allow_squash_merge").(bool)),
590596
AllowRebaseMerge: github.Ptr(d.Get("allow_rebase_merge").(bool)),
591597
AllowAutoMerge: github.Ptr(d.Get("allow_auto_merge").(bool)),
598+
AllowForking: github.Ptr(d.Get("allow_forking").(bool)),
592599
DeleteBranchOnMerge: github.Ptr(d.Get("delete_branch_on_merge").(bool)),
593600
WebCommitSignoffRequired: github.Ptr(d.Get("web_commit_signoff_required").(bool)),
594601
AutoInit: github.Ptr(d.Get("auto_init").(bool)),
@@ -841,6 +848,7 @@ func resourceGithubRepositoryRead(ctx context.Context, d *schema.ResourceData, m
841848
_ = d.Set("allow_rebase_merge", repo.GetAllowRebaseMerge())
842849
_ = d.Set("allow_squash_merge", repo.GetAllowSquashMerge())
843850
_ = d.Set("allow_update_branch", repo.GetAllowUpdateBranch())
851+
_ = d.Set("allow_forking", repo.GetAllowForking())
844852
_ = d.Set("delete_branch_on_merge", repo.GetDeleteBranchOnMerge())
845853
_ = d.Set("web_commit_signoff_required", repo.GetWebCommitSignoffRequired())
846854
_ = d.Set("has_downloads", repo.GetHasDownloads())

github/resource_github_repository_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/stretchr/testify/assert"
1616
)
1717

18-
func TestAccGithubRepositories(t *testing.T) {
18+
func TestAccGithubRepository(t *testing.T) {
1919
t.Run("creates and updates repositories without error", func(t *testing.T) {
2020
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
2121
config := fmt.Sprintf(`
@@ -558,6 +558,34 @@ func TestAccGithubRepositories(t *testing.T) {
558558
})
559559
})
560560

561+
t.Run("create_private_with_forking", func(t *testing.T) {
562+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
563+
repoName := fmt.Sprintf("tf-acc-test-%s", randomID)
564+
565+
config := fmt.Sprintf(`
566+
resource "github_repository" "test" {
567+
name = "%s"
568+
visibility = "private"
569+
570+
allow_forking = true
571+
}
572+
`, repoName)
573+
574+
resource.Test(t, resource.TestCase{
575+
PreCheck: func() { skipUnlessHasOrgs(t) },
576+
ProviderFactories: providerFactories,
577+
Steps: []resource.TestStep{
578+
{
579+
Config: config,
580+
Check: resource.ComposeTestCheckFunc(
581+
resource.TestCheckResourceAttr("github_repository.test", "visibility", "private"),
582+
resource.TestCheckResourceAttr("github_repository.test", "allow_forking", "true"),
583+
),
584+
},
585+
},
586+
})
587+
})
588+
561589
t.Run("configures vulnerability alerts for a private repository", func(t *testing.T) {
562590
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
563591
repoName := fmt.Sprintf("tf-acc-test-prv-vuln-%s", randomID)

website/docs/d/repository.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ The following arguments are supported:
5757

5858
* `allow_auto_merge` - Whether the repository allows auto-merging pull requests.
5959

60+
* `allow_forking` - Whether the repository allows private forking; this is only relevant if the repository is owned by an organization and is private or internal.
61+
6062
* `squash_merge_commit_title` - The default value for a squash merge commit title.
6163

6264
* `squash_merge_commit_message` - The default value for a squash merge commit message.

0 commit comments

Comments
 (0)