Add ForceLabelChange functionalities

Amine Hilaly created

Change summary

bridge/github/import.go | 11 ++++++-----
bug/op_label_change.go  | 27 +++++++++++++++++++++++++++
cache/bug_cache.go      | 27 +++++++++++++++++++++++++++
3 files changed, 60 insertions(+), 5 deletions(-)

Detailed changes

bridge/github/import.go 🔗

@@ -40,7 +40,7 @@ func (gi *githubImporter) ImportAll(repo *cache.RepoCache, since time.Time) erro
 	for gi.iterator.NextIssue() {
 		issue := gi.iterator.IssueValue()
 
-		fmt.Printf("importing issue: %v\n", gi.iterator.count)
+		fmt.Printf("importing issue: %v %v\n", gi.iterator.count, issue.Title)
 		// get issue edits
 		issueEdits := []userContentEdit{}
 		for gi.iterator.NextIssueEdit() {
@@ -71,12 +71,12 @@ func (gi *githubImporter) ImportAll(repo *cache.RepoCache, since time.Time) erro
 
 				err := gi.ensureTimelineComment(repo, b, item.IssueComment, commentEdits)
 				if err != nil {
-					return fmt.Errorf("timeline event creation: %v", err)
+					return fmt.Errorf("timeline comment creation: %v", err)
 				}
 
 			} else {
 				if err := gi.ensureTimelineItem(repo, b, item); err != nil {
-					return fmt.Errorf("timeline comment creation: %v", err)
+					return fmt.Errorf("timeline event creation: %v", err)
 				}
 			}
 		}
@@ -189,7 +189,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
 		if err != nil {
 			return err
 		}
-		_, _, err = b.ChangeLabelsRaw(
+		_, err = b.ForceChangeLabelsRaw(
 			author,
 			item.LabeledEvent.CreatedAt.Unix(),
 			[]string{
@@ -198,6 +198,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
 			nil,
 			map[string]string{keyGithubId: id},
 		)
+
 		return err
 
 	case "UnlabeledEvent":
@@ -211,7 +212,7 @@ func (gi *githubImporter) ensureTimelineItem(repo *cache.RepoCache, b *cache.Bug
 			return err
 		}
 
-		_, _, err = b.ChangeLabelsRaw(
+		_, err = b.ForceChangeLabelsRaw(
 			author,
 			item.UnlabeledEvent.CreatedAt.Unix(),
 			nil,

bug/op_label_change.go 🔗

@@ -234,6 +234,33 @@ func ChangeLabels(b Interface, author identity.Interface, unixTime int64, add, r
 	return results, labelOp, nil
 }
 
+// ForceChangeLabels is a convenience function to apply the operation
+// The difference with ChangeLabels is that no checks of deduplications are done. You are entirely
+// responsible of what you are doing. In the general case, you want to use ChangeLabels instead.
+// The intended use of this function is to allow importers to create legal but unexpected label changes,
+// like removing a label with no information of when it was added before.
+func ForceChangeLabels(b Interface, author identity.Interface, unixTime int64, add, remove []string) (*LabelChangeOperation, error) {
+	added := make([]Label, len(add))
+	for i, str := range add {
+		added[i] = Label(str)
+	}
+
+	removed := make([]Label, len(remove))
+	for i, str := range remove {
+		removed[i] = Label(str)
+	}
+
+	labelOp := NewLabelChangeOperation(author, unixTime, added, removed)
+
+	if err := labelOp.Validate(); err != nil {
+		return nil, err
+	}
+
+	b.Append(labelOp)
+
+	return labelOp, nil
+}
+
 func labelExist(labels []Label, label Label) bool {
 	for _, l := range labels {
 		if l == label {

cache/bug_cache.go 🔗

@@ -139,6 +139,33 @@ func (c *BugCache) ChangeLabelsRaw(author *IdentityCache, unixTime int64, added
 	return changes, op, nil
 }
 
+func (c *BugCache) ForceChangeLabels(added []string, removed []string) (*bug.LabelChangeOperation, error) {
+	author, err := c.repoCache.GetUserIdentity()
+	if err != nil {
+		return nil, err
+	}
+
+	return c.ForceChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
+}
+
+func (c *BugCache) ForceChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) (*bug.LabelChangeOperation, error) {
+	op, err := bug.ForceChangeLabels(c.bug, author.Identity, unixTime, added, removed)
+	if err != nil {
+		return nil, err
+	}
+
+	for key, value := range metadata {
+		op.SetMetadata(key, value)
+	}
+
+	err = c.notifyUpdated()
+	if err != nil {
+		return nil, err
+	}
+
+	return op, nil
+}
+
 func (c *BugCache) Open() (*bug.SetStatusOperation, error) {
 	author, err := c.repoCache.GetUserIdentity()
 	if err != nil {