atlassian-python-api icon indicating copy to clipboard operation
atlassian-python-api copied to clipboard

None bitbucket sample works

Open adison opened this issue 2 years ago • 8 comments

I run samples with my bitbucket account/password and found no samples in bitbucket really works

Most samples return 'generator' object has no attribute 'json' or Not supported in Bitbucket Cloud

BTW, it said this is a well documented API, but

  1. No document about the generator of BitbucketBase
  2. https://atlassian-python-api.readthedocs.io/ content is outdated and give no clue about how to parse result, if any?

adison avatar May 15 '23 06:05 adison

Is there any update on this?

The below example returns a generator object and its really hard trying to figure out how to process it: https://github.com/atlassian-api/atlassian-python-api/blob/master/examples/bitbucket/bitbucket_get_project_list.py

@djgoku Can you please help out.

ishan-12 avatar Jun 21 '23 00:06 ishan-12

I run samples with my bitbucket account/password and found no samples in bitbucket really works

Can you share some code? What are you wanting to access?

Here is an example that I am using:

from atlassian.bitbucket import Cloud

bitbucket = Cloud(
    username=os.getenv('BITBUCKET_USERNAME'),
    password=os.getenv('BITBUCKET_PASSWORD')) # This needs to be an app password (with correct permissions) if using bitbucket MFA.

workspace = bitbucket.workspaces.get('workspace-name')

for repository in workspace.repositories.each():
    print(repository.name)

I would check out this for more Bitbucket Cloud code : https://atlassian-python-api.readthedocs.io/bitbucket.html#bitbucket-cloud

djgoku avatar Jun 21 '23 02:06 djgoku

Is there any update on this?

The below example returns a generator object and its really hard trying to figure out how to process it: https://github.com/atlassian-api/atlassian-python-api/blob/master/examples/bitbucket/bitbucket_get_project_list.py

I can't really help you with Bitbucket Server. Here is how you can get project lists with Bitbucket Cloud:

from atlassian.bitbucket import Cloud

bitbucket = Cloud(
    username=os.getenv('BITBUCKET_USERNAME'),
    password=os.getenv('BITBUCKET_PASSWORD')) # This needs to be an app password (with correct permissions) if using bitbucket MFA.

workspace = bitbucket.workspaces.get('workspace-name')

for project in workspace.projects.each():
    print(project.name)

djgoku avatar Jun 21 '23 02:06 djgoku

@djgoku The example you shared is for Bitbucket Cloud but I am looking for examples for the Bitbucket server. I am trying to get a list of all open PRs for all repositories within a project.

ishan-12 avatar Jun 21 '23 03:06 ishan-12

@djgoku The example you shared is for Bitbucket Cloud but I am looking for examples for the Bitbucket server. I am trying to get a list of all open PRs for all repositories within a project.

I think this will get you close:

from atlassian import Bitbucket

bitbucket = Bitbucket(url="http://localhost:7990", username="admin", password="admin")

project_key = "PROJ"
repository_slug = "repository-slug"

for pull_request in bitbucket.get_pull_requests(project_key, repository_slug):
  print(f"pull request title {pull_request.title} description {pull_request.description}")

djgoku avatar Jun 21 '23 04:06 djgoku

@djgoku Thank you for the above info. In the above example, how can I use bitbucket.repo_list(project_key) or repo_all_list("project_key") to get a list of all repos, get the repo_slug from them and pass it to get pull_requests?

The issue with repo_list or repo_all_list is that they return a generator object and I am not sure what the best way to iterate over it. Can you please provide an example like the above to get a list of all repos, get the repository slug for all given just the project key?

ishan-12 avatar Jun 21 '23 04:06 ishan-12

Hi, a generator object is Python related. If used on the right side of a for loop it yields every entry. This is used for reducing load on the server since the Snext page is retrieved when needed. With the list function you can create a list out of a generator.

Spacetown avatar Jun 21 '23 04:06 Spacetown

Yup, I figure out the issue. I was setting advanced_mode to true while creating the bitbucket session:

 bitbucket = Bitbucket(
     url='http://abc.com',
     username="******",
     password="*****",
     advanced_mode=True)

Thank you for helping out @djgoku and @Spacetown.

ishan-12 avatar Jun 21 '23 15:06 ishan-12