2016-03-14 03:18:27 +00: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.
|
|
|
|
|
2016-11-07 01:57:49 +00:00
|
|
|
package gitea
|
2016-03-14 03:18:27 +00:00
|
|
|
|
2016-08-02 19:51:02 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-05-11 16:38:52 +01:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2016-08-02 19:51:02 +01:00
|
|
|
)
|
|
|
|
|
2019-05-11 16:38:52 +01:00
|
|
|
// Label is equal to structs.Label
|
|
|
|
type Label = structs.Label
|
2016-08-02 19:51:02 +01:00
|
|
|
|
2017-11-12 09:10:33 +00:00
|
|
|
// ListRepoLabels list labels of one repository
|
2016-08-02 19:51:02 +01:00
|
|
|
func (c *Client) ListRepoLabels(owner, repo string) ([]*Label, error) {
|
2016-08-02 19:59:14 +01:00
|
|
|
labels := make([]*Label, 0, 10)
|
2016-08-02 19:51:02 +01:00
|
|
|
return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels", owner, repo), nil, nil, &labels)
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// GetRepoLabel get one label of repository by repo it
|
|
|
|
// TODO: maybe we need get a label by name
|
2016-08-02 19:59:14 +01:00
|
|
|
func (c *Client) GetRepoLabel(owner, repo string, id int64) (*Label, error) {
|
2016-08-02 19:51:02 +01:00
|
|
|
label := new(Label)
|
2016-08-02 19:59:14 +01:00
|
|
|
return label, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil, label)
|
2016-08-02 19:51:02 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// CreateLabel create one label of repository
|
2019-05-11 16:38:52 +01:00
|
|
|
func (c *Client) CreateLabel(owner, repo string, opt structs.CreateLabelOption) (*Label, error) {
|
2016-08-02 19:51:02 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
label := new(Label)
|
|
|
|
return label, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/labels", owner, repo),
|
|
|
|
jsonHeader, bytes.NewReader(body), label)
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// EditLabel modify one label with options
|
2019-05-11 16:38:52 +01:00
|
|
|
func (c *Client) EditLabel(owner, repo string, id int64, opt structs.EditLabelOption) (*Label, error) {
|
2016-08-02 19:51:02 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
label := new(Label)
|
2016-08-02 19:59:14 +01:00
|
|
|
return label, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), label)
|
2016-08-02 19:51:02 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// DeleteLabel delete one label of repository by id
|
|
|
|
// TODO: maybe we need delete by name
|
2016-08-02 19:59:14 +01:00
|
|
|
func (c *Client) DeleteLabel(owner, repo string, id int64) error {
|
|
|
|
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil)
|
2016-08-02 19:51:02 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// GetIssueLabels get labels of one issue via issue id
|
2016-08-02 19:51:02 +01:00
|
|
|
func (c *Client) GetIssueLabels(owner, repo string, index int64) ([]*Label, error) {
|
2016-08-02 19:59:14 +01:00
|
|
|
labels := make([]*Label, 0, 5)
|
2016-08-02 19:51:02 +01:00
|
|
|
return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil, &labels)
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// AddIssueLabels add one or more labels to one issue
|
2019-05-11 16:38:52 +01:00
|
|
|
func (c *Client) AddIssueLabels(owner, repo string, index int64, opt structs.IssueLabelsOption) ([]*Label, error) {
|
2016-08-02 19:51:02 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-10 09:44:00 +00:00
|
|
|
var labels []*Label
|
2016-08-02 19:59:14 +01:00
|
|
|
return labels, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
|
2016-08-02 19:51:02 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// ReplaceIssueLabels replace old labels of issue with new labels
|
2019-05-11 16:38:52 +01:00
|
|
|
func (c *Client) ReplaceIssueLabels(owner, repo string, index int64, opt structs.IssueLabelsOption) ([]*Label, error) {
|
2016-08-02 19:51:02 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-10 09:44:00 +00:00
|
|
|
var labels []*Label
|
2016-08-02 19:59:14 +01:00
|
|
|
return labels, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
|
2016-08-02 19:51:02 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00: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
|
2016-08-02 19:59:14 +01:00
|
|
|
func (c *Client) DeleteIssueLabel(owner, repo string, index, label int64) error {
|
2016-08-02 19:51:02 +01:00
|
|
|
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels/%d", owner, repo, index, label), nil, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// ClearIssueLabels delete all the labels of one issue.
|
2016-08-02 19:51:02 +01:00
|
|
|
func (c *Client) ClearIssueLabels(owner, repo string, index int64) error {
|
|
|
|
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil)
|
|
|
|
return err
|
|
|
|
}
|