Use http constants (#3766)

This commit is contained in:
qwerty287 2024-06-06 11:16:45 +02:00 committed by GitHub
parent f5671ba20f
commit d28e150534
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 43 additions and 49 deletions

View file

@ -29,7 +29,7 @@ func checkForUpdate(ctx context.Context, versionURL string, force bool) (*NewVer
return nil, nil
}
req, err := http.NewRequestWithContext(ctx, "GET", versionURL, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, versionURL, nil)
if err != nil {
return nil, err
}
@ -77,7 +77,7 @@ func checkForUpdate(ctx context.Context, versionURL string, force bool) (*NewVer
func downloadNewVersion(ctx context.Context, downloadURL string) (string, error) {
log.Debug().Msgf("Downloading new version from %s ...", downloadURL)
req, err := http.NewRequestWithContext(ctx, "GET", downloadURL, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil)
if err != nil {
return "", err
}

View file

@ -41,7 +41,7 @@ func TestGetPipelines(t *testing.T) {
g.It("should not parse pipeline filter", func() {
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("DELETE", "/?before=2023-01-16&after=2023-01-15", nil)
c.Request, _ = http.NewRequest(http.MethodDelete, "/?before=2023-01-16&after=2023-01-15", nil)
GetPipelines(c)
@ -56,7 +56,7 @@ func TestGetPipelines(t *testing.T) {
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Set("store", mockStore)
c.Request, _ = http.NewRequest("DELETE", "/?2023-01-16T15:00:00Z&after=2023-01-15T15:00:00Z", nil)
c.Request, _ = http.NewRequest(http.MethodDelete, "/?2023-01-16T15:00:00Z&after=2023-01-15T15:00:00Z", nil)
GetPipelines(c)
@ -71,7 +71,7 @@ func TestGetPipelines(t *testing.T) {
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Set("store", mockStore)
c.Request, _ = http.NewRequest("DELETE", "/?before=2023-01-16T15:00:00%2B01:00&after=2023-01-15T15:00:00%2B01:00", nil)
c.Request, _ = http.NewRequest(http.MethodDelete, "/?before=2023-01-16T15:00:00%2B01:00&after=2023-01-15T15:00:00%2B01:00", nil)
GetPipelines(c)

View file

@ -285,7 +285,7 @@ func Test_bitbucket(t *testing.T) {
g.It("Should parse the hook", func() {
buf := bytes.NewBufferString(fixtures.HookPush)
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPush)

View file

@ -29,12 +29,6 @@ import (
shared_utils "go.woodpecker-ci.org/woodpecker/v2/shared/utils"
)
const (
get = "GET"
post = "POST"
del = "DELETE"
)
const (
pathUser = "%s/2.0/user/"
pathEmails = "%s/2.0/user/emails"
@ -82,35 +76,35 @@ func NewClientToken(ctx context.Context, url, client, secret string, token *oaut
func (c *Client) FindCurrent() (*Account, error) {
out := new(Account)
uri := fmt.Sprintf(pathUser, c.base)
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out, err
}
func (c *Client) ListEmail() (*EmailResp, error) {
out := new(EmailResp)
uri := fmt.Sprintf(pathEmails, c.base)
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out, err
}
func (c *Client) ListWorkspaces(opts *ListWorkspacesOpts) (*WorkspacesResp, error) {
out := new(WorkspacesResp)
uri := fmt.Sprintf(pathWorkspaces, c.base, opts.Encode())
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out, err
}
func (c *Client) FindRepo(owner, name string) (*Repo, error) {
out := new(Repo)
uri := fmt.Sprintf(pathRepo, c.base, owner, name)
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out, err
}
func (c *Client) ListRepos(workspace string, opts *ListOpts) (*RepoResp, error) {
out := new(RepoResp)
uri := fmt.Sprintf(pathRepos, c.base, workspace, opts.Encode())
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out, err
}
@ -127,44 +121,44 @@ func (c *Client) ListReposAll(workspace string) ([]*Repo, error) {
func (c *Client) FindHook(owner, name, id string) (*Hook, error) {
out := new(Hook)
uri := fmt.Sprintf(pathHook, c.base, owner, name, id)
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out, err
}
func (c *Client) ListHooks(owner, name string, opts *ListOpts) (*HookResp, error) {
out := new(HookResp)
uri := fmt.Sprintf(pathHooks, c.base, owner, name, opts.Encode())
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out, err
}
func (c *Client) CreateHook(owner, name string, hook *Hook) error {
uri := fmt.Sprintf(pathHooks, c.base, owner, name, "")
_, err := c.do(uri, post, hook, nil)
_, err := c.do(uri, http.MethodPost, hook, nil)
return err
}
func (c *Client) DeleteHook(owner, name, id string) error {
uri := fmt.Sprintf(pathHook, c.base, owner, name, id)
_, err := c.do(uri, del, nil, nil)
_, err := c.do(uri, http.MethodDelete, nil, nil)
return err
}
func (c *Client) FindSource(owner, name, revision, path string) (*string, error) {
uri := fmt.Sprintf(pathSource, c.base, owner, name, revision, path)
return c.do(uri, get, nil, nil)
return c.do(uri, http.MethodGet, nil, nil)
}
func (c *Client) CreateStatus(owner, name, revision string, status *PipelineStatus) error {
uri := fmt.Sprintf(pathStatus, c.base, owner, name, revision)
_, err := c.do(uri, post, status, nil)
_, err := c.do(uri, http.MethodPost, status, nil)
return err
}
func (c *Client) GetPermission(fullName string) (*RepoPerm, error) {
out := new(RepoPermResp)
uri := fmt.Sprintf(pathPermission, c.base, fullName)
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
if err != nil {
return nil, err
}
@ -178,7 +172,7 @@ func (c *Client) GetPermission(fullName string) (*RepoPerm, error) {
func (c *Client) ListPermissions(opts *ListOpts) (*RepoPermResp, error) {
out := new(RepoPermResp)
uri := fmt.Sprintf(pathPermissions, c.base, opts.Encode())
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out, err
}
@ -195,14 +189,14 @@ func (c *Client) ListPermissionsAll() ([]*RepoPerm, error) {
func (c *Client) ListBranches(owner, name string, opts *ListOpts) ([]*Branch, error) {
out := new(BranchResp)
uri := fmt.Sprintf(pathBranches, c.base, owner, name, opts.Encode())
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out.Values, err
}
func (c *Client) GetBranchHead(owner, name, branch string) (*Commit, error) {
out := new(CommitsResp)
uri := fmt.Sprintf(pathBranchCommits, c.base, owner, name, branch)
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
if err != nil {
return nil, err
}
@ -217,7 +211,7 @@ func (c *Client) GetUserWorkspaceMembership(workspace, user string) (string, err
opts := &ListOpts{Page: 1, PageLen: pageSize}
for {
uri := fmt.Sprintf(pathOrgPerms, c.base, workspace, opts.Encode())
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
if err != nil {
return "", err
}
@ -237,14 +231,14 @@ func (c *Client) GetUserWorkspaceMembership(workspace, user string) (string, err
func (c *Client) ListPullRequests(owner, name string, opts *ListOpts) ([]*PullRequest, error) {
out := new(PullRequestResp)
uri := fmt.Sprintf(pathPullRequests, c.base, owner, name, opts.Encode())
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out.Values, err
}
func (c *Client) GetWorkspace(name string) (*Workspace, error) {
out := new(Workspace)
uri := fmt.Sprintf(pathWorkspace, c.base, name)
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out, err
}
@ -254,7 +248,7 @@ func (c *Client) GetRepoFiles(owner, name, revision, path string, page *string)
if page != nil {
uri += "?page=" + *page
}
_, err := c.do(uri, get, nil, out)
_, err := c.do(uri, http.MethodGet, nil, out)
return out, err
}

View file

@ -32,7 +32,7 @@ func Test_parser(t *testing.T) {
g.Describe("Bitbucket parser", func() {
g.It("should ignore unsupported hook", func() {
buf := bytes.NewBufferString(fixtures.HookPush)
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, "issue:created")
@ -45,7 +45,7 @@ func Test_parser(t *testing.T) {
g.Describe("Given a pull-request hook payload", func() {
g.It("should return err when malformed", func() {
buf := bytes.NewBufferString("[]")
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullCreated)
@ -55,7 +55,7 @@ func Test_parser(t *testing.T) {
g.It("should return pull-request details", func() {
buf := bytes.NewBufferString(fixtures.HookPull)
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullCreated)
@ -68,7 +68,7 @@ func Test_parser(t *testing.T) {
g.It("should return pull-request details for a pull-request merged payload", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequestMerged)
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullMerged)
@ -81,7 +81,7 @@ func Test_parser(t *testing.T) {
g.It("should return pull-request details for a pull-request closed payload", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequestDeclined)
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullDeclined)
@ -96,7 +96,7 @@ func Test_parser(t *testing.T) {
g.Describe("Given a push hook payload", func() {
g.It("should return err when malformed", func() {
buf := bytes.NewBufferString("[]")
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPush)
@ -106,7 +106,7 @@ func Test_parser(t *testing.T) {
g.It("should return nil if missing commit sha", func() {
buf := bytes.NewBufferString(fixtures.HookPushEmptyHash)
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPush)
@ -118,7 +118,7 @@ func Test_parser(t *testing.T) {
g.It("should return push details", func() {
buf := bytes.NewBufferString(fixtures.HookPush)
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPush)

View file

@ -148,7 +148,7 @@ func Test_forgejo(t *testing.T) {
g.It("Given a PR hook", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequest)
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullRequest)
mockStore.On("GetRepoNameFallback", mock.Anything, mock.Anything).Return(fakeRepo, nil)

View file

@ -376,7 +376,7 @@ func TestForgejoParser(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
req, _ := http.NewRequest("POST", "/api/hook", bytes.NewBufferString(tc.data))
req, _ := http.NewRequest(http.MethodPost, "/api/hook", bytes.NewBufferString(tc.data))
req.Header = http.Header{}
req.Header.Set(hookEvent, tc.event)
r, p, err := parseHook(req)

View file

@ -149,7 +149,7 @@ func Test_gitea(t *testing.T) {
g.It("Given a PR hook", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequest)
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullRequest)
mockStore.On("GetRepoNameFallback", mock.Anything, mock.Anything).Return(fakeRepo, nil)

View file

@ -377,7 +377,7 @@ func TestGiteaParser(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
req, _ := http.NewRequest("POST", "/api/hook", bytes.NewBufferString(tc.data))
req, _ := http.NewRequest(http.MethodPost, "/api/hook", bytes.NewBufferString(tc.data))
req.Header = http.Header{}
req.Header.Set(hookEvent, tc.event)
r, p, err := parseHook(req)

View file

@ -40,7 +40,7 @@ const (
func testHookRequest(payload []byte, event string) *http.Request {
buf := bytes.NewBuffer(payload)
req, _ := http.NewRequest("POST", "/hook", buf)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, event)
return req

View file

@ -51,9 +51,9 @@ func NewServer(t *testing.T) *httptest.Server {
return
case "/api/v4/projects/4/hooks":
switch r.Method {
case "GET":
case http.MethodGet:
w.Write(project4PayloadHooks)
case "POST":
case http.MethodPost:
w.Write(project4PayloadHook)
w.WriteHeader(201)
}

View file

@ -71,7 +71,7 @@ func (h *http) Fetch(ctx context.Context, forge forge.Forge, user *model.User, r
Netrc: netrc,
}
status, err := utils.Send(ctx, "POST", h.endpoint, h.privateKey, body, response)
status, err := utils.Send(ctx, net_http.MethodPost, h.endpoint, h.privateKey, body, response)
if err != nil && status != 204 {
return nil, fmt.Errorf("failed to fetch config via http (%d) %w", status, err)
}

View file

@ -38,7 +38,7 @@ func TestSign(t *testing.T) {
body := []byte("{\"foo\":\"bar\"}")
req, err := http.NewRequest("GET", "http://example.com", bytes.NewBuffer(body))
req, err := http.NewRequest(http.MethodGet, "http://example.com", bytes.NewBuffer(body))
if err != nil {
t.Fatal(err)
}

View file

@ -91,7 +91,7 @@ func CheckCsrf(r *http.Request, fn SecretFunc) error {
// get and options requests are always
// enabled, without CSRF checks.
switch r.Method {
case "GET", "OPTIONS":
case http.MethodGet, http.MethodOptions:
return nil
}