From 2f616bb0da60149666b31adc432d7badd40952f7 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 12 Sep 2024 15:47:57 -0400 Subject: [PATCH] add delete repo/org action secrets --- forgejo/org_action.go | 25 +++++++++++++++++++++++++ forgejo/org_action_test.go | 5 +++++ forgejo/repo_action.go | 25 +++++++++++++++++++++++++ forgejo/repo_action_test.go | 5 +++++ 4 files changed, 60 insertions(+) diff --git a/forgejo/org_action.go b/forgejo/org_action.go index 2e8ce53..c1a90aa 100644 --- a/forgejo/org_action.go +++ b/forgejo/org_action.go @@ -89,3 +89,28 @@ func (c *Client) CreateOrgActionSecret(org string, opt CreateSecretOption) (*Res return resp, fmt.Errorf("unexpected Status: %d", status) } } + +// DeleteOrgActionSecret deletes a secret for the specified organization in Forgejo Actions. +// It takes the organization name, and the secret name as parameters. +// The function returns the HTTP response and an error, if any. +func (c *Client) DeleteOrgActionSecret(org, secretName string) (*Response, error) { + if err := escapeValidatePathSegments(&org, &secretName); err != nil { + return nil, err + } + + status, resp, err := c.getStatusCode("DELETE", fmt.Sprintf("/orgs/%s/actions/secrets/%s", org, secretName), nil, nil) + if err != nil { + return nil, err + } + + switch status { + case http.StatusNoContent: + return resp, nil + case http.StatusNotFound: + return resp, fmt.Errorf("forbidden") + case http.StatusBadRequest: + return resp, fmt.Errorf("bad request") + default: + return resp, fmt.Errorf("unexpected Status: %d", status) + } +} diff --git a/forgejo/org_action_test.go b/forgejo/org_action_test.go index c910e62..803675f 100644 --- a/forgejo/org_action_test.go +++ b/forgejo/org_action_test.go @@ -40,4 +40,9 @@ func TestCreateOrgActionSecret(t *testing.T) { secrets, _, err := c.ListOrgActionSecret(newOrg.UserName, ListOrgActionSecretOption{}) assert.NoError(t, err) assert.Len(t, secrets, 1) + + // delete org secret + resp, err = c.DeleteOrgActionSecret(newOrg.UserName, "test") + assert.NoError(t, err) + assert.Equal(t, http.StatusNoContent, resp.StatusCode) } diff --git a/forgejo/repo_action.go b/forgejo/repo_action.go index 5ed2c6c..d50f43b 100644 --- a/forgejo/repo_action.go +++ b/forgejo/repo_action.go @@ -65,3 +65,28 @@ func (c *Client) CreateRepoActionSecret(user, repo string, opt CreateSecretOptio return resp, fmt.Errorf("unexpected Status: %d", status) } } + +// DeleteRepoActionSecret deletes a secret for the specified repository in Forgejo Actions. +// It takes the repository name, owner, and the secret name as parameters. +// The function returns the HTTP response and an error, if any. +func (c *Client) DeleteRepoActionSecret(user, repo, secretName string) (*Response, error) { + if err := escapeValidatePathSegments(&user, &repo, &secretName); err != nil { + return nil, err + } + + status, resp, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/actions/secrets/%s", user, repo, secretName), nil, nil) + if err != nil { + return nil, err + } + + switch status { + case http.StatusNoContent: + return resp, nil + case http.StatusNotFound: + return resp, fmt.Errorf("forbidden") + case http.StatusBadRequest: + return resp, fmt.Errorf("bad request") + default: + return resp, fmt.Errorf("unexpected Status: %d", status) + } +} diff --git a/forgejo/repo_action_test.go b/forgejo/repo_action_test.go index d287e4d..1d8532c 100644 --- a/forgejo/repo_action_test.go +++ b/forgejo/repo_action_test.go @@ -32,4 +32,9 @@ func TestCreateRepoActionSecret(t *testing.T) { secrets, _, err := c.ListRepoActionSecret(user.UserName, repo.Name, ListRepoActionSecretOption{}) assert.NoError(t, err) assert.Len(t, secrets, 1) + + // delete repo secret + resp, err = c.DeleteRepoActionSecret(user.UserName, repo.Name, "test") + assert.NoError(t, err) + assert.Equal(t, http.StatusNoContent, resp.StatusCode) }