2018-07-30 02:36:41 +03:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-18 21:31:28 +03:00
|
|
|
"time"
|
2018-08-01 22:57:12 +03:00
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
"github.com/MichaelMure/git-bug/api/auth"
|
|
|
|
"github.com/MichaelMure/git-bug/api/graphql/graph"
|
|
|
|
"github.com/MichaelMure/git-bug/api/graphql/models"
|
2018-07-30 02:36:41 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
|
|
|
)
|
|
|
|
|
2019-03-31 22:44:14 +03:00
|
|
|
var _ graph.MutationResolver = &mutationResolver{}
|
|
|
|
|
2018-07-30 02:36:41 +03:00
|
|
|
type mutationResolver struct {
|
2018-09-02 16:45:14 +03:00
|
|
|
cache *cache.MultiRepoCache
|
2018-07-30 02:36:41 +03:00
|
|
|
}
|
|
|
|
|
2019-06-23 22:28:01 +03:00
|
|
|
func (r mutationResolver) getRepo(ref *string) (*cache.RepoCache, error) {
|
|
|
|
if ref != nil {
|
|
|
|
return r.cache.ResolveRepo(*ref)
|
2018-07-30 02:36:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return r.cache.DefaultRepo()
|
|
|
|
}
|
|
|
|
|
2020-06-18 21:31:28 +03:00
|
|
|
func (r mutationResolver) getBug(repoRef *string, bugPrefix string) (*cache.RepoCache, *cache.BugCache, error) {
|
2020-02-13 00:02:11 +03:00
|
|
|
repo, err := r.getRepo(repoRef)
|
|
|
|
if err != nil {
|
2020-06-18 21:31:28 +03:00
|
|
|
return nil, nil, err
|
2020-02-13 00:02:11 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
b, err := repo.ResolveBugPrefix(bugPrefix)
|
2020-06-18 21:31:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-06-21 23:12:04 +03:00
|
|
|
return repo, b, nil
|
2020-02-13 00:02:11 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 21:31:28 +03:00
|
|
|
func (r mutationResolver) NewBug(ctx context.Context, input models.NewBugInput) (*models.NewBugPayload, error) {
|
2019-06-16 22:29:49 +03:00
|
|
|
repo, err := r.getRepo(input.RepoRef)
|
2018-07-30 02:36:41 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-07-30 02:36:41 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
author, err := auth.UserFromCtx(ctx, repo)
|
2020-06-19 02:26:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-18 21:31:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
b, op, err := repo.NewBugRaw(author, time.Now().Unix(), input.Title, input.Message, input.Files, nil)
|
2018-07-30 02:36:41 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-07-30 02:36:41 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
return &models.NewBugPayload{
|
|
|
|
ClientMutationID: input.ClientMutationID,
|
2020-02-03 23:03:48 +03:00
|
|
|
Bug: models.NewLoadedBug(b.Snapshot()),
|
2019-06-16 22:29:49 +03:00
|
|
|
Operation: op,
|
|
|
|
}, nil
|
2018-07-30 02:36:41 +03:00
|
|
|
}
|
2018-08-01 22:57:12 +03:00
|
|
|
|
2020-06-18 21:31:28 +03:00
|
|
|
func (r mutationResolver) AddComment(ctx context.Context, input models.AddCommentInput) (*models.AddCommentPayload, error) {
|
|
|
|
repo, b, err := r.getBug(input.RepoRef, input.Prefix)
|
2018-08-01 22:57:12 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
author, err := auth.UserFromCtx(ctx, repo)
|
2020-06-19 02:26:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-18 21:31:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
op, err := b.AddCommentRaw(author, time.Now().Unix(), input.Message, input.Files, nil)
|
2018-08-01 22:57:12 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2020-02-13 00:02:11 +03:00
|
|
|
err = b.Commit()
|
2019-06-16 22:29:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &models.AddCommentPayload{
|
|
|
|
ClientMutationID: input.ClientMutationID,
|
2020-02-03 23:03:48 +03:00
|
|
|
Bug: models.NewLoadedBug(b.Snapshot()),
|
2019-06-16 22:29:49 +03:00
|
|
|
Operation: op,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:11:06 +03:00
|
|
|
func (r mutationResolver) EditComment(ctx context.Context, input models.EditCommentInput) (*models.EditCommentPayload, error) {
|
|
|
|
repo, b, err := r.getBug(input.RepoRef, input.Prefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
author, err := auth.UserFromCtx(ctx, repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-16 17:16:24 +03:00
|
|
|
op, err := b.EditCommentRaw(author, time.Now().Unix(), input.Target, input.Message, nil)
|
2021-03-16 16:11:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = b.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &models.EditCommentPayload{
|
|
|
|
ClientMutationID: input.ClientMutationID,
|
|
|
|
Bug: models.NewLoadedBug(b.Snapshot()),
|
|
|
|
Operation: op,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-18 21:31:28 +03:00
|
|
|
func (r mutationResolver) ChangeLabels(ctx context.Context, input *models.ChangeLabelInput) (*models.ChangeLabelPayload, error) {
|
|
|
|
repo, b, err := r.getBug(input.RepoRef, input.Prefix)
|
2019-06-16 22:29:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
author, err := auth.UserFromCtx(ctx, repo)
|
2020-06-19 02:26:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-18 21:31:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
results, op, err := b.ChangeLabelsRaw(author, time.Now().Unix(), input.Added, input.Removed, nil)
|
2018-08-01 22:57:12 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2020-02-13 00:02:11 +03:00
|
|
|
err = b.Commit()
|
2019-06-16 22:29:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resultsPtr := make([]*bug.LabelChangeResult, len(results))
|
|
|
|
for i, result := range results {
|
|
|
|
resultsPtr[i] = &result
|
|
|
|
}
|
|
|
|
|
|
|
|
return &models.ChangeLabelPayload{
|
|
|
|
ClientMutationID: input.ClientMutationID,
|
2020-02-03 23:03:48 +03:00
|
|
|
Bug: models.NewLoadedBug(b.Snapshot()),
|
2019-06-16 22:29:49 +03:00
|
|
|
Operation: op,
|
|
|
|
Results: resultsPtr,
|
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 21:31:28 +03:00
|
|
|
func (r mutationResolver) OpenBug(ctx context.Context, input models.OpenBugInput) (*models.OpenBugPayload, error) {
|
|
|
|
repo, b, err := r.getBug(input.RepoRef, input.Prefix)
|
2018-08-01 22:57:12 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
author, err := auth.UserFromCtx(ctx, repo)
|
2020-06-19 02:26:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-18 21:31:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
op, err := b.OpenRaw(author, time.Now().Unix(), nil)
|
2018-08-02 15:56:50 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-02 15:56:50 +03:00
|
|
|
}
|
|
|
|
|
2020-02-13 00:02:11 +03:00
|
|
|
err = b.Commit()
|
2018-08-01 22:57:12 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
return &models.OpenBugPayload{
|
|
|
|
ClientMutationID: input.ClientMutationID,
|
2020-02-03 23:03:48 +03:00
|
|
|
Bug: models.NewLoadedBug(b.Snapshot()),
|
2019-06-16 22:29:49 +03:00
|
|
|
Operation: op,
|
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 21:31:28 +03:00
|
|
|
func (r mutationResolver) CloseBug(ctx context.Context, input models.CloseBugInput) (*models.CloseBugPayload, error) {
|
|
|
|
repo, b, err := r.getBug(input.RepoRef, input.Prefix)
|
2018-08-01 22:57:12 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
author, err := auth.UserFromCtx(ctx, repo)
|
2020-06-19 02:26:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-18 21:31:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
op, err := b.CloseRaw(author, time.Now().Unix(), nil)
|
2018-08-02 15:56:50 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-02 15:56:50 +03:00
|
|
|
}
|
|
|
|
|
2020-02-13 00:02:11 +03:00
|
|
|
err = b.Commit()
|
2018-08-01 22:57:12 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
return &models.CloseBugPayload{
|
|
|
|
ClientMutationID: input.ClientMutationID,
|
2020-02-03 23:03:48 +03:00
|
|
|
Bug: models.NewLoadedBug(b.Snapshot()),
|
2019-06-16 22:29:49 +03:00
|
|
|
Operation: op,
|
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 21:31:28 +03:00
|
|
|
func (r mutationResolver) SetTitle(ctx context.Context, input models.SetTitleInput) (*models.SetTitlePayload, error) {
|
|
|
|
repo, b, err := r.getBug(input.RepoRef, input.Prefix)
|
2018-08-02 15:56:50 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-02 15:56:50 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
author, err := auth.UserFromCtx(ctx, repo)
|
2020-06-19 02:26:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-18 21:31:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
op, err := b.SetTitleRaw(author, time.Now().Unix(), input.Title, nil)
|
2018-08-01 22:57:12 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
err = b.Commit()
|
2018-08-01 22:57:12 +03:00
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2020-02-13 00:02:11 +03:00
|
|
|
return &models.SetTitlePayload{
|
2019-06-16 22:29:49 +03:00
|
|
|
ClientMutationID: input.ClientMutationID,
|
2020-02-03 23:03:48 +03:00
|
|
|
Bug: models.NewLoadedBug(b.Snapshot()),
|
2020-02-13 00:02:11 +03:00
|
|
|
Operation: op,
|
2019-06-16 22:29:49 +03:00
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|