Skip to content

Commit

Permalink
show: Unify the PrintDiscussions() code
Browse files Browse the repository at this point in the history
The printDiscussions() and printMRDiscussions() code is identical.  Merge
them into a single PrintDiscussions() function.

Unify the PrintDiscussions() function.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Sep 25, 2020
1 parent aa00f8b commit 1ee9cd5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 76 deletions.
6 changes: 3 additions & 3 deletions cmd/issue_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var issueShowCmd = &cobra.Command{
log.Fatal(err)
}

printDiscussions(discussions, since, int(issueNum))
PrintDiscussions(discussions, since, "issues", int(issueNum))
}
},
}
Expand Down Expand Up @@ -118,10 +118,10 @@ WebURL: %s
)
}

func printDiscussions(discussions []*gitlab.Discussion, since string, issueNum int) {
func PrintDiscussions(discussions []*gitlab.Discussion, since string, idstr string, idNum int) {
NewAccessTime := time.Now().UTC()

issueEntry := fmt.Sprintf("issue%d", issueNum)
issueEntry := fmt.Sprintf("%s%d", idstr, idNum)
// if specified on command line use that, o/w use config, o/w Now
var (
CompareTime time.Time
Expand Down
74 changes: 1 addition & 73 deletions cmd/mr_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ import (
"fmt"
"log"
"strings"
"time"

"github.com/araddon/dateparse"
"github.com/charmbracelet/glamour"
"github.com/fatih/color"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/config"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)
Expand Down Expand Up @@ -80,7 +76,7 @@ var mrShowCmd = &cobra.Command{
log.Fatal(err)
}

printMRDiscussions(discussions, since, int(mrNum))
PrintDiscussions(discussions, since, "mr", int(mrNum))
}
},
}
Expand Down Expand Up @@ -162,74 +158,6 @@ WebURL: %s
mr.Author.Username, milestone, labels, mr.WebURL)
}

func printMRDiscussions(discussions []*gitlab.Discussion, since string, mrNum int) {
NewAccessTime := time.Now().UTC()

mrEntry := fmt.Sprintf("mr%d", mrNum)
// if specified on command line use that, o/w use config, o/w Now
var (
CompareTime time.Time
err error
sinceIsSet = true
)
CompareTime, err = dateparse.ParseLocal(since)
if err != nil || CompareTime.IsZero() {
CompareTime = getMainConfig().GetTime(CommandPrefix + mrEntry)
if CompareTime.IsZero() {
CompareTime = time.Now().UTC()
}
sinceIsSet = false
}

// for available fields, see
// https://godoc.org/github.com/xanzy/go-gitlab#Note
// https://godoc.org/github.com/xanzy/go-gitlab#Discussion
for _, discussion := range discussions {
for i, note := range discussion.Notes {

// skip system notes
if note.System {
continue
}

indentHeader, indentNote := "", ""
commented := "commented"
if !time.Time(*note.CreatedAt).Equal(time.Time(*note.UpdatedAt)) {
commented = "updated comment"
}

if !discussion.IndividualNote {
indentNote = " "

if i == 0 {
commented = "started a discussion"
} else {
indentHeader = " "
}
}

printit := color.New().PrintfFunc()
printit(`
%s-----------------------------------`, indentHeader)

if time.Time(*note.UpdatedAt).After(CompareTime) {
printit = color.New(color.Bold).PrintfFunc()
}
printit(`
%s%s %s at %s
%s%s
`,
indentHeader, note.Author.Username, commented, time.Time(*note.UpdatedAt).String(),
indentNote, note.Body)
}
}

if sinceIsSet == false {
config.WriteConfigEntry(CommandPrefix+mrEntry, NewAccessTime, "", "")
}
}

func init() {
mrShowCmd.Flags().BoolP("no-markdown", "M", false, "Don't use markdown renderer to print the issue description")
mrShowCmd.Flags().BoolP("comments", "c", false, "Show comments for the merge request")
Expand Down

0 comments on commit 1ee9cd5

Please sign in to comment.