Skip to content

Instantly share code, notes, and snippets.

@greenido
Last active March 4, 2020 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenido/fe789101ebba231a6d565a2b81c36744 to your computer and use it in GitHub Desktop.
Save greenido/fe789101ebba231a6d565a2b81c36744 to your computer and use it in GitHub Desktop.
Fetch more details per violation from JFrog Xray REST API
//
// Fetch more details per violation
// If you wish to merge it - the issue_id is the primary key
//
// @author: Ido Green | @greenido
// @date: Feb 2020
//
// @see:
// https://greenido.wordpress.com/2019/12/04/how-to-build-an-integration-with-jfrog-xray/
// https://greenido.wordpress.com/2020/02/12/continuous-software-updates/
//
// init project
const fs = require("fs");
var rp = require('request-promise');
require("dotenv").config();
// TODO: change these values in your .env file to meet your credentials
const BASE_URL = process.env.BASE_URL;
const USERNAME = process.env.XRAY_USER;
const PASSWORD = process.env.XRAY_PASS;
const VIOLATION_URLS = process.env.VIOLATION_URLS;
// Use to have uniqe file names for a certain run
let curTime = new Date().getTime();
// The details we collecting per violation url
let outputStr = "Issue Id, Description, Summary, Properties \n";
//
// Read all the urls and fetch for each one the details we wish to gain.
//
async function fetchDetailsPerViolation() {
let auth = 'Basic ' + Buffer.from(USERNAME + ':' + PASSWORD).toString('base64');
let urls = fs.readFileSync(VIOLATION_URLS).toString().split("\n");
for(i in urls) {
console.log(urls[i]);
var options = {
'method': 'GET',
'url': urls[i],
headers: {
"Content-Type": "application/json",
Authorization: auth
}
};
await rp(options).then(function (response) {
// An example for the full response can be found here: https://gist.github.com/greenido/c25c7187fc44df0eabe6cf9b3f4792ae
let retJson = JSON.parse(response);
// Let's build our output CSV string
outputStr += '"' + retJson.issue_id + '", "' + retJson.description + '", "' +
retJson.summary + '", "' + JSON.stringify(retJson.properties) + '" \n';
})
.catch(function (error) {
console.log("😳 Error with rp call: " + JSON.stringify(options) );
})
}
}
//
// save to CSV file
//
function saveCSVfile(csvData) {
let fileName = "./xray-details-violations-" + curTime + ".csv";
fs.writeFile(fileName, csvData, err => {
if (err) {
console.log("😳 Error writing: " + fileName , err);
} else {
console.log("🥎 Successfully wrote: " + fileName);
}
});
}
//
// Start the work
//
(async () => {
await fetchDetailsPerViolation();
saveCSVfile(outputStr);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment