Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/data-sources/repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ The following arguments are supported:

- `has_wiki` - Whether the repository has the GitHub Wiki enabled.

- `has_pull_requests` - Whether the repository has pull requests enabled.

- `pull_request_creation_policy` - Who can create pull requests on the repository. Either `all` or `collaborators_only`.

- `is_template` - Whether the repository is a template repository.

- `fork` - Whether the repository is a fork.
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ The following arguments are supported:

- `has_wiki` - (Optional) Set to `true` to enable the GitHub Wiki features on the repository.

- `has_pull_requests` - (Optional) Set to `false` to disable pull requests on the repository, hiding the pull requests tab. Defaults to `true`.

- `pull_request_creation_policy` - (Optional) Restricts who can create pull requests on the repository. Can be `all` (default) to allow any user, or `collaborators_only` to restrict creation to collaborators with write access. Applicable only if `has_pull_requests` is `true`.

- `is_template` - (Optional) Set to `true` to tell GitHub that this is a template repository.

- `allow_merge_commit` - (Optional) Set to `false` to disable merge commits on the repository.
Expand Down
10 changes: 10 additions & 0 deletions github/data_source_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ func dataSourceGithubRepository() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"has_pull_requests": {
Type: schema.TypeBool,
Computed: true,
},
"pull_request_creation_policy": {
Type: schema.TypeString,
Computed: true,
},
"is_template": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -389,6 +397,8 @@ func dataSourceGithubRepositoryRead(ctx context.Context, d *schema.ResourceData,
_ = d.Set("has_issues", repo.GetHasIssues())
_ = d.Set("has_discussions", repo.GetHasDiscussions())
_ = d.Set("has_wiki", repo.GetHasWiki())
_ = d.Set("has_pull_requests", repo.GetHasPullRequests())
_ = d.Set("pull_request_creation_policy", repo.GetPullRequestCreationPolicy())
_ = d.Set("is_template", repo.GetIsTemplate())
_ = d.Set("fork", repo.GetFork())
_ = d.Set("allow_merge_commit", repo.GetAllowMergeCommit())
Expand Down
21 changes: 21 additions & 0 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@ func resourceGithubRepository() *schema.Resource {
Optional: true,
Description: "Set to 'true' to enable the GitHub Wiki features on the repository.",
},
"has_pull_requests": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Set to 'false' to disable pull requests on the repository, hiding the pull requests tab. Defaults to 'true'.",
},
"pull_request_creation_policy": {
Type: schema.TypeString,
Optional: true,
Default: "all",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"all", "collaborators_only"}, false)),
Description: "Restricts who can create pull requests on the repository. Can be 'all' (default) to allow any user, or 'collaborators_only' to restrict creation to collaborators with write access.",
},
"is_template": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -597,6 +610,7 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
HasDiscussions: new(d.Get("has_discussions").(bool)),
HasProjects: new(d.Get("has_projects").(bool)),
HasWiki: new(d.Get("has_wiki").(bool)),
HasPullRequests: new(d.Get("has_pull_requests").(bool)),
IsTemplate: new(d.Get("is_template").(bool)),
AllowMergeCommit: new(d.Get("allow_merge_commit").(bool)),
AllowSquashMerge: new(d.Get("allow_squash_merge").(bool)),
Expand Down Expand Up @@ -630,6 +644,11 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
}
}

// only configure the pull request creation policy when pull requests are enabled
if d.Get("has_pull_requests").(bool) {
repository.PullRequestCreationPolicy = new(d.Get("pull_request_creation_policy").(string))
}

// only configure allow forking if repository is not public
if visibility != "public" && (d.IsNewResource() || d.HasChange("allow_forking")) {
if allowForking, ok := d.GetOkExists("allow_forking"); ok { //nolint:staticcheck // SA1019 // We sometimes need to use GetOkExists for booleans
Expand Down Expand Up @@ -830,6 +849,8 @@ func resourceGithubRepositoryRead(ctx context.Context, d *schema.ResourceData, m
_ = d.Set("has_discussions", repo.GetHasDiscussions())
_ = d.Set("has_projects", repo.GetHasProjects())
_ = d.Set("has_wiki", repo.GetHasWiki())
_ = d.Set("has_pull_requests", repo.GetHasPullRequests())
_ = d.Set("pull_request_creation_policy", repo.GetPullRequestCreationPolicy())
_ = d.Set("is_template", repo.GetIsTemplate())
_ = d.Set("full_name", repo.GetFullName())
_ = d.Set("default_branch", repo.GetDefaultBranch())
Expand Down
94 changes: 94 additions & 0 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,100 @@ func TestAccGithubRepository(t *testing.T) {
})
})

