diff --git a/client/project_get.go b/client/project_get.go index 33c534ef..8cae3cf5 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"`