bitbucket-branch-source-plugin icon indicating copy to clipboard operation
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

Open JosefGold opened this issue 3 years ago • 10 comments

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.

JosefGold avatar Aug 26 '21 14:08 JosefGold

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.

JosefGold avatar Aug 26 '21 14:08 JosefGold

"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?

KoenDG avatar Aug 26 '21 14:08 KoenDG

Correct, I'm only referring to the deletion of the job definitions, folders and workspaces. Important distinction :D

Updated the issue.

JosefGold avatar Aug 26 '21 15:08 JosefGold

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: image

juliocesarscheidt avatar Aug 26 '21 16:08 juliocesarscheidt

@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.

KalleOlaviNiemitalo avatar Aug 26 '21 17:08 KalleOlaviNiemitalo

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

KoenDG avatar Aug 26 '21 20:08 KoenDG

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)

lindeengineering avatar Aug 27 '21 05:08 lindeengineering

And that's still only on the jenkins host, not on the github/gitlab/bitbucket/... server

KoenDG avatar Aug 27 '21 12:08 KoenDG

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!

cbxp avatar Sep 03 '21 09:09 cbxp

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.

pradeepnnv avatar Sep 29 '21 11:09 pradeepnnv