diff --git a/patches/0002-Commit-signoff-is-enforced-by-the-organization-and-cannot-be-disabled.patch b/patches/0002-Commit-signoff-is-enforced-by-the-organization-and-cannot-be-disabled.patch new file mode 100644 index 00000000..5853980c --- /dev/null +++ b/patches/0002-Commit-signoff-is-enforced-by-the-organization-and-cannot-be-disabled.patch @@ -0,0 +1,43 @@ +Prevent 422 Commit signoff is enforced by the organization and cannot be disabled. + +This patch fixes a bug in the GitHub API 2022-11-28 version when updating a repository +within an organization that has "Require sign off on web-based commits" enabled. +When trying to update the repository with `web_commit_signoff_required` field, the API +returns a 422 error: "Commit signoff is enforced by the organization and cannot be disabled". + +For more information: https://github.com/integrations/terraform-provider-github/issues/2077 + +diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go +index f28cc4c6..a4714fcc 100644 +--- a/github/resource_github_repository.go ++++ b/github/resource_github_repository.go +@@ -769,6 +769,20 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er + owner := meta.(*Owner).name + ctx := context.WithValue(context.Background(), ctxId, d.Id()) + ++ // When the organization has "Require sign off on web-based commits" enabled, ++ // the API doesn't allow you to send `web_commit_signoff_required` in order to ++ // update the repository with this field or it will throw a 422 error. ++ // As a workaround, we check if the organization requires it, and if so, ++ // we remove the field from the request. ++ if d.HasChange("web_commit_signoff_required") && meta.(*Owner).IsOrganization { ++ organization, _, err := client.Organizations.Get(ctx, owner) ++ if err == nil { ++ if organization != nil && organization.GetWebCommitSignoffRequired() { ++ repoReq.WebCommitSignoffRequired = nil ++ } ++ } ++ } ++ + repo, _, err := client.Repositories.Edit(ctx, owner, repoName, repoReq) + if err != nil { + return err +@@ -875,6 +889,8 @@ func resourceGithubRepositoryDelete(d *schema.ResourceData, meta interface{}) er + return err + } + repoReq := resourceGithubRepositoryObject(d) ++ // Always remove `web_commit_signoff_required` when archiving, to avoid 422 error ++ repoReq.WebCommitSignoffRequired = nil + log.Printf("[DEBUG] Archiving repository on delete: %s/%s", owner, repoName) + _, _, err := client.Repositories.Edit(ctx, owner, repoName, repoReq) + return err