bitbucket-branch-source-plugin
bitbucket-branch-source-plugin copied to clipboard
Failure to retrieve Team/Workspace from Bitbucket Cloud API on organization folder scan results in deleting all organization folder content
Version report
Jenkins and plugins versions report:
2.4.5
- What Operating System are you using (both controller, and any agents involved in the problem)?
Jenkins Docker
Reproduction steps
- Discovered as a result of #473
- When running Scan Organization Folder, if the plugin fails to fetch Team/Workspace from the Bitbucket Cloud API it will result in assuming all repositories don't exist anymore and will delete the folders and jobs for all of them
- Also the error is not written to the log, which complicates the ability to debug the issue
Results
Expected result:
Scanned Repositories
Actual result:
All repositories removed from the Org folder.
There's a few issues at play here.
One is faulty exception handling on BitbucketCloudApiClient.java -> getTeam
try { if (enableCache) { return cachedTeam.get(owner, request); } else { return request.call(); } } catch (Exception ex) { return null; }
Exception should be logged and re-thrown. In the current implementation, the caller assumes null was returned because the team doesn't exist.
A more complex issue, is that if a 404 is returned, like it did in this case of #473, that might be a valid response and a null should indeed be returned. However there should be some sort of failsafe to prevent the deletion of all existing repos in the Organizational folder.
"it will result in assuming all repositories don't exist anymore and will delete all of them"
You mean it will delete the workspaces and folders in Jenkins or whatever CI using this, right?
The actual repositories themselves, on server side, are untouched, right?
Correct, I'm only referring to the deletion of the job definitions, folders and workspaces. Important distinction :D
Updated the issue.
I am also facing this issue, but in fact I think the issue is with the API endpoint from Bitbucket, it seems that the plugin is using the endpoint https://api.bitbucket.org/2.0/teams
and this has been removed on August 25th, now it should be used the https://api.bitbucket.org/2.0/workspaces
as they say on the documentation here:
https://developer.atlassian.com/bitbucket/api/2/reference/resource/teams/%7Busername%7D
I have checked the code to guarantee:
@juliocesarscheidt yes it is using the wrong endpoint, but that is tracked as the separate issue https://github.com/jenkinsci/bitbucket-branch-source-plugin/issues/473. The error handling should be corrected regardless.
Correct, I'm only referring to the deletion of the job definitions, folders and workspaces. Important distinction :D
Updated the issue.
Thanks. Might be a silly question but I saw "delete" and "repository" and went aAaAaAaAaAaAaAaAaAaAaAaA
Jenkins will only delete repos from the organization if you have ticked "discard old items". If you add some "Days to keep old items", they will only get disabled. (does not solve the issue but keeps the projects in jenkins)
And that's still only on the jenkins host, not on the github/gitlab/bitbucket/... server
It seems that's a duplicate of https://github.com/jenkinsci/bitbucket-branch-source-plugin/issues/473 that has been fixed in 2.9.11, please upgrade!
Last time , issue resolved once plugin is upgraded to 2.9.11.
But ,this happened again today with similar error.
Started by timer
[Wed Sep 29 00:39:00 UTC 2021] Starting organization scan...
[Wed Sep 29 00:39:00 UTC 2021] Updating actions...
Looking up team details of xxxxxx...
Connecting to https://bitbucket.org using Bitbucket Account
Could not resolve team details
[Wed Sep 29 00:39:01 UTC 2021] Consulting Bitbucket Team/Project
Connecting to https://bitbucket.org using Bitbucket Account
Looking up repositories of user xxxxx
0 repositories were processed
After this error, all existing repos / workspaces in Jenkins were cleaned up.