add repo action secret creation

This commit is contained in:
Shane C 2024-09-12 15:13:17 -04:00
parent 136185be68
commit 0c24c1e6e7
Signed by: shanec
GPG key ID: E46B5FEA35B22FF9
3 changed files with 71 additions and 2 deletions

View file

@ -11,7 +11,7 @@ PACKAGE := codeberg.org/mvdkleijn/forgejo-sdk/forgejo
GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@v0.4.0
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.0
FORGEJO_VERSION := 7.0.4
FORGEJO_VERSION := 8.0.3
FORGEJO_DL := https://codeberg.org/forgejo/forgejo/releases/download/v$(FORGEJO_VERSION)/forgejo-$(FORGEJO_VERSION)-
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
@ -67,7 +67,7 @@ vet:
cd forgejo && $(GO) vet $(PACKAGE)
.PHONY: ci-lint
ci-lint:
ci-lint:
@cd forgejo/; echo -n "gofumpt ...";\
diff=$$($(GO) run $(GOFUMPT_PACKAGE) -extra -l .); \
if [ -n "$$diff" ]; then \

View file

@ -9,7 +9,10 @@
package forgejo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
@ -31,3 +34,34 @@ func (c *Client) ListRepoActionSecret(user, repo string, opt ListRepoActionSecre
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &secrets)
return secrets, resp, err
}
// CreateRepoActionSecret creates a secret for the specified repository in the Gitea Actions.
// It takes the repository name, owner, and the secret options as parameters.
// The function returns the HTTP response and an error, if any.
func (c *Client) CreateRepoActionSecret(user, repo string, opt CreateSecretOption) (*Response, error) {
if err := escapeValidatePathSegments(&user, &repo); err != nil {
return nil, err
}
if err := (&opt).Validate(); err != nil {
return nil, err
}
body, err := json.Marshal(&opt)
status, resp, err := c.getStatusCode("PUT", fmt.Sprintf("/repos/%s/%s/actions/secrets/%s", user, repo, opt.Name), jsonHeader, bytes.NewReader(body))
if err != nil {
return nil, err
}
switch status {
case http.StatusCreated:
return resp, nil
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)
}
}

View file

@ -0,0 +1,35 @@
package forgejo
import (
"github.com/stretchr/testify/assert"
"log"
"net/http"
"testing"
)
func TestCreateRepoActionSecret(t *testing.T) {
log.Println("== TestCreateRepoActionSecret ==")
c := newTestClient()
user := createTestUser(t, "action_user", c)
c.SetSudo(user.UserName)
repo, _, err := c.CreateRepo(CreateRepoOption{Name: "Action"})
assert.NoError(t, err)
assert.NotNil(t, repo)
// create repo secret
resp, err := c.CreateRepoActionSecret(user.UserName, repo.Name, CreateSecretOption{Name: "test", Data: "test"})
assert.NoError(t, err)
assert.Equal(t, http.StatusCreated, resp.StatusCode)
// update repo secret
resp, err = c.CreateRepoActionSecret(user.UserName, repo.Name, CreateSecretOption{Name: "test", Data: "test2"})
assert.NoError(t, err)
assert.Equal(t, http.StatusNoContent, resp.StatusCode)
// list repo secrets
secrets, _, err := c.ListRepoActionSecret(user.UserName, repo.Name, ListRepoActionSecretOption{})
assert.NoError(t, err)
assert.Len(t, secrets, 1)
}