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

gitee client pagination bug #187

Closed
wmvm0 opened this issue May 28, 2022 · 2 comments
Closed

gitee client pagination bug #187

wmvm0 opened this issue May 28, 2022 · 2 comments

Comments

@wmvm0
Copy link

wmvm0 commented May 28, 2022

gitee 分页逻辑有问题,依旧只能查询到前 100 个仓库

Page is default 0

func (s *service) List(ctx context.Context, user *core.User) ([]*core.Repository, error) {
	err := s.renew.Renew(ctx, user, false)
	if err != nil {
		return nil, err
	}

	ctx = context.WithValue(ctx, scm.TokenKey{}, &scm.Token{
		Token:   user.Token,
		Refresh: user.Refresh,
	})
	repos := []*core.Repository{}
	opts := scm.ListOptions{Size: 100}
	for {
		result, meta, err := s.client.Repositories.List(ctx, opts)
		if err != nil {
			return nil, err
		}
		for _, src := range result {
			if src != nil {
				repos = append(repos, convertRepository(src, s.visibility, s.trusted))
			}
		}
		opts.Page = meta.Page.Next
		opts.URL = meta.Page.NextURL

		if opts.Page == 0 && opts.URL == "" {
			break
		}
	}
	return repos, nil
}
func encodeListOptions(opts scm.ListOptions) string {
	params := url.Values{}
	if opts.Page != 0 {
		params.Set("page", strconv.Itoa(opts.Page))
	}
	if opts.Size != 0 {
		params.Set("per_page", strconv.Itoa(opts.Size))
	}
	return params.Encode()
}

current always default value 0

So can't paginate the query

func populatePageValues(req *scm.Request, resp *scm.Response) {
	last, totalError := strconv.Atoi(resp.Header.Get("total_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
	}
	resp.Page.First = 1
	if last != 0 {
		resp.Page.Last = last
	}
	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
		}
	}
}
@wmvm0
Copy link
Author

wmvm0 commented May 28, 2022

@kit101

@wmvm0 wmvm0 changed the title gitee-client-page-bug gitee client pagination bug May 28, 2022
@wmvm0 wmvm0 closed this as completed May 29, 2022
@MarieReRe
Copy link

Hey @wmvm0 was your issue resolved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants