Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deployment policy resource #1530

Closed

Conversation

ilmax
Copy link
Contributor

@ilmax ilmax commented Feb 2, 2023

  • Add the initial code to manage the resource
  • Add sample configuration used to test it
  • Add test

Resolves #922


Behavior

Before the change?

Not possible to set environment deployment branch policies

After the change?

You can to set environment deployment branch policies

Other information

N/A


Additional info

Pull request checklist

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Added the appropriate label for the given change

Does this introduce a breaking change?

Please see our docs on breaking changes to help!

  • Yes (Please add the Type: Breaking change label)
  • No

If Yes, what's the impact:

  • N/A

Pull request type

Please add the corresponding label for change this PR introduces:

  • Bugfix: Type: Bug
  • Feature/model/API additions: Type: Feature
  • Updates to docs or samples: Type: Documentation
  • Dependencies/code cleanup: Type: Maintenance

@ilmax
Copy link
Contributor Author

ilmax commented Feb 2, 2023

@kfcampbell I put this one up for early (tested locally via the new folder in examples and works as expected) review because, as mentioned in #922 I'm not a go expert 😅.
I'd like to know what's the approach for documentation, is it added manually? If memory serves me (and it may not) in the azurerm provider it's auto-generated so I was wondering if here it's the same.

Tests will follow soon.

@kfcampbell
Copy link
Member

@limax the approach for documentation is manual unfortunately. The idea is to create a new .html.markdown file and link it in the github.erb file; you should be able to follow the pattern from other resources. Please let me know if you're having issues!

@nickfloyd nickfloyd added Type: Feature New feature or request Priority: Normal labels Feb 21, 2023
@nickfloyd
Copy link
Contributor

Hey @ilmax are you still interested in getting your changes wrapped up here? We'd be glad to review them when ready! Also, let us know if you need more information on how to build out the docs for your change. Thanks again for making our community better by being involved! ❤️

@ilmax
Copy link
Contributor Author

ilmax commented Feb 21, 2023

Hey @nickfloyd sorry for the delay but didn't had time recently to move forward in this one, is still on my radar and I plan to finish it within next week. Sorry for the delay!

@ilmax ilmax marked this pull request as ready for review February 22, 2023 18:06
@ilmax
Copy link
Contributor Author

ilmax commented Feb 22, 2023

@kfcampbell @nickfloyd I think this may be ready for review

Copy link
Member

@kfcampbell kfcampbell left a comment

Choose a reason for hiding this comment

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

I had a couple questions I'd like to hear about before getting this in. The tests and examples look good, thanks!

Read: resourceGithubRepositoryEnvironmentDeploymentPolicyRead,
Update: resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate,
Delete: resourceGithubRepositoryEnvironmentDeploymentPolicyDelete,
Schema: map[string]*schema.Schema{
Copy link
Member

Choose a reason for hiding this comment

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

It looks like Import isn't implemented here. Is that an intentional omission?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Preamble: Not an expert in implementing a terraform provider.

The GitHub api that exposes a get deployment branch policy requires an ID, this ID is not exposed anywhere in the UI so the user has no way (unless it's queries the GitHub api) to know such ID.

One option I was thinking about was to read all the deployment branch policies and then filter by branch pattern but I thought such an implementation to be overkill, especially because this resource only contains a branch name.

What are your thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's still worth implementing with an ID even if it's not clearly visible in the GitHub UI; there's still value in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just implemented support for import

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccGithubRepositoryEnvironmentDeploymentPolicy(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Confirming that integration tests are passing for me locally; thank you for the attention to testing.

examples/repository_deployment_policy/README.md Outdated Show resolved Hide resolved
@nickfloyd
Copy link
Contributor

Hey, @ilmax Let us know your thoughts on the questions from @kfcampbell and we'll get this merged in. Thanks again for the work here ❤️

@ilmax
Copy link
Contributor Author

ilmax commented Mar 9, 2023

Hey @nickfloyd sorry for the late reply, replied to the questions

Copy link

@jjgriff93 jjgriff93 left a comment

Choose a reason for hiding this comment

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

Just some formatting suggestions :) would love to see this in the provider soon so I can get rid of my local-exec cli step!

examples/repository_deployment_policy/main.tf Outdated Show resolved Hide resolved
examples/repository_deployment_policy/main.tf Outdated Show resolved Hide resolved
examples/repository_deployment_policy/main.tf Outdated Show resolved Hide resolved
@ghost
Copy link

ghost commented Mar 17, 2023

Just some formatting suggestions :) would love to see this in the provider soon so I can get rid of my local-exec cli step!

Hi, would you mind sharing your local-exec code?

@ilmax ilmax force-pushed the feat/environment-protection-rules branch from 5e7b5a6 to 35321df Compare March 17, 2023 18:47
@ilmax
Copy link
Contributor Author

ilmax commented Mar 17, 2023

@kfcampbell @nickfloyd I implemented the import, removed the extra sample and rebased the PR, mind you taking another look?

Thanks

@jjgriff93
Copy link

Just some formatting suggestions :) would love to see this in the provider soon so I can get rid of my local-exec cli step!

Hi, would you mind sharing your local-exec code?

Sure @ivan-santos-eq, here you go:

resource "github_repository_environment" "all" {
  for_each    = local.branches_and_envs
  repository  = local.repository_name
  environment = each.value

  deployment_branch_policy {
    custom_branch_policies = true
    protected_branches     = false
  }

  # TODO: remove when https://github.com/integrations/terraform-provider-github/pull/1530 is merged
  provisioner "local-exec" {
    command = <<EOF
curl https://api.github.com/repos/${var.github_owner}/${local.repository_name}/environments/${each.value}/deployment-branch-policies \
  -H "Accept: application/vnd.github+json" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  -H "Authorization: Bearer ${var.github_access_token}" \
  -d '{"name":"${each.key}"}'
EOF
  }

  depends_on = [
    github_repository.app
  ]
}

examples/dev.tfrc Outdated Show resolved Hide resolved
@ilmax
Copy link
Contributor Author

ilmax commented Apr 4, 2023

Hello @kfcampbell @nickfloyd did you had a chance to review this one?

@kfcampbell
Copy link
Member

@ilmax 😭 😭 😭 the tests are now failing for me with the following error:

        Error: Provider produced inconsistent result after apply
        
        When applying changes to github_repository_environment_deployment_policy.test,
        provider "github" produced an unexpected new value for was present, but now
        absent.

I would imagine that there's supposed to be a value in between "for was" that describes a specific property, though I'm not sure how we ended up in this situation. Any thoughts?

@ilmax
Copy link
Contributor Author

ilmax commented Apr 11, 2023

I'll try to look into this soon

@edheliel
Copy link
Contributor

Is this still ongoing or stale?

@pragmaticivan
Copy link

Hi, do you need help with getting it over the finish line?

@edheliel
Copy link
Contributor

edheliel commented Jul 19, 2023

@kfcampbell @jjgriff93 @nickfloyd had some time and investigated. The issue was due to some double encoding of the environment name which cause s the name to be invalid and the API to not be able to find the newly created resource.

I opened a PR containing the original author's work and my fix on top of it. Feel free to review and let me know if something else is needed.

PR: #1799

@kfcampbell
Copy link
Member

Closing as #1799 has been merged. Thank you @ilmax and @edheliel!

@kfcampbell kfcampbell closed this Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to set branch names/patterns when custom_branch_policies is true.
6 participants