From 1d4d3ccdf1609610ad0fd20f75e02223a4dda668 Mon Sep 17 00:00:00 2001 From: Gal Ofri Date: Sun, 9 Mar 2025 18:00:30 +0700 Subject: [PATCH 1/7] docs: update readme and examples for updated go-github-ratelimit and introduce go-github-pagination --- README.md | 9 ++++++++- example/go.mod | 7 +++++-- example/go.sum | 6 ++++-- example/ratelimit/main.go | 32 +++++++++++++++++++++----------- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c7b9dcef090..84bce221f76 100644 --- a/README.md +++ b/README.md @@ -233,7 +233,7 @@ repos, _, err := client.Repositories.List(context.WithValue(ctx, github.SleepUnt ``` You can use [gofri/go-github-ratelimit](https://github.com/gofri/go-github-ratelimit) to handle -secondary rate limit sleep-and-retry for you. +secondary rate limit sleep-and-retry for you, as well as primary rate limit abuse-prevention and callback trigerring. Learn more about GitHub secondary rate limiting in ["About secondary rate limits"](https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#about-secondary-rate-limits). @@ -347,6 +347,13 @@ for repo := range repos.All() { For complete usage of `enrichman/gh-iter`, see the full [package docs](https://github.com/enrichman/gh-iter). +#### Middleware #### + +You can use [gofri/go-github-pagination](https://github.com/gofri/go-github-pagination) to handle +pagination for you. It supports both sync and async modes, as well as customizations. +By default, the middleware automatically paginates through all pages, aggregates results, and returns them as an array. + + ### Webhooks ### `go-github` provides structs for almost all [GitHub webhook events][] as well as functions to validate them and unmarshal JSON payloads from `http.Request` structs. diff --git a/example/go.mod b/example/go.mod index 58560bac535..94ee03576c7 100644 --- a/example/go.mod +++ b/example/go.mod @@ -1,11 +1,14 @@ module github.com/google/go-github/v69/example -go 1.23.0 +go 1.23.1 + +toolchain go1.23.5 require ( github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 github.com/bradleyfalzon/ghinstallation/v2 v2.0.4 - github.com/gofri/go-github-ratelimit v1.0.3 + github.com/gofri/go-github-pagination v1.0.0 + github.com/gofri/go-github-ratelimit/v2 v2.0.1 github.com/google/go-github/v69 v69.2.0 github.com/sigstore/sigstore-go v0.6.1 golang.org/x/crypto v0.32.0 diff --git a/example/go.sum b/example/go.sum index 1c2cbf5ba24..ff29c7c5880 100644 --- a/example/go.sum +++ b/example/go.sum @@ -134,8 +134,10 @@ github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U= github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -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/gofri/go-github-ratelimit/v2 v2.0.1 h1:jhd3iZ2XINscvYGL+kNXZosr70IZyDITZZC+MznthLI= +github.com/gofri/go-github-ratelimit/v2 v2.0.1/go.mod h1:nqGxwHNwqXx5GNRaEEhegUOvMLAx51BOJtN8eFi8FEA= +github.com/gofri/go-github-pagination v1.0.0 h1:nnCi+1xT5ybqY/plctISgiQPWZOtfSciVQlbx/hM/Yw= +github.com/gofri/go-github-pagination v1.0.0/go.mod h1:Qij55Fb4fNPjam3SB+8cLnqp4pgR8RGMyIspYXcyHX0= github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo= github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= diff --git a/example/ratelimit/main.go b/example/ratelimit/main.go index 32596bc3a96..70c7424c437 100644 --- a/example/ratelimit/main.go +++ b/example/ratelimit/main.go @@ -3,16 +3,20 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// The ratelimit command demonstrates using the github_ratelimit.SecondaryRateLimitWaiter. +// The ratelimit command demonstrates using the github_ratelimit as well as github_pagination. // By using the waiter, the client automatically sleeps and retry requests // when it hits secondary rate limits. +// It also prevents the client from abusing the API in case of a primary rate limit. package main import ( "context" "fmt" - "github.com/gofri/go-github-ratelimit/github_ratelimit" + "github.com/gofri/go-github-pagination/githubpagination" + "github.com/gofri/go-github-ratelimit/v2/github_ratelimit" + "github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_primary_ratelimit" + "github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_secondary_ratelimit" "github.com/google/go-github/v69/github" ) @@ -21,22 +25,28 @@ func main() { fmt.Print("Enter GitHub username: ") fmt.Scanf("%s", &username) - rateLimiter, err := github_ratelimit.NewRateLimitWaiterClient(nil) - if err != nil { - fmt.Printf("Error: %v\n", err) - return - } + rateLimiter := github_ratelimit.New(nil, + github_primary_ratelimit.WithLimitDetectedCallback(func(ctx *github_primary_ratelimit.CallbackContext) { + fmt.Printf("Primary rate limit detected: category %s, reset time: %v\n", ctx.Category, ctx.ResetTime) + }), + github_secondary_ratelimit.WithLimitDetectedCallback(func(ctx *github_secondary_ratelimit.CallbackContext) { + fmt.Printf("Secondary rate limit detected: reset time: %v, total sleep time: %v\n", ctx.ResetTime, ctx.TotalSleepTime) + }), + ) - client := github.NewClient(rateLimiter) + paginator := githubpagination.NewClient(rateLimiter, + githubpagination.WithPerPage(100), // default to 100 results per page + ) + client := github.NewClient(paginator) // arbitrary usage of the client - organizations, _, err := client.Organizations.List(context.Background(), username, nil) + repos, _, err := client.Repositories.ListByUser(context.Background(), username, nil) if err != nil { fmt.Printf("Error: %v\n", err) return } - for i, organization := range organizations { - fmt.Printf("%v. %v\n", i+1, organization.GetLogin()) + for i, repo := range repos { + fmt.Printf("%v. %v\n", i+1, repo.GetName()) } } From e0d02f89b9e44b0ea589d64bfe36f40cf64475c4 Mon Sep 17 00:00:00 2001 From: Gal Ofri Date: Sun, 9 Mar 2025 14:06:58 +0200 Subject: [PATCH 2/7] Update README.md Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 84bce221f76..749693c3d3a 100644 --- a/README.md +++ b/README.md @@ -233,7 +233,7 @@ repos, _, err := client.Repositories.List(context.WithValue(ctx, github.SleepUnt ``` You can use [gofri/go-github-ratelimit](https://github.com/gofri/go-github-ratelimit) to handle -secondary rate limit sleep-and-retry for you, as well as primary rate limit abuse-prevention and callback trigerring. +secondary rate limit sleep-and-retry for you, as well as primary rate limit abuse-prevention and callback triggering. Learn more about GitHub secondary rate limiting in ["About secondary rate limits"](https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#about-secondary-rate-limits). From 4aa2efc3e4cca974fd50c51e5c743063723f2888 Mon Sep 17 00:00:00 2001 From: Gal Ofri Date: Sun, 9 Mar 2025 14:07:09 +0200 Subject: [PATCH 3/7] Update README.md Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 749693c3d3a..397a7a74c79 100644 --- a/README.md +++ b/README.md @@ -352,7 +352,7 @@ For complete usage of `enrichman/gh-iter`, see the full [package docs](https://g You can use [gofri/go-github-pagination](https://github.com/gofri/go-github-pagination) to handle pagination for you. It supports both sync and async modes, as well as customizations. By default, the middleware automatically paginates through all pages, aggregates results, and returns them as an array. - +See `example/ratelimit/main.go` for usage. ### Webhooks ### From 41834cbef004816ebe2b8dfe62690f872a21b707 Mon Sep 17 00:00:00 2001 From: Gal Ofri Date: Wed, 12 Mar 2025 14:57:33 +0700 Subject: [PATCH 4/7] remove toolchain edit --- example/go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/example/go.mod b/example/go.mod index 94ee03576c7..d439c6ec484 100644 --- a/example/go.mod +++ b/example/go.mod @@ -2,8 +2,6 @@ module github.com/google/go-github/v69/example go 1.23.1 -toolchain go1.23.5 - require ( github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 github.com/bradleyfalzon/ghinstallation/v2 v2.0.4 From 6f005c726c0a40923da88d1e6bb8ebf6b21c1704 Mon Sep 17 00:00:00 2001 From: Gal Ofri Date: Wed, 12 Mar 2025 18:50:41 +0700 Subject: [PATCH 5/7] undo go.mod upgrade --- example/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/go.mod b/example/go.mod index d439c6ec484..403acd7691c 100644 --- a/example/go.mod +++ b/example/go.mod @@ -1,6 +1,6 @@ module github.com/google/go-github/v69/example -go 1.23.1 +go 1.23.0 require ( github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 From 5b33ef8c59742dd9e6d7036f3852bbb45c28cfad Mon Sep 17 00:00:00 2001 From: Gal Ofri Date: Wed, 12 Mar 2025 20:25:55 +0700 Subject: [PATCH 6/7] upgrade go-github-ratelimit to v2.0.2 for golang 1.23.0 --- example/go.mod | 2 +- example/go.sum | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/example/go.mod b/example/go.mod index 403acd7691c..e65cfeff63f 100644 --- a/example/go.mod +++ b/example/go.mod @@ -6,7 +6,7 @@ require ( github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 github.com/bradleyfalzon/ghinstallation/v2 v2.0.4 github.com/gofri/go-github-pagination v1.0.0 - github.com/gofri/go-github-ratelimit/v2 v2.0.1 + github.com/gofri/go-github-ratelimit/v2 v2.0.2 github.com/google/go-github/v69 v69.2.0 github.com/sigstore/sigstore-go v0.6.1 golang.org/x/crypto v0.32.0 diff --git a/example/go.sum b/example/go.sum index ff29c7c5880..bc2f5fbdfae 100644 --- a/example/go.sum +++ b/example/go.sum @@ -134,10 +134,12 @@ github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U= github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/gofri/go-github-ratelimit/v2 v2.0.1 h1:jhd3iZ2XINscvYGL+kNXZosr70IZyDITZZC+MznthLI= -github.com/gofri/go-github-ratelimit/v2 v2.0.1/go.mod h1:nqGxwHNwqXx5GNRaEEhegUOvMLAx51BOJtN8eFi8FEA= github.com/gofri/go-github-pagination v1.0.0 h1:nnCi+1xT5ybqY/plctISgiQPWZOtfSciVQlbx/hM/Yw= github.com/gofri/go-github-pagination v1.0.0/go.mod h1:Qij55Fb4fNPjam3SB+8cLnqp4pgR8RGMyIspYXcyHX0= +github.com/gofri/go-github-ratelimit/v2 v2.0.1 h1:jhd3iZ2XINscvYGL+kNXZosr70IZyDITZZC+MznthLI= +github.com/gofri/go-github-ratelimit/v2 v2.0.1/go.mod h1:nqGxwHNwqXx5GNRaEEhegUOvMLAx51BOJtN8eFi8FEA= +github.com/gofri/go-github-ratelimit/v2 v2.0.2 h1:gS8wAS1jTmlWGdTjAM7KIpsLjwY1S0S/gKK5hthfSXM= +github.com/gofri/go-github-ratelimit/v2 v2.0.2/go.mod h1:YBQt4gTbdcbMjJFT05YFEaECwH78P5b0IwrnbLiHGdE= github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo= github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= From 91b0abd770213bc8122ad5cb45423901a8493252 Mon Sep 17 00:00:00 2001 From: Gal Ofri Date: Fri, 14 Mar 2025 18:40:24 +0700 Subject: [PATCH 7/7] ./scripts/generate.sh --- example/go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/example/go.sum b/example/go.sum index bc2f5fbdfae..28c7b50ebfb 100644 --- a/example/go.sum +++ b/example/go.sum @@ -136,8 +136,6 @@ github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofri/go-github-pagination v1.0.0 h1:nnCi+1xT5ybqY/plctISgiQPWZOtfSciVQlbx/hM/Yw= github.com/gofri/go-github-pagination v1.0.0/go.mod h1:Qij55Fb4fNPjam3SB+8cLnqp4pgR8RGMyIspYXcyHX0= -github.com/gofri/go-github-ratelimit/v2 v2.0.1 h1:jhd3iZ2XINscvYGL+kNXZosr70IZyDITZZC+MznthLI= -github.com/gofri/go-github-ratelimit/v2 v2.0.1/go.mod h1:nqGxwHNwqXx5GNRaEEhegUOvMLAx51BOJtN8eFi8FEA= github.com/gofri/go-github-ratelimit/v2 v2.0.2 h1:gS8wAS1jTmlWGdTjAM7KIpsLjwY1S0S/gKK5hthfSXM= github.com/gofri/go-github-ratelimit/v2 v2.0.2/go.mod h1:YBQt4gTbdcbMjJFT05YFEaECwH78P5b0IwrnbLiHGdE= github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=