Skip to content

Commit

Permalink
feat: Add the ".git" suffix to the repo URL if missing (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephdt12 authored Apr 1, 2024
1 parent a9858a9 commit c4e0098
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions modules/im_cloudbuild_workspace/cb.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ locals {
location = var.location
deployment_id = var.deployment_id
service_account = local.im_sa
source_repo = var.im_deployment_repo_uri
source_repo = local.repoURL
source_repo_dir = var.im_deployment_repo_dir
tf_vars = var.im_tf_variables
})
Expand All @@ -45,7 +45,7 @@ locals {
"apply",
"projects/${var.project_id}/locations/${var.location}/deployments/${var.deployment_id}",
"--service-account=${local.im_sa}",
"--git-source-repo=${var.im_deployment_repo_uri}",
"--git-source-repo=${local.repoURL}",
var.im_deployment_repo_dir != "" ? "--git-source-directory=${var.im_deployment_repo_dir}" : "",
var.im_deployment_ref != "" ? "--git-source-ref=${var.im_deployment_ref}" : "",
var.im_tf_variables != "" ? "--input-values=${var.im_tf_variables}" : "",
Expand All @@ -66,7 +66,7 @@ resource "google_cloudbuild_trigger" "triggers" {
project = var.project_id
location = var.trigger_location
name = substr("im-${each.key}-${random_id.resources_random_id.dec}-${local.default_prefix}", 0, 64)
description = "${title(each.key)} Terraform configs for ${var.im_deployment_repo_uri} ${var.im_deployment_repo_dir}"
description = "${title(each.key)} Terraform configs for ${local.repoURL} ${var.im_deployment_repo_dir}"
include_build_logs = local.is_gh_repo ? "INCLUDE_BUILD_LOGS_WITH_STATUS" : null

repository_event_config {
Expand Down
2 changes: 1 addition & 1 deletion modules/im_cloudbuild_workspace/github.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
locals {
# GitHub repo url of form "github.com/owner/name"
is_gh_repo = var.tf_repo_type == "GITHUB"
gh_repo_url_split = local.is_gh_repo ? split("/", local.url) : []
gh_repo_url_split = local.is_gh_repo ? split("/", local.repoURLWithoutSuffix) : []
gh_name = local.is_gh_repo ? local.gh_repo_url_split[length(local.gh_repo_url_split) - 1] : ""

create_github_secret = local.is_gh_repo && var.github_personal_access_token != ""
Expand Down
2 changes: 1 addition & 1 deletion modules/im_cloudbuild_workspace/gitlab.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
locals {
# GitLab repo url of form "[host_uri]/[owners]/project"
is_gl_repo = var.tf_repo_type == "GITLAB"
gl_repo_url_split = local.is_gl_repo ? split("/", local.url) : []
gl_repo_url_split = local.is_gl_repo ? split("/", local.repoURLWithoutSuffix) : []
gl_project = local.is_gl_repo ? local.gl_repo_url_split[length(local.gl_repo_url_split) - 1] : ""

create_api_secret = local.is_gl_repo && var.gitlab_api_access_token != ""
Expand Down
7 changes: 4 additions & 3 deletions modules/im_cloudbuild_workspace/repo.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

locals {
# Remove ".git" suffix if it's included
url = trimsuffix(var.im_deployment_repo_uri, ".git")
repoURL = endswith(var.im_deployment_repo_uri, ".git") ? var.im_deployment_repo_uri : "${var.im_deployment_repo_uri}.git"
// Remove the ".git" suffix for parsing the url for owner/repo
repoURLWithoutSuffix = trimsuffix(local.repoURL, ".git")

repo = local.is_gh_repo ? local.gh_name : local.gl_project
default_prefix = local.repo
Expand Down Expand Up @@ -72,5 +73,5 @@ resource "google_cloudbuildv2_repository" "repository_connection" {
location = var.location
name = local.repo_connection_name
parent_connection = google_cloudbuildv2_connection.vcs_connection.name
remote_uri = var.im_deployment_repo_uri
remote_uri = local.repoURL
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ func TestIMCloudBuildWorkspaceGitHub(t *testing.T) {
client.AddFileToRepository(ctx, utils.GetFileContents(t, "files/main.tf"))
}

// Testing the module's feature of appending the ".git" suffix if it's missing
repoURL := strings.TrimSuffix(client.repository.GetCloneURL(), ".git")
vars := map[string]interface{}{
"im_github_pat": githubPAT,
"repository_url": client.repository.GetCloneURL(),
"repository_url": repoURL,
}
bpt := tft.NewTFBlueprintTest(t, tft.WithVars(vars))

Expand Down

0 comments on commit c4e0098

Please sign in to comment.