Skip to content

Commit

Permalink
fix nil pointer deref in provider code when there are network errors
Browse files Browse the repository at this point in the history
  • Loading branch information
roi-codefresh committed Jan 2, 2023
1 parent ba548ae commit 1973f56
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
6 changes: 3 additions & 3 deletions pkg/git/provider_gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/git/provider_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/git/provider_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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
}
Expand Down

0 comments on commit 1973f56

Please sign in to comment.