aboutsummaryrefslogblamecommitdiffstats
path: root/graphql/resolvers/mutation.go
blob: 7cc98aace70f4e8d5adaf160d038df7de4769ac2 (plain) (tree)
1
2
3
4
5
6
7
8



                 
 

                                              
                                             


                              
                                   

 
                                                                              






                                                    
                                                                                                                                               




                                          
                                                             







                                          











                                                                                                             
                        








                                          
                                                                                                                                                    




                                          




                                               
                                                   














                                                                                                                                                     




                                               
                                                 














                                                                                                           




                                               
                      














                                                                                                            




                                               
                       














                                                                                                                             




                                               
                               







                                          
package resolvers

import (
	"context"

	"github.com/MichaelMure/git-bug/bug"
	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/util"
)

type mutationResolver struct {
	cache *cache.MultiRepoCache
}

func (r mutationResolver) getRepo(repoRef *string) (*cache.RepoCache, error) {
	if repoRef != nil {
		return r.cache.ResolveRepo(*repoRef)
	}

	return r.cache.DefaultRepo()
}

func (r mutationResolver) NewBug(ctx context.Context, repoRef *string, title string, message string, files []util.Hash) (bug.Snapshot, error) {
	repo, err := r.getRepo(repoRef)
	if err != nil {
		return bug.Snapshot{}, err
	}

	b, err := repo.NewBugWithFiles(title, message, files)
	if err != nil {
		return bug.Snapshot{}, err
	}

	snap := b.Snapshot()

	return *snap, nil
}

func (r mutationResolver) Commit(ctx context.Context, repoRef *string, prefix string) (bug.Snapshot, error) {
	repo, err := r.getRepo(repoRef)
	if err != nil {
		return bug.Snapshot{}, err
	}

	b, err := repo.ResolveBugPrefix(prefix)
	if err != nil {
		return bug.Snapshot{}, err
	}

	err = b.Commit()
	if err != nil {
		return bug.Snapshot{}, err
	}

	snap := b.Snapshot()

	return *snap, nil
}

func (r mutationResolver) AddComment(ctx context.Context, repoRef *string, prefix string, message string, files []util.Hash) (bug.Snapshot, error) {
	repo, err := r.getRepo(repoRef)
	if err != nil {
		return bug.Snapshot{}, err
	}

	b, err := repo.ResolveBugPrefix(prefix)
	if err != nil {
		return bug.Snapshot{}, err
	}

	err = b.AddCommentWithFiles(message, files)
	if err != nil {
		return bug.Snapshot{}, err
	}

	snap := b.Snapshot()

	return *snap, nil
}

func (r mutationResolver) ChangeLabels(ctx context.Context, repoRef *string, prefix string, added []string, removed []string) (bug.Snapshot, error) {
	repo, err := r.getRepo(repoRef)
	if err != nil {
		return bug.Snapshot{}, err
	}

	b, err := repo.ResolveBugPrefix(prefix)
	if err != nil {
		return bug.Snapshot{}, err
	}

	err = b.ChangeLabels(nil, added, removed)
	if err != nil {
		return bug.Snapshot{}, err
	}

	snap := b.Snapshot()

	return *snap, nil
}

func (r mutationResolver) Open(ctx context.Context, repoRef *string, prefix string) (bug.Snapshot, error) {
	repo, err := r.getRepo(repoRef)
	if err != nil {
		return bug.Snapshot{}, err
	}

	b, err := repo.ResolveBugPrefix(prefix)
	if err != nil {
		return bug.Snapshot{}, err
	}

	err = b.Open()
	if err != nil {
		return bug.Snapshot{}, err
	}

	snap := b.Snapshot()

	return *snap, nil
}

func (r mutationResolver) Close(ctx context.Context, repoRef *string, prefix string) (bug.Snapshot, error) {
	repo, err := r.getRepo(repoRef)
	if err != nil {
		return bug.Snapshot{}, err
	}

	b, err := repo.ResolveBugPrefix(prefix)
	if err != nil {
		return bug.Snapshot{}, err
	}

	err = b.Close()
	if err != nil {
		return bug.Snapshot{}, err
	}

	snap := b.Snapshot()

	return *snap, nil
}

func (r mutationResolver) SetTitle(ctx context.Context, repoRef *string, prefix string, title string) (bug.Snapshot, error) {
	repo, err := r.getRepo(repoRef)
	if err != nil {
		return bug.Snapshot{}, err
	}

	b, err := repo.ResolveBugPrefix(prefix)
	if err != nil {
		return bug.Snapshot{}, err
	}

	err = b.SetTitle(title)
	if err != nil {
		return bug.Snapshot{}, err
	}

	snap := b.Snapshot()

	return *snap, nil
}