2018-09-03 14:43:00 +08: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"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdIssues represents to login a gitea server.
|
|
|
|
var CmdIssues = cli.Command{
|
|
|
|
Name: "issues",
|
2019-03-12 09:36:14 +08:00
|
|
|
Usage: "Operate with issues of the repository",
|
|
|
|
Description: `Operate with issues of the repository`,
|
2018-09-03 14:43:00 +08:00
|
|
|
Action: runIssues,
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
CmdIssuesList,
|
|
|
|
CmdIssuesCreate,
|
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "login, l",
|
2019-03-12 09:36:14 +08:00
|
|
|
Usage: "Indicate one login, optional when inside a gitea repository",
|
2018-09-03 14:43:00 +08:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "repo, r",
|
2019-03-12 09:36:14 +08:00
|
|
|
Usage: "Indicate one repository, optional when inside a gitea repository",
|
2018-09-03 14:43:00 +08:00
|
|
|
},
|
2019-04-29 01:30:03 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "output, o",
|
|
|
|
Usage: "Specify output format. (table)",
|
|
|
|
Destination: &output,
|
|
|
|
},
|
2018-09-03 14:43:00 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-04-26 01:06:53 +08:00
|
|
|
// CmdIssuesList represents a sub command of issues to list issues
|
2018-09-03 14:43:00 +08:00
|
|
|
var CmdIssuesList = cli.Command{
|
|
|
|
Name: "ls",
|
2019-03-12 09:36:14 +08:00
|
|
|
Usage: "List issues of the repository",
|
|
|
|
Description: `List issues of the repository`,
|
2018-09-03 14:43:00 +08:00
|
|
|
Action: runIssuesList,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runIssues(ctx *cli.Context) error {
|
|
|
|
if len(os.Args) == 3 {
|
|
|
|
return runIssueDetail(ctx, os.Args[2])
|
|
|
|
}
|
|
|
|
return runIssuesList(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runIssueDetail(ctx *cli.Context, index string) error {
|
|
|
|
login, owner, repo := initCommand(ctx)
|
|
|
|
|
|
|
|
if strings.HasPrefix(index, "#") {
|
|
|
|
index = index[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, err := strconv.ParseInt(index, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
issue, err := login.Client().GetIssue(owner, repo, idx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("#%d %s\n%s created %s\n\n%s", issue.Index,
|
|
|
|
issue.Title,
|
|
|
|
issue.Poster.UserName,
|
|
|
|
issue.Created.Format("2006-01-02 15:04:05"),
|
|
|
|
issue.Body,
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runIssuesList(ctx *cli.Context) error {
|
|
|
|
login, owner, repo := initCommand(ctx)
|
|
|
|
|
|
|
|
issues, err := login.Client().ListRepoIssues(owner, repo, gitea.ListIssueOption{
|
|
|
|
Page: 0,
|
|
|
|
State: string(gitea.StateOpen),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-04-29 01:30:03 +08:00
|
|
|
headers := []string{
|
|
|
|
string("Index"),
|
|
|
|
string("Name"),
|
|
|
|
string("Updated"),
|
|
|
|
string("Title"),
|
|
|
|
}
|
|
|
|
|
|
|
|
var values [][]string
|
|
|
|
|
2018-09-03 14:43:00 +08:00
|
|
|
if len(issues) == 0 {
|
2019-04-29 01:30:03 +08:00
|
|
|
Output(output, headers, values)
|
2018-09-03 14:43:00 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
name := issue.Poster.FullName
|
|
|
|
if len(name) == 0 {
|
|
|
|
name = issue.Poster.UserName
|
|
|
|
}
|
2019-04-29 01:30:03 +08:00
|
|
|
values = append(
|
|
|
|
values,
|
|
|
|
[]string{
|
|
|
|
strconv.FormatInt(issue.Index, 10),
|
|
|
|
name,
|
|
|
|
issue.Updated.Format("2006-01-02 15:04:05"),
|
|
|
|
issue.Title,
|
|
|
|
},
|
|
|
|
)
|
2018-09-03 14:43:00 +08:00
|
|
|
}
|
2019-04-29 01:30:03 +08:00
|
|
|
Output(output, headers, values)
|
2018-09-03 14:43:00 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-26 01:06:53 +08:00
|
|
|
// CmdIssuesCreate represents a sub command of issues to create issue
|
2018-09-03 14:43:00 +08:00
|
|
|
var CmdIssuesCreate = cli.Command{
|
|
|
|
Name: "create",
|
|
|
|
Usage: "Create an issue on repository",
|
|
|
|
Description: `Create an issue on repository`,
|
|
|
|
Action: runIssuesCreate,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "title, t",
|
|
|
|
Usage: "issue title to create",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "body, b",
|
|
|
|
Usage: "issue body to create",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initCommand(ctx *cli.Context) (*Login, string, string) {
|
|
|
|
err := loadConfig(yamlConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("load config file failed", yamlConfigPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
var login *Login
|
2019-03-12 00:32:09 +08:00
|
|
|
if loginFlag := getGlobalFlag(ctx, "login"); loginFlag == "" {
|
2018-09-03 14:43:00 +08:00
|
|
|
login, err = getActiveLogin()
|
|
|
|
if err != nil {
|
2019-03-12 00:32:09 +08:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
login = getLoginByName(loginFlag)
|
|
|
|
if login == nil {
|
|
|
|
log.Fatal("indicated login name", loginFlag, "does not exist")
|
2018-09-03 14:43:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 00:32:09 +08:00
|
|
|
repoPath := getGlobalFlag(ctx, "repo")
|
|
|
|
if repoPath == "" {
|
2018-09-03 14:43:00 +08:00
|
|
|
login, repoPath, err = curGitRepoPath()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
owner, repo := splitRepo(repoPath)
|
|
|
|
return login, owner, repo
|
|
|
|
}
|
|
|
|
|
2019-03-12 00:32:09 +08:00
|
|
|
func getGlobalFlag(ctx *cli.Context, flag string) string {
|
|
|
|
var val = ctx.String(flag)
|
|
|
|
if val == "" {
|
|
|
|
return ctx.GlobalString(flag)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2018-09-03 14:43:00 +08:00
|
|
|
func runIssuesCreate(ctx *cli.Context) error {
|
|
|
|
login, owner, repo := initCommand(ctx)
|
|
|
|
|
|
|
|
_, err := login.Client().CreateIssue(owner, repo, gitea.CreateIssueOption{
|
|
|
|
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
|
|
|
|
}
|