aboutsummaryrefslogtreecommitdiffstats
path: root/bug
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-25 18:01:32 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-25 18:01:32 +0200
commit6a12373965aff9f80147f8b5bff6a5a104927365 (patch)
treeccc1e34fba059512acab39286a86f6b73c5ad318 /bug
parent49c90eab26875cbf3094d9a546ad29b426e174a1 (diff)
downloadgit-bug-6a12373965aff9f80147f8b5bff6a5a104927365.tar.gz
more refactoring to have reusable bug action across different UI
Diffstat (limited to 'bug')
-rw-r--r--bug/bug.go2
-rw-r--r--bug/bug_actions.go (renamed from bug/remote_actions.go)40
-rw-r--r--bug/operations/add_comment.go5
-rw-r--r--bug/operations/create.go8
-rw-r--r--bug/operations/label_change.go64
-rw-r--r--bug/operations/set_status.go10
-rw-r--r--bug/operations/set_title.go5
7 files changed, 125 insertions, 9 deletions
diff --git a/bug/bug.go b/bug/bug.go
index 0b9a4da3..713d6edc 100644
--- a/bug/bug.go
+++ b/bug/bug.go
@@ -41,7 +41,7 @@ func NewBug() *Bug {
// Find an existing Bug matching a prefix
func FindLocalBug(repo repository.Repo, prefix string) (*Bug, error) {
- ids, err := repo.ListRefs(bugsRefPattern)
+ ids, err := repo.ListIds(bugsRefPattern)
if err != nil {
return nil, err
diff --git a/bug/remote_actions.go b/bug/bug_actions.go
index 2070ba21..85123e1c 100644
--- a/bug/remote_actions.go
+++ b/bug/bug_actions.go
@@ -3,13 +3,14 @@ package bug
import (
"fmt"
"github.com/MichaelMure/git-bug/repository"
+ "io"
"strings"
)
-const MsgNew = "new"
-const MsgInvalid = "invalid data"
-const MsgUpdated = "updated"
-const MsgNothing = "nothing to do"
+const MsgMergeNew = "new"
+const MsgMergeInvalid = "invalid data"
+const MsgMergeUpdated = "updated"
+const MsgMergeNothing = "nothing to do"
func Fetch(repo repository.Repo, remote string) error {
remoteRefSpec := fmt.Sprintf(bugsRemoteRefPattern, remote)
@@ -22,6 +23,27 @@ func Push(repo repository.Repo, remote string) error {
return repo.PushRefs(remote, bugsRefPattern+"*")
}
+func Pull(repo repository.Repo, out io.Writer, remote string) error {
+ fmt.Fprintf(out, "Fetching remote ...\n")
+
+ if err := Fetch(repo, remote); err != nil {
+ return err
+ }
+
+ fmt.Fprintf(out, "\nMerging data ...\n")
+
+ for merge := range MergeAll(repo, remote) {
+ if merge.Err != nil {
+ return merge.Err
+ }
+
+ if merge.Status != MsgMergeNothing {
+ fmt.Fprintf(out, "%s: %s\n", merge.HumanId, merge.Status)
+ }
+ }
+ return nil
+}
+
type MergeResult struct {
Err error
@@ -73,7 +95,7 @@ func MergeAll(repo repository.Repo, remote string) <-chan MergeResult {
// Check for error in remote data
if !remoteBug.IsValid() {
- out <- newMergeStatus(id, MsgInvalid)
+ out <- newMergeStatus(id, MsgMergeInvalid)
continue
}
@@ -89,7 +111,7 @@ func MergeAll(repo repository.Repo, remote string) <-chan MergeResult {
return
}
- out <- newMergeStatus(id, MsgNew)
+ out <- newMergeStatus(id, MsgMergeNew)
continue
}
@@ -108,12 +130,14 @@ func MergeAll(repo repository.Repo, remote string) <-chan MergeResult {
}
if updated {
- out <- newMergeStatus(id, MsgUpdated)
+ out <- newMergeStatus(id, MsgMergeUpdated)
} else {
- out <- newMergeStatus(id, MsgNothing)
+ out <- newMergeStatus(id, MsgMergeNothing)
}
}
}()
return out
}
+
+
diff --git a/bug/operations/add_comment.go b/bug/operations/add_comment.go
index 9ee8dc64..66725915 100644
--- a/bug/operations/add_comment.go
+++ b/bug/operations/add_comment.go
@@ -31,3 +31,8 @@ func (op AddCommentOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
return snapshot
}
+
+func Comment(b *bug.Bug, author bug.Person, message string) {
+ addCommentOp := NewAddCommentOp(author, message)
+ b.Append(addCommentOp)
+}
diff --git a/bug/operations/create.go b/bug/operations/create.go
index 72eba843..0fd1225e 100644
--- a/bug/operations/create.go
+++ b/bug/operations/create.go
@@ -33,3 +33,11 @@ func (op CreateOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
}
return snapshot
}
+
+func Create(author bug.Person, title, message string) (*bug.Bug, error) {
+ newBug := bug.NewBug()
+ createOp := NewCreateOp(author, title, message)
+ newBug.Append(createOp)
+
+ return newBug, nil
+}
diff --git a/bug/operations/label_change.go b/bug/operations/label_change.go
index ca07f6f5..b9bf86c8 100644
--- a/bug/operations/label_change.go
+++ b/bug/operations/label_change.go
@@ -1,7 +1,9 @@
package operations
import (
+ "fmt"
"github.com/MichaelMure/git-bug/bug"
+ "io"
"sort"
)
@@ -54,3 +56,65 @@ AddLoop:
return snapshot
}
+
+func ChangeLabels(out io.Writer, b *bug.Bug, author bug.Person, add, remove []string) error {
+ var added, removed []bug.Label
+
+ snap := b.Compile()
+
+ for _, str := range add {
+ label := bug.Label(str)
+
+ // check for duplicate
+ if labelExist(added, label) {
+ fmt.Fprintf(out, "label \"%s\" is a duplicate\n", str)
+ continue
+ }
+
+ // check that the label doesn't already exist
+ if labelExist(snap.Labels, label) {
+ fmt.Fprintf(out, "label \"%s\" is already set on this bug\n", str)
+ continue
+ }
+
+ added = append(added, label)
+ }
+
+ for _, str := range remove {
+ label := bug.Label(str)
+
+ // check for duplicate
+ if labelExist(removed, label) {
+ fmt.Fprintf(out, "label \"%s\" is a duplicate\n", str)
+ continue
+ }
+
+ // check that the label actually exist
+ if !labelExist(snap.Labels, label) {
+ fmt.Fprintf(out, "label \"%s\" doesn't exist on this bug\n", str)
+ continue
+ }
+
+ removed = append(removed, label)
+ }
+
+ if len(added) == 0 && len(removed) == 0 {
+ return fmt.Errorf("no label added or removed")
+ }
+
+ labelOp := NewLabelChangeOperation(author, added, removed)
+
+ b.Append(labelOp)
+
+ return nil
+}
+
+func labelExist(labels []bug.Label, label bug.Label) bool {
+ for _, l := range labels {
+ if l == label {
+ return true
+ }
+ }
+
+ return false
+}
diff --git a/bug/operations/set_status.go b/bug/operations/set_status.go
index 7c718cd6..b62409dc 100644
--- a/bug/operations/set_status.go
+++ b/bug/operations/set_status.go
@@ -25,3 +25,13 @@ func (op SetStatusOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
return snapshot
}
+
+func Open(b *bug.Bug, author bug.Person) {
+ op := NewSetStatusOp(author, bug.OpenStatus)
+ b.Append(op)
+}
+
+func Close(b *bug.Bug, author bug.Person) {
+ op := NewSetStatusOp(author, bug.ClosedStatus)
+ b.Append(op)
+}
diff --git a/bug/operations/set_title.go b/bug/operations/set_title.go
index ef934db1..295db282 100644
--- a/bug/operations/set_title.go
+++ b/bug/operations/set_title.go
@@ -25,3 +25,8 @@ func (op SetTitleOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
return snapshot
}
+
+func SetTitle(b *bug.Bug, author bug.Person, title string) {
+ setTitleOp := NewSetTitleOp(author, title)
+ b.Append(setTitleOp)
+}