Skip to content

Commit

Permalink
Merge pull request #66 from GilbertCherrie/fix_template_name
Browse files Browse the repository at this point in the history
Fix terraform template name
  • Loading branch information
agrare committed Jul 11, 2024
2 parents 612ade6 + ae80102 commit 9bd7292
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ def sync
# eg.
# https://github.ibm.com/manoj-puthran/sample-scripts/tree/v2.0/terraform/templates/hello-world
# is converted as
# "hello-world(v2.0):github.ibm.com/manoj-puthran/sample-scripts/terraform/templates"
def self.template_name_from_git_repo_url(git_repo_url, branch_name, relative_path)
# templates/hello-world
def self.template_name_from_git_repo_url(git_repo_url, relative_path)
temp_url = git_repo_url
# URI library cannot handle git urls, so just convert it to a standard url.
temp_url = temp_url.sub(':', '/').sub('git@', 'https://') if temp_url.start_with?('git@')
temp_uri = URI.parse(temp_url)
hostname = temp_uri.hostname
path = temp_uri.path
path = path[0...-4] if path.end_with?('.git')
path = path[0...-5] if path.end_with?('.git/')
Expand All @@ -64,7 +63,7 @@ def self.template_name_from_git_repo_url(git_repo_url, branch_name, relative_pat
basename = File.basename(relative_path)
parent_path = File.dirname(relative_path)
end
"#{basename}(#{branch_name}):#{hostname}#{path}/#{parent_path}"
"#{parent_path}/#{basename}"
end

private
Expand All @@ -87,7 +86,7 @@ def find_templates_in_git_repo
.select { |_dir, files| files.any? { |f| f.end_with?(".tf", ".tf.json") } }
.transform_values! { |files| files.map { |f| File.basename(f) } }
.each do |parent_dir, files|
name = self.class.template_name_from_git_repo_url(git_repository.url, scm_branch, parent_dir)
name = self.class.template_name_from_git_repo_url(git_repository.url, parent_dir)

# TODO: add parsing for input/output vars
input_vars = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def files_in_repository(git_repo_dir)
names = names_and_payloads.collect(&:first)
payloads = names_and_payloads.collect(&:second)

expect(names.first).to(eq("hello_world_local(master):#{local_repo}/"))
expect(names.first).to(eq("/hello_world_local"))

expected_hash = {
"relative_path" => File.dirname(*repo_dir_structure),
Expand Down Expand Up @@ -114,7 +114,7 @@ def files_in_repository(git_repo_dir)
names = names_and_payloads.collect(&:first)
payloads = names_and_payloads.collect(&:second)

expect(names.first).to(eq("hello-world(master):#{nested_repo}/templates"))
expect(names.first).to(eq("templates/hello-world"))

expected_hash = {
"relative_path" => File.dirname(*nested_repo_structure),
Expand Down Expand Up @@ -167,8 +167,8 @@ def files_in_repository(git_repo_dir)
expect(names).to(
eq(
[
"hello-world(master):#{multiple_templates_repo}/templates",
"single-vm(master):#{multiple_templates_repo}/templates"
"templates/hello-world",
"templates/single-vm"
]
)
)
Expand Down Expand Up @@ -213,7 +213,7 @@ def files_in_repository(git_repo_dir)
names = names_and_payloads.collect(&:first)
payloads = names_and_payloads.collect(&:second)

expect(names.first).to(eq("hello-world(master):#{nested_repo}/templates"))
expect(names.first).to(eq("templates/hello-world"))

files = JSON.parse(payloads.first)["files"]

Expand Down Expand Up @@ -241,15 +241,15 @@ def files_in_repository(git_repo_dir)
record = build_record

names = record.configuration_script_payloads.pluck(:name)
expect(names).to(match_array(["hello-world(master):#{nested_repo}/templates"]))
expect(names).to(match_array(["templates/hello-world"]))
end
end
end

describe "#template_name_from_git_repo_url" do
let(:git_url_branch_path) { ["git@example.com:manoj-puthran/sample-scripts.git", "v2.0", "terraform/templates/hello-world"] }
let(:https_url_branch_path) { ["https://example.com/manoj-puthran/sample-scripts.git", "v2.0", "terraform/templates/hello-world"] }
let(:expected_result) { "hello-world(v2.0):example.com/manoj-puthran/sample-scripts/terraform/templates" }
let(:git_url_branch_path) { ["git@example.com:manoj-puthran/sample-scripts.git", "terraform/templates/hello-world"] }
let(:https_url_branch_path) { ["https://example.com/manoj-puthran/sample-scripts.git", "terraform/templates/hello-world"] }
let(:expected_result) { "terraform/templates/hello-world" }

it "supports https urls" do
expect(described_class.template_name_from_git_repo_url(*https_url_branch_path)).to(eq(expected_result))
Expand Down Expand Up @@ -279,7 +279,7 @@ def files_in_repository(git_repo_dir)
names = names_and_payloads.collect(&:first)
payloads = names_and_payloads.collect(&:second)

expect(names.first).to(eq("hello_world_local(other_branch):#{local_repo}/"))
expect(names.first).to(eq("/hello_world_local"))

expected_hash = {
"relative_path" => File.dirname(*repo_dir_structure),
Expand Down

0 comments on commit 9bd7292

Please sign in to comment.