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

feat: resource_azuredevops_git_repository_branch #713

Merged
merged 10 commits into from
Mar 3, 2023

Conversation

ivi-vink
Copy link
Contributor

@ivi-vink ivi-vink commented Feb 11, 2023

All Submissions:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My code follows the code style of this project.
  • I ran lint checks locally prior to submission.
  • Have you checked to ensure there aren't other open PRs for the same update/change?

What about the current behavior has changed?

Issue Number: #663

Introduce the ability to manage files on branches that are managed by terraform. Maybe an extra acceptance test case should be included that creates/updates a file on a branch managed by terraform that is not the default branch.

Basically, I took inspiration from gitlab_branch and github_branch.

Does this introduce a change to go.mod, go.sum or vendor/?

  • Yes
  • No

Does this introduce a breaking change?

  • Yes
  • No

Any relevant logs, error output, etc?

To reproduce bug from linked issue:

  1. Create file on branch called test that is not default branch.
  2. apply something like this
data "azuredevops_project" "example" {
  name = "cdktf-test-project"
}

resource "azuredevops_git_repository" "example" {
  project_id = data.azuredevops_project.example.id
  name       = "Example Git Repository"
  initialization {
    init_type = "Clean"
  }
}

resource "azuredevops_git_repository_branch" "example" {
  repository_id = azuredevops_git_repository.example.id
  name          = "test"
}

resource "azuredevops_git_repository_file" "example" {
  repository_id       = azuredevops_git_repository.example.id
  file                = "hello"
  content             = "you"
  branch              = azuredevops_git_repository_branch.example.ref
  commit_message      = "test"
  overwrite_on_create = true
}
  1. get logs like this
[mike@lemptop:~/project/fork-azdo-provider/mytests]$ terraform apply
data.azuredevops_project.example: Reading...
data.azuredevops_project.example: Read complete after 0s [id=3151f6cf-4e03-4f18-b8aa-43ffa0b1634d]
azuredevops_git_repository.example: Refreshing state... [id=d8b105e2-a69a-4ee4-ad8e-7afa5fd68d7d]
azuredevops_git_repository_branch.example: Refreshing state... [id=d8b105e2-a69a-4ee4-ad8e-7afa5fd68d7d:test]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  + create

Terraform will perform the following actions:

  # azuredevops_git_repository_file.example will be created
  + resource "azuredevops_git_repository_file" "example" {
      + branch              = "refs/heads/test"
      + commit_message      = "test"
      + content             = "you"
      + file                = "hello"
      + id                  = (known after apply)
      + overwrite_on_create = true
      + repository_id       = "d8b105e2-a69a-4ee4-ad8e-7afa5fd68d7d"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

azuredevops_git_repository_file.example: Creating...
╷
│ Error: Create repositroy file failed, repositoryID: d8b105e2-a69a-4ee4-ad8e-7afa5fd68d7d, branch: refs/heads/test, file: hello. 
Error:  The path 'hello' specified in the add operation already exists. Please specify a new path.
│ Parameter name: newPush
│ 
│   with azuredevops_git_repository_file.example,
│   on main.tf line 27, in resource "azuredevops_git_repository_file" "example":
│   27: resource "azuredevops_git_repository_file" "example" {
│ 
╵

Should be fixed in second commit of this pull request. Basically we check if the file exists on the right branch as suggested in the original issue and set the ChangeType accordingly.

Other information

@ivi-vink ivi-vink closed this Feb 11, 2023
@ivi-vink ivi-vink changed the title feat: resource_azuredevops_git_repository_branch WIP: resource_azuredevops_git_repository_branch Feb 11, 2023
@ivi-vink ivi-vink reopened this Feb 11, 2023
@ivi-vink ivi-vink changed the title WIP: resource_azuredevops_git_repository_branch feat: resource_azuredevops_git_repository_branch Feb 13, 2023
@ivi-vink
Copy link
Contributor Author

@microsoft-github-policy-service agree

Copy link
Collaborator

@xuzhang3 xuzhang3 left a comment

Choose a reason for hiding this comment

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

@mvinkio Can we change the source_ref to ref, tag or commitId like the UI pages? By skip the source_ref, a totally new branch without any other commit history will be created..

@ivi-vink
Copy link
Contributor Author

@mvinkio Can we change the source_ref to ref, tag or commitId like the UI pages?

Ok, the resource schema should indeed have ref, tag, and commitId fields like in the azure devops GUI when creating new branch.

By skip the source_ref, a totally new branch without any other commit history will be created..

This creation of branch without existing commit history will be removed.

@ivi-vink
Copy link
Contributor Author

ivi-vink commented Feb 25, 2023

Also acceptance test TestAccGitRepoFile_Create_IncorrectBranch fails on my machine, it tries to apply regex too the error from the azure devops sdk GetBranch.

https://github.com/mvinkio/terraform-provider-azuredevops/blob/d3913879bbf8ee1224afe25637c3de1afef551f3/azuredevops/internal/service/git/resource_git_repository_file.go#L106-L108

It works again if I change the regex pattern to Error: Branch "foobar" does not exist in the \S+ repository.. Not sure what causes the different error message. Maybe I'm using wrong terraform version or something?

ivi-vink and others added 7 commits February 28, 2023 10:14
1. Change field source_ref to ref, tag, and commit_id.
2. Remove creation of branch without existing history

3. Remove unnecessary branch exists in acceptance test
4. Replace custom validation with suggested TestCheckResourceAttr
5. Remove Sensitive: false mistake
6. Remove unnecessary call to read in import

7. Remove is_default_branch from computed fields, since the api
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/stats/get?view=azure-devops-rest-7.1&tabs=HTTP
returns true when non-default branch is on same head commit.
Branch reference was basically a workaround that should
be unnecessary.

Also added assertions on the format of the `name` field to make
it more clear that only shortBranchName format is valid.
Co-authored-by: xuzhang3 <57888764+xuzhang3@users.noreply.github.com>
Current import forced recreate of resource if exactly one of
ref_commit_id, ref_tag, ref_branch was given. This is a bit
contradicting with the schema.

Probably we can add better import that also supports setting
ref_tag, ref_branch or ref_commit_id manually using import
expression. Something like this is also done in github_branch:

<repository_id>:<name>:<ref_(commit_id | tag | branch)>

Not sure if necessary. I'll remove import for now I
think and maybe make another issue.
@xuzhang3
Copy link
Collaborator

xuzhang3 commented Mar 3, 2023

@mvinkio LGTM 🎆

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

Successfully merging this pull request may close these issues.

2 participants