diff --git a/github/github.go b/github/github.go index 56f1be892d2..a7afd834369 100644 --- a/github/github.go +++ b/github/github.go @@ -536,12 +536,18 @@ func (r *Response) populatePageValues() { } page := q.Get("page") + since := q.Get("since") before := q.Get("before") after := q.Get("after") - if page == "" && before == "" && after == "" { + + if page == "" && before == "" && after == "" && 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 dfa0d3389ae..3a0c2931273 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{ @@ -761,6 +790,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()