[cache] BugCache: Add set metadata methods

Amine Hilaly created

[bridge/github] fix add/remove mutation requests

[bridge/github] iterator: fix typo

Change summary

bridge/github/export.go       | 13 +++++--------
bridge/github/export_query.go |  9 +++++++--
bridge/github/iterator.go     |  2 +-
cache/bug_cache.go            | 18 ++++++++++++++++++
4 files changed, 31 insertions(+), 11 deletions(-)

Detailed changes

bridge/github/export.go 🔗

@@ -10,15 +10,11 @@ import (
 	"net/http"
 	"time"
 
+	"github.com/shurcooL/githubv4"
+
 	"github.com/MichaelMure/git-bug/bridge/core"
 	"github.com/MichaelMure/git-bug/bug"
 	"github.com/MichaelMure/git-bug/cache"
-	"github.com/shurcooL/githubv4"
-)
-
-const (
-	keyGithubIdExport  = "github-id"
-	keyGithubUrlExport = "github-url"
 )
 
 // githubImporter implement the Importer interface
@@ -449,7 +445,7 @@ func (ge *githubExporter) updateGithubIssueLabels(labelableID string, added, rem
 		return fmt.Errorf("getting added labels ids: %v", err)
 	}
 
-	m := &updateIssueMutation{}
+	m := &addLabelsToLabelableMutation{}
 	inputAdd := &githubv4.AddLabelsToLabelableInput{
 		LabelableID: labelableID,
 		LabelIDs:    addedIDs,
@@ -465,13 +461,14 @@ func (ge *githubExporter) updateGithubIssueLabels(labelableID string, added, rem
 		return fmt.Errorf("getting added labels ids: %v", err)
 	}
 
+	m2 := &removeLabelsFromLabelableMutation{}
 	inputRemove := &githubv4.RemoveLabelsFromLabelableInput{
 		LabelableID: labelableID,
 		LabelIDs:    removedIDs,
 	}
 
 	// remove label labels
-	if err := ge.gc.Mutate(context.TODO(), m, inputRemove, nil); err != nil {
+	if err := ge.gc.Mutate(context.TODO(), m2, inputRemove, nil); err != nil {
 		return err
 	}
 

bridge/github/export_query.go 🔗

@@ -33,8 +33,13 @@ type updateIssueCommentMutation struct {
 	IssueComment struct {
 		ID  string `graphql:"id"`
 		URL string `graphql:"url"`
-	} `graphql:"addComment(input:$input)"`
+	} `graphql:"updateIssueComment(input:$input)"`
+}
+
+type removeLabelsFromLabelableMutation struct {
+	AddLabels struct{} `graphql:"removeLabelsFromLabelable(input:$input)"`
 }
 
-type removeLabelsMutation struct {
+type addLabelsToLabelableMutation struct {
+	RemoveLabels struct{} `graphql:"addLabelsToLabelable(input:$input)"`
 }

bridge/github/iterator.go 🔗

@@ -59,7 +59,7 @@ type iterator struct {
 	commentEdit commentEditIterator
 }
 
-// NewIterator create and initalize a new iterator
+// NewIterator create and initialize a new iterator
 func NewIterator(owner, project, token string, since time.Time) *iterator {
 	i := &iterator{
 		gc:       buildClient(token),

cache/bug_cache.go 🔗

@@ -254,6 +254,24 @@ func (c *BugCache) EditCommentRaw(author *IdentityCache, unixTime int64, target
 	return op, c.notifyUpdated()
 }
 
+func (c *BugCache) SetMetadata(target git.Hash, newMetadata map[string]string) (*bug.SetMetadataOperation, error) {
+	author, err := c.repoCache.GetUserIdentity()
+	if err != nil {
+		return nil, err
+	}
+
+	return c.SetMetadataRaw(author, time.Now().Unix(), target, nil)
+}
+
+func (c *BugCache) SetMetadataRaw(author *IdentityCache, unixTime int64, target git.Hash, newMetadata map[string]string) (*bug.SetMetadataOperation, error) {
+	op, err := bug.SetMetadata(c.bug, author.Identity, unixTime, target, newMetadata)
+	if err != nil {
+		return nil, err
+	}
+
+	return op, c.notifyUpdated()
+}
+
 func (c *BugCache) Commit() error {
 	err := c.bug.Commit(c.repoCache.repo)
 	if err != nil {