2020-12-07 20:29:48 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-08 09:40:02 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-07 20:29:48 +08:00
|
|
|
|
|
|
|
package times
|
|
|
|
|
|
|
|
import (
|
2020-12-23 12:58:36 +08:00
|
|
|
"fmt"
|
2020-12-07 20:29:48 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/tea/cmd/flags"
|
2020-12-16 01:38:22 +08:00
|
|
|
"code.gitea.io/tea/modules/context"
|
2020-12-07 20:29:48 +08: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"
|
|
|
|
)
|
|
|
|
|
2021-09-06 01:11:17 +08:00
|
|
|
// NOTE: not using NewCsvFlag, as we don't want an alias & default value.
|
|
|
|
var timeFieldsFlag = &flags.CsvFlag{
|
|
|
|
AvailableFields: print.TrackedTimeFields,
|
|
|
|
StringFlag: cli.StringFlag{
|
|
|
|
Name: "fields",
|
|
|
|
Usage: fmt.Sprintf(`Comma-separated list of fields to print. Available values:
|
|
|
|
%s
|
|
|
|
`, strings.Join(print.TrackedTimeFields, ",")),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-12-07 20:29:48 +08:00
|
|
|
// CmdTrackedTimesList represents a sub command of times to list them
|
|
|
|
var CmdTrackedTimesList = cli.Command{
|
2020-12-17 00:47:40 +08:00
|
|
|
Name: "list",
|
|
|
|
Aliases: []string{"ls"},
|
2020-12-07 20:29:48 +08:00
|
|
|
Action: RunTimesList,
|
2020-12-23 12:58:36 +08:00
|
|
|
Usage: "List tracked times on issues & pulls",
|
|
|
|
Description: `List tracked times, across repos, or on a single repo or issue:
|
|
|
|
- given a username all times on a repo by that user are shown,
|
|
|
|
- given a issue index with '#' prefix, all times on that issue are listed,
|
|
|
|
- given --mine, your times are listed across all repositories.
|
|
|
|
Depending on your permissions on the repository, only your own tracked times might be listed.`,
|
2020-12-07 20:29:48 +08:00
|
|
|
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",
|
|
|
|
},
|
2020-12-23 12:58:36 +08:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "mine",
|
|
|
|
Aliases: []string{"m"},
|
|
|
|
Usage: "Show all times tracked by you across all repositories (overrides command arguments)",
|
|
|
|
},
|
2021-09-06 01:11:17 +08:00
|
|
|
timeFieldsFlag,
|
2020-12-07 20:29:48 +08:00
|
|
|
}, flags.AllDefaultFlags...),
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunTimesList list repositories
|
2020-12-16 01:38:22 +08:00
|
|
|
func RunTimesList(cmd *cli.Context) error {
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
|
|
|
client := ctx.Login.Client()
|
2020-12-07 20:29:48 +08:00
|
|
|
|
|
|
|
var times []*gitea.TrackedTime
|
|
|
|
var err error
|
2020-12-23 12:58:36 +08:00
|
|
|
var from, until time.Time
|
|
|
|
var fields []string
|
|
|
|
|
|
|
|
if ctx.IsSet("from") {
|
|
|
|
from, err = dateparse.ParseLocal(ctx.String("from"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ctx.IsSet("until") {
|
|
|
|
until, err = dateparse.ParseLocal(ctx.String("until"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := gitea.ListTrackedTimesOptions{Since: from, Before: until}
|
2020-12-07 20:29:48 +08:00
|
|
|
|
|
|
|
user := ctx.Args().First()
|
2020-12-23 12:58:36 +08:00
|
|
|
if ctx.Bool("mine") {
|
|
|
|
times, _, err = client.GetMyTrackedTimes()
|
|
|
|
fields = []string{"created", "repo", "issue", "duration"}
|
|
|
|
} else if user == "" {
|
2020-12-07 20:29:48 +08:00
|
|
|
// get all tracked times on the repo
|
2020-12-23 12:58:36 +08:00
|
|
|
times, _, err = client.ListRepoTrackedTimes(ctx.Owner, ctx.Repo, opts)
|
|
|
|
fields = []string{"created", "issue", "user", "duration"}
|
2020-12-07 20:29:48 +08: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-23 12:58:36 +08:00
|
|
|
times, _, err = client.ListIssueTrackedTimes(ctx.Owner, ctx.Repo, issue, opts)
|
|
|
|
fields = []string{"created", "user", "duration"}
|
2020-12-07 20:29:48 +08:00
|
|
|
} else {
|
|
|
|
// get all tracked times by the specified user
|
2020-12-23 12:58:36 +08:00
|
|
|
opts.User = user
|
|
|
|
times, _, err = client.ListRepoTrackedTimes(ctx.Owner, ctx.Repo, opts)
|
|
|
|
fields = []string{"created", "issue", "duration"}
|
2020-12-07 20:29:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-23 12:58:36 +08:00
|
|
|
if ctx.IsSet("fields") {
|
2021-09-06 01:11:17 +08:00
|
|
|
if fields, err = timeFieldsFlag.GetValues(cmd); err != nil {
|
2020-12-07 20:29:48 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 12:58:36 +08:00
|
|
|
print.TrackedTimesList(times, ctx.Output, fields, ctx.Bool("total"))
|
2020-12-07 20:29:48 +08:00
|
|
|
return nil
|
|
|
|
}
|