Skip to content

Commit

Permalink
use detected host in gitlab api
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
  • Loading branch information
matthyx committed Mar 15, 2024
1 parent afc1c54 commit 1ba58cb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
42 changes: 22 additions & 20 deletions apis/gitlabapi/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ 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)
GetLatestCommit(owner, repo, branch string, headers *Headers) (*Commit, error)
}

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")
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -89,45 +91,45 @@ 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:

// 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)
}
2 changes: 1 addition & 1 deletion gitlabparser/v1/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 5 additions & 11 deletions gitlabparser/v1/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gitlabparser/v1/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down

0 comments on commit 1ba58cb

Please sign in to comment.