Skip to content

Commit 192ce70

Browse files
committed
fix: only set web_commit_signoff_required if explicitly configured
1 parent 4c95ca9 commit 192ce70

3 files changed

Lines changed: 62 additions & 42 deletions

File tree

github/resource_github_repository.go

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ func resourceGithubRepository() *schema.Resource {
287287
"web_commit_signoff_required": {
288288
Type: schema.TypeBool,
289289
Optional: true,
290-
Default: false,
291-
Description: "Require contributors to sign off on web-based commits. Defaults to 'false'.",
290+
Description: "Require contributors to sign off on web-based commits.",
292291
},
293292
"auto_init": {
294293
Type: schema.TypeBool,
@@ -586,29 +585,28 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
586585
visibility := calculateVisibility(d)
587586

588587
repository := &github.Repository{
589-
Name: github.Ptr(d.Get("name").(string)),
590-
Description: github.Ptr(d.Get("description").(string)),
591-
Homepage: github.Ptr(d.Get("homepage_url").(string)),
592-
Visibility: github.Ptr(visibility),
593-
HasDownloads: github.Ptr(d.Get("has_downloads").(bool)),
594-
HasIssues: github.Ptr(d.Get("has_issues").(bool)),
595-
HasDiscussions: github.Ptr(d.Get("has_discussions").(bool)),
596-
HasProjects: github.Ptr(d.Get("has_projects").(bool)),
597-
HasWiki: github.Ptr(d.Get("has_wiki").(bool)),
598-
IsTemplate: github.Ptr(d.Get("is_template").(bool)),
599-
AllowMergeCommit: github.Ptr(d.Get("allow_merge_commit").(bool)),
600-
AllowSquashMerge: github.Ptr(d.Get("allow_squash_merge").(bool)),
601-
AllowRebaseMerge: github.Ptr(d.Get("allow_rebase_merge").(bool)),
602-
AllowAutoMerge: github.Ptr(d.Get("allow_auto_merge").(bool)),
603-
DeleteBranchOnMerge: github.Ptr(d.Get("delete_branch_on_merge").(bool)),
604-
WebCommitSignoffRequired: github.Ptr(d.Get("web_commit_signoff_required").(bool)),
605-
AutoInit: github.Ptr(d.Get("auto_init").(bool)),
606-
LicenseTemplate: github.Ptr(d.Get("license_template").(string)),
607-
GitignoreTemplate: github.Ptr(d.Get("gitignore_template").(string)),
608-
Archived: github.Ptr(d.Get("archived").(bool)),
609-
Topics: expandStringList(d.Get("topics").(*schema.Set).List()),
610-
AllowUpdateBranch: github.Ptr(d.Get("allow_update_branch").(bool)),
611-
SecurityAndAnalysis: calculateSecurityAndAnalysis(d),
588+
Name: github.Ptr(d.Get("name").(string)),
589+
Description: github.Ptr(d.Get("description").(string)),
590+
Homepage: github.Ptr(d.Get("homepage_url").(string)),
591+
Visibility: github.Ptr(visibility),
592+
HasDownloads: github.Ptr(d.Get("has_downloads").(bool)),
593+
HasIssues: github.Ptr(d.Get("has_issues").(bool)),
594+
HasDiscussions: github.Ptr(d.Get("has_discussions").(bool)),
595+
HasProjects: github.Ptr(d.Get("has_projects").(bool)),
596+
HasWiki: github.Ptr(d.Get("has_wiki").(bool)),
597+
IsTemplate: github.Ptr(d.Get("is_template").(bool)),
598+
AllowMergeCommit: github.Ptr(d.Get("allow_merge_commit").(bool)),
599+
AllowSquashMerge: github.Ptr(d.Get("allow_squash_merge").(bool)),
600+
AllowRebaseMerge: github.Ptr(d.Get("allow_rebase_merge").(bool)),
601+
AllowAutoMerge: github.Ptr(d.Get("allow_auto_merge").(bool)),
602+
DeleteBranchOnMerge: github.Ptr(d.Get("delete_branch_on_merge").(bool)),
603+
AutoInit: github.Ptr(d.Get("auto_init").(bool)),
604+
LicenseTemplate: github.Ptr(d.Get("license_template").(string)),
605+
GitignoreTemplate: github.Ptr(d.Get("gitignore_template").(string)),
606+
Archived: github.Ptr(d.Get("archived").(bool)),
607+
Topics: expandStringList(d.Get("topics").(*schema.Set).List()),
608+
AllowUpdateBranch: github.Ptr(d.Get("allow_update_branch").(bool)),
609+
SecurityAndAnalysis: calculateSecurityAndAnalysis(d),
612610
}
613611

614612
// only configure merge commit if we are in commit merge strategy
@@ -636,6 +634,13 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
636634
}
637635
}
638636

