Skip to content

Commit

Permalink
Fixed bug in apply command (#64)
Browse files Browse the repository at this point in the history
Fixed #62 in Gitstrap.applyRepo

Bug caused the inability to create repo if it does not exist with apply command.
Several GitHub API methods return boolean responses indicated by the HTTP
status code in the response (true indicated by a 204, false indicated by a 404)

PR: #64
  • Loading branch information
orlovm authored Apr 14, 2021
1 parent b3692b8 commit a14a2d6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
10 changes: 2 additions & 8 deletions internal/github/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import (
)

func OrgExist(cli *gh.Client, ctx context.Context, name string) (bool, error) {
_, rsp, err := cli.Organizations.Get(ctx, name)
if err != nil {
return false, err
}
if rsp.StatusCode == 404 {
return false, nil
}
return true, nil
_, _, err := cli.Organizations.Get(ctx, name)
return resolveResponseByErr(err)
}
10 changes: 2 additions & 8 deletions internal/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import (
)

func RepoExist(cli *gh.Client, ctx context.Context, owner, name string) (bool, error) {
_, rsp, err := cli.Repositories.Get(ctx, owner, name)
if err != nil {
return false, err
}
if rsp.StatusCode == 404 {
return false, nil
}
return true, nil
_, _, err := cli.Repositories.Get(ctx, owner, name)
return resolveResponseByErr(err)
}
10 changes: 2 additions & 8 deletions internal/github/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import (
)

func TeamExist(cli *gh.Client, ctx context.Context, org, slug string) (bool, error) {
_, rsp, err := cli.Teams.GetTeamBySlug(ctx, org, slug)
if err != nil {
return false, err
}
if rsp.StatusCode == 404 {
return false, nil
}
return true, nil
_, _, err := cli.Teams.GetTeamBySlug(ctx, org, slug)
return resolveResponseByErr(err)
}
27 changes: 27 additions & 0 deletions internal/github/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package github

import (
"net/http"

gh "github.com/google/go-github/v33/github"
)

// resolveResponseByErr determines the result from a GitHub API response.
// In some cases it is necessary to interpret the "404" response
// as "false" condition e.g. response on get repositoty request.
// Also several GitHub API methods return boolean responses indicated by the HTTP
// status code in the response (true indicated by a 204, false indicated by a
// 404). This helper function will determine that result and hide the 404
// error if present. Any other error will be returned through as-is.
func resolveResponseByErr(err error) (bool, error) {
if err == nil {
return true, nil
}
if err, ok := err.(*gh.ErrorResponse); ok && err.Response.StatusCode == http.StatusNotFound {
// Simply false. In this one case, we do not pass the error through.
return false, nil
}

// some other real error occurred
return false, err
}

0 comments on commit a14a2d6

Please sign in to comment.