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 git repository file #225

Merged
merged 16 commits into from
Sep 3, 2021
Merged

Conversation

phillebaba
Copy link
Contributor

This change adds a new resource type called azuredevops_git_repository_file which allows users to add files to the repositories they have created. Behavior wise it mimic the file resource in the GitHub provider, so that people who have used it should feel like this is familiar.

The resource does not actually care about commit history but instead aims to make sure that the specified content in the specified file is present. If it is not it will update the content with a new commit.

Fixes #23

@phillebaba phillebaba changed the title Add git repository File Add git repository file Nov 19, 2020
@simongottschlag
Copy link

@xuzhang3 anything else needed here to get this PR merged and released? 👍

@xuzhang3
Copy link
Collaborator

@xuzhang3 anything else needed here to get this PR merged and released? 👍
I can help review and merge next week 😄. I need check the PR and the new feature, also the release pipeline will take at least 40min (AccTests and builds).

@simongottschlag
Copy link

I can help review and merge next week 😄. I need check the PR and the new feature, also the release pipeline will take at least 40min (AccTests and builds).

@xuzhang3 that's awesome! Thank you and have a great weekend 😁👍

@xuzhang3
Copy link
Collaborator

@phillebaba Can we add acceptances and document for this new resource?
AccTest Example
Document Example

@phillebaba
Copy link
Contributor Author

phillebaba commented Jan 12, 2021

Sorry I had to prioritize other things before christmas. I have started working on the tests now as other things rely on this change. Do you think I need to mock the API to get things working @xuzhang3?

@xuzhang3
Copy link
Collaborator

Hi @phillebaba , If you cannot create the resource or the test resource depends on the third part service then you can mock the API, otherwise you should create real test resources.
In this scenario, I would suggest create a test project and repo first then you create the repo file and commits, after all resources created the test should check that all the resource is created as expected.
Example test configuration:

resource "azuredevops_project" "project" {
  name       = "Sample Project"
  visibility         = "private"
  version_control    = "Git"
  work_item_template = "Agile"
}

resource "azuredevops_git_repository" "repo" {
  project_id = azuredevops_project.project.id
  name       = "Sample Empty Git Repository"
  initialization {
    init_type = "Clean"
  }
}

resource "azuredevops_git_repository_file" "repo_file" {
  repository_id = azuredevops_git_repository.repo.id
  file = "./test.go"
  content = "#Managed by Terraform"
  branch = ""
  commit_message = "init commit"
  overwrite_on_create = false
}

@phillebaba
Copy link
Contributor Author

@xuzhang3 thanks for the help!

I added some tests that will verify the simple CRUD flow, could you have a look?

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.

