Skip to content

Commit

Permalink
CORE-47 Jira autolog time (#50)
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit c30d3ac
Author: tavo <[email protected]>
Date:   Tue Dec 15 15:48:00 2020 +0200

    Style fixes

commit d688340
Author: tavo <[email protected]>
Date:   Tue Dec 15 11:45:23 2020 +0200

    Add jira auto-log

commit 77d0412
Author: tavo <[email protected]>
Date:   Tue Dec 15 10:28:30 2020 +0200

    no ssh?

commit 1463cd9
Author: tavo <[email protected]>
Date:   Tue Dec 15 10:23:19 2020 +0200

    test with older git2go

commit a9f1cf2
Author: tavo <[email protected]>
Date:   Tue Dec 15 10:11:08 2020 +0200

    test with zlib

commit 3700484
Author: tavo <[email protected]>
Date:   Tue Dec 15 09:57:27 2020 +0200

    test with win-2019

commit 1783bb6
Author: tavo <[email protected]>
Date:   Tue Dec 15 09:37:50 2020 +0200

    Install openssl for windows
  • Loading branch information
kilpkonn committed Dec 16, 2020
1 parent 121f48d commit 424e82e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Usage: gtm init [options]
Options:
-terminal=true Enable time tracking for terminal (requires Terminal plug-in).
-auto-log="" Enable automatic logging to commits for platform [gitlab].
-auto-log="" Enable automatic logging to commits for platform [gitlab, jira].
-local=false Initialize gtm locally, ak no push / fetch hooks are added.
-tags=tag1,tag2 Add tags to projects, multiple calls appends tags.
-clear-tags Clear all tags.
Expand Down
5 changes: 4 additions & 1 deletion command/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Options:
-long-duration If total-only, display total pending time in long duration format
-tags="" Project tags to report status for, i.e --tags tag1,tag2
-all=false Show status for all projects
-auto-log="" Format output for auto logging time [gitlab, jira]
-cwd="" Set cwd (useful for plugins)
`
return strings.TrimSpace(helpText)
Expand All @@ -51,7 +52,7 @@ Options:
// Run executes status command with args
func (c StatusCmd) Run(args []string) int {
var color, terminalOff, appOff, totalOnly, all, profile, longDuration bool
var tags, cwd string
var tags, cwd, autoLog string
cmdFlags := flag.NewFlagSet("status", flag.ContinueOnError)
cmdFlags.BoolVar(&color, "color", false, "Always output color even if no terminal is detected. Use this with pagers i.e 'less -R' or 'more -R'")
cmdFlags.BoolVar(&terminalOff, "terminal-off", false, "Exclude time spent in terminal (Terminal plugin is required)")
Expand All @@ -60,6 +61,7 @@ func (c StatusCmd) Run(args []string) int {
cmdFlags.BoolVar(&longDuration, "long-duration", false, "Display total time in long duration format")
cmdFlags.StringVar(&tags, "tags", "", "Project tags to show status on")
cmdFlags.BoolVar(&all, "all", false, "Show status for all projects")
cmdFlags.StringVar(&autoLog, "auto-log", "", "Format time for auto logging")
cmdFlags.StringVar(&cwd, "cwd", "", "Set cwd")
cmdFlags.BoolVar(&profile, "profile", false, "Enable profiling")
cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
Expand Down Expand Up @@ -102,6 +104,7 @@ func (c StatusCmd) Run(args []string) int {

options := report.OutputOptions{
TotalOnly: totalOnly,
AutoLog: autoLog,
LongDuration: longDuration,
TerminalOff: terminalOff,
AppOff: appOff,
Expand Down
20 changes: 15 additions & 5 deletions project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,18 @@ var (
GitLabHooks = map[string]scm.GitHook{
"prepare-commit-msg": {
Exe: "git",
Command: "echo -n \"/spend \" >> $1; gtm status -total-only >> $1",
Command: "gtm status --auto-log=gitlab >> $1",
RE: regexp.MustCompile(
`(?s)[/:a-zA-Z0-9$_=()"\.\|\-\\ ]*echo\s+-n\s+"/spend\s+"\s+>>\s+\$1;` +
`\s+gtm(.exe"|)\s+status\s+-total-only\s+>>\s+\$1\.*`),
`(?s)[/:a-zA-Z0-9$_=()"\.\|\-\\ ]*gtm(.exe"|)\s+status\s+--auto-log=gitlab\s+>>\s+\$1\.*`),
},
}

JiraHooks = map[string]scm.GitHook{
"prepare-commit-msg": {
Exe: "git",
Command: "gtm status --auto-log=jira >> $1",
RE: regexp.MustCompile(
`(?s)[/:a-zA-Z0-9$_=()"\.\|\-\\ ]*gtm(.exe"|)\s+status\s+--auto-log=jira\s+>>\s+\$1\.*`),
},
}
)
Expand Down Expand Up @@ -221,8 +229,10 @@ func SetupHooks(local bool, gitRepoPath, autoLog string) error {
for k, v := range GitLabHooks {
GitHooks[k] = v
}
case "github":
// TODO Add hooks
case "jira":
for k, v := range JiraHooks {
GitHooks[k] = v
}
}

if err := scm.SetHooks(GitHooks, gitRepoPath); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type OutputOptions struct {
Color bool
Limit int
Subdir string
AutoLog string
}

func (o OutputOptions) limitNotes(notes commitNoteDetails) commitNoteDetails {
Expand All @@ -64,6 +65,13 @@ func Status(n note.CommitNote, options OutputOptions, projPath ...string) (strin
n = n.FilterOutApp()
}

switch options.AutoLog {
case "gitlab":
return fmt.Sprintf("/spend %s", util.DurationStr(n.Total())), nil
case "jira":
return fmt.Sprintf("#time %s", util.DurationStrJira(n.Total())), nil
}

if options.TotalOnly {
if options.LongDuration {
return util.DurationStrLong(n.Total()), nil
Expand Down
17 changes: 17 additions & 0 deletions util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import (
"github.com/hako/durafmt"
)

const (
SecInMinute = 60
MinInHour = 60
HoursInDay = 8
DaysInWeek = 5
)

// Percent returns a values percent of the total
func Percent(val, total int) float64 {
if total == 0 {
Expand Down Expand Up @@ -43,6 +50,16 @@ func DurationStr(secs int) string {
return (time.Duration(secs) * time.Second).String()
}

// DurationStrJira returns seconds as duration string, i.e. 1d 9h 10m
func DurationStrJira(secs int) string {
total := (time.Duration(secs) * time.Second).Truncate(time.Second).Seconds()
weeks := int(total / (DaysInWeek * HoursInDay * MinInHour * SecInMinute))
days := int(total/(HoursInDay*MinInHour*SecInMinute)) % DaysInWeek
hours := int(total/(MinInHour*SecInMinute)) % HoursInDay
minutes := int(total/SecInMinute) % SecInMinute
return fmt.Sprintf("%dw %dd %dh %dm", weeks, days, hours, minutes)
}

// DurationStrLong returns a human readable format for the duration
func DurationStrLong(secs int) string {
d, err := durafmt.ParseString(DurationStr(secs))
Expand Down

0 comments on commit 424e82e

Please sign in to comment.