2019-10-19 12:54:16 +02:00
|
|
|
// Copyright 2019 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 (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2019-10-26 06:50:30 +02:00
|
|
|
"strconv"
|
2019-10-19 12:54:16 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
|
2020-09-19 18:00:50 +02:00
|
|
|
"github.com/muesli/termenv"
|
2020-01-04 18:44:25 +01:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-10-19 12:54:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdLabels represents to operate repositories' labels.
|
|
|
|
var CmdLabels = cli.Command{
|
|
|
|
Name: "labels",
|
2019-11-03 21:34:41 +01:00
|
|
|
Usage: "Manage issue labels",
|
|
|
|
Description: `Manage issue labels`,
|
2019-10-19 12:54:16 +02:00
|
|
|
Action: runLabels,
|
2020-01-04 18:44:25 +01:00
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
&CmdLabelCreate,
|
|
|
|
&CmdLabelUpdate,
|
|
|
|
&CmdLabelDelete,
|
2019-10-19 12:54:16 +02:00
|
|
|
},
|
2019-10-26 06:50:30 +02:00
|
|
|
Flags: append([]cli.Flag{
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2020-03-06 04:43:28 +01:00
|
|
|
Name: "save",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Usage: "Save all the labels as a file",
|
2019-10-19 12:54:16 +02:00
|
|
|
},
|
2019-10-26 06:50:30 +02:00
|
|
|
}, AllDefaultFlags...),
|
2019-10-19 12:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func runLabels(ctx *cli.Context) error {
|
2019-10-23 17:58:18 +02:00
|
|
|
login, owner, repo := initCommand()
|
2019-10-19 12:54:16 +02:00
|
|
|
|
2019-10-26 06:50:30 +02:00
|
|
|
headers := []string{
|
|
|
|
"Index",
|
|
|
|
"Color",
|
|
|
|
"Name",
|
2019-10-28 20:47:41 +01:00
|
|
|
"Description",
|
2019-10-26 06:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var values [][]string
|
|
|
|
|
2020-09-16 04:01:41 +02:00
|
|
|
labels, _, err := login.Client().ListRepoLabels(owner, repo, gitea.ListLabelsOptions{})
|
2019-10-19 12:54:16 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(labels) == 0 {
|
2019-10-26 06:50:30 +02:00
|
|
|
Output(outputValue, headers, values)
|
2019-10-19 12:54:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:00:50 +02:00
|
|
|
p := termenv.ColorProfile()
|
|
|
|
|
2019-10-19 12:54:16 +02:00
|
|
|
fPath := ctx.String("save")
|
|
|
|
if len(fPath) > 0 {
|
|
|
|
f, err := os.Create(fPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
for _, label := range labels {
|
|
|
|
fmt.Fprintf(f, "#%s %s\n", label.Color, label.Name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, label := range labels {
|
2020-09-19 18:00:50 +02:00
|
|
|
color := termenv.String(label.Color)
|
|
|
|
|
2019-10-26 06:50:30 +02:00
|
|
|
values = append(
|
|
|
|
values,
|
|
|
|
[]string{
|
|
|
|
strconv.FormatInt(label.ID, 10),
|
2020-09-19 18:00:50 +02:00
|
|
|
fmt.Sprint(color.Background(p.Color("#" + label.Color))),
|
2019-10-26 06:50:30 +02:00
|
|
|
label.Name,
|
2019-10-28 20:47:41 +01:00
|
|
|
label.Description,
|
2019-10-26 06:50:30 +02:00
|
|
|
},
|
|
|
|
)
|
2019-10-19 12:54:16 +02:00
|
|
|
}
|
2019-10-26 06:50:30 +02:00
|
|
|
Output(outputValue, headers, values)
|
2019-10-19 12:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CmdLabelCreate represents a sub command of labels to create label.
|
|
|
|
var CmdLabelCreate = cli.Command{
|
|
|
|
Name: "create",
|
2019-11-03 21:34:41 +01:00
|
|
|
Usage: "Create a label",
|
|
|
|
Description: `Create a label`,
|
2019-10-19 12:54:16 +02:00
|
|
|
Action: runLabelCreate,
|
|
|
|
Flags: []cli.Flag{
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: "name",
|
|
|
|
Usage: "label name",
|
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: "color",
|
|
|
|
Usage: "label color value",
|
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: "description",
|
|
|
|
Usage: "label description",
|
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: "file",
|
|
|
|
Usage: "indicate a label file",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitLabelLine(line string) (string, string, string) {
|
|
|
|
fields := strings.SplitN(line, ";", 2)
|
|
|
|
var color, name, description string
|
|
|
|
if len(fields) < 1 {
|
|
|
|
return "", "", ""
|
|
|
|
} else if len(fields) >= 2 {
|
|
|
|
description = strings.TrimSpace(fields[1])
|
|
|
|
}
|
|
|
|
fields = strings.Fields(fields[0])
|
|
|
|
if len(fields) <= 0 {
|
|
|
|
return "", "", ""
|
|
|
|
}
|
|
|
|
color = fields[0]
|
|
|
|
if len(fields) == 2 {
|
|
|
|
name = fields[1]
|
|
|
|
} else if len(fields) > 2 {
|
|
|
|
name = strings.Join(fields[1:], " ")
|
|
|
|
}
|
|
|
|
return color, name, description
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLabelCreate(ctx *cli.Context) error {
|
2019-10-23 17:58:18 +02:00
|
|
|
login, owner, repo := initCommand()
|
2019-10-19 12:54:16 +02:00
|
|
|
|
|
|
|
labelFile := ctx.String("file")
|
|
|
|
var err error
|
|
|
|
if len(labelFile) == 0 {
|
2020-09-16 04:01:41 +02:00
|
|
|
_, _, err = login.Client().CreateLabel(owner, repo, gitea.CreateLabelOption{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: ctx.String("name"),
|
|
|
|
Color: ctx.String("color"),
|
|
|
|
Description: ctx.String("description"),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
f, err := os.Open(labelFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
var i = 1
|
|
|
|
// FIXME: if Gitea's API support create multiple labels once, we should move to that API.
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
color, name, description := splitLabelLine(line)
|
|
|
|
if color == "" || name == "" {
|
|
|
|
log.Printf("Line %d ignored because lack of enough fields: %s\n", i, line)
|
|
|
|
} else {
|
2020-09-16 04:01:41 +02:00
|
|
|
_, _, err = login.Client().CreateLabel(owner, repo, gitea.CreateLabelOption{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: name,
|
|
|
|
Color: color,
|
|
|
|
Description: description,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CmdLabelUpdate represents a sub command of labels to update label.
|
|
|
|
var CmdLabelUpdate = cli.Command{
|
|
|
|
Name: "update",
|
2019-11-03 21:34:41 +01:00
|
|
|
Usage: "Update a label",
|
|
|
|
Description: `Update a label`,
|
2019-10-19 12:54:16 +02:00
|
|
|
Action: runLabelUpdate,
|
|
|
|
Flags: []cli.Flag{
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.IntFlag{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: "id",
|
|
|
|
Usage: "label id",
|
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: "name",
|
|
|
|
Usage: "label name",
|
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: "color",
|
|
|
|
Usage: "label color value",
|
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: "description",
|
|
|
|
Usage: "label description",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLabelUpdate(ctx *cli.Context) error {
|
2019-10-23 17:58:18 +02:00
|
|
|
login, owner, repo := initCommand()
|
2019-10-19 12:54:16 +02:00
|
|
|
|
|
|
|
id := ctx.Int64("id")
|
|
|
|
var pName, pColor, pDescription *string
|
|
|
|
name := ctx.String("name")
|
|
|
|
if name != "" {
|
|
|
|
pName = &name
|
|
|
|
}
|
|
|
|
|
|
|
|
color := ctx.String("color")
|
|
|
|
if color != "" {
|
|
|
|
pColor = &color
|
|
|
|
}
|
|
|
|
|
|
|
|
description := ctx.String("description")
|
|
|
|
if description != "" {
|
|
|
|
pDescription = &description
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2020-09-16 04:01:41 +02:00
|
|
|
_, _, err = login.Client().EditLabel(owner, repo, id, gitea.EditLabelOption{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: pName,
|
|
|
|
Color: pColor,
|
|
|
|
Description: pDescription,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CmdLabelDelete represents a sub command of labels to delete label.
|
|
|
|
var CmdLabelDelete = cli.Command{
|
|
|
|
Name: "delete",
|
2019-11-03 21:34:41 +01:00
|
|
|
Usage: "Delete a label",
|
|
|
|
Description: `Delete a label`,
|
2020-09-17 14:47:56 +02:00
|
|
|
Action: runLabelDelete,
|
2019-10-19 12:54:16 +02:00
|
|
|
Flags: []cli.Flag{
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.IntFlag{
|
2019-10-19 12:54:16 +02:00
|
|
|
Name: "id",
|
|
|
|
Usage: "label id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLabelDelete(ctx *cli.Context) error {
|
2019-10-23 17:58:18 +02:00
|
|
|
login, owner, repo := initCommand()
|
2019-10-19 12:54:16 +02:00
|
|
|
|
2020-09-16 04:01:41 +02:00
|
|
|
_, err := login.Client().DeleteLabel(owner, repo, ctx.Int64("id"))
|
2019-10-19 12:54:16 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|