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()
|
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
func (r mutationResolver) NewBug(ctx context.Context, input models.NewBugInput) (*models.NewBugPayload, error) {
|
|
|
|
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,
|
|
|
|
Bug: b.Snapshot(),
|
|
|
|
Operation: op,
|
|
|
|
}, nil
|
2018-07-30 02:36:41 +03:00
|
|
|
}
|
2018-08-01 22:57:12 +03:00
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
func (r mutationResolver) AddComment(ctx context.Context, input models.AddCommentInput) (*models.AddCommentPayload, error) {
|
|
|
|
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,
|
|
|
|
Bug: b.Snapshot(),
|
|
|
|
Operation: op,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r mutationResolver) ChangeLabels(ctx context.Context, input *models.ChangeLabelInput) (*models.ChangeLabelPayload, error) {
|
|
|
|
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,
|
|
|
|
Bug: b.Snapshot(),
|
|
|
|
Operation: op,
|
|
|
|
Results: resultsPtr,
|
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
func (r mutationResolver) OpenBug(ctx context.Context, input models.OpenBugInput) (*models.OpenBugPayload, error) {
|
|
|
|
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,
|
|
|
|
Bug: b.Snapshot(),
|
|
|
|
Operation: op,
|
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
func (r mutationResolver) CloseBug(ctx context.Context, input models.CloseBugInput) (*models.CloseBugPayload, error) {
|
|
|
|
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,
|
|
|
|
Bug: b.Snapshot(),
|
|
|
|
Operation: op,
|
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
func (r mutationResolver) SetTitle(ctx context.Context, input models.SetTitleInput) (*models.SetTitlePayload, error) {
|
|
|
|
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,
|
|
|
|
Bug: b.Snapshot(),
|
|
|
|
Operation: op,
|
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
func (r mutationResolver) Commit(ctx context.Context, input models.CommitInput) (*models.CommitPayload, error) {
|
|
|
|
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,
|
|
|
|
Bug: b.Snapshot(),
|
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
func (r mutationResolver) CommitAsNeeded(ctx context.Context, input models.CommitAsNeededInput) (*models.CommitAsNeededPayload, error) {
|
|
|
|
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,
|
|
|
|
Bug: b.Snapshot(),
|
|
|
|
}, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|