Bugzilla:BzAPI:HowTo

From MozillaWiki
Jump to: navigation, search

This specific REST API, generally referred to as "BzAPI", is DEPRECATED. For new projects, use the native REST API instead. The BMO team has implemented a compatibility layer to help existing apps transition off BzAPI onto the native REST API. Please migrate existing BzAPI-based apps to the new compatibility-layer endpoint as soon as possible, as BzAPI will be shut off at some point in the future. Direct any questions to the BMO team.

The native REST API is available in Bugzilla 5.0 and up, and on bugzilla.mozilla.org. Although BzAPI is deprecated, it can be used with older Bugzilla installations (versions prior to 5.0) to provide a REST API.


This page is a repository for tips, tricks, ideas, code samples etc. for the Bugzilla's BzAPI.

Ibuildings.nl BV have kindly contributed a PHP class for communicating with the BzAPI.

Christian Legnitto and Jeff Balogh have written a Python library (blog post).

(Note that it is not known whether these bits of code have been updated to the new 0.9 API field names.)

Heather Arthur wrote a wrapper to make the REST API even easier to use from a web page, node.js or Addon SDK package.

Burak Yiğit Kaya and Berker Peksag have implemented the Bugzilla API for Pyresto.

Example

Here is a simple example in JavaScript using XHR. It will return any bugs that you have ever commented on, that were also changed in the last day.

function handleResponse(response)
{
  var output = "";
  var json = JSON.parse(response);
  var bugs = json.bugs;

  for (var i = 0; i < bugs.length; i++) {
    output += bugs[i].id + ": " + bugs[i].summary + "\n";
  }

  alert(output);
}

function progressListener() {
  if (this.readyState == 4 && this.status == 200) {
    handleResponse(this.responseText);
  }
}

function getMyActiveBugs(bugzillaLogin)
{
  var apiURL = "https://api-dev.bugzilla.mozilla.org/0.3/bug" + 
               "?email1=" + bugzillaLogin +
               "&email1_type=equals_any&email1_comment_author=1" +
               "&changed_after=1d&changed_before=Now";
  
  var client = new XMLHttpRequest();
  client.onreadystatechange = progressListener;
  client.open("GET", apiURL);
  client.setRequestHeader('Accept',       'application/json');
  client.setRequestHeader('Content-Type', 'application/json');
  client.send();
}

getMyActiveBugs("gerv@mozilla.org");

Requests

There is no API equivalent of request.cgi, but you can get the same information via the standard search. Use the Boolean Charts, and the fields flag.requestee, flag.setter, product, component and flag to replicate the searches that request.cgi give you, and of course you can add in any other parameters you want to search on.

Flags are not returned in search results, so when you get the results back, do another search specifying only the Bug IDs, and BzAPI will give you a full bug object back for each bug, including the flags. (This is a special shortcut to get multiple full bug objects in one request.) You can then take this list and filter or present it in whatever way you choose. And because you have the bug objects, you can change the flag fields and submit them back in order to update things.