Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Fix error handling of repo APIs #198

Merged
merged 23 commits into from
Oct 31, 2021
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
1 change: 0 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func setGlobalLogger(debug bool) {
config.Encoding = "json"
} else {
config = zap.NewProductionConfig()
config.DisableStacktrace = true
}

logger, _ := config.Build()
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/github/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ func (g *Github) GetConfig(ctx context.Context, u *ent.User, r *ent.Repo) (*vo.C
Repositories.
GetContents(ctx, r.Namespace, r.Name, r.ConfigPath, &github.RepositoryContentGetOptions{})
if res.StatusCode == http.StatusNotFound {
return nil, e.NewError(
e.ErrorCodeConfigNotFound,
return nil, e.NewErrorWithMessage(
e.ErrorCodeNotFound,
"The configuration file is not found.",
err,
)
} else if err != nil {
Expand Down
42 changes: 9 additions & 33 deletions internal/pkg/github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,9 @@ func (g *Github) GetCommit(ctx context.Context, u *ent.User, r *ent.Repo, sha st
GetCommit(ctx, r.Namespace, r.Name, sha)
// Github returns Unprocessable entity if the commit is not found.
if res.StatusCode == http.StatusNotFound || res.StatusCode == http.StatusUnprocessableEntity {
return nil, e.NewError(
e.ErrorCodeRefNotFound,
err,
)
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The commit is not found.", err)
} else if err != nil {
return nil, e.NewError(
e.ErrorCodeInternalError,
err,
)
return nil, e.NewError(e.ErrorCodeInternalError, err)
}

return mapGithubCommitToCommit(cm), nil
Expand All @@ -97,10 +91,7 @@ func (g *Github) ListCommitStatuses(ctx context.Context, u *ent.User, r *ent.Rep
PerPage: 100,
})
if err != nil {
return nil, e.NewError(
e.ErrorCodeInternalError,
err,
)
return nil, err
}

for _, rs := range cs.Statuses {
Expand All @@ -115,10 +106,7 @@ func (g *Github) ListCommitStatuses(ctx context.Context, u *ent.User, r *ent.Rep
})
// check-runs secures the commit is exist.
if res.StatusCode == http.StatusUnprocessableEntity {
return nil, e.NewError(
e.ErrorCodeRefNotFound,
err,
)
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The commit is not found.", err)
} else if err != nil {
return nil, e.NewError(
e.ErrorCodeInternalError,
Expand All @@ -143,10 +131,7 @@ func (g *Github) ListBranches(ctx context.Context, u *ent.User, r *ent.Repo, pag
},
})
if err != nil {
return nil, e.NewError(
e.ErrorCodeInternalError,
err,
)
return nil, e.NewError(e.ErrorCodeInternalError, err)
}

branches := []*vo.Branch{}
Expand All @@ -162,15 +147,9 @@ func (g *Github) GetBranch(ctx context.Context, u *ent.User, r *ent.Repo, branch
Repositories.
GetBranch(ctx, r.Namespace, r.Name, branch)
if res.StatusCode == http.StatusNotFound {
return nil, e.NewError(
e.ErrorCodeRefNotFound,
err,
)
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The branch is not found.", err)
} else if err != nil {
return nil, e.NewError(
e.ErrorCodeInternalError,
err,
)
return nil, e.NewError(e.ErrorCodeInternalError, err)
}

return mapGithubBranchToBranch(b), nil
Expand Down Expand Up @@ -249,10 +228,7 @@ func (g *Github) GetTag(ctx context.Context, u *ent.User, r *ent.Repo, tag strin
}

if q.Repository.Refs.TotalCount == 0 {
return nil, e.NewError(
e.ErrorCodeRefNotFound,
nil,
)
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The tag is not found.", nil)
}

n := q.Repository.Refs.Nodes[0]
Expand All @@ -278,7 +254,7 @@ func (g *Github) CreateWebhook(ctx context.Context, u *ent.User, r *ent.Repo, c
Active: github.Bool(true),
})
if err != nil {
return -1, err
return -1, e.NewErrorWithMessage(e.ErrorCodeInternalError, "It has failed to create a webhook.", err)
}

return *h.ID, nil
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/github/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"

"github.com/gitploy-io/gitploy/ent"
"github.com/gitploy-io/gitploy/pkg/e"
"github.com/gitploy-io/gitploy/vo"
"github.com/google/go-github/v32/github"
)

func (g *Github) ListRemoteRepos(ctx context.Context, u *ent.User) ([]*vo.RemoteRepo, error) {
grs, err := g.listRemoteRepos(ctx, u)
if err != nil {
return nil, err
return nil, e.NewError(e.ErrorCodeInternalError, err)
}

remotes := make([]*vo.RemoteRepo, 0)
Expand Down
44 changes: 40 additions & 4 deletions internal/pkg/store/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package store

import (
"context"
"fmt"
"time"

"entgo.io/ent/dialect/sql"
"github.com/gitploy-io/gitploy/ent"
"github.com/gitploy-io/gitploy/ent/approval"
"github.com/gitploy-io/gitploy/ent/predicate"
"github.com/gitploy-io/gitploy/pkg/e"
)

func (s *Store) SearchApprovals(ctx context.Context, u *ent.User, ss []approval.Status, from time.Time, to time.Time, page, perPage int) ([]*ent.Approval, error) {
Expand Down Expand Up @@ -59,7 +61,7 @@ func (s *Store) ListApprovals(ctx context.Context, d *ent.Deployment) ([]*ent.Ap
}

func (s *Store) FindApprovalByID(ctx context.Context, id int) (*ent.Approval, error) {
return s.c.Approval.
ap, err := s.c.Approval.
Query().
Where(
approval.IDEQ(id),
Expand All @@ -71,10 +73,17 @@ func (s *Store) FindApprovalByID(ctx context.Context, id int) (*ent.Approval, er
WithUser()
}).
First(ctx)
if ent.IsNotFound(err) {
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The approval is not found.", err)
} else if err != nil {
return nil, e.NewError(e.ErrorCodeInternalError, err)
}

return ap, nil
}

func (s *Store) FindApprovalOfUser(ctx context.Context, d *ent.Deployment, u *ent.User) (*ent.Approval, error) {
return s.c.Approval.
ap, err := s.c.Approval.
Query().
Where(
approval.And(
Expand All @@ -89,21 +98,48 @@ func (s *Store) FindApprovalOfUser(ctx context.Context, d *ent.Deployment, u *en
WithUser()
}).
First(ctx)
if ent.IsNotFound(err) {
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The user's approval is not found.", err)
} else if err != nil {
return nil, e.NewError(e.ErrorCodeInternalError, err)
}

return ap, nil
}

func (s *Store) CreateApproval(ctx context.Context, a *ent.Approval) (*ent.Approval, error) {
return s.c.Approval.
ap, err := s.c.Approval.
Create().
SetUserID(a.UserID).
SetDeploymentID(a.DeploymentID).
Save(ctx)
if ent.IsValidationError(err) {
return nil, e.NewErrorWithMessage(
e.ErrorCodeUnprocessableEntity,
fmt.Sprintf("Failed to create a approval. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
err)
} else if err != nil {
return nil, e.NewError(e.ErrorCodeInternalError, err)
}

return ap, nil
}

func (s *Store) UpdateApproval(ctx context.Context, a *ent.Approval) (*ent.Approval, error) {
return s.c.Approval.
ap, err := s.c.Approval.
UpdateOne(a).
SetStatus(a.Status).
Save(ctx)
if ent.IsValidationError(err) {
return nil, e.NewErrorWithMessage(
e.ErrorCodeUnprocessableEntity,
fmt.Sprintf("Failed to update the approval. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
err)
} else if err != nil {
return nil, e.NewError(e.ErrorCodeInternalError, err)
}

return ap, nil
}

func (s *Store) DeleteApproval(ctx context.Context, a *ent.Approval) error {
Expand Down
59 changes: 59 additions & 0 deletions internal/pkg/store/approval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gitploy-io/gitploy/ent/approval"
"github.com/gitploy-io/gitploy/ent/enttest"
"github.com/gitploy-io/gitploy/ent/migrate"
"github.com/gitploy-io/gitploy/pkg/e"
)

func TestStore_SearchApprovals(t *testing.T) {
Expand Down Expand Up @@ -79,3 +80,61 @@ func TestStore_SearchApprovals(t *testing.T) {
})

}

func TestStore_UpdateApproval(t *testing.T) {
t.Run("Return unprocessable_entity error when the status is invalid.", func(t *testing.T) {
ctx := context.Background()

client := enttest.Open(t,
"sqlite3",
"file:ent?mode=memory&cache=shared&_fk=1",
enttest.WithMigrateOptions(migrate.WithForeignKeys(false)),
)
defer client.Close()

a := client.Approval.
Create().
SetUserID(1).
SetDeploymentID(1).
SaveX(ctx)

store := NewStore(client)

t.Log("Update the approval with the invalid value.")
a.Status = approval.Status("VALUE")
_, err := store.UpdateApproval(ctx, a)
if !e.HasErrorCode(err, e.ErrorCodeUnprocessableEntity) {
t.Fatalf("UpdateApproval return error = %v, wanted ErrorCodeUnprocessableEntity", err)
}
})

t.Run("Return the updated approval.", func(t *testing.T) {
ctx := context.Background()

client := enttest.Open(t,
"sqlite3",
"file:ent?mode=memory&cache=shared&_fk=1",
enttest.WithMigrateOptions(migrate.WithForeignKeys(false)),
)
defer client.Close()

a := client.Approval.
Create().
SetUserID(1).
SetDeploymentID(1).
SaveX(ctx)

store := NewStore(client)

t.Log("Update the approval ")
a.Status = approval.StatusApproved
a, err := store.UpdateApproval(ctx, a)
if err != nil {
t.Fatalf("UpdateApproval returns an error: %v", err)
}

if a.Status != approval.StatusApproved {
t.Fatalf("UpdateApproval status = %v, wanted %v", a.Status, approval.StatusApproved)
}
})
}
Loading