Allow batch operations on multiple entities (#512)

commands now accept multiple arguments where it makes sense.

#### before
```
NAME:
   tea issues close - Change state of an issue to 'closed'

USAGE:
   tea issues close [command options] <issue index>
```

#### after
```
NAME:
   tea issues close - Change state of one ore more issues to 'closed'

USAGE:
   tea issues close [command options] <issue index> [<issue index>...]
```

Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/512
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-authored-by: Norwin <noerw@noreply.gitea.io>
Co-committed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
Norwin 2022-09-27 04:35:59 +08:00 committed by 6543
parent 6a4ba6a689
commit 4487213581
12 changed files with 134 additions and 77 deletions

View File

@ -19,9 +19,9 @@ import (
// CmdIssuesClose represents a sub command of issues to close an issue // CmdIssuesClose represents a sub command of issues to close an issue
var CmdIssuesClose = cli.Command{ var CmdIssuesClose = cli.Command{
Name: "close", Name: "close",
Usage: "Change state of an issue to 'closed'", Usage: "Change state of one ore more issues to 'closed'",
Description: `Change state of an issue to 'closed'`, Description: `Change state of one ore more issues to 'closed'`,
ArgsUsage: "<issue index>", ArgsUsage: "<issue index> [<issue index>...]",
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
var s = gitea.StateClosed var s = gitea.StateClosed
return editIssueState(ctx, gitea.EditIssueOption{State: &s}) return editIssueState(ctx, gitea.EditIssueOption{State: &s})
@ -37,16 +37,23 @@ func editIssueState(cmd *cli.Context, opts gitea.EditIssueOption) error {
return fmt.Errorf(ctx.Command.ArgsUsage) return fmt.Errorf(ctx.Command.ArgsUsage)
} }
index, err := utils.ArgToIndex(ctx.Args().First()) indices, err := utils.ArgsToIndices(ctx.Args().Slice())
if err != nil { if err != nil {
return err return err
} }
issue, _, err := ctx.Login.Client().EditIssue(ctx.Owner, ctx.Repo, index, opts) client := ctx.Login.Client()
if err != nil { for _, index := range indices {
return err issue, _, err := client.EditIssue(ctx.Owner, ctx.Repo, index, opts)
} if err != nil {
return err
}
print.IssueDetails(issue, nil) if len(indices) > 1 {
fmt.Println(issue.HTMLURL)
} else {
print.IssueDetails(issue, nil)
}
}
return nil return nil
} }

View File

@ -15,9 +15,9 @@ import (
var CmdIssuesReopen = cli.Command{ var CmdIssuesReopen = cli.Command{
Name: "reopen", Name: "reopen",
Aliases: []string{"open"}, Aliases: []string{"open"},
Usage: "Change state of an issue to 'open'", Usage: "Change state of one or more issues to 'open'",
Description: `Change state of an issue to 'open'`, Description: `Change state of one or more issues to 'open'`,
ArgsUsage: "<issue index>", ArgsUsage: "<issue index> [<issue index>...]",
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
var s = gitea.StateOpen var s = gitea.StateOpen
return editIssueState(ctx, gitea.EditIssueOption{State: &s}) return editIssueState(ctx, gitea.EditIssueOption{State: &s})

View File

@ -13,9 +13,9 @@ import (
// CmdMilestonesClose represents a sub command of milestones to close an milestone // CmdMilestonesClose represents a sub command of milestones to close an milestone
var CmdMilestonesClose = cli.Command{ var CmdMilestonesClose = cli.Command{
Name: "close", Name: "close",
Usage: "Change state of an milestone to 'closed'", Usage: "Change state of one or more milestones to 'closed'",
Description: `Change state of an milestone to 'closed'`, Description: `Change state of one or more milestones to 'closed'`,
ArgsUsage: "<milestone name>", ArgsUsage: "<milestone name> [<milestone name>...]",
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
if ctx.Bool("force") { if ctx.Bool("force") {
return deleteMilestone(ctx) return deleteMilestone(ctx)

View File

@ -5,8 +5,11 @@
package milestones package milestones
import ( import (
"fmt"
"code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/print"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -16,9 +19,9 @@ import (
var CmdMilestonesReopen = cli.Command{ var CmdMilestonesReopen = cli.Command{
Name: "reopen", Name: "reopen",
Aliases: []string{"open"}, Aliases: []string{"open"},
Usage: "Change state of an milestone to 'open'", Usage: "Change state of one or more milestones to 'open'",
Description: `Change state of an milestone to 'open'`, Description: `Change state of one or more milestones to 'open'`,
ArgsUsage: "<milestone name>", ArgsUsage: "<milestone name> [<milestone name> ...]",
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
return editMilestoneStatus(ctx, false) return editMilestoneStatus(ctx, false)
}, },
@ -28,16 +31,31 @@ var CmdMilestonesReopen = cli.Command{
func editMilestoneStatus(cmd *cli.Context, close bool) error { func editMilestoneStatus(cmd *cli.Context, close bool) error {
ctx := context.InitCommand(cmd) ctx := context.InitCommand(cmd)
ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
client := ctx.Login.Client() if ctx.Args().Len() == 0 {
return fmt.Errorf(ctx.Command.ArgsUsage)
}
state := gitea.StateOpen state := gitea.StateOpen
if close { if close {
state = gitea.StateClosed state = gitea.StateClosed
} }
_, _, err := client.EditMilestoneByName(ctx.Owner, ctx.Repo, ctx.Args().First(), gitea.EditMilestoneOption{
State: &state,
Title: ctx.Args().First(),
})
return err client := ctx.Login.Client()
for _, ms := range ctx.Args().Slice() {
opts := gitea.EditMilestoneOption{
State: &state,
Title: ms,
}
milestone, _, err := client.EditMilestoneByName(ctx.Owner, ctx.Repo, ms, opts)
if err != nil {
return err
}
if ctx.Args().Len() > 1 {
fmt.Printf("%s/milestone/%d\n", ctx.GetRemoteRepoHTMLURL(), milestone.ID)
} else {
print.MilestoneDetails(milestone)
}
}
return nil
} }

View File

@ -74,6 +74,5 @@ func runOpen(cmd *cli.Context) error {
suffix = number suffix = number
} }
u := path.Join(ctx.Login.URL, ctx.Owner, ctx.Repo, suffix) return open.Run(path.Join(ctx.GetRemoteRepoHTMLURL(), suffix))
return open.Run(u)
} }

View File

@ -14,9 +14,9 @@ import (
// CmdPullsClose closes a given open pull request // CmdPullsClose closes a given open pull request
var CmdPullsClose = cli.Command{ var CmdPullsClose = cli.Command{
Name: "close", Name: "close",
Usage: "Change state of a pull request to 'closed'", Usage: "Change state of one or more pull requests to 'closed'",
Description: `Change state of a pull request to 'closed'`, Description: `Change state of one or more pull requests to 'closed'`,
ArgsUsage: "<pull index>", ArgsUsage: "<pull index> [<pull index>...]",
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
var s = gitea.StateClosed var s = gitea.StateClosed
return editPullState(ctx, gitea.EditPullRequestOption{State: &s}) return editPullState(ctx, gitea.EditPullRequestOption{State: &s})

View File

@ -23,16 +23,23 @@ func editPullState(cmd *cli.Context, opts gitea.EditPullRequestOption) error {
return fmt.Errorf("Please provide a Pull Request index") return fmt.Errorf("Please provide a Pull Request index")
} }
index, err := utils.ArgToIndex(ctx.Args().First()) indices, err := utils.ArgsToIndices(ctx.Args().Slice())
if err != nil { if err != nil {
return err return err
} }
pr, _, err := ctx.Login.Client().EditPullRequest(ctx.Owner, ctx.Repo, index, opts) client := ctx.Login.Client()
if err != nil { for _, index := range indices {
return err pr, _, err := client.EditPullRequest(ctx.Owner, ctx.Repo, index, opts)
} if err != nil {
return err
}
print.PullDetails(pr, nil, nil) if len(indices) > 1 {
fmt.Println(pr.HTMLURL)
} else {
print.PullDetails(pr, nil, nil)
}
}
return nil return nil
} }

View File

@ -15,9 +15,9 @@ import (
var CmdPullsReopen = cli.Command{ var CmdPullsReopen = cli.Command{
Name: "reopen", Name: "reopen",
Aliases: []string{"open"}, Aliases: []string{"open"},
Usage: "Change state of a pull request to 'open'", Usage: "Change state of one or more pull requests to 'open'",
Description: `Change state of a pull request to 'open'`, Description: `Change state of one or more pull requests to 'open'`,
ArgsUsage: "<pull index>", ArgsUsage: "<pull index> [<pull index>...]",
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
var s = gitea.StateOpen var s = gitea.StateOpen
return editPullState(ctx, gitea.EditPullRequestOption{State: &s}) return editPullState(ctx, gitea.EditPullRequestOption{State: &s})

View File

@ -17,9 +17,9 @@ import (
var CmdReleaseDelete = cli.Command{ var CmdReleaseDelete = cli.Command{
Name: "delete", Name: "delete",
Aliases: []string{"rm"}, Aliases: []string{"rm"},
Usage: "Delete a release", Usage: "Delete one or more releases",
Description: `Delete a release`, Description: `Delete one or more releases`,
ArgsUsage: "<release tag>", ArgsUsage: "<release tag> [<release tag>...]",
Action: runReleaseDelete, Action: runReleaseDelete,
Flags: append([]cli.Flag{ Flags: append([]cli.Flag{
&cli.BoolFlag{ &cli.BoolFlag{
@ -39,9 +39,8 @@ func runReleaseDelete(cmd *cli.Context) error {
ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
client := ctx.Login.Client() client := ctx.Login.Client()
tag := ctx.Args().First() if !ctx.Args().Present() {
if len(tag) == 0 { fmt.Println("Release tag needed to edit")
fmt.Println("Release tag needed to delete")
return nil return nil
} }
@ -50,18 +49,20 @@ func runReleaseDelete(cmd *cli.Context) error {
return nil return nil
} }
release, err := getReleaseByTag(ctx.Owner, ctx.Repo, tag, client) for _, tag := range ctx.Args().Slice() {
if err != nil { release, err := getReleaseByTag(ctx.Owner, ctx.Repo, tag, client)
return err if err != nil {
} return err
_, err = client.DeleteRelease(ctx.Owner, ctx.Repo, release.ID) }
if err != nil { _, err = client.DeleteRelease(ctx.Owner, ctx.Repo, release.ID)
return err if err != nil {
} return err
}
if ctx.Bool("delete-tag") { if ctx.Bool("delete-tag") {
_, err = client.DeleteTag(ctx.Owner, ctx.Repo, tag) _, err = client.DeleteTag(ctx.Owner, ctx.Repo, tag)
return err return err
}
} }
return nil return nil

View File

@ -19,9 +19,9 @@ import (
var CmdReleaseEdit = cli.Command{ var CmdReleaseEdit = cli.Command{
Name: "edit", Name: "edit",
Aliases: []string{"e"}, Aliases: []string{"e"},
Usage: "Edit a release", Usage: "Edit one or more releases",
Description: `Edit a release`, Description: `Edit one or more releases`,
ArgsUsage: "<release tag>", ArgsUsage: "<release tag> [<release tag>...]",
Action: runReleaseEdit, Action: runReleaseEdit,
Flags: append([]cli.Flag{ Flags: append([]cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
@ -62,16 +62,6 @@ func runReleaseEdit(cmd *cli.Context) error {
ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
client := ctx.Login.Client() client := ctx.Login.Client()
tag := ctx.Args().First()
if len(tag) == 0 {
fmt.Println("Release tag needed to edit")
return nil
}
release, err := getReleaseByTag(ctx.Owner, ctx.Repo, tag, client)
if err != nil {
return err
}
var isDraft, isPre *bool var isDraft, isPre *bool
if ctx.IsSet("draft") { if ctx.IsSet("draft") {
isDraft = gitea.OptionalBool(strings.ToLower(ctx.String("draft"))[:1] == "t") isDraft = gitea.OptionalBool(strings.ToLower(ctx.String("draft"))[:1] == "t")
@ -80,13 +70,28 @@ func runReleaseEdit(cmd *cli.Context) error {
isPre = gitea.OptionalBool(strings.ToLower(ctx.String("prerelease"))[:1] == "t") isPre = gitea.OptionalBool(strings.ToLower(ctx.String("prerelease"))[:1] == "t")
} }
_, _, err = client.EditRelease(ctx.Owner, ctx.Repo, release.ID, gitea.EditReleaseOption{ if !ctx.Args().Present() {
TagName: ctx.String("tag"), fmt.Println("Release tag needed to edit")
Target: ctx.String("target"), return nil
Title: ctx.String("title"), }
Note: ctx.String("note"),
IsDraft: isDraft, for _, tag := range ctx.Args().Slice() {
IsPrerelease: isPre, release, err := getReleaseByTag(ctx.Owner, ctx.Repo, tag, client)
}) if err != nil {
return err return err
}
_, _, err = client.EditRelease(ctx.Owner, ctx.Repo, release.ID, gitea.EditReleaseOption{
TagName: ctx.String("tag"),
Target: ctx.String("target"),
Title: ctx.String("title"),
Note: ctx.String("note"),
IsDraft: isDraft,
IsPrerelease: isPre,
})
if err != nil {
return err
}
}
return nil
} }

View File

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path"
"strings" "strings"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
@ -51,6 +52,13 @@ func (ctx *TeaContext) GetListOptions() gitea.ListOptions {
} }
} }
// GetRemoteRepoHTMLURL returns the web-ui url of the remote repo,
// after ensuring a remote repo is present in the context.
func (ctx *TeaContext) GetRemoteRepoHTMLURL() string {
ctx.Ensure(CtxRequirement{RemoteRepo: true})
return path.Join(ctx.Login.URL, ctx.Owner, ctx.Repo)
}
// Ensure checks if requirements on the context are set, and terminates otherwise. // Ensure checks if requirements on the context are set, and terminates otherwise.
func (ctx *TeaContext) Ensure(req CtxRequirement) { func (ctx *TeaContext) Ensure(req CtxRequirement) {
if req.LocalRepo && ctx.LocalRepo == nil { if req.LocalRepo && ctx.LocalRepo == nil {

View File

@ -10,6 +10,18 @@ import (
"strings" "strings"
) )
// ArgsToIndices take issue/pull index as string and returns int64s
func ArgsToIndices(args []string) ([]int64, error) {
indices := make([]int64, len(args))
for i, arg := range args {
var err error
if indices[i], err = ArgToIndex(arg); err != nil {
return nil, err
}
}
return indices, nil
}
// ArgToIndex take issue/pull index as string and return int64 // ArgToIndex take issue/pull index as string and return int64
func ArgToIndex(arg string) (int64, error) { func ArgToIndex(arg string) (int64, error) {
if strings.HasPrefix(arg, "#") { if strings.HasPrefix(arg, "#") {