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"
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/git"
|
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
|
|
|
}
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
func (r mutationResolver) getRepo(repoRef *string) (*cache.RepoCache, error) {
|
2018-07-30 02:36:41 +03:00
|
|
|
if repoRef != nil {
|
|
|
|
return r.cache.ResolveRepo(*repoRef)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.cache.DefaultRepo()
|
|
|
|
}
|
|
|
|
|
2019-04-09 18:43:17 +03:00
|
|
|
func (r mutationResolver) NewBug(ctx context.Context, repoRef *string, title string, message string, files []git.Hash) (*bug.Snapshot, error) {
|
2018-07-30 02:36:41 +03:00
|
|
|
repo, err := r.getRepo(repoRef)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-07-30 02:36:41 +03:00
|
|
|
}
|
|
|
|
|
2018-08-03 00:37:49 +03:00
|
|
|
b, err := repo.NewBugWithFiles(title, message, 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-04-09 18:43:17 +03:00
|
|
|
return b.Snapshot(), nil
|
2018-07-30 02:36:41 +03:00
|
|
|
}
|
2018-08-01 22:57:12 +03:00
|
|
|
|
2019-04-09 18:43:17 +03:00
|
|
|
func (r mutationResolver) Commit(ctx context.Context, repoRef *string, prefix string) (*bug.Snapshot, error) {
|
2018-08-01 22:57:12 +03:00
|
|
|
repo, err := r.getRepo(repoRef)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := repo.ResolveBugPrefix(prefix)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:35:13 +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-04-09 18:43:17 +03:00
|
|
|
return b.Snapshot(), nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-04-09 18:43:17 +03:00
|
|
|
func (r mutationResolver) AddComment(ctx context.Context, repoRef *string, prefix string, message string, files []git.Hash) (*bug.Snapshot, error) {
|
2018-08-01 22:57:12 +03:00
|
|
|
repo, err := r.getRepo(repoRef)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 15:56:50 +03:00
|
|
|
b, err := repo.ResolveBugPrefix(prefix)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-02 15:56:50 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
_, err = b.AddCommentWithFiles(message, files)
|
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-04-09 18:43:17 +03:00
|
|
|
return b.Snapshot(), nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-04-09 18:43:17 +03:00
|
|
|
func (r mutationResolver) ChangeLabels(ctx context.Context, repoRef *string, prefix string, added []string, removed []string) (*bug.Snapshot, error) {
|
2018-08-01 22:57:12 +03:00
|
|
|
repo, err := r.getRepo(repoRef)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 15:56:50 +03:00
|
|
|
b, err := repo.ResolveBugPrefix(prefix)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-02 15:56:50 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
_, _, err = b.ChangeLabels(added, removed)
|
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-04-09 18:43:17 +03:00
|
|
|
return b.Snapshot(), nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-04-09 18:43:17 +03:00
|
|
|
func (r mutationResolver) Open(ctx context.Context, repoRef *string, prefix string) (*bug.Snapshot, error) {
|
2018-08-01 22:57:12 +03:00
|
|
|
repo, err := r.getRepo(repoRef)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 15:56:50 +03:00
|
|
|
b, err := repo.ResolveBugPrefix(prefix)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-02 15:56:50 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
_, 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-04-09 18:43:17 +03:00
|
|
|
return b.Snapshot(), nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-04-09 18:43:17 +03:00
|
|
|
func (r mutationResolver) Close(ctx context.Context, repoRef *string, prefix string) (*bug.Snapshot, error) {
|
2018-08-01 22:57:12 +03:00
|
|
|
repo, err := r.getRepo(repoRef)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 15:56:50 +03:00
|
|
|
b, err := repo.ResolveBugPrefix(prefix)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-02 15:56:50 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
_, 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-04-09 18:43:17 +03:00
|
|
|
return b.Snapshot(), nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-04-09 18:43:17 +03:00
|
|
|
func (r mutationResolver) SetTitle(ctx context.Context, repoRef *string, prefix string, title string) (*bug.Snapshot, error) {
|
2018-08-01 22:57:12 +03:00
|
|
|
repo, err := r.getRepo(repoRef)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 15:56:50 +03:00
|
|
|
b, err := repo.ResolveBugPrefix(prefix)
|
|
|
|
if err != nil {
|
2019-04-09 18:43:17 +03:00
|
|
|
return nil, err
|
2018-08-02 15:56:50 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
_, err = b.SetTitle(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-04-09 18:43:17 +03:00
|
|
|
return b.Snapshot(), nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|