diff --git a/apis/gitlabapi/apis.go b/apis/gitlabapi/apis.go index a88dadd..69dd06f 100644 --- a/apis/gitlabapi/apis.go +++ b/apis/gitlabapi/apis.go @@ -10,10 +10,6 @@ import ( "github.com/kubescape/go-git-url/apis" ) -const ( - DEFAULT_HOST string = "gitlab.com" -) - type IGitLabAPI interface { GetRepoTree(owner, repo, branch string, headers *Headers) (*Tree, error) GetDefaultBranchName(owner, repo string, headers *Headers) (string, error) @@ -21,10 +17,16 @@ type IGitLabAPI interface { } type GitLabAPI struct { + host string httpClient *http.Client } -func NewGitLabAPI() *GitLabAPI { return &GitLabAPI{httpClient: &http.Client{}} } +func NewGitLabAPI(host string) *GitLabAPI { + return &GitLabAPI{ + host: host, + httpClient: &http.Client{}, + } +} func getProjectId(owner, repo string) string { return strings.ReplaceAll(owner+"/"+repo, "/", "%2F") @@ -33,7 +35,7 @@ func getProjectId(owner, repo string) string { func (gl *GitLabAPI) GetRepoTree(owner, repo, branch string, headers *Headers) (*Tree, error) { id := getProjectId(owner, repo) - treeAPI := APIRepoTree(id, branch) + treeAPI := APIRepoTree(gl.host, id, branch) body, err := apis.HttpGet(gl.httpClient, treeAPI, headers.ToMap()) if err != nil { return nil, err @@ -51,7 +53,7 @@ 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) - body, err := apis.HttpGet(gl.httpClient, APIMetadata(id), headers.ToMap()) + body, err := apis.HttpGet(gl.httpClient, APIMetadata(gl.host, id), headers.ToMap()) if err != nil { return "", err } @@ -74,7 +76,7 @@ func (gl *GitLabAPI) GetDefaultBranchName(owner, repo string, headers *Headers) func (gl *GitLabAPI) GetLatestCommit(owner, repo, branch string, headers *Headers) (*Commit, error) { id := getProjectId(owner, repo) - body, err := apis.HttpGet(gl.httpClient, APILastCommitsOfBranch(id, branch), headers.ToMap()) + body, err := apis.HttpGet(gl.httpClient, APILastCommitsOfBranch(gl.host, id, branch), headers.ToMap()) if err != nil { return nil, err } @@ -89,31 +91,31 @@ func (gl *GitLabAPI) GetLatestCommit(owner, repo, branch string, headers *Header // APIRepoTree GitLab tree api // API Ref: https://docs.gitlab.com/ee/api/repositories.html -func APIRepoTree(id, branch string) string { - return fmt.Sprintf("https://%s/api/v4/projects/%s/repository/tree?ref=%s", DEFAULT_HOST, id, branch) +func APIRepoTree(host, id, branch string) string { + return fmt.Sprintf("https://%s/api/v4/projects/%s/repository/tree?ref=%s", host, id, branch) } // APIRaw GitLab : Get raw file from repository // 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 { +func APIRaw(host, owner, repo, path string) string { id := getProjectId(owner, repo) - return fmt.Sprintf("https://%s/api/v4/projects/%s/repository/files/%s/raw", DEFAULT_HOST, id, path) + return fmt.Sprintf("https://%s/api/v4/projects/%s/repository/files/%s/raw", host, id, path) } // APIDefaultBranch GitLab : Branch metadata; list repo branches // API Ref: https://docs.gitlab.com/ee/api/branches.html#list-repository-branches // Example: https://gitlab.com/api/v4/projects/nanuchi%2Fdeveloping-with-docker/repository/branches -func APIMetadata(id string) string { - return fmt.Sprintf("https://%s/api/v4/projects/%s/repository/branches", DEFAULT_HOST, id) +func APIMetadata(host, id string) string { + return fmt.Sprintf("https://%s/api/v4/projects/%s/repository/branches", host, id) } // APILastCommits GitLab : List repository commits // API Ref: https://docs.gitlab.com/ee/api/commits.html#list-repository-commits // Example: https://gitlab.com/api/v4/projects/nanuchi%2Fdeveloping-with-docker/repository/commits -func APILastCommits(id string) string { - return fmt.Sprintf("https://%s/api/v4/projects/%s/repository/commits", DEFAULT_HOST, id) +func APILastCommits(host, id string) string { + return fmt.Sprintf("https://%s/api/v4/projects/%s/repository/commits", host, id) } // TODO: These return list of commits, and we might want just a single latest commit. Pls check with this later: @@ -121,13 +123,13 @@ func APILastCommits(id string) string { // APILastCommitsOfBranch GitLab : Last commits of specific branch // API Ref: https://docs.gitlab.com/ee/api/commits.html#list-repository-commits // Example: https://gitlab.com/api/v4/projects/nanuchi%2Fdeveloping-with-docker/repository/commits?ref_name=feature/k8s-in-hour -func APILastCommitsOfBranch(id, branch string) string { - return fmt.Sprintf("%s?ref_name=%s", APILastCommits(id), branch) +func APILastCommitsOfBranch(host, id, branch string) string { + return fmt.Sprintf("%s?ref_name=%s", APILastCommits(host, id), branch) } // APILastCommitsOfPath GitLab : Last commits of specific branch & specified path // API Ref: https://docs.gitlab.com/ee/api/commits.html#list-repository-commits // Example: https://gitlab.com/api/v4/projects/nanuchi%2Fdeveloping-with-docker/repository/commits?ref_name=master&path=app/server.js -func APILastCommitsOfPath(id, branch, path string) string { - return fmt.Sprintf("%s&path=%s", APILastCommitsOfBranch(id, branch), path) +func APILastCommitsOfPath(host, id, branch, path string) string { + return fmt.Sprintf("%s&path=%s", APILastCommitsOfBranch(host, id, branch), path) } diff --git a/gitlabparser/v1/download.go b/gitlabparser/v1/download.go index 6fe7471..c509453 100644 --- a/gitlabparser/v1/download.go +++ b/gitlabparser/v1/download.go @@ -75,7 +75,7 @@ func streamToByte(stream io.Reader) ([]byte, error) { func (gl *GitLabURL) pathsToURLs(files []string) []string { var rawURLs []string for i := range files { - rawURLs = append(rawURLs, gitlabapi.APIRaw(gl.GetOwnerName(), gl.GetRepoName(), gl.GetBranchName(), files[i])) + rawURLs = append(rawURLs, gitlabapi.APIRaw(gl.host, gl.GetOwnerName(), gl.GetRepoName(), files[i])) } return rawURLs } diff --git a/gitlabparser/v1/parser.go b/gitlabparser/v1/parser.go index 2b14990..7e74aae 100644 --- a/gitlabparser/v1/parser.go +++ b/gitlabparser/v1/parser.go @@ -11,21 +11,15 @@ import ( "github.com/kubescape/go-git-url/apis/gitlabapi" ) -// NewGitHubParser empty instance of a github parser -func NewGitLabParser() *GitLabURL { - - return &GitLabURL{ - gitLabAPI: gitlabapi.NewGitLabAPI(), +// NewGitHubParserWithURL parsed instance of a github parser +func NewGitLabParserWithURL(host, fullURL string) (*GitLabURL, error) { + gl := &GitLabURL{ + gitLabAPI: gitlabapi.NewGitLabAPI(host), token: os.Getenv("GITLAB_TOKEN"), } -} - -// NewGitHubParserWithURL parsed instance of a github parser -func NewGitLabParserWithURL(fullURL string) (*GitLabURL, error) { - gl := NewGitLabParser() if err := gl.Parse(fullURL); err != nil { - return gl, err + return nil, err } return gl, nil diff --git a/gitlabparser/v1/parser_test.go b/gitlabparser/v1/parser_test.go index d5e2e55..aa201f5 100644 --- a/gitlabparser/v1/parser_test.go +++ b/gitlabparser/v1/parser_test.go @@ -126,7 +126,7 @@ func TestNewGitHubParserWithURL(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := NewGitLabParserWithURL(tt.url) + got, err := NewGitLabParserWithURL("", tt.url) got.gitLabAPI = nil if !tt.wantErr(t, err, fmt.Sprintf("NewGitLabParserWithURL(%v)", tt.url)) { return diff --git a/init.go b/init.go index 9659a2e..945d983 100644 --- a/init.go +++ b/init.go @@ -28,7 +28,7 @@ func NewGitURL(fullURL string) (IGitURL, error) { return bitbucketparserv1.NewBitBucketParserWithURL(fullURL) } if gitlabparserv1.IsHostGitLab(hostUrl) { - return gitlabparserv1.NewGitLabParserWithURL(fullURL) + return gitlabparserv1.NewGitLabParserWithURL(hostUrl, fullURL) } return nil, fmt.Errorf("repository host '%s' not supported", hostUrl) } @@ -44,7 +44,7 @@ func NewGitAPI(fullURL string) (IGitAPI, error) { return githubparserv1.NewGitHubParserWithURL(fullURL) } if gitlabparserv1.IsHostGitLab(hostUrl) { - return gitlabparserv1.NewGitLabParserWithURL(fullURL) + return gitlabparserv1.NewGitLabParserWithURL(hostUrl, fullURL) } if azureparserv1.IsHostAzure(hostUrl) { return azureparserv1.NewAzureParserWithURL(fullURL)