feat(client): add repository actions secret

#7
This commit is contained in:
Julien Dessaux 2025-05-22 00:29:45 +02:00
parent ef2711acad
commit 908105f643
Signed by: adyxax
GPG key ID: F92E51B86E07177E
4 changed files with 51 additions and 6 deletions

View file

@ -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

View file

@ -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)
}

View 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
}

View file

@ -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)
}