Skip to content

Commit

Permalink
mr show: Add automatic comment highlights
Browse files Browse the repository at this point in the history
One of the features that is lost with an email based git workflow (ie the
kernel) is the ability to keep track of new comments. In a mailer this is
easy because the new comments remain unread but in a text-based console
it's not so easy to do.

While the --since option allows users to highlight new comments from the
comand line, it is a bad user experience as users have to remember the
date they last viewed an MR.

A better approach is to keep per-repo metadata in .git/labmetadata.hcl
that keeps record of the last time that a merge request was viewed by the
user.  This way only comments that were added since the last user access
are highlighted without requiring user intervention.

The format of the metadata for issues is:

"mr<number>" "time stamp"

For example,

	"mr9" = "2020-08-26T18:43:33.739792801Z"

Add automatic comment highlights for 'mr show'.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Aug 27, 2020
1 parent e1076ee commit cc98e63
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
55 changes: 49 additions & 6 deletions cmd/mr_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"log"
"os"
"strings"
"time"

Expand All @@ -11,6 +12,7 @@ import (
"github.com/fatih/color"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/spf13/viper"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
Expand Down Expand Up @@ -53,7 +55,7 @@ var mrShowCmd = &cobra.Command{
log.Fatal(err)
}

printMRDiscussions(discussions, since)
printMRDiscussions(discussions, since, int(mrNum))
}
},
}
Expand Down Expand Up @@ -105,11 +107,47 @@ WebURL: %s
mr.Author.Username, milestone, labels, mr.WebURL)
}

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

sinceString, err := dateparse.ParseLocal(since)
if err != nil {
sinceString = time.Now().UTC()
// default path for metadata config file
metadatafile := ".git/lab/show_metadata.hcl"

viper.Reset()
viper.AddConfigPath(".git/lab")
viper.SetConfigName("show_metadata")
viper.SetConfigType("hcl")
// write data
if _, ok := viper.ReadInConfig().(viper.ConfigFileNotFoundError); ok {
if _, err := os.Stat(".git/lab"); os.IsNotExist(err) {
os.MkdirAll(".git/lab", os.ModePerm)
}
if err := viper.WriteConfigAs(metadatafile); err != nil {
log.Fatal(err)
}
if err := viper.ReadInConfig(); err != nil {
log.Fatal(err)
}
} else {
if err := viper.ReadInConfig(); err != nil {
log.Fatal(err)
}
}

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 = viper.GetTime(mrEntry)
if CompareTime.IsZero() {
CompareTime = time.Now().UTC()
}
sinceIsSet = false
}

// for available fields, see
Expand Down Expand Up @@ -143,7 +181,7 @@ func printMRDiscussions(discussions []*gitlab.Discussion, since string) {
printit(`
%s-----------------------------------`, indentHeader)

if time.Time(*note.UpdatedAt).After(sinceString) {
if time.Time(*note.UpdatedAt).After(CompareTime) {
printit = color.New(color.Bold).PrintfFunc()
}
printit(`
Expand All @@ -155,6 +193,11 @@ func printMRDiscussions(discussions []*gitlab.Discussion, since string) {
indentNote, note.Body)
}
}

if sinceIsSet == false {
viper.Set(mrEntry, NewAccessTime)
viper.WriteConfigAs(metadatafile)
}
}

func init() {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ module github.com/zaquestion/lab

require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/araddon/dateparse v0.0.0-20200409225146-d820a6159ab1
github.com/avast/retry-go v0.0.0-20180319101611-5469272a8171
github.com/charmbracelet/glamour v0.1.1-0.20200114010931-28cbdae8e7a9
github.com/dlclark/regexp2 v1.2.0 // indirect
github.com/fatih/color v1.9.0
github.com/gdamore/tcell v1.3.0
github.com/go-git/go-git/v5 v5.0.0
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/araddon/dateparse v0.0.0-20200409225146-d820a6159ab1 h1:TEBmxO80TM04L8IuMWk77SGL1HomBmKTdzdJLLWznxI=
github.com/araddon/dateparse v0.0.0-20200409225146-d820a6159ab1/go.mod h1:SLqhdZcd+dF3TEVL2RMoob5bBP5R1P1qkox+HtCBgGI=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
Expand Down Expand Up @@ -61,6 +63,8 @@ github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
Expand Down Expand Up @@ -146,8 +150,13 @@ github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQN
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
Expand Down Expand Up @@ -290,10 +299,12 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191018095205-727590c5006e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
Expand Down

0 comments on commit cc98e63

Please sign in to comment.