From 84d08208d558dd5f61a6f87e7b43c363335ade56 Mon Sep 17 00:00:00 2001 From: kit101 Date: Sat, 16 Apr 2022 01:09:37 +0800 Subject: [PATCH] fixbug: gitee populatePageValues --- scm/driver/gitee/gitee.go | 47 ++++++++++++++++++++++------------ scm/driver/gitee/gitee_test.go | 4 +-- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/scm/driver/gitee/gitee.go b/scm/driver/gitee/gitee.go index 1403a7fe4..9bf40fd29 100644 --- a/scm/driver/gitee/gitee.go +++ b/scm/driver/gitee/gitee.go @@ -126,29 +126,42 @@ func websiteAddress(u *url.URL) string { // Response. // response header: total_page, total_count func populatePageValues(req *scm.Request, resp *scm.Response) { + // get last last, totalError := strconv.Atoi(resp.Header.Get("total_page")) + if totalError != nil { + return + } + // get curren page reqURL, err := url.Parse(req.Path) if err != nil { return } - current, currentError := strconv.Atoi(reqURL.Query().Get("page")) - if totalError != nil && currentError != nil { - return + currentPageStr := reqURL.Query().Get("page") + var current int + if currentPageStr == "" { + current = 1 + } else { + currentPage, currentError := strconv.Atoi(currentPageStr) + if currentError != nil { + return + } + current = currentPage } - resp.Page.First = 1 - if last != 0 { - resp.Page.Last = last + + // first, prev + if current <= 1 { + resp.Page.First = 0 + resp.Page.Prev = 0 + } else { + resp.Page.First = 1 + resp.Page.Prev = current - 1 } - if current != 0 { - if current < resp.Page.Last { - resp.Page.Next = current + 1 - } else { - resp.Page.Next = resp.Page.Last - } - if current > resp.Page.First { - resp.Page.Prev = current - 1 - } else { - resp.Page.Prev = resp.Page.First - } + // last, next + if current >= last { + resp.Page.Last = 0 + resp.Page.Next = 0 + } else { + resp.Page.Last = last + resp.Page.Next = current + 1 } } diff --git a/scm/driver/gitee/gitee_test.go b/scm/driver/gitee/gitee_test.go index df990b06d..7547de351 100644 --- a/scm/driver/gitee/gitee_test.go +++ b/scm/driver/gitee/gitee_test.go @@ -56,7 +56,7 @@ func TestClient_Error(t *testing.T) { func testPage(res *scm.Response) func(t *testing.T) { return func(t *testing.T) { - if got, want := res.Page.Prev, 1; got != want { + if got, want := res.Page.Prev, 0; got != want { t.Errorf("Want prev page %d, got %d", want, got) } if got, want := res.Page.Next, 2; got != want { @@ -65,7 +65,7 @@ func testPage(res *scm.Response) func(t *testing.T) { if got, want := res.Page.Last, 3; got != want { t.Errorf("Want last page %d, got %d", want, got) } - if got, want := res.Page.First, 1; got != want { + if got, want := res.Page.First, 0; got != want { t.Errorf("Want first page %d, got %d", want, got) } }