Skip to content

Commit

Permalink
todo: Implmement 'mr' and 'issue' commands
Browse files Browse the repository at this point in the history
The GitLab WebUI allows users to mark add Todo list items from a Merge
Request pages and Issue pages.  Unfortunately the go-gitlab library
does not implenet the adding of Issues to Todo lists [1].

Implement a "mr" command that allows users to add Merge Requests as Todo
list items.  Since go-gitlab does not support the adding of Issues to Todo
lists so put in a place holder for an "issue" command.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
[1] xanzy/go-gitlab#1130
  • Loading branch information
prarit committed Apr 19, 2021
1 parent 60de999 commit 5fa923a
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 11 deletions.
40 changes: 40 additions & 0 deletions cmd/todo_issue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"log"
"strconv"

"github.com/spf13/cobra"
)

var todoIssueCmd = &cobra.Command{
Use: "issue",
Short: "Add a Issue to Todo list",
Example: `lab todo issue 5678 #adds Issue 1234 to user's Todo list`,
Hidden: true,
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, err := getRemoteName("")
if err != nil {
log.Fatal(err)
}

num, err := strconv.Atoi(args[0])
if err != nil {
log.Fatal(err)
}

todoAddIssue(rn, num)

},
}

func todoAddIssue(project string, issueNum int) {
// https://github.com/xanzy/go-gitlab/pull/1130
log.Fatal("Adding issues not implemented.")
}

func init() {
todoCmd.AddCommand(todoIssueCmd)
}
51 changes: 51 additions & 0 deletions cmd/todo_mr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"fmt"
"log"
"strconv"

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

var todoMRCmd = &cobra.Command{
Use: "mr",
Short: "Add a Merge Request to Todo list",
Example: `lab todo mr 1234 #adds MR 1234 to user's Todo list`,
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, err := getRemoteName("")
if err != nil {
log.Fatal(err)
}

num, err := strconv.Atoi(args[0])
if err != nil {
log.Fatal(err)
}

todoAddMergeRequest(rn, num)
},
}

func todoAddMergeRequest(remote string, mrNum int) {
todoID, err := lab.TodoMRCreate(remote, mrNum)
if err != nil {
if err == lab.ErrNotModified {
log.Fatalf("Todo entry already exists for MR !%d", mrNum)
}
log.Fatal(err)
}

mr, err := lab.MRGet(remote, mrNum)
if err != nil {
log.Fatal(err)
}
fmt.Println(todoID, mr.WebURL)
}

func init() {
todoCmd.AddCommand(todoMRCmd)
}
19 changes: 8 additions & 11 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ func parseArgsRemoteAndProject(args []string) (string, string, error) {
return "", "", nil
}

if remote == "" {
remote = defaultRemote
}

remote, err = getRemoteName(remote)
if err != nil {
return "", "", err
Expand All @@ -187,19 +183,19 @@ func parseArgsRemoteAndBranch(args []string) (string, string, error) {
return "", "", err
}

if remote == "" {
remote = determineSourceRemote(branch)
}

remoteBranch, _ := git.UpstreamBranch(branch)
if remoteBranch != "" {
branch = remoteBranch
}

if remote == "" {
remote = determineSourceRemote(branch)
}
remote, err = getRemoteName(remote)
if err != nil {
return "", "", err
}

return remote, branch, nil
}

Expand Down Expand Up @@ -251,6 +247,10 @@ func getPipelineFromArgs(args []string, forMR bool) (string, int, error) {
}

func getRemoteName(remote string) (string, error) {
if remote == "" {
remote = defaultRemote
}

ok, err := git.IsRemote(remote)
if err != nil {
return "", err
Expand Down Expand Up @@ -323,9 +323,6 @@ func parseArgsWithGitBranchMR(args []string) (string, int64, error) {
return "", 0, err
}

if s == "" {
s = defaultRemote
}
s, err = getRemoteName(s)
if err != nil {
return "", 0, err
Expand Down
18 changes: 18 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var (
ErrGroupNotFound = errors.New("gitlab group not found")
// ErrStatusForbidden is returned when attempting to access a GitLab project with insufficient permissions
ErrStatusForbidden = errors.New("Insufficient permissions for gitlab project")
// ErrNotModified is returned when adding an already existing item to a Todo list
ErrNotModified = errors.New("Not Modified")
)

var (
Expand Down Expand Up @@ -1725,3 +1727,19 @@ func TodoMarkAllDone() error {
}
return nil
}

func TodoMRCreate(project string, mrNum int) (int, error) {
p, err := FindProject(project)
if err != nil {
return 0, err
}

todo, resp, err := lab.MergeRequests.CreateTodo(p.ID, mrNum)
if err != nil {
if resp.StatusCode == http.StatusNotModified {
return 0, ErrNotModified
}
return 0, err
}
return todo.ID, nil
}

0 comments on commit 5fa923a

Please sign in to comment.