2020-09-17 19:35:24 +00: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.
|
|
|
|
|
2020-09-30 05:11:33 +00:00
|
|
|
package milestones
|
2020-09-17 19:35:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-09-30 05:11:33 +00:00
|
|
|
"code.gitea.io/tea/cmd/flags"
|
2020-12-15 17:38:22 +00:00
|
|
|
"code.gitea.io/tea/modules/context"
|
2020-09-30 05:11:33 +00:00
|
|
|
"code.gitea.io/tea/modules/print"
|
|
|
|
"code.gitea.io/tea/modules/utils"
|
|
|
|
|
2020-09-17 19:35:24 +00:00
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-09-05 17:11:17 +00:00
|
|
|
var msIssuesFieldsFlag = flags.FieldsFlag(print.IssueFields, []string{
|
|
|
|
"index", "kind", "title", "state", "updated", "labels",
|
|
|
|
})
|
|
|
|
|
2020-09-17 19:35:24 +00:00
|
|
|
// CmdMilestonesIssues represents a sub command of milestones to manage issue/pull of an milestone
|
|
|
|
var CmdMilestonesIssues = cli.Command{
|
|
|
|
Name: "issues",
|
|
|
|
Aliases: []string{"i"},
|
|
|
|
Usage: "manage issue/pull of an milestone",
|
|
|
|
Description: "manage issue/pull of an milestone",
|
|
|
|
ArgsUsage: "<milestone name>",
|
|
|
|
Action: runMilestoneIssueList,
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
&CmdMilestoneAddIssue,
|
|
|
|
&CmdMilestoneRemoveIssue,
|
|
|
|
},
|
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "state",
|
|
|
|
Usage: "Filter by issue state (all|open|closed)",
|
|
|
|
DefaultText: "open",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "kind",
|
|
|
|
Usage: "Filter by kind (issue|pull)",
|
|
|
|
},
|
2020-09-30 05:11:33 +00:00
|
|
|
&flags.PaginationPageFlag,
|
|
|
|
&flags.PaginationLimitFlag,
|
2021-09-05 17:11:17 +00:00
|
|
|
msIssuesFieldsFlag,
|
2020-09-30 05:11:33 +00:00
|
|
|
}, flags.AllDefaultFlags...),
|
2020-09-17 19:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CmdMilestoneAddIssue represents a sub command of milestone issues to add an issue/pull to an milestone
|
|
|
|
var CmdMilestoneAddIssue = cli.Command{
|
|
|
|
Name: "add",
|
|
|
|
Aliases: []string{"a"},
|
|
|
|
Usage: "Add an issue/pull to an milestone",
|
|
|
|
Description: "Add an issue/pull to an milestone",
|
|
|
|
ArgsUsage: "<milestone name> <issue/pull index>",
|
|
|
|
Action: runMilestoneIssueAdd,
|
2020-09-30 05:11:33 +00:00
|
|
|
Flags: flags.AllDefaultFlags,
|
2020-09-17 19:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CmdMilestoneRemoveIssue represents a sub command of milestones to remove an issue/pull from an milestone
|
|
|
|
var CmdMilestoneRemoveIssue = cli.Command{
|
|
|
|
Name: "remove",
|
|
|
|
Aliases: []string{"r"},
|
|
|
|
Usage: "Remove an issue/pull to an milestone",
|
|
|
|
Description: "Remove an issue/pull to an milestone",
|
|
|
|
ArgsUsage: "<milestone name> <issue/pull index>",
|
|
|
|
Action: runMilestoneIssueRemove,
|
2020-09-30 05:11:33 +00:00
|
|
|
Flags: flags.AllDefaultFlags,
|
2020-09-17 19:35:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
func runMilestoneIssueList(cmd *cli.Context) error {
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
|
|
|
client := ctx.Login.Client()
|
2020-09-17 19:35:24 +00:00
|
|
|
|
|
|
|
state := gitea.StateOpen
|
|
|
|
switch ctx.String("state") {
|
|
|
|
case "all":
|
|
|
|
state = gitea.StateAll
|
|
|
|
case "closed":
|
|
|
|
state = gitea.StateClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
kind := gitea.IssueTypeAll
|
|
|
|
switch ctx.String("kind") {
|
|
|
|
case "issue":
|
|
|
|
kind = gitea.IssueTypeIssue
|
|
|
|
case "pull":
|
|
|
|
kind = gitea.IssueTypePull
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:04:59 +00:00
|
|
|
if ctx.Args().Len() != 1 {
|
|
|
|
return fmt.Errorf("Must specify milestone name")
|
|
|
|
}
|
2020-09-17 19:35:24 +00:00
|
|
|
|
|
|
|
milestone := ctx.Args().First()
|
|
|
|
// make sure milestone exist
|
2020-12-15 17:38:22 +00:00
|
|
|
_, _, err := client.GetMilestoneByName(ctx.Owner, ctx.Repo, milestone)
|
2020-09-17 19:35:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
issues, _, err := client.ListRepoIssues(ctx.Owner, ctx.Repo, gitea.ListIssueOption{
|
|
|
|
ListOptions: ctx.GetListOptions(),
|
2020-09-27 12:07:46 +00:00
|
|
|
Milestones: []string{milestone},
|
|
|
|
Type: kind,
|
|
|
|
State: state,
|
2020-09-17 19:35:24 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-05 17:11:17 +00:00
|
|
|
fields, err := msIssuesFieldsFlag.GetValues(cmd)
|
2020-12-21 15:41:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
print.IssuesPullsList(issues, ctx.Output, fields)
|
2020-09-17 19:35:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
func runMilestoneIssueAdd(cmd *cli.Context) error {
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
|
|
|
client := ctx.Login.Client()
|
2020-12-14 12:04:59 +00:00
|
|
|
if ctx.Args().Len() != 2 {
|
2020-09-17 19:35:24 +00:00
|
|
|
return fmt.Errorf("need two arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
mileName := ctx.Args().Get(0)
|
|
|
|
issueIndex := ctx.Args().Get(1)
|
2020-09-30 05:11:33 +00:00
|
|
|
idx, err := utils.ArgToIndex(issueIndex)
|
2020-09-17 19:35:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure milestone exist
|
2020-12-15 17:38:22 +00:00
|
|
|
mile, _, err := client.GetMilestoneByName(ctx.Owner, ctx.Repo, mileName)
|
2020-09-17 19:35:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
_, _, err = client.EditIssue(ctx.Owner, ctx.Repo, idx, gitea.EditIssueOption{
|
2020-09-17 19:35:24 +00:00
|
|
|
Milestone: &mile.ID,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
func runMilestoneIssueRemove(cmd *cli.Context) error {
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
|
|
|
client := ctx.Login.Client()
|
2020-12-14 12:04:59 +00:00
|
|
|
if ctx.Args().Len() != 2 {
|
2020-09-17 19:35:24 +00:00
|
|
|
return fmt.Errorf("need two arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
mileName := ctx.Args().Get(0)
|
|
|
|
issueIndex := ctx.Args().Get(1)
|
2020-09-30 05:11:33 +00:00
|
|
|
idx, err := utils.ArgToIndex(issueIndex)
|
2020-09-17 19:35:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
issue, _, err := client.GetIssue(ctx.Owner, ctx.Repo, idx)
|
2020-09-17 19:35:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if issue.Milestone == nil {
|
|
|
|
return fmt.Errorf("issue is not assigned to a milestone")
|
|
|
|
}
|
|
|
|
|
|
|
|
if issue.Milestone.Title != mileName {
|
|
|
|
return fmt.Errorf("issue is not assigned to this milestone")
|
|
|
|
}
|
|
|
|
|
|
|
|
zero := int64(0)
|
2020-12-15 17:38:22 +00:00
|
|
|
_, _, err = client.EditIssue(ctx.Owner, ctx.Repo, idx, gitea.EditIssueOption{
|
2020-09-17 19:35:24 +00:00
|
|
|
Milestone: &zero,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|