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 return nil, nil
} }
req, err := http.NewRequestWithContext(ctx, "GET", versionURL, nil) req, err := http.NewRequestWithContext(ctx, http.MethodGet, versionURL, nil)
if err != nil { if err != nil {
return nil, err 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) { func downloadNewVersion(ctx context.Context, downloadURL string) (string, error) {
log.Debug().Msgf("Downloading new version from %s ...", downloadURL) 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 { if err != nil {
return "", err return "", err
} }

View file

@ -41,7 +41,7 @@ func TestGetPipelines(t *testing.T) {
g.It("should not parse pipeline filter", func() { g.It("should not parse pipeline filter", func() {
c, _ := gin.CreateTestContext(httptest.NewRecorder()) 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) GetPipelines(c)
@ -56,7 +56,7 @@ func TestGetPipelines(t *testing.T) {
c, _ := gin.CreateTestContext(httptest.NewRecorder()) c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Set("store", mockStore) 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) GetPipelines(c)
@ -71,7 +71,7 @@ func TestGetPipelines(t *testing.T) {
c, _ := gin.CreateTestContext(httptest.NewRecorder()) c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Set("store", mockStore) 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) GetPipelines(c)

View file

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

View file

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

View file

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

View file

@ -148,7 +148,7 @@ func Test_forgejo(t *testing.T) {
g.It("Given a PR hook", func() { g.It("Given a PR hook", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequest) buf := bytes.NewBufferString(fixtures.HookPullRequest)
req, _ := http.NewRequest("POST", "/hook", buf) req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{} req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullRequest) req.Header.Set(hookEvent, hookPullRequest)
mockStore.On("GetRepoNameFallback", mock.Anything, mock.Anything).Return(fakeRepo, nil) 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 { for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) { 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 = http.Header{}
req.Header.Set(hookEvent, tc.event) req.Header.Set(hookEvent, tc.event)
r, p, err := parseHook(req) r, p, err := parseHook(req)

View file

@ -149,7 +149,7 @@ func Test_gitea(t *testing.T) {
g.It("Given a PR hook", func() { g.It("Given a PR hook", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequest) buf := bytes.NewBufferString(fixtures.HookPullRequest)
req, _ := http.NewRequest("POST", "/hook", buf) req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{} req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullRequest) req.Header.Set(hookEvent, hookPullRequest)
mockStore.On("GetRepoNameFallback", mock.Anything, mock.Anything).Return(fakeRepo, nil) 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 { for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) { 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 = http.Header{}
req.Header.Set(hookEvent, tc.event) req.Header.Set(hookEvent, tc.event)
r, p, err := parseHook(req) r, p, err := parseHook(req)

View file

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

View file

@ -51,9 +51,9 @@ func NewServer(t *testing.T) *httptest.Server {
return return
case "/api/v4/projects/4/hooks": case "/api/v4/projects/4/hooks":
switch r.Method { switch r.Method {
case "GET": case http.MethodGet:
w.Write(project4PayloadHooks) w.Write(project4PayloadHooks)
case "POST": case http.MethodPost:
w.Write(project4PayloadHook) w.Write(project4PayloadHook)
w.WriteHeader(201) 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, 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 { if err != nil && status != 204 {
return nil, fmt.Errorf("failed to fetch config via http (%d) %w", status, err) 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\"}") 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

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