diff --git a/Makefile b/Makefile index 674f712..1a96408 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/forgejo/repo_action.go b/forgejo/repo_action.go index 05b9043..5ed2c6c 100644 --- a/forgejo/repo_action.go +++ b/forgejo/repo_action.go @@ -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) + } +} diff --git a/forgejo/repo_action_test.go b/forgejo/repo_action_test.go new file mode 100644 index 0000000..d287e4d --- /dev/null +++ b/forgejo/repo_action_test.go @@ -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) +}