-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathpackage_registries.go
More file actions
206 lines (172 loc) · 7.78 KB
/
package_registries.go
File metadata and controls
206 lines (172 loc) · 7.78 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package buildkite
import (
"context"
"fmt"
"net/url"
)
// PackageRegistriesService handles communication with the package registries related Buildkite APIs
type PackageRegistriesService struct {
client *Client
}
// PackageRegistry represents a package registry within Buildkite
type PackageRegistry struct {
ID string `json:"id,omitempty"`
GraphQLID string `json:"graphql_id,omitempty"`
Slug string `json:"slug,omitempty"`
URL string `json:"url,omitempty"`
WebURL string `json:"web_url,omitempty"`
Name string `json:"name,omitempty"`
Ecosystem string `json:"ecosystem,omitempty"`
Description string `json:"description,omitempty"`
Emoji string `json:"emoji,omitempty"`
Color string `json:"color,omitempty"`
Public bool `json:"public,omitempty"`
OIDCPolicy string `json:"oidc_policy,omitempty"`
}
// CreatePackageRegistryInput represents the input to create a package registry.
type CreatePackageRegistryInput struct {
Name string `json:"name,omitempty"` // The name of the package registry
Ecosystem string `json:"ecosystem,omitempty"` // The ecosystem of the package registry
Description string `json:"description,omitempty"` // A description for the package registry
Emoji string `json:"emoji,omitempty"` // An emoji for the package registry, in buildkite format (eg ":rocket:")
Color string `json:"color,omitempty"` // A color for the package registry, in hex format (eg "#FF0000")
OIDCPolicy PackageRegistryOIDCPolicy `json:"oidc_policy,omitempty"` // The OIDC policy for the package registry, as a YAML or JSON string
}
// PackageRegistryOIDCPolicy represents the OIDC policy statements for a package registry.
type PackageRegistryOIDCPolicy []OIDCPolicyStatement
// OIDCPolicyStatement represents one OIDC policy statement for a package registry.
type OIDCPolicyStatement struct {
Issuer string `json:"iss"`
Scopes []string `json:"scopes,omitzero"`
Claims map[string]ClaimRule `json:"claims"`
}
// ClaimRule represents matching rules for an OIDC claim.
type ClaimRule struct {
Equals any `json:"equals,omitempty"`
NotEquals any `json:"not_equals,omitempty"`
In []any `json:"in,omitempty"`
NotIn []any `json:"not_in,omitempty"`
Matches []string `json:"matches,omitempty"`
}
// Create creates a package registry for an organization
func (rs *PackageRegistriesService) Create(ctx context.Context, organizationSlug string, cpri CreatePackageRegistryInput) (PackageRegistry, *Response, error) {
u := fmt.Sprintf("v2/packages/organizations/%s/registries", organizationSlug)
req, err := rs.client.NewRequest(ctx, "POST", u, cpri)
if err != nil {
return PackageRegistry{}, nil, fmt.Errorf("creating POST package registry request: %w", err)
}
var pr PackageRegistry
resp, err := rs.client.Do(req, &pr)
if err != nil {
return PackageRegistry{}, resp, err
}
return pr, resp, err
}
// UpdatePackageRegistryInput represents the request body for updating a package registry.
type UpdatePackageRegistryInput struct {
Name Optional[string] `json:"name,omitzero"` // The name of the package registry
Description Optional[string] `json:"description,omitzero"` // A description for the package registry
Emoji Optional[string] `json:"emoji,omitzero"` // An emoji for the package registry, in buildkite format (eg ":rocket:")
Color Optional[string] `json:"color,omitzero"` // A color for the package registry, in hex format (eg "#FF0000")
OIDCPolicy Optional[PackageRegistryOIDCPolicy] `json:"oidc_policy,omitzero"` // The OIDC policy for the package registry, as a YAML or JSON string
}
// Update updates a package registry for an organization
func (rs *PackageRegistriesService) Update(ctx context.Context, organizationSlug, registrySlug string, upri UpdatePackageRegistryInput) (PackageRegistry, *Response, error) {
u := fmt.Sprintf("v2/packages/organizations/%s/registries/%s", organizationSlug, registrySlug)
req, err := rs.client.NewRequest(ctx, "PATCH", u, upri)
if err != nil {
return PackageRegistry{}, nil, fmt.Errorf("creating PATCH package registry request: %w", err)
}
var pr PackageRegistry
resp, err := rs.client.Do(req, &pr)
if err != nil {
return PackageRegistry{}, resp, err
}
return pr, resp, err
}
// Get retrieves a package registry for an organization
func (rs *PackageRegistriesService) Get(ctx context.Context, organizationSlug, registrySlug string) (PackageRegistry, *Response, error) {
u := fmt.Sprintf("v2/packages/organizations/%s/registries/%s", organizationSlug, registrySlug)
req, err := rs.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return PackageRegistry{}, nil, fmt.Errorf("creating GET package registry request: %w", err)
}
var pr PackageRegistry
resp, err := rs.client.Do(req, &pr)
if err != nil {
return PackageRegistry{}, resp, err
}
return pr, resp, err
}
// List retrieves a list of package all package registries for an organization
func (rs *PackageRegistriesService) List(ctx context.Context, organizationSlug string) ([]PackageRegistry, *Response, error) {
u := fmt.Sprintf("v2/packages/organizations/%s/registries", organizationSlug)
req, err := rs.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, fmt.Errorf("creating GET package registry request: %w", err)
}
var prs []PackageRegistry
resp, err := rs.client.Do(req, &prs)
if err != nil {
return nil, resp, err
}
return prs, resp, err
}
type RegistryPackagesOptions struct {
Before string `url:"before,omitempty"`
After string `url:"after,omitempty"`
PerPage string `url:"per_page,omitempty"` // Should be a string of an int eg "50"
}
type RegistryPackagesLink string
func (l RegistryPackagesLink) ToOptions() (*RegistryPackagesOptions, error) {
u, err := url.Parse(string(l))
if err != nil {
return nil, fmt.Errorf("parsing link: %w", err)
}
q := u.Query()
return &RegistryPackagesOptions{
Before: q.Get("before"),
After: q.Get("after"),
PerPage: q.Get("per_page"),
}, nil
}
type RegistryPackagesLinks struct {
First RegistryPackagesLink `json:"first,omitempty"`
Previous RegistryPackagesLink `json:"prev,omitempty"`
Self RegistryPackagesLink `json:"self,omitempty"`
Next RegistryPackagesLink `json:"next,omitempty"`
}
type RegistryPackages struct {
Items []Package `json:"items"`
Links RegistryPackagesLinks `json:"links"`
}
func (rs *PackageRegistriesService) ListPackages(ctx context.Context, organizationSlug, registrySlug string, opts *RegistryPackagesOptions) (RegistryPackages, *Response, error) {
u := fmt.Sprintf("v2/packages/organizations/%s/registries/%s/packages", organizationSlug, registrySlug)
u, err := addOptions(u, opts)
if err != nil {
return RegistryPackages{}, nil, fmt.Errorf("adding query params to path: %w", err)
}
req, err := rs.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return RegistryPackages{}, nil, fmt.Errorf("creating GET registry packages request: %w", err)
}
var packages RegistryPackages
resp, err := rs.client.Do(req, &packages)
if err != nil {
return RegistryPackages{}, resp, err
}
return packages, resp, nil
}
// Delete deletes a package registry for an organization
func (rs *PackageRegistriesService) Delete(ctx context.Context, organizationSlug, registrySlug string) (*Response, error) {
u := fmt.Sprintf("v2/packages/organizations/%s/registries/%s", organizationSlug, registrySlug)
req, err := rs.client.NewRequest(ctx, "DELETE", u, nil)
if err != nil {
return nil, fmt.Errorf("creating DELETE package registry request: %w", err)
}
resp, err := rs.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}