mirror of
https://gitea.com/gitea/go-sdk.git
synced 2024-06-02 10:11:26 +05:30
8110db1e6d
- Add imports for "bytes", "encoding/json", and "net/http" in `repo_action.go` - Implement `CreateRepoActionSecret` function to create a secret for a repository in Gitea Actions - Add a new test file `repo_action_test.go` with tests for creating, updating, and listing repository action secrets <img width="518" alt="image" src="/attachments/c8e457c4-395f-4ffe-8482-0b7f5d2541cb"> Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/662 Reviewed-by: techknowlogick <techknowlogick@noreply.gitea.com> Co-authored-by: appleboy <appleboy.tw@gmail.com> Co-committed-by: appleboy <appleboy.tw@gmail.com>
67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package gitea
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
// ListRepoActionSecretOption list RepoActionSecret options
|
|
type ListRepoActionSecretOption struct {
|
|
ListOptions
|
|
}
|
|
|
|
// ListRepoActionSecret list a repository's secrets
|
|
func (c *Client) ListRepoActionSecret(user, repo string, opt ListRepoActionSecretOption) ([]*Secret, *Response, error) {
|
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
opt.setDefaults()
|
|
secrets := make([]*Secret, 0, opt.PageSize)
|
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/actions/secrets", user, repo))
|
|
link.RawQuery = opt.getURLQuery().Encode()
|
|
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 organization name 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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|