From 66a2d59d433ef443270bc458491f56c4bba28f92 Mon Sep 17 00:00:00 2001 From: Brian Langdon Date: Thu, 21 Oct 2021 13:37:43 +0100 Subject: [PATCH 1/2] handle 'since' when returned in the header when paging reponse --- github/github.go | 8 ++++- github/github_test.go | 68 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/github/github.go b/github/github.go index d4d7a54a7f1..f1dab9e72ca 100644 --- a/github/github.go +++ b/github/github.go @@ -517,10 +517,16 @@ func (r *Response) populatePageValues() { } page := q.Get("page") - if page == "" { + since := q.Get("since") + + if page == "" && since == "" { continue } + if since != "" { + page = since + } + for _, segment := range segments[1:] { switch strings.TrimSpace(segment) { case `rel="next"`: diff --git a/github/github_test.go b/github/github_test.go index 08d2c8ad990..0aa629a787d 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -648,6 +648,35 @@ func TestResponse_populatePageValues(t *testing.T) { } } +func TestResponse_populateSinceValues(t *testing.T) { + r := http.Response{ + Header: http.Header{ + "Link": {`; rel="first",` + + ` ; rel="prev",` + + ` ; rel="next",` + + ` ; rel="last"`, + }, + }, + } + + response := newResponse(&r) + if got, want := response.FirstPage, 1; got != want { + t.Errorf("response.FirstPage: %v, want %v", got, want) + } + if got, want := response.PrevPage, 2; want != got { + t.Errorf("response.PrevPage: %v, want %v", got, want) + } + if got, want := response.NextPage, 4; want != got { + t.Errorf("response.NextPage: %v, want %v", got, want) + } + if got, want := response.LastPage, 5; want != got { + t.Errorf("response.LastPage: %v, want %v", got, want) + } + if got, want := response.NextPageToken, ""; want != got { + t.Errorf("response.NextPageToken: %v, want %v", got, want) + } +} + func TestResponse_cursorPagination(t *testing.T) { r := http.Response{ Header: http.Header{ @@ -727,6 +756,45 @@ func TestResponse_populatePageValues_invalid(t *testing.T) { } } +func TestResponse_populateSinceValues_invalid(t *testing.T) { + r := http.Response{ + Header: http.Header{ + "Link": {`,` + + `; rel="first",` + + `https://api.github.com/?since=2; rel="prev",` + + `; rel="next",` + + `; rel="last"`, + }, + }, + } + + response := newResponse(&r) + if got, want := response.FirstPage, 0; got != want { + t.Errorf("response.FirstPage: %v, want %v", got, want) + } + if got, want := response.PrevPage, 0; got != want { + t.Errorf("response.PrevPage: %v, want %v", got, want) + } + if got, want := response.NextPage, 0; got != want { + t.Errorf("response.NextPage: %v, want %v", got, want) + } + if got, want := response.LastPage, 0; got != want { + t.Errorf("response.LastPage: %v, want %v", got, want) + } + + // more invalid URLs + r = http.Response{ + Header: http.Header{ + "Link": {`; rel="first"`}, + }, + } + + response = newResponse(&r) + if got, want := response.FirstPage, 0; got != want { + t.Errorf("response.FirstPage: %v, want %v", got, want) + } +} + func TestDo(t *testing.T) { client, mux, _, teardown := setup() defer teardown() From 8825238ed6a00abd2488d31db0210f0c2a8bed0d Mon Sep 17 00:00:00 2001 From: Brian Langdon Date: Wed, 24 Nov 2021 09:43:34 +0000 Subject: [PATCH 2/2] Update github.go --- github/github.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/github/github.go b/github/github.go index 1047d773d19..a7afd834369 100644 --- a/github/github.go +++ b/github/github.go @@ -536,11 +536,11 @@ func (r *Response) populatePageValues() { } page := q.Get("page") - since := q.Get("since") + since := q.Get("since") before := q.Get("before") after := q.Get("after") - if page == "" && before == "" && after == "" && since == "" { + if page == "" && before == "" && after == "" && since == "" { continue }