From 82e55d30e783dbe8b33269a2b745d1520ddd5224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Zywert?= Date: Sun, 18 Jul 2021 08:19:13 +0200 Subject: [PATCH] time spent input now properly handles worklog without whitespace For example - `1h30m` --- CHANGELOG.md | 4 ++++ gojira/prompt.go | 2 +- gojira/tempo.go | 2 +- gojira/utils.go | 3 +++ gojira/utils_test.go | 22 ++++++++++++++++++++-- main.go | 2 +- 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb0dcc9..131ffd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.2.2] - 2021-05-04 +### Fixed +- time spent input now properly handles lack of whitespace between time parts - `1h30m` + ## [0.2.1] - 2021-05-04 ### Fixed - `gojira worklogs` now properly reports overall time spent after editing work log diff --git a/gojira/prompt.go b/gojira/prompt.go index e02546a..3892a79 100644 --- a/gojira/prompt.go +++ b/gojira/prompt.go @@ -30,7 +30,7 @@ func PromptForTimeSpent(promptLabel string) (string, error) { return "", err } - return result, nil + return FormatTimeSpent(TimeSpentToSeconds(result)), nil } func PromptForIssueSelection(issues []Issue) (Issue, error) { diff --git a/gojira/tempo.go b/gojira/tempo.go index b1e59e4..dc3218e 100644 --- a/gojira/tempo.go +++ b/gojira/tempo.go @@ -78,7 +78,7 @@ func GetWorkLogs() []WorkLog { } func TimeSpentToSeconds(timeSpent string) int { - r, _ := regexp.Compile("(([0-9]+)h)?(([0-9]+)m)?") + r, _ := regexp.Compile("(([0-9]+)h)?\\s?(([0-9]+)m)?") match := r.FindStringSubmatch(timeSpent) var timeSpentSeconds int = 0 diff --git a/gojira/utils.go b/gojira/utils.go index 768f998..67e1156 100644 --- a/gojira/utils.go +++ b/gojira/utils.go @@ -31,6 +31,9 @@ func FormatTimeSpent(timeSpentSeconds int) string { timeSpent = fmt.Sprintf("%vh", intPart) } if floatPart > 0 { + if (intPart) > 0 { + timeSpent += " " + } timeSpent = timeSpent + fmt.Sprintf("%vm", math.Round(floatPart*60)) } return timeSpent diff --git a/gojira/utils_test.go b/gojira/utils_test.go index 347c7eb..19d7a73 100644 --- a/gojira/utils_test.go +++ b/gojira/utils_test.go @@ -12,7 +12,7 @@ func TestFormatTimeSpent(t *testing.T) { {3600, "1h"}, {60, "1m"}, {900, "15m"}, - {7080, "1h58m"}, + {7080, "1h 58m"}, {901, "15m"}, {959, "16m"}, } @@ -33,7 +33,7 @@ func TestCalculateTimeSpent(t *testing.T) { {TimeSpentSeconds: 901}, // 15m {TimeSpentSeconds: 959}, // 16m } - expectedTimeSpent := "3h32m" + expectedTimeSpent := "3h 32m" actualTimeSpent := CalculateTimeSpent(fixture) @@ -60,3 +60,21 @@ func TestFindIssueKeyInString(t *testing.T) { } } + +func TestTimeSpentToSeconds(t *testing.T) { + fixtures := []struct { + TimeSpent string + expectedTimeSpentInSeconds int + }{ + {"1h 30m", 5400}, + {"1h30m", 5400}, + {"29m", 1740}, + } + + for _, fixture := range fixtures { + timeSpentInSeconds := TimeSpentToSeconds(fixture.TimeSpent) + if timeSpentInSeconds != fixture.expectedTimeSpentInSeconds { + t.Errorf("Incorrect timeSpent - got %d instead of %d", timeSpentInSeconds, fixture.expectedTimeSpentInSeconds) + } + } +} diff --git a/main.go b/main.go index cf14825..21f9ea1 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ func main() { Calling without arguments will try to detect issue from git branch, otherwise it will display list of last updated issues you're are assigned to.`, - Version: "0.2.1", + Version: "0.2.2", Before: func(context *cli.Context) error { if context.Args().First() != "config" { // dont' check envs on ConfigCommand