Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nil pointer deref in provider code when there are network errors #403

Merged
merged 1 commit into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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