close
Skip to content

Add github_repository_environment_deployment_protection_rule resource - #3566

Open
stockmaj wants to merge 5 commits into
integrations:mainfrom
stockmaj:dpr-6.13
Open

stockmaj wants to merge 5 commits into
integrations:mainfrom
stockmaj:dpr-6.13

Conversation

@stockmaj

Copy link
Copy Markdown

Summary

Adds a github_repository_environment_deployment_protection_rule resource that enables a GitHub App as a custom deployment protection rule on an environment — the deployment_protection_rule gate. The provider can currently manage required reviewers and branch policies on an environment, but has no way to declaratively attach an App-based deployment protection rule.

Resolves #2225.

Details

  • CRUD + import over go-github's CreateCustomDeploymentProtectionRule / GetCustomDeploymentProtectionRule / DisableCustomDeploymentProtectionRule.
  • Schema: repository + environment + integration_id (the App's ID), mirroring the existing github_repository_environment_deployment_policy resource for consistency.
  • Import ID format <repository>:<environment>:<rule_id>.
  • Docs page added under docs/resources/.

Branch is cut from the v6.13.0 release.

Enables a GitHub App as a custom deployment protection rule on an environment
(the deployment_protection_rule gate), which the provider previously could not
express declaratively — resolves integrations#2225.

CRUD + import over go-github's Create/Get/DisableCustomDeploymentProtectionRule;
schema is repository + environment + integration_id, mirroring the existing
github_repository_environment_deployment_policy resource. Based on v6.13.0.
@github-actions

Copy link
Copy Markdown

👋 Hi, and thank you for this contribution!

This repo is maintained by GitHub and community members on a best-effort basis. We'll get to this as soon as we can.

You can help us prioritize by joining the discussion on open issues and PRs, sharing details on the changes you need, and reviewing other contributions.


🤖 This is an automated message.

stockmaj and others added 4 commits July 22, 2026 09:23
InternalValidate rejects the resource: with no UpdateContext, every attribute
must be ForceNew. repository was Required-only. Mark it ForceNew and drop the
now-redundant diffRepository CustomizeDiff (it keys off a repository_id this
resource doesn't have). `go test -run TestProvider` (InternalValidate) passes.
main upgraded go-github v88 -> v89; the CreateCustomDeploymentProtectionRule
request type moved packages. Match the rest of the provider.
@@ -0,0 +1,67 @@
---

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation should be generated through templates after running make generatedocs.

I've tested locally and deleting it is actually not working.
I would expect the following templates:

  • templates/resources/repository_environment_deployment_protection_rule.md.tmpl
  • examples/resources/repository_environment_deployment_protection_rule/example_1.tf (usage)
  • examples/resources/repository_environment_deployment_protection_rule/example_2.tf (import block)

Look into templates/resources/repository_environment_deployment_policy.md.tmpl for example

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing tests resource_github_repository_environment_deployment_protection_rule_test.go with TestAccGithubRepositoryEnvironmentDeploymentProtectionRule

}
d.SetId(id)

return resourceGithubRepositoryEnvironmentDeploymentProtectionRuleRead(ctx, d, m)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/integrations/terraform-provider-github/blob/main/ARCHITECTURE.md#crud-function-signatures

// Never call Read at end of Create, set any Computed fields in Create

)

func resourceGithubRepositoryEnvironmentDeploymentProtectionRule() *schema.Resource {
return &schema.Resource{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This resource is missing a Description field used for the documentation generation

func resourceGithubRepositoryEnvironmentDeploymentProtectionRule() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"repository": {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have a computed repository_id to be resilient to repository name changes

rule, _, err := client.Repositories.GetCustomDeploymentProtectionRule(ctx, owner, repoName, url.PathEscape(envName), ruleID)
if err != nil {
var ghErr *github.ErrorResponse
if errors.As(err, &ghErr) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use errors.AsType*github.ErrorResponse instead

return diag.FromErr(err)
}
if rule.App != nil {
if err := d.Set("integration_id", int(rule.App.GetID())); err != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of error, this would keep the old value in state?

return diag.FromErr(fmt.Errorf("invalid rule id: %s", ruleIDStr))
}

_, err = client.Repositories.DisableCustomDeploymentProtectionRule(ctx, owner, repoName, url.PathEscape(envName), ruleID)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should treat 404 as deleted

@acouvreur

Copy link
Copy Markdown
Contributor

@stockmaj let me know if you are willing to continue this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT]: Add support for deployment protection rules

2 participants