2019-04-25 13:06:53 -04:00
|
|
|
// Copyright 2016 The Gogs 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 gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-09-15 22:01:41 -04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-04-25 13:06:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Label a label to an issue or a pr
|
|
|
|
type Label struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
// example: 00aabb
|
2019-10-13 11:13:33 -04:00
|
|
|
Color string `json:"color"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
URL string `json:"url"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-05-27 09:48:09 -04:00
|
|
|
// ListLabelsOptions options for listing repository's labels
|
|
|
|
type ListLabelsOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// ListRepoLabels list labels of one repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ListRepoLabels(owner, repo string, opt ListLabelsOptions) ([]*Label, *Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
opt.setDefaults()
|
|
|
|
labels := make([]*Label, 0, opt.PageSize)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels?%s", owner, repo, opt.getURLQuery().Encode()), nil, nil, &labels)
|
|
|
|
return labels, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoLabel get one label of repository by repo it
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetRepoLabel(owner, repo string, id int64) (*Label, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
label := new(Label)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil, label)
|
|
|
|
return label, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateLabelOption options for creating a label
|
|
|
|
type CreateLabelOption struct {
|
2019-10-13 11:13:33 -04:00
|
|
|
Name string `json:"name"`
|
2019-04-25 13:06:53 -04:00
|
|
|
// example: #00aabb
|
2019-10-13 11:13:33 -04:00
|
|
|
Color string `json:"color"`
|
|
|
|
Description string `json:"description"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 22:01:41 -04:00
|
|
|
// Validate the CreateLabelOption struct
|
|
|
|
func (opt CreateLabelOption) Validate() error {
|
|
|
|
aw, err := regexp.MatchString("^#?[0-9,a-f,A-F]{6}$", opt.Color)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !aw {
|
|
|
|
return fmt.Errorf("invalid color format")
|
|
|
|
}
|
|
|
|
if len(strings.TrimSpace(opt.Name)) == 0 {
|
|
|
|
return fmt.Errorf("empty name not allowed")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// CreateLabel create one label of repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) CreateLabel(owner, repo string, opt CreateLabelOption) (*Label, *Response, error) {
|
|
|
|
if err := opt.Validate(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-07-13 18:38:05 -04:00
|
|
|
if len(opt.Color) == 6 {
|
|
|
|
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
|
|
|
opt.Color = "#" + opt.Color
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
label := new(Label)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("POST",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/labels", owner, repo),
|
2019-04-25 13:06:53 -04:00
|
|
|
jsonHeader, bytes.NewReader(body), label)
|
2020-09-15 22:01:41 -04:00
|
|
|
return label, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// EditLabelOption options for editing a label
|
|
|
|
type EditLabelOption struct {
|
2019-10-13 11:13:33 -04:00
|
|
|
Name *string `json:"name"`
|
|
|
|
Color *string `json:"color"`
|
|
|
|
Description *string `json:"description"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 22:01:41 -04:00
|
|
|
// Validate the EditLabelOption struct
|
|
|
|
func (opt EditLabelOption) Validate() error {
|
|
|
|
if opt.Color != nil {
|
|
|
|
aw, err := regexp.MatchString("^#?[0-9,a-f,A-F]{6}$", *opt.Color)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !aw {
|
|
|
|
return fmt.Errorf("invalid color format")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opt.Name != nil {
|
|
|
|
if len(strings.TrimSpace(*opt.Name)) == 0 {
|
|
|
|
return fmt.Errorf("empty name not allowed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// EditLabel modify one label with options
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) EditLabel(owner, repo string, id int64, opt EditLabelOption) (*Label, *Response, error) {
|
|
|
|
if err := opt.Validate(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
label := new(Label)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), label)
|
|
|
|
return label, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteLabel delete one label of repository by id
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) DeleteLabel(owner, repo string, id int64) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil)
|
|
|
|
return resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetIssueLabels get labels of one issue via issue id
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetIssueLabels(owner, repo string, index int64, opts ListLabelsOptions) ([]*Label, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
labels := make([]*Label, 0, 5)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/labels?%s", owner, repo, index, opts.getURLQuery().Encode()), nil, nil, &labels)
|
|
|
|
return labels, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2019-10-13 11:13:33 -04:00
|
|
|
// IssueLabelsOption a collection of labels
|
|
|
|
type IssueLabelsOption struct {
|
|
|
|
// list of label IDs
|
|
|
|
Labels []int64 `json:"labels"`
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// AddIssueLabels add one or more labels to one issue
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) AddIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
var labels []*Label
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
|
|
|
|
return labels, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceIssueLabels replace old labels of issue with new labels
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ReplaceIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
var labels []*Label
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
|
|
|
|
return labels, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteIssueLabel delete one label of one issue by issue id and label id
|
|
|
|
// TODO: maybe we need delete by label name and issue id
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) DeleteIssueLabel(owner, repo string, index, label int64) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels/%d", owner, repo, index, label), nil, nil)
|
|
|
|
return resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClearIssueLabels delete all the labels of one issue.
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ClearIssueLabels(owner, repo string, index int64) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil)
|
|
|
|
return resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|