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 Terraform JSON support #5293
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @melendezd! I look forward to working through this PR with you.
@@ -44,6 +46,13 @@ def terraform_files | |||
map { |f| fetch_file_from_host(f.name) } | |||
end | |||
|
|||
def terraform_json_files |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, I think the code would be cleaner if we eliminated the distinction between tf and json files by removing this method and adding f.name.end_with?(".tf", ".tf.json")
on line 43. Just include the json files in the terraform_files list. I'm not sure if you attempted this approach and hit more complication that was difficult to overcome?
@@ -61,9 +65,19 @@ def updated_terraform_file_content(file) | |||
|
|||
case new_req[:source][:type] | |||
when "git" | |||
update_git_declaration(new_req, old_req, content, file.name) | |||
content = | |||
if terraform_json_file?(file.name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal of the removal of the distinction between tf and json files would be to eliminate the branches here and in registry declaration, we would need to pass the filename into update_registry_declaration to handle the file appropriately, but in general pushing these conditionals down feels better to me.
url_match.sub(old_req[:source][:ref], new_req[:source][:ref]) | ||
end | ||
end | ||
JSON.pretty_generate(tf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you mentioned in your PR comment this may reformat the json which I think we'd prefer to avoid to limit noisy PRs at the very least. I'm not a regex expert, but I think we should take a shot at building a regex to match/sub for json. This would be the final piece to prevent branching as we could just handle this new regex the same as we currently do for terragrunt/terraform in git_declaration_regex
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback! I think you got to the bottom of it. Really the only reason for the distinction between .tf and .tf.json files in the fetcher was because I foresaw having to make the distinction in the updater. I didn't initially think about the messy diffs that this approach could create -- I could give the json regex a shot.
I've updated the file updater to use regex substitutions in the JSON files. The provider declaration regex was quite difficult because the source and version can be declared in either order, but I believe my approach works as long as there aren't any references to named values (like |
@melendezd do you mind rebasing and also squashing down some of the smaller commits? We merge PR's with a merge strategy currently, not a squash strategy, so what's in the PR history is what eventually gets committed. To be clear: No need to squash to only a single commit--if there are multiple logical changes, feel free to keep them separate for easier review, but I suspect there's not 32 separate logical change sets here... 😀 Tidying this up will also make it easier to review the PR. |
gentle nudge @melendezd |
My bad, this got away from me. I'm a bit busy at the moment but will let you know when I get back on this. |
@melendezd any chance things have slowed down enough to get this across the finish line? |
179584a
to
32c5b8c
Compare
How's this look? :) |
32c5b8c
to
c550ec7
Compare
@jeffwidman - Are there any further updates necessary for this to be put on the schedule for being reviewed? |
I started to have a look:
|
c550ec7
to
87edfbb
Compare
87edfbb
to
30b4e9d
Compare
Fix #4080.
Adding support for updating Terraform JSON files with
.tf.json
file extension. Most of the tests present for.tf
files are duplicated for.tf.json
files (with some restructuring offile_updater_spec.rb
to make this easier and less repetitious) since the functionality should be essentially the same for the two formats.Currently, the modified file updater pulls the JSON into an object, modifies the object, and
JSON.pretty_generate
's it out to the file. This may change the formatting of the original JSON file (tab width, for example). I'm not sure if this is okay or if there is a better approach to avoid this issue.Additionally, I noticed that a couple tests
file_updater_spec.rb
check the content of the original file rather than the output of the file updater, creating a test that will pass no matter what the updater is doing. I'm not sure if I'm misunderstanding but I feel it's worth mentioning. Here's a snippet from main:dependabot-core/terraform/spec/dependabot/terraform/file_updater_spec.rb
Lines 810 to 830 in ca9f236
and the corresponding snippet from my PR:
https://github.com/yoyomeng2/dependabot-core/blob/f6258ad94f53360c2b39920ff6303bcfcfb03094/terraform/spec/dependabot/terraform/file_updater_spec.rb#L887-L894
Finally, I'm unsure if there's more verification that needs to be done other than the unit tests before being able to merge.
Any help or feedback is greatly appreciated. Thanks!