637+
// only configure web commit signoff if explicitly set in the configuration
638+
if webCommitSignoffRequired, ok := d.GetOkExists("web_commit_signoff_required"); ok { //nolint:staticcheck,SA1019 // We sometimes need to use GetOkExists for booleans
639+
if val, ok := webCommitSignoffRequired.(bool); ok {
640+
repository.WebCommitSignoffRequired = github.Ptr(val)
641+
}
642+
}
643+
639644
return repository
640645
}
641646

@@ -945,20 +950,6 @@ func resourceGithubRepositoryUpdate(ctx context.Context, d *schema.ResourceData,
945950
owner := meta.(*Owner).name
946951
ctx = context.WithValue(ctx, ctxId, d.Id())
947952

948-
// When the organization has "Require sign off on web-based commits" enabled,
949-
// the API doesn't allow you to send `web_commit_signoff_required` in order to
950-
// update the repository with this field or it will throw a 422 error.
951-
// As a workaround, we check if the organization requires it, and if so,
952-
// we remove the field from the request.
953-
if d.HasChange("web_commit_signoff_required") && meta.(*Owner).IsOrganization {
954-
organization, _, err := client.Organizations.Get(ctx, owner)
955-
if err == nil {
956-
if organization != nil && organization.GetWebCommitSignoffRequired() {
957-
repoReq.WebCommitSignoffRequired = nil
958-
}
959-
}
960-
}
961-
962953
repo, _, err := client.Repositories.Edit(ctx, owner, repoName, repoReq)
963954
if err != nil {
964955
return diag.FromErr(err)
@@ -1041,8 +1032,6 @@ func resourceGithubRepositoryDelete(ctx context.Context, d *schema.ResourceData,
10411032
return diag.FromErr(err)
10421033
}
10431034
repoReq := resourceGithubRepositoryObject(d)
1044-
// Always remove `web_commit_signoff_required` when archiving, to avoid 422 error
1045-
repoReq.WebCommitSignoffRequired = nil
10461035
log.Printf("[DEBUG] Archiving repository on delete: %s/%s", owner, repoName)
10471036
_, _, err := client.Repositories.Edit(ctx, owner, repoName, repoReq)
10481037
return diag.FromErr(err)

github/resource_github_repository_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,37 @@ resource "github_repository" "test" {
13271327
},
13281328
})
13291329
})
1330+
1331+
t.Run("creates repos with explicit web commit signoff required", func(t *testing.T) {
1332+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
1333+
testRepoName := fmt.Sprintf("%scommit-signoff-%s", testResourcePrefix, randomID)
1334+
config := `
1335+
resource "github_repository" "test" {
1336+
name = "%s"
1337+
auto_init = true
1338+
web_commit_signoff_required = %s
1339+
}
1340+
`
1341+
1342+
resource.Test(t, resource.TestCase{
1343+
PreCheck: func() { skipUnauthenticated(t) },
1344+
ProviderFactories: providerFactories,
1345+
Steps: []resource.TestStep{
1346+
{
1347+
Config: fmt.Sprintf(config, testRepoName, "false"),
1348+
Check: resource.ComposeTestCheckFunc(
1349+
resource.TestCheckResourceAttr("github_repository.test", "web_commit_signoff_required", "false"),
1350+
),
1351+
},
1352+
{
1353+
Config: fmt.Sprintf(config, testRepoName, "true"),
1354+
Check: resource.ComposeTestCheckFunc(
1355+
resource.TestCheckResourceAttr("github_repository.test", "web_commit_signoff_required", "true"),
1356+
),
1357+
},
1358+
},
1359+
})
1360+
})
13301361
}
13311362

13321363
func Test_expandPages(t *testing.T) {

website/docs/r/repository.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ The following arguments are supported:
112112

113113
* `delete_branch_on_merge` - (Optional) Automatically delete head branch after a pull request is merged. Defaults to `false`.
114114

115-
* `web_commit_signoff_required` - (Optional) Require contributors to sign off on web-based commits. See more [here](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/managing-repository-settings/managing-the-commit-signoff-policy-for-your-repository). Defaults to `false`.
115+
* `web_commit_signoff_required` - (Optional) Require contributors to sign off on web-based commits. See more [here](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/managing-repository-settings/managing-the-commit-signoff-policy-for-your-repository).
116116

117117
* `has_downloads` - (**DEPRECATED**) (Optional) Set to `true` to enable the (deprecated) downloads features on the repository. This attribute is no longer in use, but it hasn't been removed yet. It will be removed in a future version. See [this discussion](https://github.com/orgs/community/discussions/102145#discussioncomment-8351756).
118118

0 commit comments

Comments
 (0)