Skip to content

Commit

Permalink
mr/issue: Add subscribe/unsubscribe subcommands
Browse files Browse the repository at this point in the history
For projects where one is only tangentially involved, it sometimes
makes sense to subscribe to an individual issue or merge request
rather than the project as a whole.

Support that with corresponding subcommands.
  • Loading branch information
fmuellner committed Feb 25, 2021
1 parent 80edd01 commit 77f64f3
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cmd/issue_subscribe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"fmt"
"log"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var issueSubscribeCmd = &cobra.Command{
Use: "subscribe [remote] <id>",
Aliases: []string{},
Short: "Subscribe to issue",
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, id, err := parseArgsRemoteAndID(args)
if err != nil {
log.Fatal(err)
}

p, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}

err = lab.IssueSubscribe(p.ID, int(id))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Subscribed to issue #%d\n", id)
},
}

func init() {
issueCmd.AddCommand(issueSubscribeCmd)
carapace.Gen(issueSubscribeCmd).PositionalCompletion(
action.Remotes(),
action.Issues(issueList),
)
}
44 changes: 44 additions & 0 deletions cmd/issue_unsubscribe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"fmt"
"log"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var issueUnsubscribeCmd = &cobra.Command{
Use: "unsubscribe [remote] <id>",
Aliases: []string{},
Short: "Unubscribe from issue",
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, id, err := parseArgsRemoteAndID(args)
if err != nil {
log.Fatal(err)
}

p, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}

err = lab.IssueUnsubscribe(p.ID, int(id))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Unsubscribed from issue #%d\n", id)
},
}

func init() {
issueCmd.AddCommand(issueUnsubscribeCmd)
carapace.Gen(issueUnsubscribeCmd).PositionalCompletion(
action.Remotes(),
action.Issues(issueList),
)
}
44 changes: 44 additions & 0 deletions cmd/mr_subscribe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"fmt"
"log"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var mrSubscribeCmd = &cobra.Command{
Use: "subscribe [remote] <id>",
Aliases: []string{},
Short: "Subscribe to merge request",
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, id, err := parseArgsWithGitBranchMR(args)
if err != nil {
log.Fatal(err)
}

p, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}

err = lab.MRSubscribe(p.ID, int(id))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Subscribed to merge request !%d\n", id)
},
}

func init() {
mrCmd.AddCommand(mrSubscribeCmd)
carapace.Gen(mrSubscribeCmd).PositionalCompletion(
action.Remotes(),
action.MergeRequests(mrList),
)
}
44 changes: 44 additions & 0 deletions cmd/mr_unsubscribe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"fmt"
"log"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var mrUnsubscribeCmd = &cobra.Command{
Use: "unsubscribe [remote] <id>",
Aliases: []string{},
Short: "Unubscribe from merge request",
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, id, err := parseArgsWithGitBranchMR(args)
if err != nil {
log.Fatal(err)
}

p, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}

err = lab.MRUnsubscribe(p.ID, int(id))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Unsubscribed from merge request !%d\n", id)
},
}

func init() {
mrCmd.AddCommand(mrUnsubscribeCmd)
carapace.Gen(mrUnsubscribeCmd).PositionalCompletion(
action.Remotes(),
action.MergeRequests(mrList),
)
}
48 changes: 48 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,30 @@ func MRUnapprove(pid interface{}, id int) error {
return nil
}

// MRSubscribe subscribes to an mr on a GitLab project
func MRSubscribe(pid interface{}, id int) error {
_, resp, err := lab.MergeRequests.SubscribeToMergeRequest(pid, id, nil)
if resp != nil && resp.StatusCode == http.StatusNotModified {
return errors.New("Already subscribed")
}
if err != nil {
return err
}
return nil
}

// MRUnsubscribe unsubscribes from a previously mr on a GitLab project
func MRUnsubscribe(pid interface{}, id int) error {
_, resp, err := lab.MergeRequests.UnsubscribeFromMergeRequest(pid, id, nil)
if resp != nil && resp.StatusCode == http.StatusNotModified {
return errors.New("Not subscribed")
}
if err != nil {
return err
}
return nil
}

// MRThumbUp places a thumb up/down on a merge request
func MRThumbUp(pid interface{}, id int) error {
_, _, err := lab.AwardEmoji.CreateMergeRequestAwardEmoji(pid, id, &gitlab.CreateAwardEmojiOptions{
Expand Down Expand Up @@ -744,6 +768,30 @@ func IssueListDiscussions(project string, issueNum int) ([]*gitlab.Discussion, e
return discussions, nil
}

// IssueSubscribe subscribes to an issue on a GitLab project
func IssueSubscribe(pid interface{}, id int) error {
_, resp, err := lab.Issues.SubscribeToIssue(pid, id, nil)
if resp != nil && resp.StatusCode == http.StatusNotModified {
return errors.New("Already subscribed")
}
if err != nil {
return err
}
return nil
}

// IssueUnsubscribe unsubscribes from an issue on a GitLab project
func IssueUnsubscribe(pid interface{}, id int) error {
_, resp, err := lab.Issues.UnsubscribeFromIssue(pid, id, nil)
if resp != nil && resp.StatusCode == http.StatusNotModified {
return errors.New("Not subscribed")
}
if err != nil {
return err
}
return nil
}

// GetCommit returns top Commit by ref (hash, branch or tag).
func GetCommit(pid interface{}, ref string) (*gitlab.Commit, error) {
c, _, err := lab.Commits.GetCommit(pid, ref)
Expand Down

0 comments on commit 77f64f3

Please sign in to comment.