0

We are trying to search specific text in Bitbucket Commits from our JAVA Code. We need to use REST web service for it

We tried to use APT /2.0/repositories/{username}/{repo_slug}/commits but it return all commits of this repository

We only need those commit details which having specific text says "xyz"

Again we found one code search API https://api.bitbucket.org/2.0/teams/{username}/search/code here it give error : Server returned HTTP response code: 405 Method Not Allowed

String commitsUrl="https://api.bitbucket.org/2.0/teams/"+bitbucketUsername+"/search/code";

        URL url = new URL(commitsUrl);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestMethod("GET");         
        String encoded = Base64.getEncoder().encodeToString((bitbucketUsername+":"+bitbucketPasscode).getBytes(StandardCharsets.UTF_8));
        connection.setRequestProperty("Authorization", "Basic "+encoded);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        connection.setDoOutput(true);
        String body = "search_query="+search_query;
        os = connection.getOutputStream();
        os.write(body.getBytes(StandardCharsets.UTF_8));

        connection.connect();
        br = new BufferedReader(new InputStreamReader((connection.getInputStream())));

Showing Error: 405 Method Not Allowed at

Expected: Returning some JSON as per description in link https://developer.atlassian.com/bitbucket/api/2/reference/resource/teams/%7Busername%7D/search/code

ved
  • 1
  • 4

1 Answers1

0

Unfortunately we didnt found any option to search in commit comments.

Now we are approaching in other way:

We are taking all commits using REST API repositories{username}{repo_slug}commits And manually compare out search text by its comment field.

String commitsUrl=ROOT_REPOSITORY+bitbucketUsername+"/"+repoName+"/commits/";

URL url = new URL(urlString);

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

connection.setRequestMethod(methodType);    

connection.setRequestProperty("Authorization", "Bearer  "+token);

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

connection.connect();

BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));

StringBuilder sb = new StringBuilder();

String output;

while ((output = br.readLine()) != null) {
  sb.append(output);
}

JSONObject root = new JSONObject(sb.toString());

JSONArray array = root.getJSONArray("values");

for(int i=0; i< array.length(); i++) {

JSONObject obj = (JSONObject) array.get(i);



String message = obj.getString("message");


//From this message string search your text
ved
  • 1
  • 4