2020-12-07 13:29:48 +01: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 times
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/tea/cmd/flags"
|
2020-12-15 18:38:22 +01:00
|
|
|
"code.gitea.io/tea/modules/context"
|
2020-12-07 13:29:48 +01:00
|
|
|
"code.gitea.io/tea/modules/print"
|
|
|
|
"code.gitea.io/tea/modules/utils"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"github.com/araddon/dateparse"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdTrackedTimesList represents a sub command of times to list them
|
|
|
|
var CmdTrackedTimesList = cli.Command{
|
|
|
|
Name: "ls",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Action: RunTimesList,
|
|
|
|
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]",
|
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}, flags.AllDefaultFlags...),
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunTimesList list repositories
|
2020-12-15 18:38:22 +01:00
|
|
|
func RunTimesList(cmd *cli.Context) error {
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
|
|
|
client := ctx.Login.Client()
|
2020-12-07 13:29:48 +01:00
|
|
|
|
|
|
|
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-12-15 18:38:22 +01:00
|
|
|
times, _, err = client.GetRepoTrackedTimes(ctx.Owner, ctx.Repo)
|
2020-12-07 13:29:48 +01:00
|
|
|
} else if strings.HasPrefix(user, "#") {
|
|
|
|
// get all tracked times on the specified issue
|
|
|
|
issue, err := utils.ArgToIndex(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-15 18:38:22 +01:00
|
|
|
times, _, err = client.ListTrackedTimes(ctx.Owner, ctx.Repo, issue, gitea.ListTrackedTimesOptions{})
|
2020-12-07 13:29:48 +01:00
|
|
|
} else {
|
|
|
|
// get all tracked times by the specified user
|
2020-12-15 18:38:22 +01:00
|
|
|
times, _, err = client.GetUserTrackedTimes(ctx.Owner, ctx.Repo, user)
|
2020-12-07 13:29:48 +01: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-12-15 18:38:22 +01:00
|
|
|
print.TrackedTimesList(times, ctx.Output, from, until, ctx.Bool("total"))
|
2020-12-07 13:29:48 +01:00
|
|
|
return nil
|
|
|
|
}
|