node-bitbucket icon indicating copy to clipboard operation
node-bitbucket copied to clipboard

repositories.listBranches is throwing an error

Open techie55 opened this issue 6 years ago • 10 comments

Looks like a side-effect of the bitbucket API upgrade: I am getting the following error when using the repositories.listBranches method:

code:500 error:"network timeout at: https://api.bitbucket.org/2.0/repositories/roofstockinc/roofstock.Databases/refs/branches?page=1&pagelen=100" headers:Object {} message:"network timeout at: https://api.bitbucket.org/2.0/repositories/roofstockinc/roofstock.Databases/refs/branches?page=1&pagelen=100" name:"HTTPError" stack:"HTTPError: network timeout at: https://api.bitbucket.org/2.0/repositories/roofstockinc/roofstock.Databases/refs/branches?page=1&pagelen=100\n at fetch.then.then.catch.error ("

I tried the following:

  1. Use clientOptions and pass it to the bitbucket.authenticate method
  2. I am attaching a sample code. Would appreciate if you could provide a working sample of using Token and ClientKey and Secret. I was able to get it to work without the bitbucket package by making direct REST API calls in Node.js. I first called the https://bitbucket.org/site/oauth2/access_token endpoint passing in the Base64 encoding od Key:Secret. This returns the Token Using the Token I was then able to call the https://api.bitbucket.org/2.0/repositories/{token}/{repositoryname}/refs/branches?page=1&pagelen=100 endpoint

I think the issue with the bitbucket module is that I am not able to pass in grant_type: 'client_credentials'

index.txt

techie55 avatar Aug 10 '19 07:08 techie55

Currently the oauth plugin is not implemented correctly and so it's not included into the npm package.

That's why you can't use oauth2 endpoints to generate the access token with the bitbucket client.

What you need to do instead is, get the access token without using this library (Using one of the 5 methods mentioned in https://developer.atlassian.com/bitbucket/api/2/reference/meta/authentication#oauth-2).

But once you get the access token, you should be able to use that with this library like this:

const clientOptions = {
  options: {
    timeout: 10
  }
}

const bitbucket = new Bitbucket(clientOptions)

// no need to await this function, it's synchronous
bitbucket.authenticate({
  type: 'token',
  token: 'ACCESS TOKEN'
})

// now, you can use `bitbucket` as usual

This also means that, if you need to refresh the token, you'll have to do that without the help of this library and then call the bitbucket.authenticate function once again with new access token.

N.B.: There's plan to include a working oauth plugin in next major update of the library: [email protected]

MunifTanjim avatar Aug 10 '19 08:08 MunifTanjim

Thanks @MunifTanjim for the quick response. I tried using your suggestion and getting the same error: stack:"HTTPError: network timeout at: https://api.bitbucket.org/2.0/repositories/****/******/refs/branches?page=1&pagelen=100\n at fetch.then.then.catch.error (..\node_modules\bitbucket\src\request\fetch.js:67:13)\n at <anonymous>\n at process._tickCallback (internal/process/next_tick.js:189:7)"

techie55 avatar Aug 10 '19 16:08 techie55

Can you show me the console output when your run your script with DEBUG=bitbucket environment variable set?


Using the Token I was then able to call the https://api.bitbucket.org/2.0/repositories/{token}/{repositoryname}/refs/branches?page=1&pagelen=100 endpoint

Also, I don't recognize this endpoint... Where did you find it? Shouldn't that be /repositories/{username}/{repo_name}/refs/branches?

MunifTanjim avatar Aug 10 '19 16:08 MunifTanjim

Hi MunifTanjim, The console output is given below: Requesting page 1 of roofstock.Databases branches bitbucket REQUEST: { timeout: 10, method: 'GET', url: ' https://api.bitbucket.org/2.0/repositories//***/refs/branches?page=1&pagelen=100 ', headers: { accept: 'application/json', 'user-agent': 'NodeBitbucket/1.5.0', authorization: 'Bearer *****' }, body: undefined } +0ms Error getting branches to merge into:

Thanks, Johnson

On Sat, Aug 10, 2019 at 9:30 AM Munif Tanjim [email protected] wrote:

Can you show me the console output when your run your script with DEBUG=bitbucket environment variable set?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MunifTanjim/node-bitbucket/issues/38?email_source=notifications&email_token=ABBCRM7FEMHBWGEYJMHHZD3QD3UJRA5CNFSM4IKY2RFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4AQYHQ#issuecomment-520162334, or mute the thread https://github.com/notifications/unsubscribe-auth/ABBCRM7NUYXPNZ2YGFB2LNLQD3UJRANCNFSM4IKY2RFA .

techie55 avatar Aug 10 '19 19:08 techie55

It looks alright :thinking: I think the library is working as intended...

Are you sure that:

  • Repository roofstockinc/roofstock.Databases exists?
  • Your access token has sufficient permission?

MunifTanjim avatar Aug 10 '19 19:08 MunifTanjim

Yes, I have verified by directly calling the API using a sample app. So its nothing to do with permissions.

On Sat, Aug 10, 2019 at 12:37 PM Munif Tanjim [email protected] wrote:

It looks alright 🤔 I think the library is working as intended...

Are you sure that:

  • Repository roofstockinc/roofstock.Databases exists?
  • Your access token has sufficient permission?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MunifTanjim/node-bitbucket/issues/38?email_source=notifications&email_token=ABBCRM2JMAGCI3YLWXFXQU3QD4KGPA5CNFSM4IKY2RFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4ATWJI#issuecomment-520174373, or mute the thread https://github.com/notifications/unsubscribe-auth/ABBCRM5OH7SX5MB2WUOKMG3QD4KGPANCNFSM4IKY2RFA .

techie55 avatar Aug 10 '19 20:08 techie55

Using the Token I was then able to call the https://api.bitbucket.org/2.0/repositories/{token}/{repositoryname}/refs/branches?page=1&pagelen=100 endpoint

Can you provide me the documentation link for the endpoint that you called directly?

MunifTanjim avatar Aug 10 '19 20:08 MunifTanjim

Given below is the sample code snippet of the direct API call. These are the links I referred: https://confluence.atlassian.com/bitbucket/rest-apis-222724129.html https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html

const getToken = async () => { try { Request.post( { "headers": { "content-type": "application/x-www-form-urlencoded", "Authorization": "Basic *****" }, form: { grant_type: 'client_credentials'}, "url": "https://bitbucket.org/site/oauth2/access_token" } , (error, response, body) => { if(error) { return console.dir(error); } console.dir(JSON.parse(body)); return JSON.parse(body)["access_token"]; });

} catch (error) { logAndExitScript(Error getting branches to merge into: ${ responseErrorMessage(error)}); } }

const getBranchNames = async () => { try { let url = "https://api.bitbucket.org/2.0/repositories/%7B" + accountId + "%7D/roofstock.Databases/refs/branches?page=1&pagelen=100" let bearer = "Bearer " + token; Request.get( { "headers": { "content-type": "application/json", "Authorization": bearer }, "url": url } , (error, response, body) => { if(error) { return console.dir(error); } console.dir(JSON.parse(body)); });

} catch (error) { logAndExitScript(Error getting branches to merge into: ${ responseErrorMessage(error)}); } }

let token = await getToken();

// // Get branch names // let branchNames = await getBranchNames();

On Sat, Aug 10, 2019 at 1:07 PM Munif Tanjim [email protected] wrote:

Using the Token I was then able to call the https://api.bitbucket.org/2.0/repositories/{token}/{repositoryname}/refs/branches?page=1&pagelen=100 endpoint

Can you provide me the documentation link for the endpoint that you called directly?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MunifTanjim/node-bitbucket/issues/38?email_source=notifications&email_token=ABBCRM3CG6JF7Y5TY2CZFPDQD4NWTA5CNFSM4IKY2RFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4AUEPY#issuecomment-520176191, or mute the thread https://github.com/notifications/unsubscribe-auth/ABBCRM7Q37JRBAEAHPWZONDQD4NWTANCNFSM4IKY2RFA .

techie55 avatar Aug 10 '19 20:08 techie55

I don't know what you are passing as the params. But this works perfectly for me:

bitbucket.authenticate({
  type: 'token',
  token: 'cgvePiCxbVV-xxx-xxx_xxx=' // access_token
})

bitbucket.repositories
  .listBranches({ username: 'MunifTanjim', repo_slug: 'repo_name' })
  .then(({ data, headers }) => {
    console.log(data)
  })
  .catch(err => {
    console.error(err)
  })

I think it's something to do with the username param. What exactly are you passing as username?

MunifTanjim avatar Aug 18 '19 17:08 MunifTanjim

Thanks MunifTanjim/node-bitbucket, I think I might have gone wrong at

.listBranches({ username: 'MunifTanjim',

I was passing the UUID for the userName since I thought bitbucket had retired the username.

Anyway, I ended up rewriting my whole application using direct BitBucket API calls and using Token, and UUID in place of username.

Thanks,

Johnson

On Sun, Aug 18, 2019 at 10:01 AM Munif Tanjim [email protected] wrote:

I don't know what you are passing as the params. But this works perfectly for me:

bb.authenticate({ type: 'token', token: 'cgvePiCxbVV-xxx-xxx_xxx=' // access_token }) bb.repositories .listBranches({ username: 'MunifTanjim', repo_slug: 'hugotest' }) .then(({ data, headers }) => { console.log(data) }) .catch(err => { console.error(err) })

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MunifTanjim/node-bitbucket/issues/38?email_source=notifications&email_token=ABBCRM34KDW34WTAUK7E33DQFF55JA5CNFSM4IKY2RFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4RD66Q#issuecomment-522338170, or mute the thread https://github.com/notifications/unsubscribe-auth/ABBCRM4LXS2SLABT4EN6CYTQFF55JANCNFSM4IKY2RFA .

techie55 avatar Aug 19 '19 00:08 techie55