Skip to content

Commit

Permalink
fixbug: gitee populatePageValues
Browse files Browse the repository at this point in the history
  • Loading branch information
kit101 committed Apr 15, 2022
1 parent f73f51e commit 84d0820
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
47 changes: 30 additions & 17 deletions scm/driver/gitee/gitee.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions scm/driver/gitee/gitee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 84d0820

Please sign in to comment.