Skip to content

Commit

Permalink
Reuse summarize flag for merge
Browse files Browse the repository at this point in the history
  • Loading branch information
berlam committed Oct 4, 2019
1 parent 7d6f938 commit aa36466
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {
addCmd.PersistentFlags().IntVar(&conf.Month, internal.FlagMonth, int(time.Now().Month()), "specify the month")
addCmd.PersistentFlags().IntVar(&conf.Day, internal.FlagDay, time.Now().Day(), "specify the day")
addCmd.PersistentFlags().StringVar(&conf.Task, internal.FlagTask, "", "specify the task")
addCmd.PersistentFlags().BoolVar(&conf.Duration.Merge, internal.FlagMerge, false, "merge effort on same day and task")
addCmd.PersistentFlags().BoolVarP(&conf.Duration.Summarize, internal.FlagSummarize, "s", false, "sum effort on same day and task")
addCmd.MarkFlagRequired(internal.FlagTask)
}

Expand Down Expand Up @@ -56,7 +56,7 @@ var addJiraCmd = &cobra.Command{
conf.Day,
pkg.Task(conf.Task),
duration,
conf.Duration.Merge,
conf.Duration.Summarize,
cli.Confirmation,
)
return nil
Expand Down
2 changes: 0 additions & 2 deletions internal/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const (
FlagEmpty = "empty"
FlagDecimal = "decimal"
FlagNegate = "negate"
FlagMerge = "merge"
FlagYear = "year"
FlagMonth = "month"
FlagDay = "day"
Expand Down Expand Up @@ -46,7 +45,6 @@ type DurationOptions struct {
Empty bool `mapstructure:"empty"`
Decimal bool `mapstructure:"decimal"`
Negate bool `mapstructure:"negate"`
Merge bool `mapstructure:"merge"`
}

func (c *Configuration) Server() *url.URL {
Expand Down
23 changes: 17 additions & 6 deletions pkg/jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func GetBulkTimesheet(client *http.Client, server *url.URL, userinfo *url.Userin
return do(api, year, month, projects, accounts)
}

func AddWorklogItem(client *http.Client, server *url.URL, userinfo *url.Userinfo, year int, month time.Month, day int, task pkg.Task, duration time.Duration, merge bool, confirm model.WorklogFunc) {
func AddWorklogItem(client *http.Client, server *url.URL, userinfo *url.Userinfo, year int, month time.Month, day int, task pkg.Task, duration time.Duration, sum bool, confirm model.WorklogFunc) {
var err error
api, err := getApiVersion(client, server, userinfo)
if err != nil {
Expand All @@ -115,11 +115,10 @@ func AddWorklogItem(client *http.Client, server *url.URL, userinfo *url.Userinfo
}

key := model.IssueKey(task)
date := time.Date(year, month, day, 0, 0, 0, 0, location)

if !merge {
if !sum {
// Add new effort
err = api.AddWorklog(key, date, duration)
err = api.AddWorklog(key, adjustDateTime(location, duration, year, month, day), duration)
if err != nil {
log.Println("Could not add effort.", err)
}
Expand All @@ -130,7 +129,7 @@ func AddWorklogItem(client *http.Client, server *url.URL, userinfo *url.Userinfo
var effort []model.Worklog
err = api.Worklog(key, func(worklog model.Worklog) bool {
wd := worklog.Date().In(location)
if worklog.Author().Id() == account && date.Year() == wd.Year() && date.Month() == wd.Month() && date.Day() == wd.Day() {
if worklog.Author().Id() == account && year == wd.Year() && month == wd.Month() && day == wd.Day() {
effort = append(effort, worklog)
}
return true
Expand All @@ -146,7 +145,7 @@ func AddWorklogItem(client *http.Client, server *url.URL, userinfo *url.Userinfo
}

// Add new effort
err = api.AddWorklog(key, date, duration)
err = api.AddWorklog(key, adjustDateTime(location, duration, year, month, day), duration)
if err != nil {
log.Println("Could not add effort.", err)
return
Expand All @@ -163,6 +162,18 @@ func AddWorklogItem(client *http.Client, server *url.URL, userinfo *url.Userinfo
}
}

func adjustDateTime(location *time.Location, duration time.Duration, year int, month time.Month, day int) time.Time {
// Get the current date and time
// Sub the given duration
date := time.Now().In(location).Add(-duration)
// If we have a different day then, adjust the time to 00:00:00
// Also applies, if we are not on the current day
if date.Year() != year || date.Month() != month || date.Day() != day {
date = time.Date(year, month, day, 0, 0, 0, 0, location)
}
return date
}

func RemoveWorklogItem(client *http.Client, server *url.URL, userinfo *url.Userinfo, year int, month time.Month, day int, task pkg.Task, confirm model.WorklogFunc) {
var err error
api, err := getApiVersion(client, server, userinfo)
Expand Down

0 comments on commit aa36466

Please sign in to comment.