2020-09-30 05:11:33 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-08 01:40:02 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-30 05:11:33 +00:00
|
|
|
|
|
|
|
package labels
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-22 14:23:09 +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/sdk/gitea"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdLabelCreate represents a sub command of labels to create label.
|
|
|
|
var CmdLabelCreate = cli.Command{
|
|
|
|
Name: "create",
|
2020-12-16 16:47:40 +00:00
|
|
|
Aliases: []string{"c"},
|
2020-09-30 05:11:33 +00:00
|
|
|
Usage: "Create a label",
|
|
|
|
Description: `Create a label`,
|
2022-09-13 18:14:02 +00:00
|
|
|
ArgsUsage: " ", // command does not accept arguments
|
2020-09-30 05:11:33 +00:00
|
|
|
Action: runLabelCreate,
|
2021-06-22 14:23:09 +00:00
|
|
|
Flags: append([]cli.Flag{
|
2020-09-30 05:11:33 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "name",
|
|
|
|
Usage: "label name",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "color",
|
|
|
|
Usage: "label color value",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "description",
|
|
|
|
Usage: "label description",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "file",
|
|
|
|
Usage: "indicate a label file",
|
|
|
|
},
|
2021-06-22 14:23:09 +00:00
|
|
|
}, flags.AllDefaultFlags...),
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
func runLabelCreate(cmd *cli.Context) error {
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
2020-09-30 05:11:33 +00:00
|
|
|
|
|
|
|
labelFile := ctx.String("file")
|
|
|
|
var err error
|
|
|
|
if len(labelFile) == 0 {
|
2020-12-15 17:38:22 +00:00
|
|
|
_, _, err = ctx.Login.Client().CreateLabel(ctx.Owner, ctx.Repo, gitea.CreateLabelOption{
|
2020-09-30 05:11:33 +00: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
|
|
|
|
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-12-15 17:38:22 +00:00
|
|
|
_, _, err = ctx.Login.Client().CreateLabel(ctx.Owner, ctx.Repo, gitea.CreateLabelOption{
|
2020-09-30 05:11:33 +00:00
|
|
|
Name: name,
|
|
|
|
Color: color,
|
|
|
|
Description: description,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:18:10 +00:00
|
|
|
return err
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|