Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Rest API to search a string in all the repositories in Bitbucket?

amit raj
Contributor
September 26, 2018

Hello Friends,

I would like to search a string in all the Bitbucket server repositories using Rest API.

I can search the code from the Bitbucket Server "Search Box" but it is not possible if there are multiple strings to search and generate a report.

a common use case would be- search all repositories where a common password is exposed.

Is there any Rest API available or could you suggest any workaround?

Thank you2018-09-26 18_18_16-Projects - Bitbucket.png

4 answers

2 accepted

1 vote
Answer accepted

Hi @amit raj 

 

After upgrade Bitbucket data-center 7.6.0 and upgrade elasticsearch 6.8.6, I'm getting error like below.

 

Do you have any idea regarding this problem?

 

{
"error": {
"root_cause": [ {
"type": "index_not_found_exception",
"reason": "no such index",
"resource.type": "index_or_alias",
"resource.id": "bitbucket-search-v1",
"index_uuid": "_na_",
"index": "bitbucket-search-v1"
}],
"type": "index_not_found_exception",
"reason": "no such index",
"resource.type": "index_or_alias",
"resource.id": "bitbucket-search-v1",
"index_uuid": "_na_",
"index": "bitbucket-search-v1"
},
"status": 404
}

1 vote
Answer accepted
amit raj
Contributor
May 1, 2019

Hi All,

 I was able to search a string using ELS Rest api. 

this is the the endpoint-

http://<ELSEARCHURL>:9200/bitbucket-search-v1/_search?size=1000&from=0&q=<string-to-search>

eg. if your Bitbucket ELS is hosted on 192.168.10.1 at port 9200 and you would like to search the string "Test"

http://192.168.10.1:9200/bitbucket-search-v1/_search?size=1000&from=0&q=test

please note that it is huge search you need to iterate the loop until search does not find a string.

I wrote a small python program to do that.

https://github.com/inboxamitraj/Bitbucket-Python-Scripts/blob/master/bbsearch.py

Thank you

Mary Slocum August 13, 2021 edited

Hi Amit,

Nice script! 

One problem is that it returns repositoryId and projectId, but not the repo/project name. Is there a way to look up repo/project name by id? Also, is there any way to tell which branch in the repo that the search string was found (as a repo could have a lot of branches)?

Thanks,

Mary

Like amit raj likes this
0 votes
amit raj
Contributor
September 26, 2018

I think we can do something using elastic search Rest endpoints as Bitbucket usage elastic search behind the scene, working on it.

MiniBurgula
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 1, 2019

Hi @amit raj , have you any luck ? I am also looking for same feature in bitbucket api.

amit raj
Contributor
May 1, 2019

Hi @MiniBurgula 

yes. I was able to search a string using ELS Rest api. 

this is the the endpoint-

http://<ELSEARCHURL>:9200/bitbucket-search-v1/_search?size=1000&from=0&q=<string-to-search>

eg. if your Bitbucket ELS is hosted on 192.168.10.1 at port 9200 and you would like to search the string "Test"

http://192.168.10.1:9200/bitbucket-search-v1/_search?size=1000&from=0&q=test

please note that it is huge search you need to iterate the loop until search does not find a string.

I wrote a small python program to do that.

Thank you

Like Apolinar Ramos likes this
MiniBurgula
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 2, 2019

Thanks for the information.

Namita K July 19, 2022

Hi amitraj,

I cannot find your script - bbsearch.py . Is it possible to paste it here? 

Like amit raj likes this
Namita K July 19, 2022

Hi @amit raj

I cannot find your script - bbsearch.py . Is it possible to paste it here? 

Like amit raj likes this
Namita K July 20, 2022

Hi @amit raj  can you help me with the code pls

Like amit raj likes this
sdeals
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 15, 2022
0 votes
Edwin Kyalangalilwa
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 26, 2018

Hi @amit raj,

This link will be helpful. Bitbucket code search API is now available

amit raj
Contributor
September 26, 2018 edited

Thank you Edwin , I think this is only for Bitbucket Cloud, doesn't work for Bitbucket Server.

Like 2 people like this
Like 2 people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Upcoming Bitbucket Events