aboutsummaryrefslogtreecommitdiffstats
path: root/graphql/resolvers/mutation.go
diff options
context:
space:
mode:
authorLuke Granger-Brown <git@lukegb.com>2020-06-18 19:31:28 +0100
committerMichael Muré <batolettre@gmail.com>2020-06-27 22:58:22 +0200
commit766aff2b2f9db339d7c42321fe6cd37309631be3 (patch)
tree32f8f2f58e58ec7b13ee93af0374038384d5ed50 /graphql/resolvers/mutation.go
parent8a38897f79876b9372715150795b9da570af5a86 (diff)
downloadgit-bug-766aff2b2f9db339d7c42321fe6cd37309631be3.tar.gz
Change graphql Go handlers to pluck identity out of context instead.
Diffstat (limited to 'graphql/resolvers/mutation.go')
-rw-r--r--graphql/resolvers/mutation.go107
1 files changed, 64 insertions, 43 deletions
diff --git a/graphql/resolvers/mutation.go b/graphql/resolvers/mutation.go
index 80d6fb1a..62b92aaa 100644
--- a/graphql/resolvers/mutation.go
+++ b/graphql/resolvers/mutation.go
@@ -2,36 +2,17 @@ package resolvers
import (
"context"
+ "errors"
+ "time"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/graphql/graph"
"github.com/MichaelMure/git-bug/graphql/models"
- "github.com/vektah/gqlparser/gqlerror"
+ "github.com/MichaelMure/git-bug/identity"
)
-var _ graph.MutationResolver = &readonlyMutationResolver{}
-
-type readonlyMutationResolver struct{}
-
-func (readonlyMutationResolver) NewBug(_ context.Context, _ models.NewBugInput) (*models.NewBugPayload, error) {
- return nil, gqlerror.Errorf("readonly mode")
-}
-func (readonlyMutationResolver) AddComment(_ context.Context, input models.AddCommentInput) (*models.AddCommentPayload, error) {
- return nil, gqlerror.Errorf("readonly mode")
-}
-func (readonlyMutationResolver) ChangeLabels(_ context.Context, input *models.ChangeLabelInput) (*models.ChangeLabelPayload, error) {
- return nil, gqlerror.Errorf("readonly mode")
-}
-func (readonlyMutationResolver) OpenBug(_ context.Context, input models.OpenBugInput) (*models.OpenBugPayload, error) {
- return nil, gqlerror.Errorf("readonly mode")
-}
-func (readonlyMutationResolver) CloseBug(_ context.Context, input models.CloseBugInput) (*models.CloseBugPayload, error) {
- return nil, gqlerror.Errorf("readonly mode")
-}
-func (readonlyMutationResolver) SetTitle(_ context.Context, input models.SetTitleInput) (*models.SetTitlePayload, error) {
- return nil, gqlerror.Errorf("readonly mode")
-}
+var ErrNotAuthenticated = errors.New("not authenticated or read-only")
var _ graph.MutationResolver = &mutationResolver{}
@@ -47,22 +28,32 @@ func (r mutationResolver) getRepo(ref *string) (*cache.RepoCache, error) {
return r.cache.DefaultRepo()
}
-func (r mutationResolver) getBug(repoRef *string, bugPrefix string) (*cache.BugCache, error) {
+func (r mutationResolver) getBug(repoRef *string, bugPrefix string) (*cache.RepoCache, *cache.BugCache, error) {
repo, err := r.getRepo(repoRef)
if err != nil {
- return nil, err
+ return nil, nil, err
}
- return repo.ResolveBugPrefix(bugPrefix)
+ bug, err := repo.ResolveBugPrefix(bugPrefix)
+ if err != nil {
+ return nil, nil, err
+ }
+ return repo, bug, nil
}
-func (r mutationResolver) NewBug(_ context.Context, input models.NewBugInput) (*models.NewBugPayload, error) {
+func (r mutationResolver) NewBug(ctx context.Context, input models.NewBugInput) (*models.NewBugPayload, error) {
repo, err := r.getRepo(input.RepoRef)
if err != nil {
return nil, err
}
- b, op, err := repo.NewBugWithFiles(input.Title, input.Message, input.Files)
+ id := identity.ForContext(ctx, repo)
+ if id == nil {
+ return nil, ErrNotAuthenticated
+ }
+ author := cache.NewIdentityCache(repo, id)
+
+ b, op, err := repo.NewBugRaw(author, time.Now().Unix(), input.Title, input.Message, input.Files, nil)
if err != nil {
return nil, err
}
@@ -74,13 +65,19 @@ func (r mutationResolver) NewBug(_ context.Context, input models.NewBugInput) (*
}, nil
}
-func (r mutationResolver) AddComment(_ context.Context, input models.AddCommentInput) (*models.AddCommentPayload, error) {
- b, err := r.getBug(input.RepoRef, input.Prefix)
+func (r mutationResolver) AddComment(ctx context.Context, input models.AddCommentInput) (*models.AddCommentPayload, error) {
+ repo, b, err := r.getBug(input.RepoRef, input.Prefix)
if err != nil {
return nil, err
}
- op, err := b.AddCommentWithFiles(input.Message, input.Files)
+ id := identity.ForContext(ctx, repo)
+ if id == nil {
+ return nil, ErrNotAuthenticated
+ }
+ author := cache.NewIdentityCache(repo, id)
+
+ op, err := b.AddCommentRaw(author, time.Now().Unix(), input.Message, input.Files, nil)
if err != nil {
return nil, err
}
@@ -97,13 +94,19 @@ func (r mutationResolver) AddComment(_ context.Context, input models.AddCommentI
}, nil
}
-func (r mutationResolver) ChangeLabels(_ context.Context, input *models.ChangeLabelInput) (*models.ChangeLabelPayload, error) {
- b, err := r.getBug(input.RepoRef, input.Prefix)
+func (r mutationResolver) ChangeLabels(ctx context.Context, input *models.ChangeLabelInput) (*models.ChangeLabelPayload, error) {
+ repo, b, err := r.getBug(input.RepoRef, input.Prefix)
if err != nil {
return nil, err
}
- results, op, err := b.ChangeLabels(input.Added, input.Removed)
+ id := identity.ForContext(ctx, repo)
+ if id == nil {
+ return nil, ErrNotAuthenticated
+ }
+ author := cache.NewIdentityCache(repo, id)
+
+ results, op, err := b.ChangeLabelsRaw(author, time.Now().Unix(), input.Added, input.Removed, nil)
if err != nil {
return nil, err
}
@@ -126,13 +129,19 @@ func (r mutationResolver) ChangeLabels(_ context.Context, input *models.ChangeLa
}, nil
}
-func (r mutationResolver) OpenBug(_ context.Context, input models.OpenBugInput) (*models.OpenBugPayload, error) {
- b, err := r.getBug(input.RepoRef, input.Prefix)
+func (r mutationResolver) OpenBug(ctx context.Context, input models.OpenBugInput) (*models.OpenBugPayload, error) {
+ repo, b, err := r.getBug(input.RepoRef, input.Prefix)
if err != nil {
return nil, err
}
- op, err := b.Open()
+ id := identity.ForContext(ctx, repo)
+ if id == nil {
+ return nil, ErrNotAuthenticated
+ }
+ author := cache.NewIdentityCache(repo, id)
+
+ op, err := b.OpenRaw(author, time.Now().Unix(), nil)
if err != nil {
return nil, err
}
@@ -149,13 +158,19 @@ func (r mutationResolver) OpenBug(_ context.Context, input models.OpenBugInput)
}, nil
}
-func (r mutationResolver) CloseBug(_ context.Context, input models.CloseBugInput) (*models.CloseBugPayload, error) {
- b, err := r.getBug(input.RepoRef, input.Prefix)
+func (r mutationResolver) CloseBug(ctx context.Context, input models.CloseBugInput) (*models.CloseBugPayload, error) {
+ repo, b, err := r.getBug(input.RepoRef, input.Prefix)
if err != nil {
return nil, err
}
- op, err := b.Close()
+ id := identity.ForContext(ctx, repo)
+ if id == nil {
+ return nil, ErrNotAuthenticated
+ }
+ author := cache.NewIdentityCache(repo, id)
+
+ op, err := b.CloseRaw(author, time.Now().Unix(), nil)
if err != nil {
return nil, err
}
@@ -172,13 +187,19 @@ func (r mutationResolver) CloseBug(_ context.Context, input models.CloseBugInput
}, nil
}
-func (r mutationResolver) SetTitle(_ context.Context, input models.SetTitleInput) (*models.SetTitlePayload, error) {
- b, err := r.getBug(input.RepoRef, input.Prefix)
+func (r mutationResolver) SetTitle(ctx context.Context, input models.SetTitleInput) (*models.SetTitlePayload, error) {
+ repo, b, err := r.getBug(input.RepoRef, input.Prefix)
if err != nil {
return nil, err
}
- op, err := b.SetTitle(input.Title)
+ id := identity.ForContext(ctx, repo)
+ if id == nil {
+ return nil, ErrNotAuthenticated
+ }
+ author := cache.NewIdentityCache(repo, id)
+
+ op, err := b.SetTitleRaw(author, time.Now().Unix(), input.Title, nil)
if err != nil {
return nil, err
}