From e50bf8662e7e4ca5f369fff89bebb4f994e76944 Mon Sep 17 00:00:00 2001 From: roi-codefresh Date: Mon, 2 Jan 2023 18:12:50 +0200 Subject: [PATCH] fix nil pointer deref in provider code when there are network errors (#403) Signed-off-by: roi.kramer Signed-off-by: roi.kramer --- pkg/git/provider_gitea.go | 6 +++--- pkg/git/provider_github.go | 14 +++++++------- pkg/git/provider_gitlab.go | 5 ++--- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pkg/git/provider_gitea.go b/pkg/git/provider_gitea.go index 87cd7644..fcd7f426 100644 --- a/pkg/git/provider_gitea.go +++ b/pkg/git/provider_gitea.go @@ -72,7 +72,7 @@ func (g *gitea) CreateRepository(_ context.Context, orgRepo string) (defaultBran } if err != nil { - if res.StatusCode == 404 { + if res != nil && res.StatusCode == 404 { return "", fmt.Errorf("owner %s not found: %w", opts.Owner, err) } @@ -90,7 +90,7 @@ func (g *gitea) GetDefaultBranch(_ context.Context, orgRepo string) (string, err r, res, err := g.client.GetRepo(opts.Owner, opts.Name) if err != nil { - if res.StatusCode == 404 { + if res != nil && res.StatusCode == 404 { return "", fmt.Errorf("owner %s not found: %w", opts.Owner, err) } @@ -114,7 +114,7 @@ func (g *gitea) GetAuthor(_ context.Context) (username, email string, err error) func (g *gitea) getAuthenticatedUser() (*gt.User, error) { authUser, res, err := g.client.GetMyUserInfo() if err != nil { - if res.StatusCode == 401 { + if res != nil && res.StatusCode == 401 { return nil, ErrAuthenticationFailed(err) } diff --git a/pkg/git/provider_github.go b/pkg/git/provider_github.go index 089fa7b9..b0f88095 100644 --- a/pkg/git/provider_github.go +++ b/pkg/git/provider_github.go @@ -33,13 +33,13 @@ func newGithub(opts *ProviderOptions) (Provider, error) { if err != nil { return nil, err } - + transport := &gh.BasicAuthTransport{ - Username: opts.Auth.Username, - Password: opts.Auth.Password, + Username: opts.Auth.Username, + Password: opts.Auth.Password, Transport: underlyingTransport, } - + hc.Transport = transport } @@ -83,7 +83,7 @@ func (g *github) CreateRepository(ctx context.Context, orgRepo string) (defaultB Private: gh.Bool(opts.Private), }) if err != nil { - if res.StatusCode == 404 { + if res != nil && res.StatusCode == 404 { return "", fmt.Errorf("owner %s not found: %w", opts.Owner, err) } @@ -101,7 +101,7 @@ func (g *github) GetDefaultBranch(ctx context.Context, orgRepo string) (string, r, res, err := g.Repositories.Get(ctx, opts.Owner, opts.Name) if err != nil { - if res.StatusCode == 404 { + if res != nil && res.StatusCode == 404 { return "", fmt.Errorf("owner %s not found: %w", opts.Owner, err) } @@ -137,7 +137,7 @@ func (g *github) GetAuthor(ctx context.Context) (username, email string, err err func (g *github) getAuthenticatedUser(ctx context.Context) (*gh.User, error) { authUser, res, err := g.Users.Get(ctx, "") if err != nil { - if res.StatusCode == 401 { + if res != nil && res.StatusCode == 401 { return nil, ErrAuthenticationFailed(err) } diff --git a/pkg/git/provider_gitlab.go b/pkg/git/provider_gitlab.go index 2fbf61b5..685f1231 100644 --- a/pkg/git/provider_gitlab.go +++ b/pkg/git/provider_gitlab.go @@ -106,7 +106,7 @@ func (g *gitlab) GetDefaultBranch(ctx context.Context, orgRepo string) (string, p, res, err := g.client.GetProject(orgRepo, &gl.GetProjectOptions{}) if err != nil { - if res.StatusCode == 404 { + if res != nil && res.StatusCode == 404 { return "", fmt.Errorf("owner \"%s\" not found: %w", opts.Owner, err) } @@ -138,7 +138,7 @@ func (g *gitlab) GetAuthor(_ context.Context) (username, email string, err error func (g *gitlab) getAuthenticatedUser() (*gl.User, error) { authUser, res, err := g.client.CurrentUser() if err != nil { - if res.StatusCode == 401 { + if res != nil && res.StatusCode == 401 { return nil, ErrAuthenticationFailed(err) } @@ -150,7 +150,6 @@ func (g *gitlab) getAuthenticatedUser() (*gl.User, error) { func (g *gitlab) getGroupIdByName(groupName string) (int, error) { group, _, err := g.client.GetGroup(groupName, &gl.GetGroupOptions{}) - if err != nil { return 0, err }