Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Commit 8431434

Browse files
lstahlmanunknwon
authored andcommitted
API Label Support (#35)
1 parent 442b4e5 commit 8431434

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

issue_label.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,93 @@
44

55
package gogs
66

7+
import (
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
11+
)
12+
713
type Label struct {
14+
ID int64 `json:"id"`
815
Name string `json:"name"`
916
Color string `json:"color"`
1017
}
18+
19+
type LabelOption struct {
20+
Name string `json:"name"`
21+
Color string `json:"color"`
22+
}
23+
24+
func (c *Client) ListRepoLabels(owner, repo string) ([]*Label, error) {
25+
labels := make([]*Label, 0)
26+
return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels", owner, repo), nil, nil, &labels)
27+
}
28+
29+
func (c *Client) GetRepoLabel(owner, repo string, index int64) (*Label, error) {
30+
label := new(Label)
31+
return label, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, index), nil, nil, label)
32+
}
33+
34+
func (c *Client) CreateLabel(owner, repo string, opt LabelOption) (*Label, error) {
35+
body, err := json.Marshal(&opt)
36+
if err != nil {
37+
return nil, err
38+
}
39+
label := new(Label)
40+
return label, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/labels", owner, repo),
41+
jsonHeader, bytes.NewReader(body), label)
42+
}
43+
44+
func (c *Client) EditLabel(owner, repo string, index int64, opt LabelOption) (*Label, error) {
45+
body, err := json.Marshal(&opt)
46+
if err != nil {
47+
return nil, err
48+
}
49+
label := new(Label)
50+
return label, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, index),
51+
jsonHeader, bytes.NewReader(body), label)
52+
}
53+
54+
func (c *Client) DeleteLabel(owner, repo string, index int64) error {
55+
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, index), nil, nil)
56+
return err
57+
}
58+
59+
type IssueLabelsOption struct {
60+
Labels []int64 `json:"labels"`
61+
}
62+
63+
func (c *Client) GetIssueLabels(owner, repo string, index int64) ([]*Label, error) {
64+
labels := make([]*Label, 0)
65+
return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil, &labels)
66+
}
67+
68+
func (c *Client) AddIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, error) {
69+
body, err := json.Marshal(&opt)
70+
if err != nil {
71+
return nil, err
72+
}
73+
labels := make([]*Label, 0)
74+
return labels, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index),
75+
jsonHeader, bytes.NewReader(body), &labels)
76+
}
77+
78+
func (c *Client) ReplaceIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, error) {
79+
body, err := json.Marshal(&opt)
80+
if err != nil {
81+
return nil, err
82+
}
83+
labels := make([]*Label, 0)
84+
return labels, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index),
85+
jsonHeader, bytes.NewReader(body), &labels)
86+
}
87+
88+
func (c *Client) DeleteIssueLabel(owner, repo string, index int64, label int64) error {
89+
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels/%d", owner, repo, index, label), nil, nil)
90+
return err
91+
}
92+
93+
func (c *Client) ClearIssueLabels(owner, repo string, index int64) error {
94+
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil)
95+
return err
96+
}

0 commit comments

Comments
 (0)