Skip to content

Instantly share code, notes, and snippets.

@mcrider
Created September 7, 2012 21:04
Show Gist options
  • Save mcrider/3669614 to your computer and use it in GitHub Desktop.
Save mcrider/3669614 to your computer and use it in GitHub Desktop.
Fetch bugs from a bugzilla instance and display in JSON format
<?php
/*
* Fetch open bugs from a bugzilla database and insert them into github bugs
* TODO: Format in a way Github likes (see http://developer.github.com/v3/issues/)
* TODO: Feed into import script (see https://gist.github.com/1816061)
* NOTE: Need to lookup github usernames by email. Not sure if comments will be accepted if the user is not authenticated
* Written by Matt Crider (github: mcrider)
*/
define('PRODUCT_INITIALS', 'OJS'); // Change to OCS/OHS/OMP as appropriate
define('JSON_RPC_URL', 'http://pkp.sfu.ca/bugzilla/jsonrpc.cgi');
class BugzillaBugFetcher {
// Helper function to get data from server using CURL
function fetchBugData($url) {
// Initializing curl
$ch = curl_init( $url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$result = json_decode(curl_exec($ch)); // Getting jSON result string and convert to array
return $result;
}
/**
* Get IDs for all open bugs for a product
* @return array
*/
function getOpenBugIds() {
// jSON URL which should be requested
$url = JSON_RPC_URL . '?method=Bug.search';
// Request parameters
$params = array('product' => array(PRODUCT_INITIALS),
'status' => array("NEW","UNCOMFIRMED","ASSIGNED","REOPENED","VERIFIED"),
'include_fields' => array('id'));
$url .= '&params=[' . json_encode($params) . ']';
$result = $this->fetchBugData($url);
$bugIds = array();
foreach($result->result->bugs as $bugId) {
$bugIds[] = $bugId->id;
}
// Return array of valid bug IDs
return $bugIds;
}
/**
* Get metadata and comments for an array of bugs
* @param $bugIds array
* @return array
*/
function getBugData($bugIds) {
// jSON URL which should be requested
$url = JSON_RPC_URL . '?method=Bug.get';
// Request parameters
$params = array('ids' => $bugIds,
'include_fields' => array("id","last_change_time","status","creation_time","version","component","summary","assigned_to","cc"));
$url .= '&params=[' . json_encode($params) . ']';
$result = $this->fetchBugData($url);
$bugs = $result->result->bugs;
foreach($bugs as $bug) {
// Add comments to bug
$bug->comments = $this->getBugComments($bug->id);
var_dump($bug);
}
// Return array of valid bugs with metadata
return $bugs;
}
/**
* Get comments for a bug
* @param $bugId int
* @return array
*/
function getBugComments($bugId) {
// jSON URL which should be requested
$url = JSON_RPC_URL . '?method=Bug.comments';
// Request parameters
$params = array('ids' => array($bugId));
$url .= '&params=[' . json_encode($params) . ']';
$result = $this->fetchBugData($url);
// Return array of valid bugs with metadata
return $result->result->bugs->$bugId->comments;
}
}
$bugFetcher = new BugzillaBugFetcher();
var_dump($bugFetcher->getBugData($bugFetcher->getOpenBugIds()));
?>
@ajdf24
Copy link

ajdf24 commented Aug 3, 2016

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment