MunifTanjim / node-bitbucket

Bitbucket API client for Browser and Node.js
https://bitbucketjs.netlify.app
MIT License
105 stars 31 forks source link

Invalid page error when sending page and q on bitbucket.repositories.listBranches #89

Closed Style87 closed 3 years ago

Style87 commented 3 years ago

When calling bitbucket.repositories.listBranches if I pass both the page and q parameters I get the error Invalid page. The call works fine when only the page or q parameter is sent individually.

MunifTanjim commented 3 years ago

Can you provide some code examples?

Style87 commented 3 years ago
let response, page=1;
do {
  try {
    response = await bitbucketConn.repositories
      .listBranches({
        repo_slug: this._uuid,
        workspace: this._workspaceUUID,
        q: 'name~"release"',
        page: page
      });
  } catch (e) {
    let { message, error, headers, request, status } = e;
    console.log(error);
    console.log(request.url);
    return false;
  }
  page++;
} while (response.data.next)

Results in

{type: "error", error: {…}}
     error: {message: "Invalid page"}
     type: "error"
     __proto__: Object
https://api.bitbucket.org/2.0/repositories/{WORKSPACE}/{REPO_SLUG}/refs/branches?q=name~%22release%22&page=1

If I remove q: 'name~"release"' or page: page it works fine. I can get filtered results or paginated results but not filtered paginated results. Also for context the code above is run in a Repository class that knows it's uuid and it's workspace's uuid.

MunifTanjim commented 3 years ago

This is not an issue with this library.

If you do:

curl -s "https://api.bitbucket.org/2.0/repositories/atlassian/aui/refs/branches?q=name~%22issue%22&pagelen=1"

You'll see that see that if you include q, then the next page's link Bitbucket returns is this:

  "next": "https://api.bitbucket.org/2.0/repositories/atlassian/aui/refs/branches?q=name%7E%22issue%22&pagelen=1&page=67Fg"

But if you don't inlcude q then it returns this:

  "next": "https://api.bitbucket.org/2.0/repositories/atlassian/aui/refs/branches?pagelen=1&page=2"

Notice the value of page is different. Without q it's 2, but with q it's 67Fg.

If you use q and numerical page value, then that error is acutally returned from the BitBucket's API:

image


So if you want to use the q param and paginate, you'll have to:

I hope that helps.

Style87 commented 3 years ago

@MunifTanjim, thanks a lot for the analysis and the code snippet. You're 100% correct and I have working code now. Secondary question is there somewhere I can donate some money to you?

MunifTanjim commented 3 years ago

Glad that it helped you.

Secondary question is there somewhere I can donate some money to you?

I'm not currently accepting any donation. But I really appreciate that you considered it. Thanks!