Skip to content

Commit

Permalink
Merge pull request #13 from kubescape/fix-gitlab
Browse files Browse the repository at this point in the history
fix gitlab project ID generation
  • Loading branch information
David Wertenteil authored Jan 4, 2024
2 parents 36432da + 5dd5ab2 commit 0a7f7ed
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions apis/gitlabapi/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"strings"

"github.com/kubescape/go-git-url/apis"
)
Expand All @@ -25,8 +26,12 @@ type GitLabAPI struct {

func NewGitLabAPI() *GitLabAPI { return &GitLabAPI{httpClient: &http.Client{}} }

func getProjectId(owner, repo string) string {
return strings.ReplaceAll(owner+"/"+repo, "/", "%2F")
}

func (gl *GitLabAPI) GetRepoTree(owner, repo, branch string, headers *Headers) (*Tree, error) {
id := owner + "%2F" + repo
id := getProjectId(owner, repo)

treeAPI := APIRepoTree(id, branch)
body, err := apis.HttpGet(gl.httpClient, treeAPI, headers.ToMap())
Expand All @@ -44,8 +49,8 @@ func (gl *GitLabAPI) GetRepoTree(owner, repo, branch string, headers *Headers) (
}

func (gl *GitLabAPI) GetDefaultBranchName(owner, repo string, headers *Headers) (string, error) {
id := getProjectId(owner, repo)

id := owner + "%2F" + repo
body, err := apis.HttpGet(gl.httpClient, APIMetadata(id), headers.ToMap())
if err != nil {
return "", err
Expand All @@ -67,8 +72,7 @@ func (gl *GitLabAPI) GetDefaultBranchName(owner, repo string, headers *Headers)
}

func (gl *GitLabAPI) GetLatestCommit(owner, repo, branch string, headers *Headers) (*Commit, error) {

id := owner + "%2F" + repo
id := getProjectId(owner, repo)

body, err := apis.HttpGet(gl.httpClient, APILastCommitsOfBranch(id, branch), headers.ToMap())
if err != nil {
Expand All @@ -93,7 +97,8 @@ func APIRepoTree(id, branch string) string {
// API Ref: https://docs.gitlab.com/ee/api/repository_files.html#get-raw-file-from-repository
// Example: https://gitlab.com/api/v4/projects/23383112/repository/files/app%2Findex.html/raw
func APIRaw(owner, repo, branch, path string) string {
id := owner + "%2F" + repo
id := getProjectId(owner, repo)

return fmt.Sprintf("https://%s/api/v4/projects/%s/repository/files/%s/raw", DEFAULT_HOST, id, path)
}

Expand Down

0 comments on commit 0a7f7ed

Please sign in to comment.