Hi @phillebaba you need add xxx.markdown file for azuredevops_git_repository_file

},
"commit_message": {
Type: schema.TypeString,
Optional: true,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would suggest change commit_message to comment. Since this is a git push comment, can we remove the Computed: true, and change Optional to Required?

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 am trying to follow the standards set forward by the GitHub Terraform provider which has a similar resource. I would prefer if we could try to make this resource as similar as possible.

Choose a reason for hiding this comment

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

@xuzhang3 and @phillebaba just wondering if you have any final decisions on whether commit_message will change to comment. The reason I ask is, we are forging ahead with a version of the provider in our environment. All other change decisions in this PR will not have a major effect on us in the future but the commit_message change could mean we may have to do a lot of Terraform state manipulation down the track if we choose to go with commit_message and the upstream chooses comment.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@russellmccloy-coles My suggestion is based on the git push api, service use comment as commit-message. I'm ok with commit_message.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In git command, it is called message git commit --message . In API, it is comment. If a simple word can represent the configuration meanings, I prefer the simple one.

Comment on lines 160 to 167
if item != nil {
if !overwriteOnCreate {
return fmt.Errorf("Refusing to overwrite existing file. Configure `overwrite_on_create` to `true` to override.")
} else {
changeType = git.VersionControlChangeTypeValues.Edit
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if item != nil {
if !overwriteOnCreate {
return fmt.Errorf("Refusing to overwrite existing file. Configure `overwrite_on_create` to `true` to override.")
} else {
changeType = git.VersionControlChangeTypeValues.Edit
}
}
if item != nil && !overwriteOnCreate {
return fmt.Errorf("Refusing to overwrite existing file. Configure `overwrite_on_create` to `true` to override.")
}
changeType = git.VersionControlChangeTypeValues.Edit

Copy link

@russellmccloy-coles russellmccloy-coles Feb 11, 2021

Choose a reason for hiding this comment

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

@xuzhang3 the logic you have suggested here is different to the original logic:

  1. if fails the test TestAccGitRepoFile_CreateAndUpdate
  2. changeType = git.VersionControlChangeTypeValues.Edit always gets run when line 161 is not run

Test result:

API server listening at: 127.0.0.1:42024
=== RUN   TestAccGitRepoFile_CreateAndUpdate
    testing.go:673: Step 0 error: errors during apply:
        
        Error: The path 'foo.txt' does not exist at commit '64fba23b7b92e632619b0758c1eeb059fa685425'
        Parameter name: newPush
        
          on /tmp/tf-test256550528/main.tf line 2:
          (source code not available)
        
        
--- FAIL: TestAccGitRepoFile_CreateAndUpdate (15.91s)
FAIL

Whereas in the original code if: item != nil is true no further code gets run

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right about this one. Idiomatic go seems to be about never using else statements so I will change this as the else statement is not required after the error is returned.

@russellmccloy-coles
Copy link

Hi @phillebaba , I'm keen to use this feature. Can I help with the PR? I'm happy to make the adjustments as per @xuzhang3 's request, and push it to another branch. Also happy to push to your branch, but I won't have permissions.

cheers
Russ

@phillebaba phillebaba force-pushed the feature/git-commit branch 4 times, most recently from cf65937 to d8d647e Compare February 11, 2021 10:56
@phillebaba
Copy link
Contributor Author

@russellmccloy-coles sorry this keeps falling to the bottom of my backlog and I end up picking it up when I have the time. Do you want contributor permissions to my fork in case I become slow at responding again? Just make sure that you have signed the CLA in that case.

@xuzhang3 the majority of the changes requested should be done now. I just need to run through the acceptance tests once more. I will try to be quick to respond from now on so that we can get this PR merged ASAP 😄

@russellmccloy-coles
Copy link

@russellmccloy-coles sorry this keeps falling to the bottom of my backlog and I end up picking it up when I have the time. Do you want contributor permissions to my fork in case I become slow at responding again? Just make sure that you have signed the CLA in that case.

@xuzhang3 the majority of the changes requested should be done now. I just need to run through the acceptance tests once more. I will try to be quick to respond from now on so that we can get this PR merged ASAP 😄

@phillebaba yes please... that would be great as we have this as our number one focus right now

@russellmccloy-coles
Copy link

@phillebaba yes please... that would be great as we have this as our number one focus right now

@phillebaba I know you are busy but any luck adding me as a contributor?

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.

@phillebaba Two changes:

  1. getCommits use branch in xxx format while the parameter in getLastCommitId is in refs/heads/xxx format
  2. If this resource support import, terraform should be able find the resource though APIs

@russellmccloy-coles
Copy link

@phillebaba just incase it helps. I have dumped my changes which we now have running in our environment into this branch on your repository:
https://github.com/phillebaba/terraform-provider-azuredevops/tree/feature/russells_code_incase_it_helps_you
Feel free to ignore, just thought i't may help.

Notes:

  • new function - waitForFilePush
  • all CRUD functions now call this new method: waitForFilePush
  • some other minor stuff

@xuzhang3
Copy link
Collaborator

xuzhang3 commented Mar 3, 2021

@xuzhang3 have you had time to look at this?

terraform import not working
image

Philip Laine and others added 4 commits September 2, 2021 13:59
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.

LGTM

@xuzhang3 xuzhang3 merged commit f851c63 into microsoft:master Sep 3, 2021
@drdamour
Copy link
Contributor

is this supposed to detect changes on the repo?
IE:
i use this to crate file X with content X with commit message X
i then modify file Y (no change to content X)
i run tf apply, and i get a change in my tf state
image

@drdamour
Copy link
Contributor

and it's pushing a noop commit...kinda makes this worthless as i happen to enjoy commiting to my repos ;)

image

@phillebaba
Copy link
Contributor Author

This has honestly been very hard to test as the git API for Azure DevOps is not really documented plus there are a couple of bugs which have turned up from some of my testing. Using a git lib like libgit2 is not really possible so the API is the only option. This is only an initial feature implementation, and if you find issues fixing those would be very helpful for everyone!

@drdamour
Copy link
Contributor

@xuzhang3 maybe this resource needs an "expiermental" status or something equivalent to that then

@phillebaba
Copy link
Contributor Author

Why not just fix the underlying bug instead?

@xuzhang3
Copy link
Collaborator

xuzhang3 commented Mar 8, 2022

@drdamour this resource only cover a minimal features like create/push etc. of the AzGit repository files. Can out be more specific what bug it is or features you want? You can create a new issue for feature requirement.

@drdamour
Copy link
Contributor

drdamour commented Mar 8, 2022

@xuzhang3 well that's what i'm asking, what's the feature it's supposed to support? becaues it's not just create, everytime i run an apply it applys a commit, that seems wrong

@xuzhang3
Copy link
Collaborator

xuzhang3 commented Mar 9, 2022

@drdamour this is a bug, this resource get the last commit message from repo and treat it as local update.

@drdamour
Copy link
Contributor

i went to open a bug, but saw you opened #557 thx

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.

Support for Git files
6 participants