Skip to content

Commit

Permalink
normal errors this time
Browse files Browse the repository at this point in the history
  • Loading branch information
walteh committed Nov 20, 2023
1 parent 083c99c commit 91b399e
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 147 deletions.
5 changes: 0 additions & 5 deletions calculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/rs/zerolog"
"github.com/walteh/terrors"
"golang.org/x/mod/semver"
)

Expand All @@ -20,10 +19,6 @@ type Calculation struct {
Skip bool
}

var (
ErrValidatingCalculation = terrors.New("ErrValidatingCalculation")
)

type CalculationOutput struct {
BaseTags []string
HeadTags []string
Expand Down
1 change: 0 additions & 1 deletion cmd/simver_github_actions/provider.go

This file was deleted.

6 changes: 2 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package simver

import (
"github.com/walteh/terrors"
)
import "errors"

var (
Err = terrors.New("simver.Err")
Err = errors.New("simver.Err")
)
21 changes: 8 additions & 13 deletions gitexec/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ package gitexec

import (
"context"
"errors"
"os"
"strconv"
"strings"

"github.com/go-faster/errors"
"github.com/walteh/simver"
"github.com/walteh/terrors"
)

var (
Err = terrors.New("ErrExecGitHubActions")
)

func BuildGitHubActionsProviders() (simver.GitProvider, simver.TagProvider, simver.TagWriter, simver.PRProvider, simver.PRResolver, error) {
Expand Down Expand Up @@ -48,17 +43,17 @@ func BuildGitHubActionsProviders() (simver.GitProvider, simver.TagProvider, simv

git, err := NewGitProvider(c)
if err != nil {
return nil, nil, nil, nil, nil, Err.Trace(err, "error creating git provider")
return nil, nil, nil, nil, nil, errors.Wrap(err, "error creating git provider")
}

gh, err := NewGHProvider(pr)
if err != nil {
return nil, nil, nil, nil, nil, Err.Trace(err, "error creating gh provider")
return nil, nil, nil, nil, nil, errors.Wrap(err, "error creating gh provider")
}

gha, err := WrapGitProviderInGithubActions(git)
if err != nil {
return nil, nil, nil, nil, nil, Err.Trace(err, "error creating gh provider")
return nil, nil, nil, nil, nil, errors.Wrap(err, "error creating gh provider")
}

return gha, git, git, gh, &PullRequestResolver{gh, git}, nil
Expand All @@ -81,12 +76,12 @@ func (p *PullRequestResolver) CurrentPR(ctx context.Context) (*simver.PRDetails,

n, err := strconv.Atoi(num)
if err != nil {
return nil, Err.Trace(err, "error converting PR number to int")
return nil, errors.Wrap(err, "error converting PR number to int")
}

pr, exists, err := p.gh.PRDetailsByPRNumber(ctx, n)
if err != nil {
return nil, Err.Trace(err, "error getting PR details by PR number")
return nil, errors.Wrap(err, "error getting PR details by PR number")
}

if !exists {
Expand All @@ -106,7 +101,7 @@ func (p *PullRequestResolver) CurrentPR(ctx context.Context) (*simver.PRDetails,

pr, exists, err := p.gh.PRDetailsByCommit(ctx, sha)
if err != nil {
return nil, Err.Trace(err, "error getting PR details by commit")
return nil, errors.Wrap(err, "error getting PR details by commit")
}

if exists {
Expand All @@ -122,7 +117,7 @@ func (p *PullRequestResolver) CurrentPR(ctx context.Context) (*simver.PRDetails,
// get the parent commit
parent, err := p.git.CommitFromRef(ctx, "HEAD^")
if err != nil {
return nil, Err.Trace(err, "error getting parent commit")
return nil, errors.Wrap(err, "error getting parent commit")
}

return simver.NewPushSimulatedPRDetails(parent, sha, branch), nil
Expand Down
33 changes: 17 additions & 16 deletions gitexec/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"os"
"os/exec"

"github.com/go-faster/errors"
"github.com/rs/zerolog"
"github.com/walteh/simver"
)

var _ simver.PRProvider = (*ghProvider)(nil)

var (
ErrExecGH = simver.Err.Child("ErrExecGH")
ErrExecGH = errors.New("simver.ErrExecGH")
)

type ghProvider struct {
Expand All @@ -35,11 +36,11 @@ type GHProvierOpts struct {

func NewGHProvider(opts *GHProvierOpts) (simver.PRProvider, error) {
if opts.GitHubToken == "" {
return nil, ErrExecGH.Trace("GitHub token is required")
return nil, errors.Wrap(ErrExecGH, "GitHub token is required")
}

if opts.RepoPath == "" {
return nil, ErrExecGH.Trace("Repo path is required")
return nil, errors.Wrap(ErrExecGH, "Repo path is required")
}

if opts.GHExecutable == "" {
Expand All @@ -49,15 +50,15 @@ func NewGHProvider(opts *GHProvierOpts) (simver.PRProvider, error) {
// check if gh is in PATH
_, err := exec.LookPath("gh")
if err != nil {
return nil, ErrExecGH.Trace("gh executable is required")
return nil, errors.Wrap(ErrExecGH, "gh executable is required")
}

if opts.Org == "" {
return nil, ErrExecGH.Trace("org is required")
return nil, errors.Wrap(ErrExecGH, "org is required")
}

if opts.Repo == "" {
return nil, ErrExecGH.Trace("repo is required")
return nil, errors.Wrap(ErrExecGH, "repo is required")
}

return &ghProvider{
Expand Down Expand Up @@ -165,7 +166,7 @@ func (p *ghProvider) PRDetailsByPRNumber(ctx context.Context, prnum int) (*simve
cmd := p.gh(ctx, "pr", "view", fmt.Sprintf("%d", prnum), "--json", githubPRDetailsCliQuery)
out, err := cmd.Output()
if err != nil {
return nil, false, ErrExecGH.Trace(err)
return nil, false, errors.Wrap(err, "gh pr view")
}

byt := append([]byte("["), out...)
Expand All @@ -183,7 +184,7 @@ func (p *ghProvider) PRDetailsByBranch(ctx context.Context, branch string) (*sim
cmd := p.gh(ctx, "pr", "list", "--state", "all", "--head", branch, "--json", githubPRDetailsCliQuery)
out, err := cmd.Output()
if err != nil {
return nil, false, ErrExecGH.Trace(err)
return nil, false, errors.Wrap(err, "gh pr list")
}

return p.getRelevantPR(ctx, out)
Expand All @@ -199,13 +200,13 @@ func (p *ghProvider) getBaseCommit(ctx context.Context, dets *simver.PRDetails)
}

if cmt == "" {
return "", ErrExecGH.Trace("no commit to get base commit from")
return "", errors.Wrap(ErrExecGH, "no commit to get base commit from")
}

cmd := p.gh(ctx, "api", "-H", "Accept: application/vnd.github+json", fmt.Sprintf("/repos/%s/%s/git/commits/%s", p.Org, p.Repo, cmt))
out, err := cmd.Output()
if err != nil {
return "", ErrExecGH.Trace(err)
return "", errors.Wrap(err, "gh api")
}

var dat struct {
Expand All @@ -216,11 +217,11 @@ func (p *ghProvider) getBaseCommit(ctx context.Context, dets *simver.PRDetails)

err = json.Unmarshal(out, &dat)
if err != nil {
return "", ErrExecGH.Trace(err)
return "", errors.Wrap(err, "json unmarshal")
}

if len(dat.Parents) < 1 {
return "", ErrExecGH.Trace("no parents found")
return "", errors.Wrap(ErrExecGH, "no parents found")
}

return dat.Parents[0].Sha, nil
Expand All @@ -232,7 +233,7 @@ func (p *ghProvider) getRootCommit(ctx context.Context) (string, error) {
cmd := p.gh(ctx, "api", "-H", "Accept: application/vnd.github+json", fmt.Sprintf("/repos/%s/%s/git/ref/heads/main", p.Org, p.Repo))
out, err := cmd.Output()
if err != nil {
return "", ErrExecGH.Trace(err)
return "", errors.Wrap(err, "gh api")
}

var dat struct {
Expand All @@ -243,11 +244,11 @@ func (p *ghProvider) getRootCommit(ctx context.Context) (string, error) {

err = json.Unmarshal(out, &dat)
if err != nil {
return "", ErrExecGH.Trace(err)
return "", errors.Wrap(err, "json unmarshal")
}

if dat.Object.Sha == "" {
return "", ErrExecGH.Trace("no sha found")
return "", errors.Wrap(ErrExecGH, "no sha found")
}

return dat.Object.Sha, nil
Expand All @@ -264,7 +265,7 @@ func (p *ghProvider) PRDetailsByCommit(ctx context.Context, commitHash string) (
cmd := p.gh(ctx, "pr", "list", "--search", commitHash, "--state", "all", "--json", githubPRDetailsCliQuery)
out, err := cmd.Output()
if err != nil {
return nil, false, ErrExecGH.Trace(err)
return nil, false, errors.Wrap(err, "gh pr list")
}

return p.getRelevantPR(ctx, out)
Expand Down
29 changes: 15 additions & 14 deletions gitexec/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"os/exec"
"strings"

"github.com/go-faster/errors"
"github.com/rs/zerolog"
"github.com/walteh/simver"
)

var (
ErrExecGit = simver.Err.Child("ErrExecGit")
ErrExecGit = errors.New("simver.ErrExecGit")
)

var _ simver.GitProvider = (*gitProvider)(nil)
Expand Down Expand Up @@ -70,41 +71,41 @@ func (p *gitProvider) RepoName(_ context.Context) (string, string, error) {

func NewGitProvider(opts *GitProviderOpts) (*gitProvider, error) {
if opts.RepoPath == "" {
return nil, ErrExecGit.Trace("repo path is required")
return nil, errors.Wrap(ErrExecGit, "repo path is required")
}

if opts.Token == "" {
return nil, ErrExecGit.Trace("token is required")
if !opts.ReadOnly && opts.Token == "" {
return nil, errors.Wrap(ErrExecGit, "token is required for read/write")
}

if opts.User == "" {
return nil, ErrExecGit.Trace("user is required")
return nil, errors.Wrap(ErrExecGit, "user is required")
}

if opts.Email == "" {
return nil, ErrExecGit.Trace("email is required")
return nil, errors.Wrap(ErrExecGit, "email is required")
}

if opts.TokenEnvName == "" {
return nil, ErrExecGit.Trace("token env name is required")
return nil, errors.Wrap(ErrExecGit, "token env name is required")
}

if opts.GitExecutable == "" {
opts.GitExecutable = "git"
}

if opts.Org == "" {
return nil, ErrExecGit.Trace("org is required")
return nil, errors.Wrap(ErrExecGit, "org is required")
}

if opts.Repo == "" {
return nil, ErrExecGit.Trace("repo is required")
return nil, errors.Wrap(ErrExecGit, "repo is required")
}

// check if git is in PATH
_, err := exec.LookPath("git")
if err != nil {
return nil, ErrExecGit.Trace("git executable is required")
return nil, errors.Wrap(ErrExecGit, "git executable is required")
}

return &gitProvider{
Expand Down Expand Up @@ -151,7 +152,7 @@ func (p *gitProvider) CommitFromRef(ctx context.Context, str string) (string, er
cmd := p.git(ctx, "rev-parse", str)
out, err := cmd.Output()
if err != nil {
return "", ErrExecGit.Trace(err)
return "", errors.Wrap(err, "git rev-parse "+str)
}

res := strings.TrimSpace(string(out))
Expand All @@ -168,7 +169,7 @@ func (p *gitProvider) Branch(ctx context.Context) (string, error) {
cmd := p.git(ctx, "branch", "--contains", "HEAD")
out, err := cmd.Output()
if err != nil {
return "", ErrExecGit.Trace(err)
return "", errors.Wrap(err, "git branch --contains HEAD")
}
lines := strings.Split(string(out), "\n")
res := ""
Expand All @@ -180,7 +181,7 @@ func (p *gitProvider) Branch(ctx context.Context) (string, error) {
}

if res == "" {
return "", ErrExecGit.Trace()
return "", errors.Wrap(ErrExecGit, "could not find current branch")
}

zerolog.Ctx(ctx).Debug().Str("branch", res).Msg("got branch")
Expand All @@ -196,7 +197,7 @@ func (p *gitProvider) GetHeadRef(ctx context.Context) (string, error) {
cmd := p.git(ctx, "rev-parse", "HEAD")
out, err := cmd.Output()
if err != nil {
return "", ErrExecGit.Trace(err)
return "", errors.Wrap(err, "git rev-parse HEAD")
}

res := strings.TrimSpace(string(out))
Expand Down
Loading

0 comments on commit 91b399e

Please sign in to comment.