t.Run("manages pull request settings without error", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
testRepoName := fmt.Sprintf("%spr-settings-%s", testResourcePrefix, randomID)

// Restrict pull request creation to collaborators only.
configRestricted := fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
description = "Terraform acceptance tests %[1]s"
has_pull_requests = true
pull_request_creation_policy = "collaborators_only"
visibility = "%s"
}
`, testRepoName, testAccConf.testRepositoryVisibility)

// Disable pull requests entirely.
configDisabled := fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
description = "Terraform acceptance tests %[1]s"
has_pull_requests = false
visibility = "%s"
}
`, testRepoName, testAccConf.testRepositoryVisibility)

checks := map[string]resource.TestCheckFunc{
"restricted": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "has_pull_requests",
"true",
),
resource.TestCheckResourceAttr(
"github_repository.test", "pull_request_creation_policy",
"collaborators_only",
),
),
"disabled": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "has_pull_requests",
"false",
),
),
}

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: configRestricted,
Check: checks["restricted"],
},
{
Config: configDisabled,
Check: checks["disabled"],
},
},
})
})

t.Run("defaults pull request settings without error", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
testRepoName := fmt.Sprintf("%spr-defaults-%s", testResourcePrefix, randomID)
config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
description = "Terraform acceptance tests %[1]s"
visibility = "%s"
}
`, testRepoName, testAccConf.testRepositoryVisibility)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "has_pull_requests",
"true",
),
resource.TestCheckResourceAttr(
"github_repository.test", "pull_request_creation_policy",
"all",
),
)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
})

t.Run("updates a repositories name without error", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
oldName := fmt.Sprintf(`%srename-%s`, testResourcePrefix, randomID)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-jose/go-jose/v4 v4.1.4
github.com/gofri/go-github-ratelimit/v2 v2.0.2
github.com/google/go-cmp v0.7.0
github.com/google/go-github/v88 v88.0.0
github.com/google/go-github/v88 v88.0.1-0.20260605122729-bbd1294f1602
github.com/google/uuid v1.6.0
github.com/hashicorp/go-cty v1.5.0
github.com/hashicorp/go-retryablehttp v0.7.8
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/go-github/v88 v88.0.0 h1:dZA9IKkPK1eXZj4ypngnpRj5FwdpTv4whix2PrQMP7M=
github.com/google/go-github/v88 v88.0.0/go.mod h1:rufTDgn2N45wjhukLTyxmvc9nilSp3mr3Rgtt6b1MPw=
github.com/google/go-github/v88 v88.0.1-0.20260605122729-bbd1294f1602 h1:2US8Ojrwi+Yi/IG5QYkRrEF211lyryqIo0Q+QlntZ10=
github.com/google/go-github/v88 v88.0.1-0.20260605122729-bbd1294f1602/go.mod h1:rufTDgn2N45wjhukLTyxmvc9nilSp3mr3Rgtt6b1MPw=
github.com/google/go-querystring v1.2.0 h1:yhqkPbu2/OH+V9BfpCVPZkNmUXhb2gBxJArfhIxNtP0=
github.com/google/go-querystring v1.2.0/go.mod h1:8IFJqpSRITyJ8QhQ13bmbeMBDfmeEJZD5A0egEOmkqU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down
4 changes: 4 additions & 0 deletions templates/data-sources/repository.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ The following arguments are supported:

- `has_wiki` - Whether the repository has the GitHub Wiki enabled.

- `has_pull_requests` - Whether the repository has pull requests enabled.

- `pull_request_creation_policy` - Who can create pull requests on the repository. Either `all` or `collaborators_only`.

- `is_template` - Whether the repository is a template repository.

- `fork` - Whether the repository is a fork.
Expand Down
4 changes: 4 additions & 0 deletions templates/resources/repository.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ The following arguments are supported:

- `has_wiki` - (Optional) Set to `true` to enable the GitHub Wiki features on the repository.

- `has_pull_requests` - (Optional) Set to `false` to disable pull requests on the repository, hiding the pull requests tab. Defaults to `true`.

- `pull_request_creation_policy` - (Optional) Restricts who can create pull requests on the repository. Can be `all` (default) to allow any user, or `collaborators_only` to restrict creation to collaborators with write access. Applicable only if `has_pull_requests` is `true`.

- `is_template` - (Optional) Set to `true` to tell GitHub that this is a template repository.

- `allow_merge_commit` - (Optional) Set to `false` to disable merge commits on the repository.
Expand Down