Repository navigation
Conversation
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.
|
👋 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. |
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 @@ | |||
| --- | |||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Missing tests resource_github_repository_environment_deployment_protection_rule_test.go with TestAccGithubRepositoryEnvironmentDeploymentProtectionRule
| } | ||
| d.SetId(id) | ||
|
|
||
| return resourceGithubRepositoryEnvironmentDeploymentProtectionRuleRead(ctx, d, m) |
There was a problem hiding this comment.
// Never call Read at end of Create, set any Computed fields in Create
| ) | ||
|
|
||
| func resourceGithubRepositoryEnvironmentDeploymentProtectionRule() *schema.Resource { | ||
| return &schema.Resource{ |
There was a problem hiding this comment.
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": { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
You should treat 404 as deleted
|
@stockmaj let me know if you are willing to continue this pull request. |
|
@acouvreur Yes, I'm willing to continue this PR. I'll work through your review comments and push an update. |
- Add a resource Description for docs generation. - Add computed repository_id and use diffRepository so a repository rename updates the resource in place instead of replacing it; add an Update function that rebuilds the ID after a rename. - Add computed protection_rule_id and read repository, environment and rule ID from state rather than parsing the resource ID. - Set Computed fields in Create instead of calling Read. - Use errors.AsType[*github.ErrorResponse] for error checks. - Always set integration_id in Read so a stale value cannot stay in state. - Treat a 404 on delete as already deleted, so destroy succeeds when the rule, environment or repository is already gone. - Stop escaping the environment name: go-github v92 escapes it, so the extra url.PathEscape double-encoded names containing `/` or spaces.
Add TestAccGithubRepositoryEnvironmentDeploymentProtectionRule covering create and delete (with a CheckDestroy that lists the environment's rules), import, repository rename and repository change. The tests need an App that can be enabled as a custom deployment protection rule, read from GH_TEST_DEPLOYMENT_PROTECTION_RULE_APP_ID, and skip without it. Add unit tests for Read and Delete against a mock API, covering 404 handling, the integration_id refresh and environment name escaping.
Replace the hand-written page with a template and examples rendered by `make generatedocs`.
…ing one The acceptance test lists the apps available as custom deployment protection rules on the test environment and skips when there are none, so it needs no new test configuration.
Update only rewrote the ID, so a repository change that diffRepository could not resolve at plan time applied as a no-op and left the rule on the old repository. Update now checks the new name still resolves to repository_id. Also validate integration_id, skip the acceptance tests when the test credentials cannot list protection rule apps, and list the resource in RESOURCES.md.
Resolves #2225
Before the change?
After the change?
github_repository_environment_deployment_protection_ruleenables an App as a custom deployment protection rule on an environment, and disables it on destroy.github_repository_environment_deployment_policy:repository_idis tracked, so renaming the repository updates the resource in place, and pointing it at a different repository replaces it.make testandmake lintcheck. I haven't run the acceptance tests: my test account has no App that can be enabled as a protection rule. If the test org has one, the acceptance tests will cover create, import, rename and repository change.Pull request checklist
Does this introduce a breaking change?
Please see our docs on breaking changes to help!