parent
ef2711acad
commit
908105f643
4 changed files with 51 additions and 6 deletions
|
@ -47,7 +47,7 @@ func (c *Client) sendPaginated(ctx context.Context, method string, uriRef *url.U
|
|||
query.Set("page", strconv.Itoa(page))
|
||||
uriRef.RawQuery = query.Encode()
|
||||
var res json.RawMessage
|
||||
count, err := c.Send(ctx, method, uriRef, payload, &res)
|
||||
count, err := c.send(ctx, method, uriRef, payload, &res)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send: %w", err)
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ func (c *Client) sendPaginated(ctx context.Context, method string, uriRef *url.U
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Send(ctx context.Context, method string, uriRef *url.URL, payload any, response any) (int, error) {
|
||||
func (c *Client) send(ctx context.Context, method string, uriRef *url.URL, payload any, response any) (int, error) {
|
||||
uri := c.baseURI.ResolveReference(uriRef)
|
||||
|
||||
var payloadReader io.Reader
|
||||
|
@ -101,8 +101,10 @@ func (c *Client) Send(ctx context.Context, method string, uriRef *url.URL, paylo
|
|||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return 0, fmt.Errorf("non 2XX status code received: %d, %q", resp.StatusCode, body)
|
||||
}
|
||||
if len(body) > 0 {
|
||||
if err = json.Unmarshal(body, response); err != nil {
|
||||
return 0, fmt.Errorf("response body unmarshal failed: %w", err)
|
||||
return 0, fmt.Errorf("response body unmarshal failed %s: %w", string(body), err)
|
||||
}
|
||||
}
|
||||
if count, err := strconv.Atoi(resp.Header.Get("x-total-count")); err != nil {
|
||||
return 0, nil
|
||||
|
|
|
@ -114,7 +114,7 @@ func (c *Client) RepositoriesList(ctx context.Context) ([]Repository, error) {
|
|||
for {
|
||||
query.Set("page", strconv.Itoa(page))
|
||||
uriRef.RawQuery = query.Encode()
|
||||
count, err := c.Send(ctx, "GET", &uriRef, nil, &response)
|
||||
count, err := c.send(ctx, "GET", &uriRef, nil, &response)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to search repositories: %w", err)
|
||||
}
|
||||
|
|
43
internal/client/secrets.go
Normal file
43
internal/client/secrets.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Secret struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (c *Client) RepoActionSecretDelete(ctx context.Context, owner string, repo string, name string) error {
|
||||
uriRef := url.URL{Path: path.Join("api/v1/repos", owner, repo, "actions/secrets", name)}
|
||||
if _, err := c.send(ctx, "DELETE", &uriRef, nil, nil); err != nil {
|
||||
return fmt.Errorf("failed to delete repository secret: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) RepoActionSecretPut(ctx context.Context, owner string, repo string, name string, data string) error {
|
||||
uriRef := url.URL{Path: path.Join("api/v1/repos", owner, repo, "actions/secrets", name)}
|
||||
type Payload struct {
|
||||
Data string `json:"data"`
|
||||
}
|
||||
payload := Payload{Data: data}
|
||||
if _, err := c.send(ctx, "PUT", &uriRef, &payload, nil); err != nil {
|
||||
return fmt.Errorf("failed to put repository secret: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) RepoActionSecretsList(ctx context.Context, owner string, repo string) ([]Secret, error) {
|
||||
uriRef := url.URL{Path: path.Join("api/v1/repos", owner, repo, "actions/secrets")}
|
||||
var response []Secret
|
||||
if err := c.sendPaginated(ctx, "GET", &uriRef, nil, &response); err != nil {
|
||||
return nil, fmt.Errorf("failed to list repository secrets: %w", err)
|
||||
}
|
||||
return response, nil
|
||||
}
|
|
@ -48,7 +48,7 @@ func (c *Client) UsersList(ctx context.Context) ([]User, error) {
|
|||
for {
|
||||
query.Set("page", strconv.Itoa(page))
|
||||
uriRef.RawQuery = query.Encode()
|
||||
count, err := c.Send(ctx, "GET", &uriRef, nil, &response)
|
||||
count, err := c.send(ctx, "GET", &uriRef, nil, &response)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to search users: %w", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue