2018-09-03 06:43:00 +00:00
|
|
|
// Copyright 2018 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"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
|
2020-09-19 16:00:50 +00:00
|
|
|
"github.com/charmbracelet/glamour"
|
2020-01-04 17:44:25 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-09-03 06:43:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdIssues represents to login a gitea server.
|
|
|
|
var CmdIssues = cli.Command{
|
|
|
|
Name: "issues",
|
2019-11-03 20:34:41 +00:00
|
|
|
Usage: "List and create issues",
|
|
|
|
Description: `List and create issues`,
|
2020-04-22 16:09:26 +00:00
|
|
|
ArgsUsage: "[<issue index>]",
|
2018-09-03 06:43:00 +00:00
|
|
|
Action: runIssues,
|
2020-01-04 17:44:25 +00:00
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
&CmdIssuesList,
|
|
|
|
&CmdIssuesCreate,
|
2020-04-22 16:09:26 +00:00
|
|
|
&CmdIssuesReopen,
|
|
|
|
&CmdIssuesClose,
|
2018-09-03 06:43:00 +00:00
|
|
|
},
|
2019-10-23 15:58:18 +00:00
|
|
|
Flags: AllDefaultFlags,
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
// CmdIssuesList represents a sub command of issues to list issues
|
2018-09-03 06:43:00 +00:00
|
|
|
var CmdIssuesList = cli.Command{
|
|
|
|
Name: "ls",
|
2019-03-12 01:36:14 +00:00
|
|
|
Usage: "List issues of the repository",
|
|
|
|
Description: `List issues of the repository`,
|
2018-09-03 06:43:00 +00:00
|
|
|
Action: runIssuesList,
|
2020-03-29 13:16:06 +00:00
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "state",
|
|
|
|
Usage: "Filter by issue state (all|open|closed)",
|
|
|
|
DefaultText: "open",
|
|
|
|
},
|
|
|
|
}, AllDefaultFlags...),
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runIssues(ctx *cli.Context) error {
|
2020-03-29 13:16:06 +00:00
|
|
|
if ctx.Args().Len() == 1 {
|
|
|
|
return runIssueDetail(ctx, ctx.Args().First())
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
|
|
|
return runIssuesList(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runIssueDetail(ctx *cli.Context, index string) error {
|
2019-10-23 15:58:18 +00:00
|
|
|
login, owner, repo := initCommand()
|
2018-09-03 06:43:00 +00:00
|
|
|
|
2020-04-19 03:09:03 +00:00
|
|
|
idx, err := argToIndex(index)
|
2018-09-03 06:43:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-16 02:01:41 +00:00
|
|
|
issue, _, err := login.Client().GetIssue(owner, repo, idx)
|
2018-09-03 06:43:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-19 16:00:50 +00:00
|
|
|
in := fmt.Sprintf("# #%d %s\n%s created %s\n\n%s\n", issue.Index,
|
2018-09-03 06:43:00 +00:00
|
|
|
issue.Title,
|
|
|
|
issue.Poster.UserName,
|
|
|
|
issue.Created.Format("2006-01-02 15:04:05"),
|
|
|
|
issue.Body,
|
|
|
|
)
|
2020-09-19 16:00:50 +00:00
|
|
|
out, err := glamour.Render(in, getGlamourTheme())
|
|
|
|
fmt.Print(out)
|
2018-09-03 06:43:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runIssuesList(ctx *cli.Context) error {
|
2019-10-23 15:58:18 +00:00
|
|
|
login, owner, repo := initCommand()
|
2018-09-03 06:43:00 +00:00
|
|
|
|
2020-03-29 13:16:06 +00:00
|
|
|
state := gitea.StateOpen
|
|
|
|
switch ctx.String("state") {
|
|
|
|
case "all":
|
|
|
|
state = gitea.StateAll
|
|
|
|
case "open":
|
|
|
|
state = gitea.StateOpen
|
|
|
|
case "closed":
|
|
|
|
state = gitea.StateClosed
|
|
|
|
}
|
|
|
|
|
2020-09-16 02:01:41 +00:00
|
|
|
issues, _, err := login.Client().ListRepoIssues(owner, repo, gitea.ListIssueOption{
|
2020-05-27 13:48:09 +00:00
|
|
|
State: state,
|
2020-04-01 08:35:42 +00:00
|
|
|
Type: gitea.IssueTypeIssue,
|
2018-09-03 06:43:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-09-15 08:38:18 +00:00
|
|
|
headers := []string{
|
|
|
|
"Index",
|
2020-09-17 19:35:24 +00:00
|
|
|
"Title",
|
2020-03-29 13:16:06 +00:00
|
|
|
"State",
|
|
|
|
"Author",
|
2020-09-17 19:35:24 +00:00
|
|
|
"Milestone",
|
2019-09-15 08:38:18 +00:00
|
|
|
"Updated",
|
|
|
|
}
|
|
|
|
|
|
|
|
var values [][]string
|
|
|
|
|
2018-09-03 06:43:00 +00:00
|
|
|
if len(issues) == 0 {
|
2019-10-26 14:25:54 +00:00
|
|
|
Output(outputValue, headers, values)
|
2018-09-03 06:43:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
2020-09-17 19:35:24 +00:00
|
|
|
author := issue.Poster.FullName
|
|
|
|
if len(author) == 0 {
|
|
|
|
author = issue.Poster.UserName
|
|
|
|
}
|
|
|
|
mile := ""
|
|
|
|
if issue.Milestone != nil {
|
|
|
|
mile = issue.Milestone.Title
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
2019-09-15 08:38:18 +00:00
|
|
|
values = append(
|
|
|
|
values,
|
|
|
|
[]string{
|
|
|
|
strconv.FormatInt(issue.Index, 10),
|
2020-09-17 19:35:24 +00:00
|
|
|
issue.Title,
|
2020-03-29 13:16:06 +00:00
|
|
|
string(issue.State),
|
2020-09-17 19:35:24 +00:00
|
|
|
author,
|
|
|
|
mile,
|
2019-09-15 08:38:18 +00:00
|
|
|
issue.Updated.Format("2006-01-02 15:04:05"),
|
|
|
|
},
|
|
|
|
)
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
2019-10-26 14:25:54 +00:00
|
|
|
Output(outputValue, headers, values)
|
2018-09-03 06:43:00 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
// CmdIssuesCreate represents a sub command of issues to create issue
|
2018-09-03 06:43:00 +00:00
|
|
|
var CmdIssuesCreate = cli.Command{
|
|
|
|
Name: "create",
|
|
|
|
Usage: "Create an issue on repository",
|
|
|
|
Description: `Create an issue on repository`,
|
|
|
|
Action: runIssuesCreate,
|
2019-10-23 15:58:18 +00:00
|
|
|
Flags: append([]cli.Flag{
|
2020-01-04 17:44:25 +00:00
|
|
|
&cli.StringFlag{
|
2020-03-06 03:43:28 +00:00
|
|
|
Name: "title",
|
|
|
|
Aliases: []string{"t"},
|
|
|
|
Usage: "issue title to create",
|
2018-09-03 06:43:00 +00:00
|
|
|
},
|
2020-01-04 17:44:25 +00:00
|
|
|
&cli.StringFlag{
|
2020-03-06 03:43:28 +00:00
|
|
|
Name: "body",
|
|
|
|
Aliases: []string{"b"},
|
|
|
|
Usage: "issue body to create",
|
2018-09-03 06:43:00 +00:00
|
|
|
},
|
2019-10-23 15:58:18 +00:00
|
|
|
}, LoginRepoFlags...),
|
2019-03-11 16:32:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 06:43:00 +00:00
|
|
|
func runIssuesCreate(ctx *cli.Context) error {
|
2019-10-23 15:58:18 +00:00
|
|
|
login, owner, repo := initCommand()
|
2018-09-03 06:43:00 +00:00
|
|
|
|
2020-09-16 02:01:41 +00:00
|
|
|
_, _, err := login.Client().CreateIssue(owner, repo, gitea.CreateIssueOption{
|
2018-09-03 06:43:00 +00:00
|
|
|
Title: ctx.String("title"),
|
|
|
|
Body: ctx.String("body"),
|
|
|
|
// TODO:
|
|
|
|
//Assignee string `json:"assignee"`
|
|
|
|
//Assignees []string `json:"assignees"`
|
|
|
|
//Deadline *time.Time `json:"due_date"`
|
|
|
|
//Milestone int64 `json:"milestone"`
|
|
|
|
//Labels []int64 `json:"labels"`
|
|
|
|
//Closed bool `json:"closed"`
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-22 16:09:26 +00:00
|
|
|
|
|
|
|
// CmdIssuesReopen represents a sub command of issues to open an issue
|
|
|
|
var CmdIssuesReopen = cli.Command{
|
|
|
|
Name: "reopen",
|
|
|
|
Aliases: []string{"open"},
|
|
|
|
Usage: "Change state of an issue to 'open'",
|
|
|
|
Description: `Change state of an issue to 'open'`,
|
|
|
|
ArgsUsage: "<issue index>",
|
|
|
|
Action: func(ctx *cli.Context) error {
|
2020-05-27 13:48:09 +00:00
|
|
|
var s = gitea.StateOpen
|
2020-04-22 16:09:26 +00:00
|
|
|
return editIssueState(ctx, gitea.EditIssueOption{State: &s})
|
|
|
|
},
|
|
|
|
Flags: AllDefaultFlags,
|
|
|
|
}
|
|
|
|
|
|
|
|
// CmdIssuesClose represents a sub command of issues to close an issue
|
|
|
|
var CmdIssuesClose = cli.Command{
|
|
|
|
Name: "close",
|
|
|
|
Usage: "Change state of an issue to 'closed'",
|
|
|
|
Description: `Change state of an issue to 'closed'`,
|
|
|
|
ArgsUsage: "<issue index>",
|
|
|
|
Action: func(ctx *cli.Context) error {
|
2020-05-27 13:48:09 +00:00
|
|
|
var s = gitea.StateClosed
|
2020-04-22 16:09:26 +00:00
|
|
|
return editIssueState(ctx, gitea.EditIssueOption{State: &s})
|
|
|
|
},
|
|
|
|
Flags: AllDefaultFlags,
|
|
|
|
}
|
|
|
|
|
|
|
|
// editIssueState abstracts the arg parsing to edit the given issue
|
|
|
|
func editIssueState(ctx *cli.Context, opts gitea.EditIssueOption) error {
|
|
|
|
login, owner, repo := initCommand()
|
|
|
|
if ctx.Args().Len() == 0 {
|
|
|
|
log.Fatal(ctx.Command.ArgsUsage)
|
|
|
|
}
|
|
|
|
|
|
|
|
index, err := argToIndex(ctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-16 02:01:41 +00:00
|
|
|
_, _, err = login.Client().EditIssue(owner, repo, index, opts)
|
2020-04-22 16:09:26 +00:00
|
|
|
return err
|
|
|
|
}
|