-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabels.go
More file actions
154 lines (135 loc) · 5.72 KB
/
labels.go
File metadata and controls
154 lines (135 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package forge
import (
"context"
"iter"
"dappco.re/go/core/forge/types"
)
// LabelService handles repository labels, organisation labels, and issue labels.
// No Resource embedding — paths are heterogeneous.
//
// Usage:
//
// f := forge.NewForge("https://forge.lthn.ai", "token")
// _, err := f.Labels.ListRepoLabels(ctx, "core", "go-forge")
type LabelService struct {
client *Client
}
func newLabelService(c *Client) *LabelService {
return &LabelService{client: c}
}
// ListRepoLabels returns all labels for a repository.
func (s *LabelService) ListRepoLabels(ctx context.Context, owner, repo string) ([]types.Label, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/labels", pathParams("owner", owner, "repo", repo))
return ListAll[types.Label](ctx, s.client, path, nil)
}
// IterRepoLabels returns an iterator over all labels for a repository.
func (s *LabelService) IterRepoLabels(ctx context.Context, owner, repo string) iter.Seq2[types.Label, error] {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/labels", pathParams("owner", owner, "repo", repo))
return ListIter[types.Label](ctx, s.client, path, nil)
}
// GetRepoLabel returns a single label by ID.
func (s *LabelService) GetRepoLabel(ctx context.Context, owner, repo string, id int64) (*types.Label, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/labels/{id}", pathParams("owner", owner, "repo", repo, "id", int64String(id)))
var out types.Label
if err := s.client.Get(ctx, path, &out); err != nil {
return nil, err
}
return &out, nil
}
// CreateRepoLabel creates a new label in a repository.
func (s *LabelService) CreateRepoLabel(ctx context.Context, owner, repo string, opts *types.CreateLabelOption) (*types.Label, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/labels", pathParams("owner", owner, "repo", repo))
var out types.Label
if err := s.client.Post(ctx, path, opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// EditRepoLabel updates an existing label in a repository.
func (s *LabelService) EditRepoLabel(ctx context.Context, owner, repo string, id int64, opts *types.EditLabelOption) (*types.Label, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/labels/{id}", pathParams("owner", owner, "repo", repo, "id", int64String(id)))
var out types.Label
if err := s.client.Patch(ctx, path, opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteRepoLabel deletes a label from a repository.
func (s *LabelService) DeleteRepoLabel(ctx context.Context, owner, repo string, id int64) error {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/labels/{id}", pathParams("owner", owner, "repo", repo, "id", int64String(id)))
return s.client.Delete(ctx, path)
}
// ListOrgLabels returns all labels for an organisation.
func (s *LabelService) ListOrgLabels(ctx context.Context, org string) ([]types.Label, error) {
path := ResolvePath("/api/v1/orgs/{org}/labels", pathParams("org", org))
return ListAll[types.Label](ctx, s.client, path, nil)
}
// IterOrgLabels returns an iterator over all labels for an organisation.
func (s *LabelService) IterOrgLabels(ctx context.Context, org string) iter.Seq2[types.Label, error] {
path := ResolvePath("/api/v1/orgs/{org}/labels", pathParams("org", org))
return ListIter[types.Label](ctx, s.client, path, nil)
}
// CreateOrgLabel creates a new label in an organisation.
func (s *LabelService) CreateOrgLabel(ctx context.Context, org string, opts *types.CreateLabelOption) (*types.Label, error) {
path := ResolvePath("/api/v1/orgs/{org}/labels", pathParams("org", org))
var out types.Label
if err := s.client.Post(ctx, path, opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// GetOrgLabel returns a single label for an organisation.
func (s *LabelService) GetOrgLabel(ctx context.Context, org string, id int64) (*types.Label, error) {
path := ResolvePath("/api/v1/orgs/{org}/labels/{id}", pathParams("org", org, "id", int64String(id)))
var out types.Label
if err := s.client.Get(ctx, path, &out); err != nil {
return nil, err
}
return &out, nil
}
// EditOrgLabel updates an existing label in an organisation.
func (s *LabelService) EditOrgLabel(ctx context.Context, org string, id int64, opts *types.EditLabelOption) (*types.Label, error) {
path := ResolvePath("/api/v1/orgs/{org}/labels/{id}", pathParams("org", org, "id", int64String(id)))
var out types.Label
if err := s.client.Patch(ctx, path, opts, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteOrgLabel deletes a label from an organisation.
func (s *LabelService) DeleteOrgLabel(ctx context.Context, org string, id int64) error {
path := ResolvePath("/api/v1/orgs/{org}/labels/{id}", pathParams("org", org, "id", int64String(id)))
return s.client.Delete(ctx, path)
}
// ListLabelTemplates returns all available label template names.
func (s *LabelService) ListLabelTemplates(ctx context.Context) ([]string, error) {
var out []string
if err := s.client.Get(ctx, "/api/v1/label/templates", &out); err != nil {
return nil, err
}
return out, nil
}
// IterLabelTemplates returns an iterator over all available label template names.
func (s *LabelService) IterLabelTemplates(ctx context.Context) iter.Seq2[string, error] {
return func(yield func(string, error) bool) {
items, err := s.ListLabelTemplates(ctx)
if err != nil {
yield("", err)
return
}
for _, item := range items {
if !yield(item, nil) {
return
}
}
}
}
// GetLabelTemplate returns all labels for a label template.
func (s *LabelService) GetLabelTemplate(ctx context.Context, name string) ([]types.LabelTemplate, error) {
path := ResolvePath("/api/v1/label/templates/{name}", pathParams("name", name))
var out []types.LabelTemplate
if err := s.client.Get(ctx, path, &out); err != nil {
return nil, err
}
return out, nil
}