From 1e815cacf4687cb25bf95c11326eb1cae92c0d4c Mon Sep 17 00:00:00 2001 From: Douglas Parsons Date: Thu, 15 Sep 2022 10:04:57 +0100 Subject: [PATCH 1/2] Parse Gitlab repo slugs from the response URL rather than using Project Name Closes #76 --- client/project_get.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/client/project_get.go b/client/project_get.go index 33c534ef..ae475e29 100644 --- a/client/project_get.go +++ b/client/project_get.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "strings" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -14,6 +15,16 @@ type Repository struct { Repo string } +// getRepoNameFromURL is a helper method to extract the repo name from a gitlab URL. +// This is necessary as Gitlab doesn't return the repository slug in the API response, +// Because this information isn't present, the only way to obtain it is to parse the URL. +func getRepoNameFromURL(url string) string { + url = strings.TrimSuffix(url, ".git") + urlParts := strings.Split(url, "/") + + return urlParts[len(urlParts)-1] +} + // Repository is a helper method to convert the ProjectResponse Repository information into a more // digestible format. func (r *ProjectResponse) Repository() *Repository { @@ -29,7 +40,7 @@ func (r *ProjectResponse) Repository() *Repository { case "gitlab": return &Repository{ Type: "gitlab", - Repo: fmt.Sprintf("%s/%s", r.Link.ProjectNamespace, r.Link.ProjectName), + Repo: fmt.Sprintf("%s/%s", r.Link.ProjectNamespace, getRepoNameFromURL(r.Link.ProjectURL)), } case "bitbucket": return &Repository{ @@ -60,7 +71,7 @@ type ProjectResponse struct { Slug string `json:"slug"` // gitlab ProjectNamespace string `json:"projectNamespace"` - ProjectName string `json:"projectName"` + ProjectURL string `json:"projectUrl"` ProjectID int64 `json:"projectId,string"` } `json:"link"` Name string `json:"name"` From 39ef93915abd129849f8ffc1e0fb010f57e8d8b9 Mon Sep 17 00:00:00 2001 From: Douglas Parsons Date: Thu, 15 Sep 2022 10:21:19 +0100 Subject: [PATCH 2/2] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Florentin / 珞辰 --- client/project_get.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/project_get.go b/client/project_get.go index ae475e29..8cae3cf5 100644 --- a/client/project_get.go +++ b/client/project_get.go @@ -15,8 +15,8 @@ type Repository struct { Repo string } -// getRepoNameFromURL is a helper method to extract the repo name from a gitlab URL. -// This is necessary as Gitlab doesn't return the repository slug in the API response, +// getRepoNameFromURL is a helper method to extract the repo name from a GitLab URL. +// This is necessary as GitLab doesn't return the repository slug in the API response, // Because this information isn't present, the only way to obtain it is to parse the URL. func getRepoNameFromURL(url string) string { url = strings.TrimSuffix(url, ".git")