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

Datetime issues

Open martyweb opened this issue 3 months ago • 3 comments

When getting "commits" from bitbucket cloud the date attribute is returning an exception.

        for commit in commits:
            print(commit)
            
            try:
                print(commit.date)
            except Exception as e:
                print(f"Error: {e}")

Output

...
'date': '2025-09-18T21:26:38+00:00',
...

Error: time data '2025-09-18T21:26:38+00:00Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'

I can get it to work if I update the BitbucketBase.get_time() method

Current

            if sys.version_info <= (3, 7):
                value_str = RE_TIMEZONE.sub(r"\1\2", value_str)
            try:
                value_str = value_str[:26] + "Z"
                value = datetime.strptime(value_str, self.CONF_TIMEFORMAT)
            except ValueError:
                value = datetime.strptime(
                    value_str,
                    "%Y-%m-%dT%H:%M:%S.%fZ",
                )

Working code

            if sys.version_info <= (3, 7):
                value_str = RE_TIMEZONE.sub(r"\1\2", value_str)
            try:
                
                value = datetime.strptime(value_str[:26] + "Z", self.CONF_TIMEFORMAT)
            except ValueError:
                value = datetime.strptime(
                    value_str,
                    "%Y-%m-%dT%H:%M:%S%f%z",
                )

Other info

python --version
Python 3.11.13

pip list
Package              Version     Editable project location
-------------------- ----------- ----------------------------
atlassian-python-api 4.0.7
bb-data              0.1.0       /home/marty/projects/bb-data
beautifulsoup4       4.13.5
certifi              2025.8.3
charset-normalizer   3.4.3
Deprecated           1.2.18
idna                 3.10
jmespath             1.0.1
numpy                2.3.3
oauthlib             3.3.1
pandas               2.3.2
pip                  24.0
python-dateutil      2.9.0.post0
python-dotenv        1.1.1
pytz                 2025.2
requests             2.32.5
requests-oauthlib    2.0.0
setuptools           68.1.2
six                  1.17.0
soupsieve            2.8
typing_extensions    4.15.0
tzdata               2025.2
urllib3              2.5.0
wheel                0.42.0
wrapt                1.17.3

martyweb avatar Sep 19 '25 18:09 martyweb

Hi 👋,

I’d like to contribute to this issue as part of Hacktoberfest. Please let me know if it’s available to work on.

/assign

PR-HARIHARAN avatar Oct 04 '25 04:10 PR-HARIHARAN

Hi @martyweb 👋

I tried to reproduce the bug but couldn’t — I only get a 401 Unauthorized when running the example code.

from atlassian import Bitbucket

# Initialize Bitbucket Cloud client
bitbucket = Bitbucket(
    url='https://api.bitbucket.org/',
    username='YOUR_USERNAME',
    password='YOUR_APP_PASSWORD',  # I used my generated API token here
    cloud=True
)

# Get commits from a repository
commits = bitbucket.get_commits('YOUR_WORKSPACE', 'YOUR_REPO')

# Try to access the date - this will fail
for commit in commits:
    print(commit)
    try:
        print(commit.date)
    except Exception as e:
        print(f"Error: {e}")

I created an API key (with read access) and used it in place of the app password, but the request still fails with 401 Unauthorized.

Do you have any additional steps or configuration details I should follow to reproduce this on Bitbucket?

Thanks!

PR-HARIHARAN avatar Oct 04 '25 13:10 PR-HARIHARAN

Sorry, this example assumed you have the proper access setup. If you're getting an Unauthorized error then your username and/or token isn't setup to read the commits on the repository you're trying to query.

Check your token is in the YOUR_WORKSPACE, it has Commit:Read access, and your YOUR_REPO is within the YOUR_WORKSPACE.

martyweb avatar Oct 04 '25 14:10 martyweb