Skip to content

Commit

Permalink
Prevent NPE in gitlab client if connection cannot be estabilished (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorhun authored May 10, 2024
1 parent a6ea449 commit fd2513f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions controllers/component_build_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,11 @@ func TestGetGitProvider(t *testing.T) {
componentRepoUrl: "12345",
expectError: true,
},
{
name: "should return error if git source URL is empty",
componentRepoUrl: "",
expectError: true,
},
}

for _, tt := range tests {
Expand Down
20 changes: 17 additions & 3 deletions pkg/git/gitlab/gitlab_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func refineGitHostingServiceError(response *http.Response, originErr error) erro
func (g *GitlabClient) getBranch(projectPath, branchName string) (*gitlab.Branch, error) {
branch, resp, err := g.client.Branches.GetBranch(projectPath, branchName)
if err != nil {
if resp == nil {
return nil, err
}
if resp.StatusCode == 404 {
return nil, nil
}
Expand All @@ -117,6 +120,9 @@ func (g *GitlabClient) getBranch(projectPath, branchName string) (*gitlab.Branch
func (g *GitlabClient) branchExist(projectPath, branchName string) (bool, error) {
_, resp, err := g.client.Branches.GetBranch(projectPath, branchName)
if err != nil {
if resp == nil {
return false, err
}
if resp.StatusCode == 404 {
return false, nil
}
Expand Down Expand Up @@ -163,7 +169,7 @@ func (g *GitlabClient) filesUpToDate(projectPath, branchName string, files []gp.
}
fileContent, resp, err := g.client.RepositoryFiles.GetRawFile(projectPath, file.FullPath, opts)
if err != nil {
if resp.StatusCode != 404 {
if resp == nil || resp.StatusCode != 404 {
return false, err
}
return false, nil
Expand All @@ -187,6 +193,9 @@ func (g *GitlabClient) filesExistInDirectory(projectPath, branchName, directoryP
}
dirContent, resp, err := g.client.Repositories.ListTree(projectPath, opts)
if err != nil {
if resp == nil {
return nil, err
}
if resp.StatusCode == 404 {
return existingFiles, nil
}
Expand Down Expand Up @@ -216,7 +225,7 @@ func (g *GitlabClient) commitFilesIntoBranch(projectPath, branchName, commitMess
opts := &gitlab.GetRawFileOptions{Ref: &branchName}
_, resp, err := g.client.RepositoryFiles.GetRawFile(projectPath, file.FullPath, opts)
if err != nil {
if resp.StatusCode != 404 {
if resp == nil || resp.StatusCode != 404 {
return err
}
fileAction = gitlab.FileCreate
Expand Down Expand Up @@ -351,6 +360,9 @@ func (g *GitlabClient) updatePaCWebhook(projectPath string, webhookId int, webho

func (g *GitlabClient) deleteWebhook(projectPath string, webhookId int) error {
resp, err := g.client.Projects.DeleteProjectHook(projectPath, webhookId)
if resp == nil {
return err
}
if resp.StatusCode == 404 {
return nil
}
Expand All @@ -374,10 +386,12 @@ func getPaCWebhookOpts(webhookTargetUrl, webhookSecret string) *gitlab.AddProjec
}
}

// IsRepositoryPublic returns true if the repository could be accessed without authentication
func (g *GitlabClient) getProjectInfo(projectPath string) (*gitlab.Project, error) {
project, resp, err := g.client.Projects.GetProject(projectPath, &gitlab.GetProjectOptions{})
if err != nil {
if resp == nil {
return nil, err
}
if resp.StatusCode == 404 {
return nil, nil
}
Expand Down

0 comments on commit fd2513f

Please sign in to comment.