Last active
December 11, 2015 11:56
-
-
Save jezhalford/3370f539858952e77e43 to your computer and use it in GitHub Desktop.
Here's an easy to use tool that downloads issues from SonarQube and outputs them in CSV format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/* | |
* Downloads open issues from SonarQube and outputs them as a CSV file | |
*/ | |
var request = require("request"); | |
var json2csv = require("json2csv"); | |
var util = require("util"); | |
var current_page = 1; | |
var pages = Number.MAX_VALUE; | |
var handle_response = function(error, response, data) { | |
if (error || response.statusCode !== 200) { | |
throw error || response.statusCode; | |
} | |
json2csv({ data: data.issues, hasCSVColumnTitle: false }, function(err, csv) { | |
if (err) { | |
throw err; | |
} | |
console.log(csv); | |
}); | |
if(pages > current_page) { | |
current_page++; | |
request_issues(current_page); | |
} | |
}; | |
var handle_first_page_response = function(error, response, data) { | |
if(error || response.statusCode !== 200) { | |
throw error || response.statusCode; | |
} | |
pages = data.paging.pages; | |
var fields = Object.keys(data.issues[0]); | |
json2csv({ data: data.issues, fields: fields }, function(err, csv) { | |
if (err) { | |
throw err; | |
} | |
console.log(csv); | |
}); | |
if(pages > current_page) { | |
current_page++; | |
request_issues(current_page); | |
} | |
}; | |
var request_issues = function(page) { | |
var url = util.format( | |
"%s/api/issues/search?componentRoots=%s&resolved=false&pageSize=-1&pageIndex=%d", | |
sonar, | |
projects, | |
page | |
); | |
var handler = (page === 1 ? handle_first_page_response : handle_response); | |
request({ | |
url: url, | |
json: true | |
}, handler); | |
}; | |
if (process.argv.length < 4) { | |
console.log('Usage: sonar-csv sonar-url project-key-1 project-key-2 ...'); | |
console.log(' eg: sonar-csv http://sonar.example.com abc-my-project'); | |
process.exit(1); | |
} | |
var sonar = process.argv[2]; | |
var projects = process.argv.slice(3).join(','); | |
request_issues(current_page); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/* | |
* Downloads open issues from SonarQube and outputs them as a CSV file | |
*/ | |
if (count($argv) < 3) { | |
echo 'Usage: sonar-csv sonar-url project-key-1 project-key-2 ...' . PHP_EOL; | |
echo ' e.g. http://sonarqube.example.com my-project' . PHP_EOL . PHP_EOL; | |
exit; | |
} | |
array_shift($argv); | |
$sonarUrl = array_shift($argv); | |
$repos = implode(',', $argv); | |
$url = $sonarUrl . '/api/issues/search?componentRoots=' . $repos . | |
'&resolved=false&pageSize=-1&pageIndex=%d'; | |
$pages = PHP_INT_MAX; | |
$currentPage = 1; | |
$haveOutputtedHeaders = false; | |
$out = fopen('php://output', 'w'); | |
while($currentPage <= $pages) { | |
$raw = file_get_contents(sprintf($url, $currentPage)); | |
$data = json_decode($raw, true); | |
$pages = $data['paging']['pages']; | |
foreach ($data['issues'] as $issue) { | |
if(!$haveOutputtedHeaders) { | |
fputcsv($out, array_keys($issue)); | |
$haveOutputtedHeaders = true; | |
} | |
fputcsv($out, array_values($issue)); | |
} | |
$currentPage++; | |
} | |
fclose($out); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment