Skip to content

Commit 9febb45

Browse files
authored
chore: Replace http.Method* constants with string literals (#3696)
1 parent 7e700a7 commit 9febb45

10 files changed

+36
-37
lines changed

.golangci.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ linters:
2525
- unparam
2626
- whitespace
2727
settings:
28+
forbidigo:
29+
forbid:
30+
- pattern: ^reflect\.DeepEqual$
31+
msg: "Use cmp.Equal instead of reflect.DeepEqual"
32+
- pattern: "^http\\.Method[A-Z][a-z]*$"
33+
msg: "Use string literals instead of http.Method constants (https://pkg.go.dev/net/http#pkg-constants)"
2834
gocritic:
2935
disable-all: true
3036
enabled-checks:
@@ -41,10 +47,6 @@ linters:
4147
4248
Use of this source code is governed by a BSD-style
4349
license that can be found in the LICENSE file.
44-
forbidigo:
45-
forbid:
46-
- pattern: ^reflect\.DeepEqual$
47-
msg: "Use cmp.Equal instead of reflect.DeepEqual"
4850
gosec:
4951
excludes:
5052
# duplicates errcheck

github/github.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ func (c *Client) NewFormRequest(urlStr string, body io.Reader, opts ...RequestOp
587587
return nil, err
588588
}
589589

590-
req, err := http.NewRequest(http.MethodPost, u.String(), body)
590+
req, err := http.NewRequest("POST", u.String(), body)
591591
if err != nil {
592592
return nil, err
593593
}
@@ -1525,7 +1525,7 @@ func GetRateLimitCategory(method, path string) RateLimitCategory {
15251525

15261526
// https://docs.github.com/en/rest/search/search#search-code
15271527
case strings.HasPrefix(path, "/search/code") &&
1528-
method == http.MethodGet:
1528+
method == "GET":
15291529
return CodeSearchCategory
15301530

15311531
case strings.HasPrefix(path, "/search/"):
@@ -1534,13 +1534,13 @@ func GetRateLimitCategory(method, path string) RateLimitCategory {
15341534
return GraphqlCategory
15351535
case strings.HasPrefix(path, "/app-manifests/") &&
15361536
strings.HasSuffix(path, "/conversions") &&
1537-
method == http.MethodPost:
1537+
method == "POST":
15381538
return IntegrationManifestCategory
15391539

15401540
// https://docs.github.com/rest/migrations/source-imports#start-an-import
15411541
case strings.HasPrefix(path, "/repos/") &&
15421542
strings.HasSuffix(path, "/import") &&
1543-
method == http.MethodPut:
1543+
method == "PUT":
15441544
return SourceImportCategory
15451545

15461546
// https://docs.github.com/rest/code-scanning#upload-an-analysis-as-sarif-data
@@ -1554,7 +1554,7 @@ func GetRateLimitCategory(method, path string) RateLimitCategory {
15541554
// https://docs.github.com/en/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository
15551555
case strings.HasPrefix(path, "/repos/") &&
15561556
strings.HasSuffix(path, "/dependency-graph/snapshots") &&
1557-
method == http.MethodPost:
1557+
method == "POST":
15581558
return DependencySnapshotsCategory
15591559

15601560
// https://docs.github.com/en/enterprise-cloud@latest/rest/orgs/orgs?apiVersion=2022-11-28#get-the-audit-log-for-an-organization

github/github_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ func TestNewRequest_errorForNoTrailingSlash(t *testing.T) {
646646
t.Fatalf("url.Parse returned unexpected error: %v.", err)
647647
}
648648
c.BaseURL = u
649-
if _, err := c.NewRequest(http.MethodGet, "test", nil); test.wantError && err == nil {
649+
if _, err := c.NewRequest("GET", "test", nil); test.wantError && err == nil {
650650
t.Fatal("Expected error to be returned.")
651651
} else if !test.wantError && err != nil {
652652
t.Fatalf("NewRequest returned unexpected error: %v.", err)
@@ -1247,62 +1247,62 @@ func TestDo_rateLimitCategory(t *testing.T) {
12471247
category RateLimitCategory
12481248
}{
12491249
{
1250-
method: http.MethodGet,
1250+
method: "GET",
12511251
url: "/",
12521252
category: CoreCategory,
12531253
},
12541254
{
1255-
method: http.MethodGet,
1255+
method: "GET",
12561256
url: "/search/issues?q=rate",
12571257
category: SearchCategory,
12581258
},
12591259
{
1260-
method: http.MethodGet,
1260+
method: "GET",
12611261
url: "/graphql",
12621262
category: GraphqlCategory,
12631263
},
12641264
{
1265-
method: http.MethodPost,
1265+
method: "POST",
12661266
url: "/app-manifests/code/conversions",
12671267
category: IntegrationManifestCategory,
12681268
},
12691269
{
1270-
method: http.MethodGet,
1270+
method: "GET",
12711271
url: "/app-manifests/code/conversions",
12721272
category: CoreCategory, // only POST requests are in the integration manifest category
12731273
},
12741274
{
1275-
method: http.MethodPut,
1275+
method: "PUT",
12761276
url: "/repos/google/go-github/import",
12771277
category: SourceImportCategory,
12781278
},
12791279
{
1280-
method: http.MethodGet,
1280+
method: "GET",
12811281
url: "/repos/google/go-github/import",
12821282
category: CoreCategory, // only PUT requests are in the source import category
12831283
},
12841284
{
1285-
method: http.MethodPost,
1285+
method: "POST",
12861286
url: "/repos/google/go-github/code-scanning/sarifs",
12871287
category: CodeScanningUploadCategory,
12881288
},
12891289
{
1290-
method: http.MethodGet,
1290+
method: "GET",
12911291
url: "/scim/v2/organizations/ORG/Users",
12921292
category: ScimCategory,
12931293
},
12941294
{
1295-
method: http.MethodPost,
1295+
method: "POST",
12961296
url: "/repos/google/go-github/dependency-graph/snapshots",
12971297
category: DependencySnapshotsCategory,
12981298
},
12991299
{
1300-
method: http.MethodGet,
1300+
method: "GET",
13011301
url: "/search/code?q=rate",
13021302
category: CodeSearchCategory,
13031303
},
13041304
{
1305-
method: http.MethodGet,
1305+
method: "GET",
13061306
url: "/orgs/google/audit-log",
13071307
category: AuditLogCategory,
13081308
},

github/orgs_credential_authorizations.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package github
88
import (
99
"context"
1010
"fmt"
11-
"net/http"
1211
)
1312

1413
// CredentialAuthorization represents a credential authorized through SAML SSO.
@@ -78,7 +77,7 @@ func (s *OrganizationsService) ListCredentialAuthorizations(ctx context.Context,
7877
return nil, nil, err
7978
}
8079

81-
req, err := s.client.NewRequest(http.MethodGet, u, nil)
80+
req, err := s.client.NewRequest("GET", u, nil)
8281
if err != nil {
8382
return nil, nil, err
8483
}
@@ -100,7 +99,7 @@ func (s *OrganizationsService) ListCredentialAuthorizations(ctx context.Context,
10099
//meta:operation DELETE /orgs/{org}/credential-authorizations/{credential_id}
101100
func (s *OrganizationsService) RemoveCredentialAuthorization(ctx context.Context, org string, credentialID int64) (*Response, error) {
102101
u := fmt.Sprintf("orgs/%v/credential-authorizations/%v", org, credentialID)
103-
req, err := s.client.NewRequest(http.MethodDelete, u, nil)
102+
req, err := s.client.NewRequest("DELETE", u, nil)
104103
if err != nil {
105104
return nil, err
106105
}

github/orgs_credential_authorizations_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestOrganizationsService_ListCredentialAuthorizations(t *testing.T) {
2020
client, mux, _ := setup(t)
2121

2222
mux.HandleFunc("/orgs/o/credential-authorizations", func(w http.ResponseWriter, r *http.Request) {
23-
testMethod(t, r, http.MethodGet)
23+
testMethod(t, r, "GET")
2424
testFormValues(t, r, values{"per_page": "2", "page": "2", "login": "l"})
2525
fmt.Fprint(w, `[
2626
{
@@ -77,7 +77,7 @@ func TestOrganizationsService_RemoveCredentialAuthorization(t *testing.T) {
7777
client, mux, _ := setup(t)
7878

7979
mux.HandleFunc("/orgs/o/credential-authorizations/1", func(w http.ResponseWriter, r *http.Request) {
80-
testMethod(t, r, http.MethodDelete)
80+
testMethod(t, r, "DELETE")
8181
w.WriteHeader(http.StatusNoContent)
8282
})
8383

github/orgs_personal_access_tokens.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"context"
1010
"errors"
1111
"fmt"
12-
"net/http"
1312
"net/url"
1413
"strings"
1514
)
@@ -101,7 +100,7 @@ func (s *OrganizationsService) ListFineGrainedPersonalAccessTokens(ctx context.C
101100
return nil, nil, err
102101
}
103102

104-
req, err := s.client.NewRequest(http.MethodGet, u, opts)
103+
req, err := s.client.NewRequest("GET", u, opts)
105104
if err != nil {
106105
return nil, nil, err
107106
}
@@ -132,7 +131,7 @@ type ReviewPersonalAccessTokenRequestOptions struct {
132131
func (s *OrganizationsService) ReviewPersonalAccessTokenRequest(ctx context.Context, org string, requestID int64, opts ReviewPersonalAccessTokenRequestOptions) (*Response, error) {
133132
u := fmt.Sprintf("orgs/%v/personal-access-token-requests/%v", org, requestID)
134133

135-
req, err := s.client.NewRequest(http.MethodPost, u, &opts)
134+
req, err := s.client.NewRequest("POST", u, &opts)
136135
if err != nil {
137136
return nil, err
138137
}

github/orgs_personal_access_tokens_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func TestOrganizationsService_ReviewPersonalAccessTokenRequest(t *testing.T) {
172172
v := new(ReviewPersonalAccessTokenRequestOptions)
173173
assertNilError(t, json.NewDecoder(r.Body).Decode(v))
174174

175-
testMethod(t, r, http.MethodPost)
175+
testMethod(t, r, "POST")
176176
if !cmp.Equal(v, &input) {
177177
t.Errorf("Request body = %+v, want %+v", v, input)
178178
}

github/reactions.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package github
88
import (
99
"context"
1010
"fmt"
11-
"net/http"
1211
)
1312

1413
// ReactionsService provides access to the reactions-related functions in the
@@ -530,7 +529,7 @@ func (s *ReactionsService) DeleteTeamDiscussionCommentReactionByOrgIDAndTeamID(c
530529
}
531530

532531
func (s *ReactionsService) deleteReaction(ctx context.Context, url string) (*Response, error) {
533-
req, err := s.client.NewRequest(http.MethodDelete, url, nil)
532+
req, err := s.client.NewRequest("DELETE", url, nil)
534533
if err != nil {
535534
return nil, err
536535
}

github/repos_contents.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (s *RepositoriesService) DownloadContents(ctx context.Context, owner, repo,
157157
if contents.GetDownloadURL() == "" {
158158
return nil, resp, fmt.Errorf("no download link found for %s", filepath)
159159
}
160-
dlReq, err := http.NewRequestWithContext(ctx, http.MethodGet, *contents.DownloadURL, nil)
160+
dlReq, err := http.NewRequestWithContext(ctx, "GET", *contents.DownloadURL, nil)
161161
if err != nil {
162162
return nil, resp, err
163163
}
@@ -206,7 +206,7 @@ func (s *RepositoriesService) DownloadContentsWithMeta(ctx context.Context, owne
206206
if contents.GetDownloadURL() == "" {
207207
return nil, contents, resp, fmt.Errorf("no download link found for %s", filepath)
208208
}
209-
dlReq, err := http.NewRequestWithContext(ctx, http.MethodGet, *contents.DownloadURL, nil)
209+
dlReq, err := http.NewRequestWithContext(ctx, "GET", *contents.DownloadURL, nil)
210210
if err != nil {
211211
return nil, contents, resp, err
212212
}

github/repos_hooks_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func TestRepositoriesService_Subscribe(t *testing.T) {
575575
client, mux, _ := setup(t)
576576

577577
mux.HandleFunc("/hub", func(_ http.ResponseWriter, r *http.Request) {
578-
testMethod(t, r, http.MethodPost)
578+
testMethod(t, r, "POST")
579579
testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
580580
testFormValues(t, r, values{
581581
"hub.mode": "subscribe",
@@ -608,7 +608,7 @@ func TestRepositoriesService_Unsubscribe(t *testing.T) {
608608
client, mux, _ := setup(t)
609609

610610
mux.HandleFunc("/hub", func(_ http.ResponseWriter, r *http.Request) {
611-
testMethod(t, r, http.MethodPost)
611+
testMethod(t, r, "POST")
612612
testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
613613
testFormValues(t, r, values{
614614
"hub.mode": "unsubscribe",

0 commit comments

Comments
 (0)