From 92434e90d3cb3cbd8a544e66531476ae7afcebbb Mon Sep 17 00:00:00 2001 From: Will Roden Date: Sat, 26 Aug 2023 17:51:12 -0500 Subject: [PATCH 1/3] fix linting --- .github/workflows/linter.yml | 3 +- .golangci.yml | 49 +++++++------- example/basicauth/main.go | 2 +- example/commitpr/main.go | 2 +- example/go.mod | 15 +++-- example/go.sum | 58 ++++++---------- example/tagprotection/main.go | 2 +- example/tokenauth/main.go | 2 +- github/actions_runners_test.go | 4 +- github/actions_secrets.go | 2 +- github/actions_workflow_runs_test.go | 2 +- github/actions_workflows_test.go | 4 +- github/activity_notifications_test.go | 2 +- github/activity_test.go | 2 +- github/activity_watching_test.go | 2 +- github/admin_orgs_test.go | 6 +- github/admin_test.go | 4 +- github/admin_users_test.go | 2 +- github/apps_test.go | 4 +- github/code-scanning_test.go | 6 +- ...erprise_code_security_and_analysis_test.go | 2 +- github/gists_comments_test.go | 4 +- github/gists_test.go | 4 +- github/git_blobs_test.go | 2 +- github/git_commits_test.go | 6 +- github/git_refs_test.go | 6 +- github/git_tags_test.go | 2 +- github/github.go | 12 ++-- github/github_test.go | 38 ++++++++--- github/interactions_orgs_test.go | 2 +- github/interactions_repos_test.go | 2 +- github/issue_import_test.go | 18 ++--- github/issues_assignees_test.go | 4 +- github/issues_comments_test.go | 4 +- github/issues_labels_test.go | 8 +-- github/issues_milestones_test.go | 4 +- github/issues_test.go | 4 +- github/migrations_source_import_test.go | 8 +-- github/migrations_test.go | 8 +-- github/migrations_user_test.go | 6 +- github/misc_test.go | 2 +- github/orgs_actions_allowed_test.go | 2 +- github/orgs_actions_permissions_test.go | 2 +- github/orgs_hooks_configuration_test.go | 2 +- github/orgs_hooks_test.go | 4 +- github/orgs_members_test.go | 6 +- github/orgs_personal_access_tokens_test.go | 2 +- github/orgs_projects_test.go | 2 +- github/orgs_test.go | 2 +- github/projects_test.go | 16 ++--- github/pulls_comments_test.go | 6 +- github/pulls_reviews_test.go | 8 +-- github/pulls_test.go | 5 +- github/reactions_test.go | 24 +++---- github/repos_actions_access_test.go | 2 +- github/repos_actions_allowed_test.go | 2 +- github/repos_actions_permissions_test.go | 2 +- github/repos_autolinks_test.go | 4 +- github/repos_collaborators_test.go | 4 +- github/repos_comments_test.go | 4 +- github/repos_commits_test.go | 6 +- github/repos_deployments_test.go | 4 +- github/repos_environments_test.go | 6 +- github/repos_hooks_configuration_test.go | 2 +- github/repos_hooks_test.go | 4 +- github/repos_keys_test.go | 2 +- github/repos_merging_test.go | 4 +- github/repos_pages_test.go | 8 +-- github/repos_prereceive_hooks_test.go | 2 +- github/repos_projects_test.go | 2 +- github/repos_releases.go | 4 +- github/repos_releases_test.go | 6 +- github/repos_statuses_test.go | 2 +- github/repos_tags_test.go | 2 +- github/repos_test.go | 37 ++++------- github/secret_scanning_test.go | 2 +- github/strings.go | 4 +- github/teams_discussion_comments_test.go | 4 +- github/teams_discussions_test.go | 8 +-- github/teams_members_test.go | 8 +-- github/teams_test.go | 18 ++--- github/users_administration_test.go | 2 +- github/users_emails_test.go | 6 +- github/users_gpg_keys_test.go | 2 +- github/users_keys_test.go | 2 +- github/users_projects_test.go | 2 +- github/users_ssh_signing_keys_test.go | 2 +- github/users_test.go | 2 +- scrape/apps_test.go | 4 +- scrape/forms_test.go | 10 ++- scrape/scrape_test.go | 16 +++-- test/fields/fields.go | 5 -- test/integration/authorizations_test.go | 66 ------------------- update-urls/activity-events_test.go | 5 +- update-urls/main.go | 12 ++-- update-urls/reactions_test.go | 5 +- 96 files changed, 319 insertions(+), 375 deletions(-) diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index a494d8fb77..f4e6fefda7 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -9,7 +9,6 @@ jobs: lint: strategy: matrix: - go-version: [1.x] platform: [ubuntu-latest] # golangci-lint will only process a single module, so we need to call it @@ -17,7 +16,7 @@ jobs: # since that needs libsodium to run. working-directory: - "" - # - example # Fails with "no go files to analyze" + - example - scrape - update-urls runs-on: ${{ matrix.platform }} diff --git a/.golangci.yml b/.golangci.yml index 4d3fc17813..3e286f045f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,34 +1,39 @@ +run: + build-tags: + - integration linters: - # TODO: fix errors so that all of the linters below pass. - # The linters that are commented out, as well as those explicitly disabled, - # are currently failing. We should fix those failures or define exclusion - # rules, and then enable those linters. enable: - dogsled - dupl - gofmt - goimports - # - gosec + - gosec - misspell - nakedret - stylecheck - # - unconvert - # - unparam + - unconvert + - unparam - whitespace - disable: - - errcheck - - gosimple - - staticcheck - - ineffassign - - unused +linters-settings: + gosec: + excludes: + # performance issue: see https://github.com/golangci/golangci-lint/issues/4039 + # and https://github.com/securego/gosec/issues/1007 + - G602 issues: - exclude: - - composites exclude-rules: - - linters: - - dogsled - text: "declaration has 3 blank identifiers" - path: _test\.go - - linters: - - dupl - path: _test\.go + - linters: + - dupl + - unparam + - gosec + - dogsled + path: _test\.go + + # We need to pass nil Context in order to test DoBare erroring on nil ctx. + - linters: [ staticcheck ] + text: 'SA1012: do not pass a nil Context' + path: _test\.go + + # We need to use sha1 for validating signatures + - linters: [ gosec ] + text: 'G505: Blocklisted import crypto/sha1: weak cryptographic primitive' diff --git a/example/basicauth/main.go b/example/basicauth/main.go index f9431b2503..e1e23850f0 100644 --- a/example/basicauth/main.go +++ b/example/basicauth/main.go @@ -32,7 +32,7 @@ func main() { username, _ := r.ReadString('\n') fmt.Print("GitHub Password: ") - bytePassword, _ := term.ReadPassword(int(syscall.Stdin)) + bytePassword, _ := term.ReadPassword(syscall.Stdin) password := string(bytePassword) tp := github.BasicAuthTransport{ diff --git a/example/commitpr/main.go b/example/commitpr/main.go index 2e81387017..fa3535c939 100644 --- a/example/commitpr/main.go +++ b/example/commitpr/main.go @@ -133,7 +133,7 @@ func pushCommit(ref *github.Reference, tree *github.Tree) (err error) { // Create the commit using the tree. date := time.Now() - author := &github.CommitAuthor{Date: &github.Timestamp{date}, Name: authorName, Email: authorEmail} + author := &github.CommitAuthor{Date: &github.Timestamp{Time: date}, Name: authorName, Email: authorEmail} commit := &github.Commit{Author: author, Message: commitMessage, Tree: tree, Parents: []*github.Commit{parent.Commit}} newCommit, _, err := client.Git.CreateCommit(ctx, *sourceOwner, *sourceRepo, commit) if err != nil { diff --git a/example/go.mod b/example/go.mod index 081ce56d12..b59c4c9f41 100644 --- a/example/go.mod +++ b/example/go.mod @@ -6,19 +6,22 @@ require ( github.com/bradleyfalzon/ghinstallation/v2 v2.0.4 github.com/gofri/go-github-ratelimit v1.0.3 github.com/google/go-github/v54 v54.0.0 - golang.org/x/crypto v0.7.0 - golang.org/x/oauth2 v0.7.0 + golang.org/x/crypto v0.12.0 + golang.org/x/oauth2 v0.11.0 + golang.org/x/term v0.11.0 google.golang.org/appengine v1.6.7 ) require ( + github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect + github.com/cloudflare/circl v1.3.3 // indirect github.com/golang-jwt/jwt/v4 v4.0.0 // indirect - github.com/golang/protobuf v1.5.2 // indirect + github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-github/v41 v41.0.0 // indirect github.com/google/go-querystring v1.1.0 // indirect - golang.org/x/net v0.9.0 // indirect - golang.org/x/sys v0.7.0 // indirect - golang.org/x/term v0.7.0 // indirect + golang.org/x/net v0.14.0 // indirect + golang.org/x/sys v0.11.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect ) // Use version at HEAD, not the latest published. diff --git a/example/go.sum b/example/go.sum index f231e16988..310546165e 100644 --- a/example/go.sum +++ b/example/go.sum @@ -1,73 +1,57 @@ +github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA= +github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g= github.com/bradleyfalzon/ghinstallation/v2 v2.0.4 h1:tXKVfhE7FcSkhkv0UwkLvPDeZ4kz6OXd0PKPlFqf81M= github.com/bradleyfalzon/ghinstallation/v2 v2.0.4/go.mod h1:B40qPqJxWE0jDZgOR1JmaMy+4AY1eBP+IByOvqyAKp0= -github.com/gofri/go-github-ratelimit v1.0.1 h1:sgefSzxhnvwZ+wR9uZ4l9TnjgLuNiwipJVzJL4YLj9A= -github.com/gofri/go-github-ratelimit v1.0.1/go.mod h1:OnCi5gV+hAG/LMR7llGhU7yHt44se9sYgKPnafoL7RY= +github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= +github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I= +github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= +github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= github.com/gofri/go-github-ratelimit v1.0.3 h1:Ocs2jaYokZDzgvqaajX+g04dqFyVqL0JQzoO7d2wmlk= github.com/gofri/go-github-ratelimit v1.0.3/go.mod h1:OnCi5gV+hAG/LMR7llGhU7yHt44se9sYgKPnafoL7RY= github.com/golang-jwt/jwt/v4 v4.0.0 h1:RAqyYixv1p7uEnocuy8P1nru5wprCh/MH2BIlW5z5/o= github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-github/v41 v41.0.0 h1:HseJrM2JFf2vfiZJ8anY2hqBjdfY1Vlj/K27ueww4gg= github.com/google/go-github/v41 v41.0.0/go.mod h1:XgmCA5H323A9rtgExdTcnDkcqp6S30AVACCBDOonIxg= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU= -golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= -golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= +golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= -golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= -golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= -golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs= +golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= +golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/oauth2 v0.11.0 h1:vPL4xzxBM4niKCW6g9whtaWVXTJf1U5e4aZxxFx/gbU= +golang.org/x/oauth2 v0.11.0/go.mod h1:LdF7O/8bLR/qWK9DrpXmbHLTouvRHK0SgJl0GmDBchk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= +golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0= +golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= diff --git a/example/tagprotection/main.go b/example/tagprotection/main.go index 8f85c7c0cb..1e464b270c 100644 --- a/example/tagprotection/main.go +++ b/example/tagprotection/main.go @@ -39,7 +39,7 @@ func main() { pattern = strings.TrimSpace(pattern) fmt.Print("GitHub Token: ") - byteToken, _ := term.ReadPassword(int(syscall.Stdin)) + byteToken, _ := term.ReadPassword(syscall.Stdin) println() token := string(byteToken) diff --git a/example/tokenauth/main.go b/example/tokenauth/main.go index a9e845bed3..1189175589 100644 --- a/example/tokenauth/main.go +++ b/example/tokenauth/main.go @@ -20,7 +20,7 @@ import ( func main() { fmt.Print("GitHub Token: ") - byteToken, _ := term.ReadPassword(int(syscall.Stdin)) + byteToken, _ := term.ReadPassword(syscall.Stdin) println() token := string(byteToken) diff --git a/github/actions_runners_test.go b/github/actions_runners_test.go index 8063152f93..d059d6e82a 100644 --- a/github/actions_runners_test.go +++ b/github/actions_runners_test.go @@ -65,7 +65,7 @@ func TestActionsService_GenerateOrgJITConfig(t *testing.T) { mux.HandleFunc("/orgs/o/actions/runners/generate-jitconfig", func(w http.ResponseWriter, r *http.Request) { v := new(GenerateJITConfigRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -109,7 +109,7 @@ func TestActionsService_GenerateRepoJITConfig(t *testing.T) { mux.HandleFunc("/repos/o/r/actions/runners/generate-jitconfig", func(w http.ResponseWriter, r *http.Request) { v := new(GenerateJITConfigRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { diff --git a/github/actions_secrets.go b/github/actions_secrets.go index 316badb70d..49985e12af 100644 --- a/github/actions_secrets.go +++ b/github/actions_secrets.go @@ -23,7 +23,7 @@ type PublicKey struct { // do not error out when unmarshaling. func (p *PublicKey) UnmarshalJSON(data []byte) error { var pk struct { - KeyID interface{} `json:"key_id,string"` + KeyID interface{} `json:"key_id"` Key *string `json:"key"` } diff --git a/github/actions_workflow_runs_test.go b/github/actions_workflow_runs_test.go index 3104e6dfaf..92cd3ad4f2 100644 --- a/github/actions_workflow_runs_test.go +++ b/github/actions_workflow_runs_test.go @@ -1247,7 +1247,7 @@ func TestActionService_PendingDeployments(t *testing.T) { mux.HandleFunc("/repos/o/r/actions/runs/399444496/pending_deployments", func(w http.ResponseWriter, r *http.Request) { v := new(PendingDeploymentsRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { diff --git a/github/actions_workflows_test.go b/github/actions_workflows_test.go index 5e0dbbc9c1..66ad164f4a 100644 --- a/github/actions_workflows_test.go +++ b/github/actions_workflows_test.go @@ -243,7 +243,7 @@ func TestActionsService_CreateWorkflowDispatchEventByID(t *testing.T) { } mux.HandleFunc("/repos/o/r/actions/workflows/72844/dispatches", func(w http.ResponseWriter, r *http.Request) { var v CreateWorkflowDispatchEventRequest - json.NewDecoder(r.Body).Decode(&v) + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "POST") if !cmp.Equal(v, event) { @@ -287,7 +287,7 @@ func TestActionsService_CreateWorkflowDispatchEventByFileName(t *testing.T) { } mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/dispatches", func(w http.ResponseWriter, r *http.Request) { var v CreateWorkflowDispatchEventRequest - json.NewDecoder(r.Body).Decode(&v) + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "POST") if !cmp.Equal(v, event) { diff --git a/github/activity_notifications_test.go b/github/activity_notifications_test.go index 830c8b10cf..815238ebb5 100644 --- a/github/activity_notifications_test.go +++ b/github/activity_notifications_test.go @@ -251,7 +251,7 @@ func TestActivityService_SetThreadSubscription(t *testing.T) { mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) { v := new(Subscription) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { diff --git a/github/activity_test.go b/github/activity_test.go index ef7cf32fca..e2d800220d 100644 --- a/github/activity_test.go +++ b/github/activity_test.go @@ -21,7 +21,7 @@ func TestActivityService_List(t *testing.T) { testMethod(t, r, "GET") w.WriteHeader(http.StatusOK) - w.Write(feedsJSON) + assertWrite(t, w, feedsJSON) }) ctx := context.Background() diff --git a/github/activity_watching_test.go b/github/activity_watching_test.go index 15b0f33078..da43f5a481 100644 --- a/github/activity_watching_test.go +++ b/github/activity_watching_test.go @@ -196,7 +196,7 @@ func TestActivityService_SetRepositorySubscription(t *testing.T) { mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) { v := new(Subscription) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { diff --git a/github/admin_orgs_test.go b/github/admin_orgs_test.go index 0282425bf0..55963441c7 100644 --- a/github/admin_orgs_test.go +++ b/github/admin_orgs_test.go @@ -25,7 +25,7 @@ func TestAdminOrgs_Create(t *testing.T) { mux.HandleFunc("/admin/organizations", func(w http.ResponseWriter, r *http.Request) { v := new(createOrgRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") want := &createOrgRequest{Login: String("github"), Admin: String("ghAdmin")} @@ -67,7 +67,7 @@ func TestAdminOrgs_Rename(t *testing.T) { mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) { v := new(renameOrgRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") want := &renameOrgRequest{Login: String("the-new-octocats")} @@ -105,7 +105,7 @@ func TestAdminOrgs_RenameByName(t *testing.T) { mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) { v := new(renameOrgRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") want := &renameOrgRequest{Login: String("the-new-octocats")} diff --git a/github/admin_test.go b/github/admin_test.go index 207121c35c..dbe0bd2802 100644 --- a/github/admin_test.go +++ b/github/admin_test.go @@ -25,7 +25,7 @@ func TestAdminService_UpdateUserLDAPMapping(t *testing.T) { mux.HandleFunc("/admin/ldap/users/u/mapping", func(w http.ResponseWriter, r *http.Request) { v := new(UserLDAPMapping) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { @@ -73,7 +73,7 @@ func TestAdminService_UpdateTeamLDAPMapping(t *testing.T) { mux.HandleFunc("/admin/ldap/teams/1/mapping", func(w http.ResponseWriter, r *http.Request) { v := new(TeamLDAPMapping) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/admin_users_test.go b/github/admin_users_test.go index 354943d50a..2dac611709 100644 --- a/github/admin_users_test.go +++ b/github/admin_users_test.go @@ -22,7 +22,7 @@ func TestAdminUsers_Create(t *testing.T) { mux.HandleFunc("/admin/users", func(w http.ResponseWriter, r *http.Request) { v := new(createUserRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") want := &createUserRequest{Login: String("github"), Email: String("email@domain.com")} diff --git a/github/apps_test.go b/github/apps_test.go index d3e7c43423..f87d500687 100644 --- a/github/apps_test.go +++ b/github/apps_test.go @@ -400,7 +400,7 @@ func TestAppsService_CreateInstallationTokenWithOptions(t *testing.T) { mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) { v := new(InstallationTokenOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, installationTokenOptions) { t.Errorf("request sent %+v, want %+v", v, installationTokenOptions) @@ -431,7 +431,7 @@ func TestAppsService_CreateAttachement(t *testing.T) { testHeader(t, r, "Accept", mediaTypeContentAttachmentsPreview) w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"id":1,"title":"title1","body":"body1"}`)) + assertWrite(t, w, []byte(`{"id":1,"title":"title1","body":"body1"}`)) }) ctx := context.Background() diff --git a/github/code-scanning_test.go b/github/code-scanning_test.go index 20d4817d3f..b02c145a94 100644 --- a/github/code-scanning_test.go +++ b/github/code-scanning_test.go @@ -60,7 +60,7 @@ func TestCodeScanningService_UploadSarif(t *testing.T) { mux.HandleFunc("/repos/o/r/code-scanning/sarifs", func(w http.ResponseWriter, r *http.Request) { v := new(SarifAnalysis) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") want := &SarifAnalysis{CommitSHA: String("abc"), Ref: String("ref/head/main"), Sarif: String("abc"), CheckoutURI: String("uri"), StartedAt: &Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)}, ToolName: String("codeql-cli")} if !cmp.Equal(v, want) { @@ -275,7 +275,7 @@ func TestCodeScanningService_ListAlertsForOrg(t *testing.T) { }, } if !cmp.Equal(alerts, want) { - t.Errorf("CodeScanning.ListAlertsForOrg returned %+v, want %+v", *&alerts, *&want) + t.Errorf("CodeScanning.ListAlertsForOrg returned %+v, want %+v", alerts, want) } const methodName = "ListAlertsForOrg" @@ -401,7 +401,7 @@ func TestCodeScanningService_ListAlertsForOrgLisCursorOptions(t *testing.T) { }, } if !cmp.Equal(alerts, want) { - t.Errorf("CodeScanning.ListAlertsForOrg returned %+v, want %+v", *&alerts, *&want) + t.Errorf("CodeScanning.ListAlertsForOrg returned %+v, want %+v", alerts, want) } const methodName = "ListAlertsForOrg" diff --git a/github/enterprise_code_security_and_analysis_test.go b/github/enterprise_code_security_and_analysis_test.go index 24bf89c51e..25dbd94170 100644 --- a/github/enterprise_code_security_and_analysis_test.go +++ b/github/enterprise_code_security_and_analysis_test.go @@ -77,7 +77,7 @@ func TestEnterpriseService_UpdateCodeSecurityAndAnalysis(t *testing.T) { mux.HandleFunc("/enterprises/e/code_security_and_analysis", func(w http.ResponseWriter, r *http.Request) { v := new(EnterpriseSecurityAnalysisSettings) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/gists_comments_test.go b/github/gists_comments_test.go index c9c9706897..593c9eeddc 100644 --- a/github/gists_comments_test.go +++ b/github/gists_comments_test.go @@ -169,7 +169,7 @@ func TestGistsService_CreateComment(t *testing.T) { mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) { v := new(GistComment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -222,7 +222,7 @@ func TestGistsService_EditComment(t *testing.T) { mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) { v := new(GistComment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/gists_test.go b/github/gists_test.go index 3066d585a2..995363ec4c 100644 --- a/github/gists_test.go +++ b/github/gists_test.go @@ -484,7 +484,7 @@ func TestGistsService_Create(t *testing.T) { mux.HandleFunc("/gists", func(w http.ResponseWriter, r *http.Request) { v := new(Gist) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -546,7 +546,7 @@ func TestGistsService_Edit(t *testing.T) { mux.HandleFunc("/gists/1", func(w http.ResponseWriter, r *http.Request) { v := new(Gist) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/git_blobs_test.go b/github/git_blobs_test.go index c2cc455ab8..94cdd34e7a 100644 --- a/github/git_blobs_test.go +++ b/github/git_blobs_test.go @@ -118,7 +118,7 @@ func TestGitService_CreateBlob(t *testing.T) { mux.HandleFunc("/repos/o/r/git/blobs", func(w http.ResponseWriter, r *http.Request) { v := new(Blob) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") diff --git a/github/git_commits_test.go b/github/git_commits_test.go index efdb0a27dc..ddb1672ea7 100644 --- a/github/git_commits_test.go +++ b/github/git_commits_test.go @@ -174,7 +174,7 @@ func TestGitService_CreateCommit(t *testing.T) { mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) { v := new(createCommit) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") @@ -232,7 +232,7 @@ func TestGitService_CreateSignedCommit(t *testing.T) { mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) { v := new(createCommit) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") @@ -335,7 +335,7 @@ Commit Message.`) mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) { v := new(createCommit) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") diff --git a/github/git_refs_test.go b/github/git_refs_test.go index d37504945d..1014a44aa5 100644 --- a/github/git_refs_test.go +++ b/github/git_refs_test.go @@ -393,7 +393,7 @@ func TestGitService_CreateRef(t *testing.T) { mux.HandleFunc("/repos/o/r/git/refs", func(w http.ResponseWriter, r *http.Request) { v := new(createRefRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, args) { @@ -482,7 +482,7 @@ func TestGitService_UpdateRef(t *testing.T) { mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) { v := new(updateRefRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, args) { @@ -635,7 +635,7 @@ func TestGitService_UpdateRef_pathEscape(t *testing.T) { mux.HandleFunc("/repos/o/r/git/refs/heads/b#1", func(w http.ResponseWriter, r *http.Request) { v := new(updateRefRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, args) { diff --git a/github/git_tags_test.go b/github/git_tags_test.go index 0d45ee2689..44084141a0 100644 --- a/github/git_tags_test.go +++ b/github/git_tags_test.go @@ -58,7 +58,7 @@ func TestGitService_CreateTag(t *testing.T) { mux.HandleFunc("/repos/o/r/git/tags", func(w http.ResponseWriter, r *http.Request) { v := new(createTagRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { diff --git a/github/github.go b/github/github.go index aad4a7335d..6a93538ef2 100644 --- a/github/github.go +++ b/github/github.go @@ -753,7 +753,7 @@ func (c *Client) BareDo(ctx context.Context, req *http.Request) (*Response, erro }, err } // If we've hit a secondary rate limit, don't make further requests before Retry After. - if err := c.checkSecondaryRateLimitBeforeDo(ctx, req); err != nil { + if err := c.checkSecondaryRateLimitBeforeDo(req); err != nil { return &Response{ Response: err.Response, }, err @@ -885,7 +885,7 @@ func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory rat // current client state in order to quickly check if *AbuseRateLimitError can be immediately returned // from Client.Do, and if so, returns it so that Client.Do can skip making a network API call unnecessarily. // Otherwise it returns nil, and Client.Do should proceed normally. -func (c *Client) checkSecondaryRateLimitBeforeDo(ctx context.Context, req *http.Request) *AbuseRateLimitError { +func (c *Client) checkSecondaryRateLimitBeforeDo(req *http.Request) *AbuseRateLimitError { c.rateMu.Lock() secondary := c.secondaryRateLimitReset c.rateMu.Unlock() @@ -1162,7 +1162,11 @@ func CheckResponse(r *http.Response) error { errorResponse := &ErrorResponse{Response: r} data, err := io.ReadAll(r.Body) if err == nil && data != nil { - json.Unmarshal(data, errorResponse) + err = json.Unmarshal(data, errorResponse) + if err != nil { + // reset the response as if this never happened + errorResponse = &ErrorResponse{Response: r} + } } // Re-populate error response body because GitHub error responses are often // undocumented and inconsistent. @@ -1517,7 +1521,7 @@ func (c *Client) roundTripWithOptionalFollowRedirect(ctx context.Context, u stri // If redirect response is returned, follow it if followRedirects && resp.StatusCode == http.StatusMovedPermanently { - resp.Body.Close() + _ = resp.Body.Close() u = resp.Header.Get("Location") resp, err = c.roundTripWithOptionalFollowRedirect(ctx, u, false, opts...) } diff --git a/github/github_test.go b/github/github_test.go index 94606edd72..bf130cdb66 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -110,7 +110,7 @@ func testFormValues(t *testing.T, r *http.Request, values values) { want.Set(k, v) } - r.ParseForm() + assertNilError(t, r.ParseForm()) if got := r.Form; !cmp.Equal(got, want) { t.Errorf("Request parameters: %v, want %v", got, want) } @@ -276,6 +276,19 @@ func testErrorResponseForStatusCode(t *testing.T, code int) { } } +func assertNilError(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Errorf("unexpected error: %v", err) + } +} + +func assertWrite(t *testing.T, w io.Writer, data []byte) { + t.Helper() + _, err := w.Write(data) + assertNilError(t, err) +} + func TestNewClient(t *testing.T) { c := NewClient(nil) @@ -1062,7 +1075,8 @@ func TestDo(t *testing.T) { req, _ := client.NewRequest("GET", ".", nil) body := new(foo) ctx := context.Background() - client.Do(ctx, req, body) + _, err := client.Do(ctx, req, body) + assertNilError(t, err) want := &foo{"a"} if !cmp.Equal(body, want) { @@ -1337,11 +1351,14 @@ func TestDo_rateLimit_noNetworkCall(t *testing.T) { // First request is made, and it makes the client aware of rate reset time being in the future. req, _ := client.NewRequest("GET", "first", nil) ctx := context.Background() - client.Do(ctx, req, nil) + _, err := client.Do(ctx, req, nil) + if err == nil { + t.Error("Expected error to be returned.") + } // Second request should not cause a network call to be made, since client can predict a rate limit error. req, _ = client.NewRequest("GET", "second", nil) - _, err := client.Do(ctx, req, nil) + _, err = client.Do(ctx, req, nil) if madeNetworkCall { t.Fatal("Network call was made, even though rate limit is known to still be exceeded.") @@ -1394,11 +1411,14 @@ func TestDo_rateLimit_ignoredFromCache(t *testing.T) { // First request is made so afterwards we can check the returned rate limit headers were ignored. req, _ := client.NewRequest("GET", "first", nil) ctx := context.Background() - client.Do(ctx, req, nil) + _, err := client.Do(ctx, req, nil) + if err == nil { + t.Error("Expected error to be returned.") + } // Second request should not by hindered by rate limits. req, _ = client.NewRequest("GET", "second", nil) - _, err := client.Do(ctx, req, nil) + _, err = client.Do(ctx, req, nil) if err != nil { t.Fatalf("Second request failed, even though the rate limits from the cache should've been ignored: %v", err) @@ -2464,7 +2484,8 @@ func TestUnauthenticatedRateLimitedTransport(t *testing.T) { unauthedClient.BaseURL = client.BaseURL req, _ := unauthedClient.NewRequest("GET", ".", nil) ctx := context.Background() - unauthedClient.Do(ctx, req, nil) + _, err := unauthedClient.Do(ctx, req, nil) + assertNilError(t, err) } func TestUnauthenticatedRateLimitedTransport_missingFields(t *testing.T) { @@ -2539,7 +2560,8 @@ func TestBasicAuthTransport(t *testing.T) { basicAuthClient.BaseURL = client.BaseURL req, _ := basicAuthClient.NewRequest("GET", ".", nil) ctx := context.Background() - basicAuthClient.Do(ctx, req, nil) + _, err := basicAuthClient.Do(ctx, req, nil) + assertNilError(t, err) } func TestBasicAuthTransport_transport(t *testing.T) { diff --git a/github/interactions_orgs_test.go b/github/interactions_orgs_test.go index 9258c92f50..f014bd5fee 100644 --- a/github/interactions_orgs_test.go +++ b/github/interactions_orgs_test.go @@ -59,7 +59,7 @@ func TestInteractionsService_UpdateRestrictionsForOrg(t *testing.T) { mux.HandleFunc("/orgs/o/interaction-limits", func(w http.ResponseWriter, r *http.Request) { v := new(InteractionRestriction) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview) diff --git a/github/interactions_repos_test.go b/github/interactions_repos_test.go index 0a9f51245e..fd48ae7130 100644 --- a/github/interactions_repos_test.go +++ b/github/interactions_repos_test.go @@ -59,7 +59,7 @@ func TestInteractionsService_UpdateRestrictionsForRepo(t *testing.T) { mux.HandleFunc("/repos/o/r/interaction-limits", func(w http.ResponseWriter, r *http.Request) { v := new(InteractionRestriction) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview) diff --git a/github/issue_import_test.go b/github/issue_import_test.go index 48f96ab3a5..17c7c17ffe 100644 --- a/github/issue_import_test.go +++ b/github/issue_import_test.go @@ -38,14 +38,14 @@ func TestIssueImportService_Create(t *testing.T) { mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) { v := new(IssueImportRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeIssueImportAPI) if !cmp.Equal(v, input) { t.Errorf("Request body = %+v, want %+v", v, input) } - w.Write(issueImportResponseJSON) + assertWrite(t, w, issueImportResponseJSON) }) ctx := context.Background() @@ -96,7 +96,7 @@ func TestIssueImportService_Create_defered(t *testing.T) { mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) { v := new(IssueImportRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeIssueImportAPI) if !cmp.Equal(v, input) { @@ -104,7 +104,7 @@ func TestIssueImportService_Create_defered(t *testing.T) { } w.WriteHeader(http.StatusAccepted) - w.Write(issueImportResponseJSON) + assertWrite(t, w, issueImportResponseJSON) }) ctx := context.Background() @@ -142,7 +142,7 @@ func TestIssueImportService_Create_badResponse(t *testing.T) { mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) { v := new(IssueImportRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeIssueImportAPI) if !cmp.Equal(v, input) { @@ -150,7 +150,7 @@ func TestIssueImportService_Create_badResponse(t *testing.T) { } w.WriteHeader(http.StatusAccepted) - w.Write([]byte("{[}")) + assertWrite(t, w, []byte("{[}")) }) ctx := context.Background() @@ -178,7 +178,7 @@ func TestIssueImportService_CheckStatus(t *testing.T) { testMethod(t, r, "GET") testHeader(t, r, "Accept", mediaTypeIssueImportAPI) w.WriteHeader(http.StatusOK) - w.Write(issueImportResponseJSON) + assertWrite(t, w, issueImportResponseJSON) }) ctx := context.Background() @@ -224,7 +224,7 @@ func TestIssueImportService_CheckStatusSince(t *testing.T) { testMethod(t, r, "GET") testHeader(t, r, "Accept", mediaTypeIssueImportAPI) w.WriteHeader(http.StatusOK) - w.Write([]byte(fmt.Sprintf("[%s]", issueImportResponseJSON))) + assertWrite(t, w, []byte(fmt.Sprintf("[%s]", issueImportResponseJSON))) }) ctx := context.Background() @@ -261,7 +261,7 @@ func TestIssueImportService_CheckStatusSince_badResponse(t *testing.T) { testMethod(t, r, "GET") testHeader(t, r, "Accept", mediaTypeIssueImportAPI) w.WriteHeader(http.StatusOK) - w.Write([]byte("{badly-formed JSON")) + assertWrite(t, w, []byte("{badly-formed JSON")) }) ctx := context.Background() diff --git a/github/issues_assignees_test.go b/github/issues_assignees_test.go index 26773b1154..5273136143 100644 --- a/github/issues_assignees_test.go +++ b/github/issues_assignees_test.go @@ -176,7 +176,7 @@ func TestIssuesService_AddAssignees(t *testing.T) { var assignees struct { Assignees []string `json:"assignees,omitempty"` } - json.NewDecoder(r.Body).Decode(&assignees) + assertNilError(t, json.NewDecoder(r.Body).Decode(&assignees)) testMethod(t, r, "POST") want := []string{"user1", "user2"} @@ -220,7 +220,7 @@ func TestIssuesService_RemoveAssignees(t *testing.T) { var assignees struct { Assignees []string `json:"assignees,omitempty"` } - json.NewDecoder(r.Body).Decode(&assignees) + assertNilError(t, json.NewDecoder(r.Body).Decode(&assignees)) testMethod(t, r, "DELETE") want := []string{"user1", "user2"} diff --git a/github/issues_comments_test.go b/github/issues_comments_test.go index d35deedaf1..70f923481b 100644 --- a/github/issues_comments_test.go +++ b/github/issues_comments_test.go @@ -163,7 +163,7 @@ func TestIssuesService_CreateComment(t *testing.T) { mux.HandleFunc("/repos/o/r/issues/1/comments", func(w http.ResponseWriter, r *http.Request) { v := new(IssueComment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -216,7 +216,7 @@ func TestIssuesService_EditComment(t *testing.T) { mux.HandleFunc("/repos/o/r/issues/comments/1", func(w http.ResponseWriter, r *http.Request) { v := new(IssueComment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/issues_labels_test.go b/github/issues_labels_test.go index 06f4c6b676..88b7be7168 100644 --- a/github/issues_labels_test.go +++ b/github/issues_labels_test.go @@ -113,7 +113,7 @@ func TestIssuesService_CreateLabel(t *testing.T) { mux.HandleFunc("/repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) { v := new(Label) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -166,7 +166,7 @@ func TestIssuesService_EditLabel(t *testing.T) { mux.HandleFunc("/repos/o/r/labels/n", func(w http.ResponseWriter, r *http.Request) { v := new(Label) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { @@ -302,7 +302,7 @@ func TestIssuesService_AddLabelsToIssue(t *testing.T) { mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) { var v []string - json.NewDecoder(r.Body).Decode(&v) + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -389,7 +389,7 @@ func TestIssuesService_ReplaceLabelsForIssue(t *testing.T) { mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) { var v []string - json.NewDecoder(r.Body).Decode(&v) + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { diff --git a/github/issues_milestones_test.go b/github/issues_milestones_test.go index b6472f3468..decaf2c112 100644 --- a/github/issues_milestones_test.go +++ b/github/issues_milestones_test.go @@ -118,7 +118,7 @@ func TestIssuesService_CreateMilestone(t *testing.T) { mux.HandleFunc("/repos/o/r/milestones", func(w http.ResponseWriter, r *http.Request) { v := new(Milestone) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -171,7 +171,7 @@ func TestIssuesService_EditMilestone(t *testing.T) { mux.HandleFunc("/repos/o/r/milestones/1", func(w http.ResponseWriter, r *http.Request) { v := new(Milestone) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/issues_test.go b/github/issues_test.go index 37b2c2ad0f..aa767d6508 100644 --- a/github/issues_test.go +++ b/github/issues_test.go @@ -265,7 +265,7 @@ func TestIssuesService_Create(t *testing.T) { mux.HandleFunc("/repos/o/r/issues", func(w http.ResponseWriter, r *http.Request) { v := new(IssueRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -318,7 +318,7 @@ func TestIssuesService_Edit(t *testing.T) { mux.HandleFunc("/repos/o/r/issues/1", func(w http.ResponseWriter, r *http.Request) { v := new(IssueRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/migrations_source_import_test.go b/github/migrations_source_import_test.go index bcb49de1c9..d357ff10b8 100644 --- a/github/migrations_source_import_test.go +++ b/github/migrations_source_import_test.go @@ -28,7 +28,7 @@ func TestMigrationService_StartImport(t *testing.T) { mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { v := new(Import) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { @@ -111,7 +111,7 @@ func TestMigrationService_UpdateImport(t *testing.T) { mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { v := new(Import) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { @@ -192,7 +192,7 @@ func TestMigrationService_MapCommitAuthor(t *testing.T) { mux.HandleFunc("/repos/o/r/import/authors/1", func(w http.ResponseWriter, r *http.Request) { v := new(SourceImportAuthor) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { @@ -235,7 +235,7 @@ func TestMigrationService_SetLFSPreference(t *testing.T) { mux.HandleFunc("/repos/o/r/import/lfs", func(w http.ResponseWriter, r *http.Request) { v := new(Import) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/migrations_test.go b/github/migrations_test.go index 4a5c0fc6c0..7ddc6300a8 100644 --- a/github/migrations_test.go +++ b/github/migrations_test.go @@ -24,7 +24,7 @@ func TestMigrationService_StartMigration(t *testing.T) { testHeader(t, r, "Accept", mediaTypeMigrationsPreview) w.WriteHeader(http.StatusCreated) - w.Write(migrationJSON) + assertWrite(t, w, migrationJSON) }) opt := &MigrationOptions{ @@ -64,7 +64,7 @@ func TestMigrationService_ListMigrations(t *testing.T) { testHeader(t, r, "Accept", mediaTypeMigrationsPreview) w.WriteHeader(http.StatusOK) - w.Write([]byte(fmt.Sprintf("[%s]", migrationJSON))) + assertWrite(t, w, []byte(fmt.Sprintf("[%s]", migrationJSON))) }) ctx := context.Background() @@ -100,7 +100,7 @@ func TestMigrationService_MigrationStatus(t *testing.T) { testHeader(t, r, "Accept", mediaTypeMigrationsPreview) w.WriteHeader(http.StatusOK) - w.Write(migrationJSON) + assertWrite(t, w, migrationJSON) }) ctx := context.Background() @@ -141,7 +141,7 @@ func TestMigrationService_MigrationArchiveURL(t *testing.T) { testMethod(t, r, "GET") w.WriteHeader(http.StatusOK) - w.Write([]byte("0123456789abcdef")) + assertWrite(t, w, []byte("0123456789abcdef")) }) ctx := context.Background() diff --git a/github/migrations_user_test.go b/github/migrations_user_test.go index 358bb9bdcf..0a303ce634 100644 --- a/github/migrations_user_test.go +++ b/github/migrations_user_test.go @@ -24,7 +24,7 @@ func TestMigrationService_StartUserMigration(t *testing.T) { testHeader(t, r, "Accept", mediaTypeMigrationsPreview) w.WriteHeader(http.StatusCreated) - w.Write(userMigrationJSON) + assertWrite(t, w, userMigrationJSON) }) opt := &UserMigrationOptions{ @@ -62,7 +62,7 @@ func TestMigrationService_ListUserMigrations(t *testing.T) { testHeader(t, r, "Accept", mediaTypeMigrationsPreview) w.WriteHeader(http.StatusOK) - w.Write([]byte(fmt.Sprintf("[%s]", userMigrationJSON))) + assertWrite(t, w, []byte(fmt.Sprintf("[%s]", userMigrationJSON))) }) ctx := context.Background() @@ -95,7 +95,7 @@ func TestMigrationService_UserMigrationStatus(t *testing.T) { testHeader(t, r, "Accept", mediaTypeMigrationsPreview) w.WriteHeader(http.StatusOK) - w.Write(userMigrationJSON) + assertWrite(t, w, userMigrationJSON) }) ctx := context.Background() diff --git a/github/misc_test.go b/github/misc_test.go index 4e88ec5452..69427ebf24 100644 --- a/github/misc_test.go +++ b/github/misc_test.go @@ -26,7 +26,7 @@ func TestMarkdown(t *testing.T) { } mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { v := new(markdownRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { diff --git a/github/orgs_actions_allowed_test.go b/github/orgs_actions_allowed_test.go index 6af0521352..6461dcf9a3 100644 --- a/github/orgs_actions_allowed_test.go +++ b/github/orgs_actions_allowed_test.go @@ -56,7 +56,7 @@ func TestOrganizationsService_EditActionsAllowed(t *testing.T) { mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { v := new(ActionsAllowed) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { diff --git a/github/orgs_actions_permissions_test.go b/github/orgs_actions_permissions_test.go index 9664c79eda..d35965eccb 100644 --- a/github/orgs_actions_permissions_test.go +++ b/github/orgs_actions_permissions_test.go @@ -57,7 +57,7 @@ func TestOrganizationsService_EditActionsPermissions(t *testing.T) { mux.HandleFunc("/orgs/o/actions/permissions", func(w http.ResponseWriter, r *http.Request) { v := new(ActionsPermissions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { diff --git a/github/orgs_hooks_configuration_test.go b/github/orgs_hooks_configuration_test.go index a79f8a56c5..337c721f59 100644 --- a/github/orgs_hooks_configuration_test.go +++ b/github/orgs_hooks_configuration_test.go @@ -72,7 +72,7 @@ func TestOrganizationsService_EditHookConfiguration(t *testing.T) { mux.HandleFunc("/orgs/o/hooks/1/config", func(w http.ResponseWriter, r *http.Request) { v := new(HookConfig) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/orgs_hooks_test.go b/github/orgs_hooks_test.go index 0fe45565b9..843f60e1e3 100644 --- a/github/orgs_hooks_test.go +++ b/github/orgs_hooks_test.go @@ -70,7 +70,7 @@ func TestOrganizationsService_CreateHook(t *testing.T) { mux.HandleFunc("/orgs/o/hooks", func(w http.ResponseWriter, r *http.Request) { v := new(createHookRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") want := &createHookRequest{Name: "web"} @@ -159,7 +159,7 @@ func TestOrganizationsService_EditHook(t *testing.T) { mux.HandleFunc("/orgs/o/hooks/1", func(w http.ResponseWriter, r *http.Request) { v := new(Hook) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/orgs_members_test.go b/github/orgs_members_test.go index 0b5068e55e..9526442cda 100644 --- a/github/orgs_members_test.go +++ b/github/orgs_members_test.go @@ -445,7 +445,7 @@ func TestOrganizationsService_EditOrgMembership_AuthenticatedUser(t *testing.T) mux.HandleFunc("/user/memberships/orgs/o", func(w http.ResponseWriter, r *http.Request) { v := new(Membership) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { @@ -489,7 +489,7 @@ func TestOrganizationsService_EditOrgMembership_SpecifiedUser(t *testing.T) { mux.HandleFunc("/orgs/o/memberships/u", func(w http.ResponseWriter, r *http.Request) { v := new(Membership) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { @@ -647,7 +647,7 @@ func TestOrganizationsService_CreateOrgInvitation(t *testing.T) { mux.HandleFunc("/orgs/o/invitations", func(w http.ResponseWriter, r *http.Request) { v := new(CreateOrgInvitationOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { diff --git a/github/orgs_personal_access_tokens_test.go b/github/orgs_personal_access_tokens_test.go index 93ee1b3db9..9426385f4a 100644 --- a/github/orgs_personal_access_tokens_test.go +++ b/github/orgs_personal_access_tokens_test.go @@ -25,7 +25,7 @@ func TestOrganizationsService_ReviewPersonalAccessTokenRequest(t *testing.T) { mux.HandleFunc("/orgs/o/personal-access-token-requests/1", func(w http.ResponseWriter, r *http.Request) { v := new(ReviewPersonalAccessTokenRequestOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, http.MethodPost) if !cmp.Equal(v, &input) { diff --git a/github/orgs_projects_test.go b/github/orgs_projects_test.go index 390917d4fa..0853ed9b97 100644 --- a/github/orgs_projects_test.go +++ b/github/orgs_projects_test.go @@ -64,7 +64,7 @@ func TestOrganizationsService_CreateProject(t *testing.T) { testHeader(t, r, "Accept", mediaTypeProjectsPreview) v := &ProjectOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, input) { t.Errorf("Request body = %+v, want %+v", v, input) } diff --git a/github/orgs_test.go b/github/orgs_test.go index ca85af25b7..98a6b5257e 100644 --- a/github/orgs_test.go +++ b/github/orgs_test.go @@ -269,7 +269,7 @@ func TestOrganizationsService_Edit(t *testing.T) { mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) { v := new(Organization) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testHeader(t, r, "Accept", mediaTypeMemberAllowedRepoCreationTypePreview) testMethod(t, r, "PATCH") diff --git a/github/projects_test.go b/github/projects_test.go index c4ccbd092a..369e121ae0 100644 --- a/github/projects_test.go +++ b/github/projects_test.go @@ -102,7 +102,7 @@ func TestProjectsService_UpdateProject(t *testing.T) { testHeader(t, r, "Accept", mediaTypeProjectsPreview) v := &ProjectOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, input) { t.Errorf("Request body = %+v, want %+v", v, input) } @@ -284,7 +284,7 @@ func TestProjectsService_CreateProjectColumn(t *testing.T) { testHeader(t, r, "Accept", mediaTypeProjectsPreview) v := &ProjectColumnOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, input) { t.Errorf("Request body = %+v, want %+v", v, input) } @@ -329,7 +329,7 @@ func TestProjectsService_UpdateProjectColumn(t *testing.T) { testHeader(t, r, "Accept", mediaTypeProjectsPreview) v := &ProjectColumnOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, input) { t.Errorf("Request body = %+v, want %+v", v, input) } @@ -400,7 +400,7 @@ func TestProjectsService_MoveProjectColumn(t *testing.T) { testHeader(t, r, "Accept", mediaTypeProjectsPreview) v := &ProjectColumnMoveOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, input) { t.Errorf("Request body = %+v, want %+v", v, input) } @@ -515,7 +515,7 @@ func TestProjectsService_CreateProjectCard(t *testing.T) { testHeader(t, r, "Accept", mediaTypeProjectsPreview) v := &ProjectCardOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, input) { t.Errorf("Request body = %+v, want %+v", v, input) } @@ -563,7 +563,7 @@ func TestProjectsService_UpdateProjectCard(t *testing.T) { testHeader(t, r, "Accept", mediaTypeProjectsPreview) v := &ProjectCardOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, input) { t.Errorf("Request body = %+v, want %+v", v, input) } @@ -634,7 +634,7 @@ func TestProjectsService_MoveProjectCard(t *testing.T) { testHeader(t, r, "Accept", mediaTypeProjectsPreview) v := &ProjectCardMoveOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, input) { t.Errorf("Request body = %+v, want %+v", v, input) } @@ -670,7 +670,7 @@ func TestProjectsService_AddProjectCollaborator(t *testing.T) { testHeader(t, r, "Accept", mediaTypeProjectsPreview) v := &ProjectCollaboratorOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, opt) { t.Errorf("Request body = %+v, want %+v", v, opt) } diff --git a/github/pulls_comments_test.go b/github/pulls_comments_test.go index 47d08d2b77..828651b84a 100644 --- a/github/pulls_comments_test.go +++ b/github/pulls_comments_test.go @@ -269,7 +269,7 @@ func TestPullRequestsService_CreateComment(t *testing.T) { wantAcceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview} mux.HandleFunc("/repos/o/r/pulls/1/comments", func(w http.ResponseWriter, r *http.Request) { v := new(PullRequestComment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) // TODO: remove custom Accept header assertion when the API fully launches. testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) @@ -324,7 +324,7 @@ func TestPullRequestsService_CreateCommentInReplyTo(t *testing.T) { mux.HandleFunc("/repos/o/r/pulls/1/comments", func(w http.ResponseWriter, r *http.Request) { v := new(PullRequestComment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -368,7 +368,7 @@ func TestPullRequestsService_EditComment(t *testing.T) { mux.HandleFunc("/repos/o/r/pulls/comments/1", func(w http.ResponseWriter, r *http.Request) { v := new(PullRequestComment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/pulls_reviews_test.go b/github/pulls_reviews_test.go index 7931ba0514..bafa9f06a3 100644 --- a/github/pulls_reviews_test.go +++ b/github/pulls_reviews_test.go @@ -365,7 +365,7 @@ func TestPullRequestsService_CreateReview(t *testing.T) { mux.HandleFunc("/repos/o/r/pulls/1/reviews", func(w http.ResponseWriter, r *http.Request) { v := new(PullRequestReviewRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -468,7 +468,7 @@ func TestPullRequestsService_CreateReview_addHeader(t *testing.T) { mux.HandleFunc("/repos/o/r/pulls/1/reviews", func(w http.ResponseWriter, r *http.Request) { v := new(PullRequestReviewRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -532,7 +532,7 @@ func TestPullRequestsService_SubmitReview(t *testing.T) { mux.HandleFunc("/repos/o/r/pulls/1/reviews/1/events", func(w http.ResponseWriter, r *http.Request) { v := new(PullRequestReviewRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -585,7 +585,7 @@ func TestPullRequestsService_DismissReview(t *testing.T) { mux.HandleFunc("/repos/o/r/pulls/1/reviews/1/dismissals", func(w http.ResponseWriter, r *http.Request) { v := new(PullRequestReviewDismissalRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { diff --git a/github/pulls_test.go b/github/pulls_test.go index 4ea6bfa9d1..5a9582b896 100644 --- a/github/pulls_test.go +++ b/github/pulls_test.go @@ -361,7 +361,7 @@ func TestPullRequestsService_Create(t *testing.T) { mux.HandleFunc("/repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) { v := new(NewPullRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -488,7 +488,8 @@ func TestPullRequestsService_Edit(t *testing.T) { mux.HandleFunc(fmt.Sprintf("/repos/o/r/pulls/%v", i), func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") testBody(t, r, tt.wantUpdate+"\n") - io.WriteString(w, tt.sendResponse) + _, err := io.WriteString(w, tt.sendResponse) + assertNilError(t, err) madeRequest = true }) diff --git a/github/reactions_test.go b/github/reactions_test.go index f84c338329..e77631e99a 100644 --- a/github/reactions_test.go +++ b/github/reactions_test.go @@ -112,7 +112,7 @@ func TestReactionsService_CreateCommentReaction(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusCreated) - w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) + assertWrite(t, w, []byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) }) ctx := context.Background() @@ -149,7 +149,7 @@ func TestReactionsService_ListIssueReactions(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusOK) - w.Write([]byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) + assertWrite(t, w, []byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) }) ctx := context.Background() @@ -193,7 +193,7 @@ func TestReactionsService_CreateIssueReaction(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusCreated) - w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) + assertWrite(t, w, []byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) }) ctx := context.Background() @@ -230,7 +230,7 @@ func TestReactionsService_ListIssueCommentReactions(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusOK) - w.Write([]byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) + assertWrite(t, w, []byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) }) ctx := context.Background() @@ -274,7 +274,7 @@ func TestReactionsService_CreateIssueCommentReaction(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusCreated) - w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) + assertWrite(t, w, []byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) }) ctx := context.Background() @@ -311,7 +311,7 @@ func TestReactionsService_ListPullRequestCommentReactions(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusOK) - w.Write([]byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) + assertWrite(t, w, []byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) }) ctx := context.Background() @@ -355,7 +355,7 @@ func TestReactionsService_CreatePullRequestCommentReaction(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusCreated) - w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) + assertWrite(t, w, []byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) }) ctx := context.Background() @@ -392,7 +392,7 @@ func TestReactionsService_ListTeamDiscussionReactions(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusOK) - w.Write([]byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) + assertWrite(t, w, []byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) }) ctx := context.Background() @@ -436,7 +436,7 @@ func TestReactionsService_CreateTeamDiscussionReaction(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusCreated) - w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) + assertWrite(t, w, []byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) }) ctx := context.Background() @@ -473,7 +473,7 @@ func TestReactionService_ListTeamDiscussionCommentReactions(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusOK) - w.Write([]byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) + assertWrite(t, w, []byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) }) ctx := context.Background() @@ -517,7 +517,7 @@ func TestReactionService_CreateTeamDiscussionCommentReaction(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusCreated) - w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) + assertWrite(t, w, []byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) }) ctx := context.Background() @@ -878,7 +878,7 @@ func TestReactionService_CreateReleaseReaction(t *testing.T) { testHeader(t, r, "Accept", mediaTypeReactionsPreview) w.WriteHeader(http.StatusCreated) - w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"rocket"}`)) + assertWrite(t, w, []byte(`{"id":1,"user":{"login":"l","id":2},"content":"rocket"}`)) }) const methodName = "CreateReleaseReaction" diff --git a/github/repos_actions_access_test.go b/github/repos_actions_access_test.go index 7085ac7d5c..accfe2abf1 100644 --- a/github/repos_actions_access_test.go +++ b/github/repos_actions_access_test.go @@ -57,7 +57,7 @@ func TestRepositoriesService_EditActionsAccessLevel(t *testing.T) { mux.HandleFunc("/repos/o/r/actions/permissions/access", func(w http.ResponseWriter, r *http.Request) { v := new(RepositoryActionsAccessLevel) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { diff --git a/github/repos_actions_allowed_test.go b/github/repos_actions_allowed_test.go index 6736f909f9..62ff11d966 100644 --- a/github/repos_actions_allowed_test.go +++ b/github/repos_actions_allowed_test.go @@ -56,7 +56,7 @@ func TestRepositoriesService_EditActionsAllowed(t *testing.T) { mux.HandleFunc("/repos/o/r/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { v := new(ActionsAllowed) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { diff --git a/github/repos_actions_permissions_test.go b/github/repos_actions_permissions_test.go index 1d6012f62d..ecff0d5fbc 100644 --- a/github/repos_actions_permissions_test.go +++ b/github/repos_actions_permissions_test.go @@ -57,7 +57,7 @@ func TestRepositoriesService_EditActionsPermissions(t *testing.T) { mux.HandleFunc("/repos/o/r/actions/permissions", func(w http.ResponseWriter, r *http.Request) { v := new(ActionsPermissionsRepository) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { diff --git a/github/repos_autolinks_test.go b/github/repos_autolinks_test.go index a666dbf3ac..37de5b5f5a 100644 --- a/github/repos_autolinks_test.go +++ b/github/repos_autolinks_test.go @@ -69,13 +69,13 @@ func TestRepositoriesService_AddAutolink(t *testing.T) { } mux.HandleFunc("/repos/o/r/autolinks", func(w http.ResponseWriter, r *http.Request) { v := new(AutolinkOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, opt) { t.Errorf("Request body = %+v, want %+v", v, opt) } w.WriteHeader(http.StatusOK) - w.Write([]byte(` + assertWrite(t, w, []byte(` { "key_prefix": "TICKET-", "url_template": "https://example.com/TICKET?query=", diff --git a/github/repos_collaborators_test.go b/github/repos_collaborators_test.go index 8953d36bcc..5aa0ad90f7 100644 --- a/github/repos_collaborators_test.go +++ b/github/repos_collaborators_test.go @@ -268,13 +268,13 @@ func TestRepositoriesService_AddCollaborator(t *testing.T) { opt := &RepositoryAddCollaboratorOptions{Permission: "admin"} mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) { v := new(RepositoryAddCollaboratorOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, opt) { t.Errorf("Request body = %+v, want %+v", v, opt) } w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"permissions": "write","url": "https://api.github.com/user/repository_invitations/1296269","html_url": "https://github.com/octocat/Hello-World/invitations","id":1,"permissions":"write","repository":{"url":"s","name":"r","id":1},"invitee":{"login":"u"},"inviter":{"login":"o"}}`)) + assertWrite(t, w, []byte(`{"permissions": "write","url": "https://api.github.com/user/repository_invitations/1296269","html_url": "https://github.com/octocat/Hello-World/invitations","id":1,"permissions":"write","repository":{"url":"s","name":"r","id":1},"invitee":{"login":"u"},"inviter":{"login":"o"}}`)) }) ctx := context.Background() collaboratorInvitation, _, err := client.Repositories.AddCollaborator(ctx, "o", "r", "u", opt) diff --git a/github/repos_comments_test.go b/github/repos_comments_test.go index b3579bf5a8..00871cd72f 100644 --- a/github/repos_comments_test.go +++ b/github/repos_comments_test.go @@ -117,7 +117,7 @@ func TestRepositoriesService_CreateComment(t *testing.T) { mux.HandleFunc("/repos/o/r/commits/s/comments", func(w http.ResponseWriter, r *http.Request) { v := new(RepositoryComment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -215,7 +215,7 @@ func TestRepositoriesService_UpdateComment(t *testing.T) { mux.HandleFunc("/repos/o/r/comments/1", func(w http.ResponseWriter, r *http.Request) { v := new(RepositoryComment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/repos_commits_test.go b/github/repos_commits_test.go index 5840e67788..fb8b45d443 100644 --- a/github/repos_commits_test.go +++ b/github/repos_commits_test.go @@ -243,7 +243,7 @@ func TestRepositoriesService_GetCommitSHA1(t *testing.T) { testMethod(t, r, "GET") testHeader(t, r, "Accept", mediaTypeV3SHA) - fmt.Fprintf(w, sha1) + fmt.Fprint(w, sha1) }) ctx := context.Background() @@ -299,7 +299,7 @@ func TestRepositoriesService_NonAlphabetCharacter_GetCommitSHA1(t *testing.T) { testMethod(t, r, "GET") testHeader(t, r, "Accept", mediaTypeV3SHA) - fmt.Fprintf(w, sha1) + fmt.Fprint(w, sha1) }) ctx := context.Background() @@ -339,7 +339,7 @@ func TestRepositoriesService_TrailingPercent_GetCommitSHA1(t *testing.T) { testMethod(t, r, "GET") testHeader(t, r, "Accept", mediaTypeV3SHA) - fmt.Fprintf(w, sha1) + fmt.Fprint(w, sha1) }) ctx := context.Background() diff --git a/github/repos_deployments_test.go b/github/repos_deployments_test.go index c3d6637e37..4ee2c4f1c3 100644 --- a/github/repos_deployments_test.go +++ b/github/repos_deployments_test.go @@ -97,7 +97,7 @@ func TestRepositoriesService_CreateDeployment(t *testing.T) { mux.HandleFunc("/repos/o/r/deployments", func(w http.ResponseWriter, r *http.Request) { v := new(DeploymentRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview} @@ -256,7 +256,7 @@ func TestRepositoriesService_CreateDeploymentStatus(t *testing.T) { mux.HandleFunc("/repos/o/r/deployments/1/statuses", func(w http.ResponseWriter, r *http.Request) { v := new(DeploymentStatusRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview} diff --git a/github/repos_environments_test.go b/github/repos_environments_test.go index 0f93d29088..870c99003a 100644 --- a/github/repos_environments_test.go +++ b/github/repos_environments_test.go @@ -184,7 +184,7 @@ func TestRepositoriesService_CreateEnvironment(t *testing.T) { mux.HandleFunc("/repos/o/r/environments/e", func(w http.ResponseWriter, r *http.Request) { v := new(CreateUpdateEnvironment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") want := &CreateUpdateEnvironment{WaitTimer: Int(30), CanAdminsBypass: Bool(true)} @@ -229,7 +229,7 @@ func TestRepositoriesService_CreateEnvironment_noEnterprise(t *testing.T) { mux.HandleFunc("/repos/o/r/environments/e", func(w http.ResponseWriter, r *http.Request) { v := new(CreateUpdateEnvironment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if callCount == 0 { @@ -269,7 +269,7 @@ func TestRepositoriesService_createNewEnvNoEnterprise(t *testing.T) { mux.HandleFunc("/repos/o/r/environments/e", func(w http.ResponseWriter, r *http.Request) { v := new(createUpdateEnvironmentNoEnterprise) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") want := &createUpdateEnvironmentNoEnterprise{ diff --git a/github/repos_hooks_configuration_test.go b/github/repos_hooks_configuration_test.go index 0bdf5cbe9e..27c43d225d 100644 --- a/github/repos_hooks_configuration_test.go +++ b/github/repos_hooks_configuration_test.go @@ -72,7 +72,7 @@ func TestRepositoriesService_EditHookConfiguration(t *testing.T) { mux.HandleFunc("/repos/o/r/hooks/1/config", func(w http.ResponseWriter, r *http.Request) { v := new(HookConfig) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/repos_hooks_test.go b/github/repos_hooks_test.go index 82e8b554d6..84c9f5ee6f 100644 --- a/github/repos_hooks_test.go +++ b/github/repos_hooks_test.go @@ -23,7 +23,7 @@ func TestRepositoriesService_CreateHook(t *testing.T) { mux.HandleFunc("/repos/o/r/hooks", func(w http.ResponseWriter, r *http.Request) { v := new(createHookRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") want := &createHookRequest{Name: "web"} @@ -167,7 +167,7 @@ func TestRepositoriesService_EditHook(t *testing.T) { mux.HandleFunc("/repos/o/r/hooks/1", func(w http.ResponseWriter, r *http.Request) { v := new(Hook) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/repos_keys_test.go b/github/repos_keys_test.go index ba60b6ce9a..8c29c126ee 100644 --- a/github/repos_keys_test.go +++ b/github/repos_keys_test.go @@ -113,7 +113,7 @@ func TestRepositoriesService_CreateKey(t *testing.T) { mux.HandleFunc("/repos/o/r/keys", func(w http.ResponseWriter, r *http.Request) { v := new(Key) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { diff --git a/github/repos_merging_test.go b/github/repos_merging_test.go index 09a03a19a6..b4c50178ab 100644 --- a/github/repos_merging_test.go +++ b/github/repos_merging_test.go @@ -27,7 +27,7 @@ func TestRepositoriesService_Merge(t *testing.T) { mux.HandleFunc("/repos/o/r/merges", func(w http.ResponseWriter, r *http.Request) { v := new(RepositoryMergeRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -91,7 +91,7 @@ func TestRepositoriesService_MergeUpstream(t *testing.T) { mux.HandleFunc("/repos/o/r/merge-upstream", func(w http.ResponseWriter, r *http.Request) { v := new(RepoMergeUpstreamRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { diff --git a/github/repos_pages_test.go b/github/repos_pages_test.go index 21ffb18127..5dd735c00f 100644 --- a/github/repos_pages_test.go +++ b/github/repos_pages_test.go @@ -32,7 +32,7 @@ func TestRepositoriesService_EnablePagesLegacy(t *testing.T) { mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { v := new(createPagesRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeEnablePagesAPIPreview) @@ -82,7 +82,7 @@ func TestRepositoriesService_EnablePagesWorkflow(t *testing.T) { mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { v := new(createPagesRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeEnablePagesAPIPreview) @@ -133,7 +133,7 @@ func TestRepositoriesService_UpdatePagesLegacy(t *testing.T) { mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { v := new(PagesUpdate) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") want := &PagesUpdate{CNAME: String("www.my-domain.com"), BuildType: String("legacy"), Source: &PagesSource{Branch: String("gh-pages")}} @@ -172,7 +172,7 @@ func TestRepositoriesService_UpdatePagesWorkflow(t *testing.T) { mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { v := new(PagesUpdate) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") want := &PagesUpdate{CNAME: String("www.my-domain.com"), BuildType: String("workflow")} diff --git a/github/repos_prereceive_hooks_test.go b/github/repos_prereceive_hooks_test.go index 303e0e1265..b286068047 100644 --- a/github/repos_prereceive_hooks_test.go +++ b/github/repos_prereceive_hooks_test.go @@ -116,7 +116,7 @@ func TestRepositoriesService_UpdatePreReceiveHook(t *testing.T) { mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) { v := new(PreReceiveHook) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/repos_projects_test.go b/github/repos_projects_test.go index 558cc23a00..f381841421 100644 --- a/github/repos_projects_test.go +++ b/github/repos_projects_test.go @@ -64,7 +64,7 @@ func TestRepositoriesService_CreateProject(t *testing.T) { testHeader(t, r, "Accept", mediaTypeProjectsPreview) v := &ProjectOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, input) { t.Errorf("Request body = %+v, want %+v", v, input) } diff --git a/github/repos_releases.go b/github/repos_releases.go index 464c2ee1e2..e2aa845af2 100644 --- a/github/repos_releases.go +++ b/github/repos_releases.go @@ -352,7 +352,7 @@ func (s *RepositoriesService) DownloadReleaseAsset(ctx context.Context, owner, r } if err := CheckResponse(resp); err != nil { - resp.Body.Close() + _ = resp.Body.Close() return nil, "", err } @@ -371,7 +371,7 @@ func (s *RepositoriesService) downloadReleaseAssetFromURL(ctx context.Context, f return nil, err } if err := CheckResponse(resp); err != nil { - resp.Body.Close() + _ = resp.Body.Close() return nil, err } return resp.Body, nil diff --git a/github/repos_releases_test.go b/github/repos_releases_test.go index fa33a64cd1..b2691e1dc2 100644 --- a/github/repos_releases_test.go +++ b/github/repos_releases_test.go @@ -226,7 +226,7 @@ func TestRepositoriesService_CreateRelease(t *testing.T) { mux.HandleFunc("/repos/o/r/releases", func(w http.ResponseWriter, r *http.Request) { v := new(repositoryReleaseRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") want := &repositoryReleaseRequest{ @@ -291,7 +291,7 @@ func TestRepositoriesService_EditRelease(t *testing.T) { mux.HandleFunc("/repos/o/r/releases/1", func(w http.ResponseWriter, r *http.Request) { v := new(repositoryReleaseRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") want := &repositoryReleaseRequest{ @@ -547,7 +547,7 @@ func TestRepositoriesService_EditReleaseAsset(t *testing.T) { mux.HandleFunc("/repos/o/r/releases/assets/1", func(w http.ResponseWriter, r *http.Request) { v := new(ReleaseAsset) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/repos_statuses_test.go b/github/repos_statuses_test.go index c9e03acb8d..cd9b0db70b 100644 --- a/github/repos_statuses_test.go +++ b/github/repos_statuses_test.go @@ -69,7 +69,7 @@ func TestRepositoriesService_CreateStatus(t *testing.T) { mux.HandleFunc("/repos/o/r/statuses/r", func(w http.ResponseWriter, r *http.Request) { v := new(RepoStatus) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { diff --git a/github/repos_tags_test.go b/github/repos_tags_test.go index d1eb8aa90a..0195706fe6 100644 --- a/github/repos_tags_test.go +++ b/github/repos_tags_test.go @@ -68,7 +68,7 @@ func TestRepositoriesService_CreateTagProtection(t *testing.T) { mux.HandleFunc("/repos/o/r/tags/protection", func(w http.ResponseWriter, r *http.Request) { v := new(tagProtectionRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") want := &tagProtectionRequest{Pattern: "tag*"} diff --git a/github/repos_test.go b/github/repos_test.go index a3164edf55..df99b6fc8b 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -230,7 +230,7 @@ func TestRepositoriesService_Create_user(t *testing.T) { wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) { v := new(createRepoRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) @@ -280,7 +280,7 @@ func TestRepositoriesService_Create_org(t *testing.T) { wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} mux.HandleFunc("/orgs/o/repos", func(w http.ResponseWriter, r *http.Request) { v := new(createRepoRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) @@ -314,7 +314,7 @@ func TestRepositoriesService_CreateFromTemplate(t *testing.T) { mux.HandleFunc("/repos/to/tr/generate", func(w http.ResponseWriter, r *http.Request) { v := new(TemplateRepoRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeRepositoryTemplatePreview) @@ -478,7 +478,7 @@ func TestRepositoriesService_Edit(t *testing.T) { wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) { v := new(Repository) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) @@ -1041,7 +1041,7 @@ func TestRepositoriesService_RenameBranch(t *testing.T) { mux.HandleFunc("/repos/o/r/branches/b/rename", func(w http.ResponseWriter, r *http.Request) { v := new(renameBranchRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") want := &renameBranchRequest{NewName: "nn"} @@ -1083,9 +1083,6 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) { defer teardown() mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { - v := new(ProtectionRequest) - json.NewDecoder(r.Body).Decode(v) - testMethod(t, r, "GET") // TODO: remove custom Accept header when this API fully launches testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) @@ -1359,7 +1356,7 @@ func TestRepositoriesService_UpdateBranchProtection_Contexts(t *testing.T) { mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { v := new(ProtectionRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { @@ -1536,7 +1533,7 @@ func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) { mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { v := new(ProtectionRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { @@ -1677,7 +1674,7 @@ func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T) mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { v := new(ProtectionRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { @@ -1791,7 +1788,7 @@ func TestRepositoriesService_UpdateBranchProtection_RequireLastPushApproval(t *t mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { v := new(ProtectionRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { @@ -1907,9 +1904,6 @@ func TestRepositoriesService_GetRequiredStatusChecks(t *testing.T) { defer teardown() mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) { - v := new(ProtectionRequest) - json.NewDecoder(r.Body).Decode(v) - testMethod(t, r, "GET") fmt.Fprint(w, `{ "strict": true, @@ -2008,7 +2002,7 @@ func TestRepositoriesService_UpdateRequiredStatusChecks_Contexts(t *testing.T) { mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) { v := new(RequiredStatusChecksRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { @@ -2086,7 +2080,7 @@ func TestRepositoriesService_UpdateRequiredStatusChecks_Checks(t *testing.T) { mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) { v := new(RequiredStatusChecksRequest) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { @@ -2172,9 +2166,6 @@ func TestRepositoriesService_ListRequiredStatusChecksContexts(t *testing.T) { defer teardown() mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks/contexts", func(w http.ResponseWriter, r *http.Request) { - v := new(ProtectionRequest) - json.NewDecoder(r.Body).Decode(v) - testMethod(t, r, "GET") fmt.Fprint(w, `["x", "y", "z"]`) }) @@ -2307,7 +2298,7 @@ func TestRepositoriesService_UpdatePullRequestReviewEnforcement(t *testing.T) { mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) { v := new(PullRequestReviewsEnforcementUpdate) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { @@ -3259,7 +3250,7 @@ func TestRepositoriesService_Transfer(t *testing.T) { mux.HandleFunc("/repos/o/r/transfer", func(w http.ResponseWriter, r *http.Request) { var v TransferRequest - json.NewDecoder(r.Body).Decode(&v) + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -3303,7 +3294,7 @@ func TestRepositoriesService_Dispatch(t *testing.T) { mux.HandleFunc("/repos/o/r/dispatches", func(w http.ResponseWriter, r *http.Request) { var v DispatchRequestOptions - json.NewDecoder(r.Body).Decode(&v) + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { diff --git a/github/secret_scanning_test.go b/github/secret_scanning_test.go index 7527591338..2ca439aa47 100644 --- a/github/secret_scanning_test.go +++ b/github/secret_scanning_test.go @@ -357,7 +357,7 @@ func TestSecretScanningService_UpdateAlert(t *testing.T) { testMethod(t, r, "PATCH") v := new(SecretScanningAlertUpdateOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) want := &SecretScanningAlertUpdateOptions{State: String("resolved"), Resolution: String("used_in_tests")} diff --git a/github/strings.go b/github/strings.go index 5611b96a88..147c515e2c 100644 --- a/github/strings.go +++ b/github/strings.go @@ -8,8 +8,6 @@ package github import ( "bytes" "fmt" - "io" - "reflect" ) @@ -27,7 +25,7 @@ func Stringify(message interface{}) string { // stringifyValue was heavily inspired by the goprotobuf library. -func stringifyValue(w io.Writer, val reflect.Value) { +func stringifyValue(w *bytes.Buffer, val reflect.Value) { if val.Kind() == reflect.Ptr && val.IsNil() { w.Write([]byte("")) return diff --git a/github/teams_discussion_comments_test.go b/github/teams_discussion_comments_test.go index f6b5433b7b..eaac112bf2 100644 --- a/github/teams_discussion_comments_test.go +++ b/github/teams_discussion_comments_test.go @@ -247,7 +247,7 @@ func TestTeamsService_CreateComment(t *testing.T) { handlerFunc := func(w http.ResponseWriter, r *http.Request) { v := new(DiscussionComment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, &input) { @@ -319,7 +319,7 @@ func TestTeamsService_EditComment(t *testing.T) { input := DiscussionComment{Body: String("e")} handlerFunc := func(w http.ResponseWriter, r *http.Request) { v := new(DiscussionComment) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, &input) { diff --git a/github/teams_discussions_test.go b/github/teams_discussions_test.go index 0e73751964..a9c7434ec1 100644 --- a/github/teams_discussions_test.go +++ b/github/teams_discussions_test.go @@ -324,7 +324,7 @@ func TestTeamsService_CreateDiscussionByID(t *testing.T) { mux.HandleFunc("/organizations/1/team/2/discussions", func(w http.ResponseWriter, r *http.Request) { v := new(TeamDiscussion) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, &input) { @@ -368,7 +368,7 @@ func TestTeamsService_CreateDiscussionBySlug(t *testing.T) { mux.HandleFunc("/orgs/o/teams/s/discussions", func(w http.ResponseWriter, r *http.Request) { v := new(TeamDiscussion) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, &input) { @@ -412,7 +412,7 @@ func TestTeamsService_EditDiscussionByID(t *testing.T) { mux.HandleFunc("/organizations/1/team/2/discussions/3", func(w http.ResponseWriter, r *http.Request) { v := new(TeamDiscussion) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, &input) { @@ -456,7 +456,7 @@ func TestTeamsService_EditDiscussionBySlug(t *testing.T) { mux.HandleFunc("/orgs/o/teams/s/discussions/3", func(w http.ResponseWriter, r *http.Request) { v := new(TeamDiscussion) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, &input) { diff --git a/github/teams_members_test.go b/github/teams_members_test.go index 5ef1735160..bdaf1d7217 100644 --- a/github/teams_members_test.go +++ b/github/teams_members_test.go @@ -333,7 +333,7 @@ func TestTeamsService__AddTeamMembershipByID(t *testing.T) { mux.HandleFunc("/organizations/1/team/2/memberships/u", func(w http.ResponseWriter, r *http.Request) { v := new(TeamAddTeamMembershipOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, opt) { @@ -377,7 +377,7 @@ func TestTeamsService__AddTeamMembershipByID_notFound(t *testing.T) { mux.HandleFunc("/organizations/1/team/2/memberships/u", func(w http.ResponseWriter, r *http.Request) { v := new(TeamAddTeamMembershipOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, opt) { @@ -422,7 +422,7 @@ func TestTeamsService__AddTeamMembershipBySlug(t *testing.T) { mux.HandleFunc("/orgs/o/teams/s/memberships/u", func(w http.ResponseWriter, r *http.Request) { v := new(TeamAddTeamMembershipOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, opt) { @@ -466,7 +466,7 @@ func TestTeamsService__AddTeamMembershipBySlug_notFound(t *testing.T) { mux.HandleFunc("/orgs/o/teams/s/memberships/u", func(w http.ResponseWriter, r *http.Request) { v := new(TeamAddTeamMembershipOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, opt) { diff --git a/github/teams_test.go b/github/teams_test.go index d86fab7fd6..d215605c61 100644 --- a/github/teams_test.go +++ b/github/teams_test.go @@ -195,7 +195,7 @@ func TestTeamsService_CreateTeam(t *testing.T) { mux.HandleFunc("/orgs/o/teams", func(w http.ResponseWriter, r *http.Request) { v := new(NewTeam) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, &input) { @@ -248,7 +248,7 @@ func TestTeamsService_EditTeamByID(t *testing.T) { mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { v := new(NewTeam) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, &input) { @@ -298,7 +298,7 @@ func TestTeamsService_EditTeamByID_RemoveParent(t *testing.T) { t.Errorf("Unable to read body: %v", err) } body = string(buf) - json.NewDecoder(bytes.NewBuffer(buf)).Decode(v) + assertNilError(t, json.NewDecoder(bytes.NewBuffer(buf)).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, &input) { @@ -332,7 +332,7 @@ func TestTeamsService_EditTeamBySlug(t *testing.T) { mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { v := new(NewTeam) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, &input) { @@ -382,7 +382,7 @@ func TestTeamsService_EditTeamBySlug_RemoveParent(t *testing.T) { t.Errorf("Unable to read body: %v", err) } body = string(buf) - json.NewDecoder(bytes.NewBuffer(buf)).Decode(v) + assertNilError(t, json.NewDecoder(bytes.NewBuffer(buf)).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, &input) { @@ -798,7 +798,7 @@ func TestTeamsService_AddTeamRepoByID(t *testing.T) { mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { v := new(TeamAddTeamRepoOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, opt) { @@ -833,7 +833,7 @@ func TestTeamsService_AddTeamRepoBySlug(t *testing.T) { mux.HandleFunc("/orgs/org/teams/slug/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { v := new(TeamAddTeamRepoOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, opt) { @@ -1174,7 +1174,7 @@ func TestTeamsService_AddTeamProjectByID(t *testing.T) { testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) v := &TeamProjectOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, opt) { t.Errorf("Request body = %+v, want %+v", v, opt) } @@ -1213,7 +1213,7 @@ func TestTeamsService_AddTeamProjectBySlug(t *testing.T) { testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) v := &TeamProjectOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, opt) { t.Errorf("Request body = %+v, want %+v", v, opt) } diff --git a/github/users_administration_test.go b/github/users_administration_test.go index ff8f93584c..3400edfcca 100644 --- a/github/users_administration_test.go +++ b/github/users_administration_test.go @@ -100,7 +100,7 @@ func TestUsersServiceReason_Suspend(t *testing.T) { mux.HandleFunc("/users/u/suspended", func(w http.ResponseWriter, r *http.Request) { v := new(UserSuspendOptions) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PUT") if !cmp.Equal(v, input) { diff --git a/github/users_emails_test.go b/github/users_emails_test.go index ad9d32e949..9f7f92bc4d 100644 --- a/github/users_emails_test.go +++ b/github/users_emails_test.go @@ -59,7 +59,7 @@ func TestUsersService_AddEmails(t *testing.T) { mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) { var v []string - json.NewDecoder(r.Body).Decode(&v) + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { @@ -101,7 +101,7 @@ func TestUsersService_DeleteEmails(t *testing.T) { mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) { var v []string - json.NewDecoder(r.Body).Decode(&v) + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "DELETE") if !cmp.Equal(v, input) { @@ -149,7 +149,7 @@ func TestUsersService_SetEmailVisibility(t *testing.T) { mux.HandleFunc("/user/email/visibility", func(w http.ResponseWriter, r *http.Request) { v := new(UserEmail) - json.NewDecoder(r.Body).Decode(&v) + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/github/users_gpg_keys_test.go b/github/users_gpg_keys_test.go index 79ebd2d6f4..e0d581fbbc 100644 --- a/github/users_gpg_keys_test.go +++ b/github/users_gpg_keys_test.go @@ -134,7 +134,7 @@ mQINBFcEd9kBEACo54TDbGhKlXKWMvJgecEUKPPcv7XdnpKdGb3LRw5MvFwT0V0f var gpgKey struct { ArmoredPublicKey *string `json:"armored_public_key,omitempty"` } - json.NewDecoder(r.Body).Decode(&gpgKey) + assertNilError(t, json.NewDecoder(r.Body).Decode(&gpgKey)) testMethod(t, r, "POST") if gpgKey.ArmoredPublicKey == nil || *gpgKey.ArmoredPublicKey != input { diff --git a/github/users_keys_test.go b/github/users_keys_test.go index ac5c10d033..f3a8218e59 100644 --- a/github/users_keys_test.go +++ b/github/users_keys_test.go @@ -125,7 +125,7 @@ func TestUsersService_CreateKey(t *testing.T) { mux.HandleFunc("/user/keys", func(w http.ResponseWriter, r *http.Request) { v := new(Key) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { diff --git a/github/users_projects_test.go b/github/users_projects_test.go index 28c4610728..1a298dedaa 100644 --- a/github/users_projects_test.go +++ b/github/users_projects_test.go @@ -64,7 +64,7 @@ func TestUsersService_CreateProject(t *testing.T) { testHeader(t, r, "Accept", mediaTypeProjectsPreview) v := &CreateUserProjectOptions{} - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) if !cmp.Equal(v, input) { t.Errorf("Request body = %+v, want %+v", v, input) } diff --git a/github/users_ssh_signing_keys_test.go b/github/users_ssh_signing_keys_test.go index 3bf59e108d..c7093d7f61 100644 --- a/github/users_ssh_signing_keys_test.go +++ b/github/users_ssh_signing_keys_test.go @@ -125,7 +125,7 @@ func TestUsersService_CreateSSHSigningKey(t *testing.T) { mux.HandleFunc("/user/ssh_signing_keys", func(w http.ResponseWriter, r *http.Request) { v := new(Key) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") if !cmp.Equal(v, input) { diff --git a/github/users_test.go b/github/users_test.go index f71df5cfd9..80020007e4 100644 --- a/github/users_test.go +++ b/github/users_test.go @@ -252,7 +252,7 @@ func TestUsersService_Edit(t *testing.T) { mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) { v := new(User) - json.NewDecoder(r.Body).Decode(v) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") if !cmp.Equal(v, input) { diff --git a/scrape/apps_test.go b/scrape/apps_test.go index 32d7944940..5c462e58ea 100644 --- a/scrape/apps_test.go +++ b/scrape/apps_test.go @@ -33,7 +33,7 @@ func Test_AppRestrictionsEnabled(t *testing.T) { defer cleanup() mux.HandleFunc("/organizations/o/settings/oauth_application_policy", func(w http.ResponseWriter, r *http.Request) { - copyTestFile(w, tt.testFile) + copyTestFile(t, w, tt.testFile) }) got, err := client.AppRestrictionsEnabled("o") @@ -52,7 +52,7 @@ func Test_ListOAuthApps(t *testing.T) { defer cleanup() mux.HandleFunc("/organizations/e/settings/oauth_application_policy", func(w http.ResponseWriter, r *http.Request) { - copyTestFile(w, "access-restrictions-enabled.html") + copyTestFile(t, w, "access-restrictions-enabled.html") }) got, err := client.ListOAuthApps("e") diff --git a/scrape/forms_test.go b/scrape/forms_test.go index 2c7b9fef52..b9bd60cb21 100644 --- a/scrape/forms_test.go +++ b/scrape/forms_test.go @@ -94,7 +94,10 @@ func Test_FetchAndSumbitForm(t *testing.T) { `) }) mux.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) { - r.ParseForm() + err := r.ParseForm() + if err != nil { + t.Errorf("error parsing form: %v", err) + } want := url.Values{"hidden": {"h"}, "name": {"n"}} if got := r.Form; !cmp.Equal(got, want) { t.Errorf("submitted form contained values %v, want %v", got, want) @@ -103,7 +106,10 @@ func Test_FetchAndSumbitForm(t *testing.T) { }) setValues := func(values url.Values) { values.Set("name", "n") } - fetchAndSubmitForm(client.Client, client.baseURL.String()+"/", setValues) + _, err := fetchAndSubmitForm(client.Client, client.baseURL.String()+"/", setValues) + if err != nil { + t.Errorf("fetchAndSubmitForm returned err: %v", err) + } if !submitted { t.Error("form was never submitted") } diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index 74e867b641..f2b928dd9a 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -6,6 +6,7 @@ import ( "net/http/httptest" "net/url" "os" + "testing" ) // setup a test HTTP server along with a scrape.Client that is configured to @@ -21,13 +22,18 @@ func setup() (client *Client, mux *http.ServeMux, cleanup func()) { return client, mux, server.Close } -func copyTestFile(w io.Writer, filename string) error { +func copyTestFile(t *testing.T, w io.Writer, filename string) { + t.Helper() f, err := os.Open("testdata/" + filename) if err != nil { - return err + t.Errorf("unable to open test file: %v", err) } - defer f.Close() - _, err = io.Copy(w, f) - return err + if err != nil { + t.Errorf("failure copying test file: %v", err) + } + err = f.Close() + if err != nil { + t.Errorf("failure closing test file: %v", err) + } } diff --git a/test/fields/fields.go b/test/fields/fields.go index 8f5d1ffc05..62f6455f49 100644 --- a/test/fields/fields.go +++ b/test/fields/fields.go @@ -31,10 +31,6 @@ import ( var ( client *github.Client - // auth indicates whether tests are being run with an OAuth token. - // Tests can use this flag to skip certain tests when run without auth. - auth bool - skipURLs = flag.Bool("skip_urls", false, "skip url fields") ) @@ -47,7 +43,6 @@ func main() { client = github.NewClient(nil) } else { client = github.NewTokenClient(context.Background(), token) - auth = true } for _, tt := range []struct { diff --git a/test/integration/authorizations_test.go b/test/integration/authorizations_test.go index ff4980672c..39dfdd6b25 100644 --- a/test/integration/authorizations_test.go +++ b/test/integration/authorizations_test.go @@ -10,9 +10,7 @@ package integration import ( "context" - "math/rand" "os" - "strconv" "strings" "testing" "time" @@ -21,8 +19,6 @@ import ( ) const msgEnvMissing = "Skipping test because the required environment variable (%v) is not present." -const envKeyGitHubUsername = "GITHUB_USERNAME" -const envKeyGitHubPassword = "GITHUB_PASSWORD" const envKeyClientID = "GITHUB_CLIENT_ID" const envKeyClientSecret = "GITHUB_CLIENT_SECRET" const envKeyAccessToken = "GITHUB_ACCESS_TOKEN" @@ -31,7 +27,6 @@ const InvalidTokenValue = "iamnotacroken" // TestAuthorizationsAppOperations tests the application/token related operations, such // as creating, testing, resetting and revoking application OAuth tokens. func TestAuthorizationsAppOperations(t *testing.T) { - appAuthenticatedClient := getOAuthAppClient(t) // We know these vars are set because getOAuthAppClient would have @@ -107,44 +102,8 @@ func TestAuthorizationsAppOperations(t *testing.T) { failIfNotStatusCode(t, resp, 404) } -// generatePersonalAuthTokenRequest is a helper function that generates an -// AuthorizationRequest for a Personal Access Token (no client id). -func generatePersonalAuthTokenRequest() *github.AuthorizationRequest { - - rand := randString() - auth := github.AuthorizationRequest{ - Note: github.String("Personal token: Note generated by test: " + rand), - Scopes: []github.Scope{github.ScopePublicRepo}, - Fingerprint: github.String("Personal token: Fingerprint generated by test: " + rand), - } - - return &auth -} - -// generatePersonalAuthTokenRequest is a helper function that generates an -// AuthorizationRequest for an OAuth application Token (uses client id). -func generateAppAuthTokenRequest(clientID string, clientSecret string) *github.AuthorizationRequest { - - rand := randString() - auth := github.AuthorizationRequest{ - Note: github.String("App token: Note generated by test: " + rand), - Scopes: []github.Scope{github.ScopePublicRepo}, - Fingerprint: github.String("App token: Fingerprint generated by test: " + rand), - ClientID: github.String(clientID), - ClientSecret: github.String(clientSecret), - } - - return &auth -} - -// randString returns a (kinda) random string for uniqueness purposes. -func randString() string { - return strconv.FormatInt(rand.NewSource(time.Now().UnixNano()).Int63(), 10) -} - // failOnError invokes t.Fatal() if err is present. func failOnError(t *testing.T, err error) { - if err != nil { t.Fatal(err) } @@ -152,33 +111,9 @@ func failOnError(t *testing.T, err error) { // failIfNotStatusCode invokes t.Fatal() if the response's status code doesn't match the expected code. func failIfNotStatusCode(t *testing.T, resp *github.Response, expectedCode int) { - if resp.StatusCode != expectedCode { t.Fatalf("Expected HTTP status code [%v] but received [%v]", expectedCode, resp.StatusCode) } - -} - -// getUserPassClient returns a GitHub client for authorization testing. The client -// uses BasicAuth via GH username and password passed in environment variables -// (and will skip the calling test if those vars are not present). -func getUserPassClient(t *testing.T) *github.Client { - username, ok := os.LookupEnv(envKeyGitHubUsername) - if !ok { - t.Skipf(msgEnvMissing, envKeyGitHubUsername) - } - - password, ok := os.LookupEnv(envKeyGitHubPassword) - if !ok { - t.Skipf(msgEnvMissing, envKeyGitHubPassword) - } - - tp := github.BasicAuthTransport{ - Username: strings.TrimSpace(username), - Password: strings.TrimSpace(password), - } - - return github.NewClient(tp.Client()) } // getOAuthAppClient returns a GitHub client for authorization testing. The client @@ -190,7 +125,6 @@ func getUserPassClient(t *testing.T) *github.Client { // // See GitHub API docs: https://developer.com/v3/oauth_authorizations/#check-an-authorization func getOAuthAppClient(t *testing.T) *github.Client { - username, ok := os.LookupEnv(envKeyClientID) if !ok { t.Skipf(msgEnvMissing, envKeyClientID) diff --git a/update-urls/activity-events_test.go b/update-urls/activity-events_test.go index 9c5b3415df..10dce865b2 100644 --- a/update-urls/activity-events_test.go +++ b/update-urls/activity-events_test.go @@ -48,10 +48,7 @@ func TestPipeline_ActivityEvents_FirstStripAllURLsAndDestroyReceivers(t *testing } func TestParseWebPageEndpoints_ActivityEvents(t *testing.T) { - got, err := parseWebPageEndpoints(activityEventsTestWebPage) - if err != nil { - t.Fatal(err) - } + got := parseWebPageEndpoints(activityEventsTestWebPage) testWebPageHelper(t, got, activityEventsWant) } diff --git a/update-urls/main.go b/update-urls/main.go index 0a3c53c24d..77938be6de 100644 --- a/update-urls/main.go +++ b/update-urls/main.go @@ -474,7 +474,7 @@ func findAllServices(pkgs map[string]*ast.Package) servicesMap { } logf("Step 1 - Processing %v ...", filename) - if err := findClientServices(filename, f, services); err != nil { + if err := findClientServices(f, services); err != nil { log.Fatal(err) } } @@ -581,12 +581,14 @@ func (dc *documentCache) CacheDocFromInternet(urlWithID, filename string, pos to logf("GET %q ...", fullURL) time.Sleep(httpGetDelay) + //nolint:gosec // G107: Potential HTTP request made with variable url resp, err := http.Get(fullURL) check("Unable to get URL: %v: %v", fullURL, err) switch resp.StatusCode { case http.StatusTooManyRequests, http.StatusServiceUnavailable: logf("Sleeping 60 seconds and trying again...") time.Sleep(60 * time.Second) + //nolint:gosec // G107: Potential HTTP request made with variable url resp, err = http.Get(fullURL) check("Unable to get URL: %v: %v", fullURL, err) case http.StatusOK: @@ -602,7 +604,7 @@ func (dc *documentCache) CacheDocFromInternet(urlWithID, filename string, pos to b, err := io.ReadAll(resp.Body) check("Unable to read body of URL: %v, %v", url, err) check("Unable to close body of URL: %v, %v", url, resp.Body.Close()) - dc.apiDocs[url], err = parseWebPageEndpoints(string(b)) + dc.apiDocs[url] = parseWebPageEndpoints(string(b)) check("Unable to parse web page endpoints: url: %v, filename: %v, err: %v", url, filename, err) logf("Found %v web page fragment identifiers.", len(dc.apiDocs[url])) if len(dc.apiDocs[url]) == 0 { @@ -1138,7 +1140,7 @@ func processCallExpr(expr *ast.CallExpr) (recv, funcName string, args []string) } // findClientServices finds all go-github services from the Client struct. -func findClientServices(filename string, f *ast.File, services servicesMap) error { +func findClientServices(f *ast.File, services servicesMap) error { for _, decl := range f.Decls { switch decl := decl.(type) { case *ast.GenDecl: @@ -1196,7 +1198,7 @@ func endpointsEqual(a, b *Endpoint) bool { // parseWebPageEndpoints returns endpoint information, mapped by // web page fragment identifier. -func parseWebPageEndpoints(buf string) (map[string][]*Endpoint, error) { +func parseWebPageEndpoints(buf string) map[string][]*Endpoint { result := map[string][]*Endpoint{} // The GitHub v3 API web pages do not appear to be auto-generated @@ -1259,7 +1261,7 @@ func parseWebPageEndpoints(buf string) (map[string][]*Endpoint, error) { } } - return result, nil + return result } func stripHTML(s string) string { diff --git a/update-urls/reactions_test.go b/update-urls/reactions_test.go index a3071966b9..8d0504ec69 100644 --- a/update-urls/reactions_test.go +++ b/update-urls/reactions_test.go @@ -48,10 +48,7 @@ func TestPipeline_Reactions_FirstStripAllURLsAndDestroyReceivers(t *testing.T) { } func TestParseWebPageEndpoints_Reactions(t *testing.T) { - got, err := parseWebPageEndpoints(reactionsTestWebPage) - if err != nil { - t.Fatal(err) - } + got := parseWebPageEndpoints(reactionsTestWebPage) testWebPageHelper(t, got, reactionsWant) } From 8b802ce9ee1b1a9c008ee1a1a00428fa625fa100 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Sun, 27 Aug 2023 09:56:17 -0500 Subject: [PATCH 2/3] add TestRepositoriesService_DownloadReleaseAsset_FollowRedirectToError --- github/repos_releases_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/github/repos_releases_test.go b/github/repos_releases_test.go index b2691e1dc2..d7efd096d2 100644 --- a/github/repos_releases_test.go +++ b/github/repos_releases_test.go @@ -512,6 +512,36 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirect(t *testing.T) { } } +func TestRepositoriesService_DownloadReleaseAsset_FollowRedirectToError(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/releases/assets/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", defaultMediaType) + // /yo, below will be served as baseURLPath/yo + http.Redirect(w, r, baseURLPath+"/yo", http.StatusFound) + }) + mux.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", "*/*") + w.WriteHeader(http.StatusNotFound) + }) + + ctx := context.Background() + resp, loc, err := client.Repositories.DownloadReleaseAsset(ctx, "o", "r", 1, http.DefaultClient) + if err == nil { + t.Error("Repositories.DownloadReleaseAsset did not return an error") + } + if resp != nil { + resp.Close() + t.Error("Repositories.DownloadReleaseAsset returned stream, want nil") + } + if loc != "" { + t.Errorf(`Repositories.DownloadReleaseAsset returned "%s", want empty ""`, loc) + } +} + func TestRepositoriesService_DownloadReleaseAsset_APIError(t *testing.T) { client, mux, _, teardown := setup() defer teardown() From 1f47817c1feeef6eadfb65bfb19f7735ea12a6dc Mon Sep 17 00:00:00 2001 From: Will Roden Date: Thu, 7 Sep 2023 16:11:17 -0500 Subject: [PATCH 3/3] lint fix --- github/enterprise_actions_runners_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/github/enterprise_actions_runners_test.go b/github/enterprise_actions_runners_test.go index a1d3f17a89..36b2aaefe8 100644 --- a/github/enterprise_actions_runners_test.go +++ b/github/enterprise_actions_runners_test.go @@ -24,7 +24,10 @@ func TestEnterpriseService_GenerateEnterpriseJITConfig(t *testing.T) { mux.HandleFunc("/enterprises/o/actions/runners/generate-jitconfig", func(w http.ResponseWriter, r *http.Request) { v := new(GenerateJITConfigRequest) - json.NewDecoder(r.Body).Decode(v) + err := json.NewDecoder(r.Body).Decode(v) + if err != nil { + t.Errorf("Request body decode failed: %v", err) + } testMethod(t, r, "POST") if !cmp.Equal(v, input) {