这是indexloc提供的服务,不要输入任何密码
Skip to content

Parse Gitlab repo slugs from the response URL rather than using Project #77

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

Merged
merged 2 commits into from
Sep 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions client/project_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strings"

"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand All @@ -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 {
Expand All @@ -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{
Expand Down Expand Up @@ -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"`
Expand Down