Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixbug: gitee populatePageValues #167

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 0 additions & 17 deletions scm/driver/gitee/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,6 @@ func (s *issueService) List(ctx context.Context, repo string, opts scm.IssueList
path := fmt.Sprintf("repos/%s/issues?%s", repo, encodeIssueListOptions(opts))
out := []*issue{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
if err != nil {
res.Page.First = 1
if opts.Page-1 < 1 {
res.Page.Prev = 1
} else {
res.Page.Prev = opts.Page - 1
}
totalPage, perr := strconv.Atoi(res.Header.Get("total_page"))
if perr != nil {
res.Page.Last = totalPage
if opts.Page+1 > totalPage {
res.Page.Next = totalPage
} else {
res.Page.Next = opts.Page + 1
}
}
}
return convertIssueList(out), res, err
}

Expand Down