2018-07-30 02:36:41 +03:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-08-01 22:57:12 +03:00
|
|
|
|
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
|
|
|
"github.com/MichaelMure/git-bug/graphql/graph"
|
2019-06-16 22:29:49 +03:00
|
|
|
"github.com/MichaelMure/git-bug/graphql/models"
|
2018-07-30 02:36:41 +03:00
|
|
|
)
|
|
|
|
|
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-02-03 23:03:48 +03:00
|
|
|
func (r mutationResolver) NewBug(_ 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
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
b, op, err := repo.NewBugWithFiles(input.Title, input.Message, input.Files)
|
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-02-03 23:03:48 +03:00
|
|
|
func (r mutationResolver) AddComment(_ context.Context, input models.AddCommentInput) (*models.AddCommentPayload, error) {
|
2019-06-16 22:29:49 +03:00
|
|
|
repo, err := r.getRepo(input.RepoRef)
|
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
|
|
|
b, err := repo.ResolveBugPrefix(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
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
op, err := b.AddCommentWithFiles(input.Message, input.Files)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
func (r mutationResolver) ChangeLabels(_ context.Context, input *models.ChangeLabelInput) (*models.ChangeLabelPayload, error) {
|
2019-06-16 22:29:49 +03:00
|
|
|
repo, err := r.getRepo(input.RepoRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := repo.ResolveBugPrefix(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
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
results, op, err := b.ChangeLabels(input.Added, input.Removed)
|
|
|
|
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-02-03 23:03:48 +03:00
|
|
|
func (r mutationResolver) OpenBug(_ context.Context, input models.OpenBugInput) (*models.OpenBugPayload, error) {
|
2019-06-16 22:29:49 +03:00
|
|
|
repo, err := r.getRepo(input.RepoRef)
|
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
|
|
|
b, err := repo.ResolveBugPrefix(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
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
op, err := b.Open()
|
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-02-03 23:03:48 +03:00
|
|
|
func (r mutationResolver) CloseBug(_ context.Context, input models.CloseBugInput) (*models.CloseBugPayload, error) {
|
2019-06-16 22:29:49 +03:00
|
|
|
repo, err := r.getRepo(input.RepoRef)
|
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
|
|
|
b, err := repo.ResolveBugPrefix(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
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
op, err := b.Close()
|
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-02-03 23:03:48 +03:00
|
|
|
func (r mutationResolver) SetTitle(_ context.Context, input models.SetTitleInput) (*models.SetTitlePayload, error) {
|
2019-06-16 22:29:49 +03:00
|
|
|
repo, err := r.getRepo(input.RepoRef)
|
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
|
|
|
b, err := repo.ResolveBugPrefix(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
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
op, err := b.SetTitle(input.Title)
|
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.SetTitlePayload{
|
|
|
|
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-02-03 23:03:48 +03:00
|
|
|
func (r mutationResolver) Commit(_ context.Context, input models.CommitInput) (*models.CommitPayload, error) {
|
2019-06-16 22:29:49 +03:00
|
|
|
repo, err := r.getRepo(input.RepoRef)
|
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
|
|
|
b, err := repo.ResolveBugPrefix(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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
return &models.CommitPayload{
|
|
|
|
ClientMutationID: input.ClientMutationID,
|
2020-02-03 23:03:48 +03:00
|
|
|
Bug: models.NewLoadedBug(b.Snapshot()),
|
2019-06-16 22:29:49 +03:00
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
func (r mutationResolver) CommitAsNeeded(_ context.Context, input models.CommitAsNeededInput) (*models.CommitAsNeededPayload, error) {
|
2019-06-16 22:29:49 +03:00
|
|
|
repo, err := r.getRepo(input.RepoRef)
|
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
|
|
|
b, err := repo.ResolveBugPrefix(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
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
err = b.CommitAsNeeded()
|
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.CommitAsNeededPayload{
|
|
|
|
ClientMutationID: input.ClientMutationID,
|
2020-02-03 23:03:48 +03:00
|
|
|
Bug: models.NewLoadedBug(b.Snapshot()),
|
2019-06-16 22:29:49 +03:00
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|