2020-03-05 22:43:28 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-09-30 01:11:33 -04:00
|
|
|
"code.gitea.io/tea/cmd/flags"
|
|
|
|
"code.gitea.io/tea/cmd/times"
|
|
|
|
"code.gitea.io/tea/modules/config"
|
|
|
|
"code.gitea.io/tea/modules/print"
|
|
|
|
"code.gitea.io/tea/modules/utils"
|
2020-03-05 22:43:28 -05:00
|
|
|
|
2020-09-30 01:11:33 -04:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2020-03-05 22:43:28 -05:00
|
|
|
"github.com/araddon/dateparse"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdTrackedTimes represents the command to operate repositories' times.
|
|
|
|
var CmdTrackedTimes = cli.Command{
|
|
|
|
Name: "times",
|
|
|
|
Aliases: []string{"time"},
|
|
|
|
Usage: "Operate on tracked times of a repository's issues & pulls",
|
|
|
|
Description: `Operate on tracked times of a repository's issues & pulls.
|
|
|
|
Depending on your permissions on the repository, only your own tracked
|
|
|
|
times might be listed.`,
|
|
|
|
ArgsUsage: "[username | #issue]",
|
|
|
|
Action: runTrackedTimes,
|
|
|
|
Subcommands: []*cli.Command{
|
2020-09-30 01:11:33 -04:00
|
|
|
×.CmdTrackedTimesAdd,
|
|
|
|
×.CmdTrackedTimesDelete,
|
|
|
|
×.CmdTrackedTimesReset,
|
2020-03-05 22:43:28 -05:00
|
|
|
},
|
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "from",
|
|
|
|
Aliases: []string{"f"},
|
|
|
|
Usage: "Show only times tracked after this date",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "until",
|
|
|
|
Aliases: []string{"u"},
|
|
|
|
Usage: "Show only times tracked before this date",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "total",
|
|
|
|
Aliases: []string{"t"},
|
|
|
|
Usage: "Print the total duration at the end",
|
|
|
|
},
|
2020-09-30 01:11:33 -04:00
|
|
|
}, flags.AllDefaultFlags...),
|
2020-03-05 22:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func runTrackedTimes(ctx *cli.Context) error {
|
2020-09-30 01:11:33 -04:00
|
|
|
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
|
2020-03-05 22:43:28 -05:00
|
|
|
client := login.Client()
|
|
|
|
|
|
|
|
if err := client.CheckServerVersionConstraint(">= 1.11"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var times []*gitea.TrackedTime
|
|
|
|
var err error
|
|
|
|
|
|
|
|
user := ctx.Args().First()
|
|
|
|
fmt.Println(ctx.Command.ArgsUsage)
|
|
|
|
if user == "" {
|
|
|
|
// get all tracked times on the repo
|
2020-09-15 22:01:41 -04:00
|
|
|
times, _, err = client.GetRepoTrackedTimes(owner, repo)
|
2020-03-05 22:43:28 -05:00
|
|
|
} else if strings.HasPrefix(user, "#") {
|
|
|
|
// get all tracked times on the specified issue
|
2020-09-30 01:11:33 -04:00
|
|
|
issue, err := utils.ArgToIndex(user)
|
2020-04-18 23:09:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-03-05 22:43:28 -05:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
times, _, err = client.ListTrackedTimes(owner, repo, issue, gitea.ListTrackedTimesOptions{})
|
2020-03-05 22:43:28 -05:00
|
|
|
} else {
|
|
|
|
// get all tracked times by the specified user
|
2020-09-15 22:01:41 -04:00
|
|
|
times, _, err = client.GetUserTrackedTimes(owner, repo, user)
|
2020-03-05 22:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var from, until time.Time
|
|
|
|
if ctx.String("from") != "" {
|
|
|
|
from, err = dateparse.ParseLocal(ctx.String("from"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ctx.String("until") != "" {
|
|
|
|
until, err = dateparse.ParseLocal(ctx.String("until"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 01:11:33 -04:00
|
|
|
print.TrackedTimesList(times, flags.GlobalOutputValue, from, until, ctx.Bool("total"))
|
2020-03-05 22:43:28 -05:00
|
|
|
return nil
|
|
|
|
}
|