Skip to content

Instantly share code, notes, and snippets.

@jungleeforce
Last active January 19, 2021 06:00
Show Gist options
  • Save jungleeforce/af83f36fec0aa9a102c6 to your computer and use it in GitHub Desktop.
Save jungleeforce/af83f36fec0aa9a102c6 to your computer and use it in GitHub Desktop.
Node.js + ALM + REST API = Awesome
/********
Copyright (c) <2015> <junglee Force([email protected])>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**********/
/**
check out the post here https://jungleeforce.wordpress.com/2015/01/21/node-js-alm-rest-api-awesome/
**/
var https = require('https'),
fs = require('fs'),
config = JSON.parse(fs.readFileSync('config.json'));//this refers to a file where I have all my config like host, userName, password Etc
//this is added to avoid the TLS error. Uncomment if you get a TLS error while authenticating.
//process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
//set the correct options for the call.
var options = {
host : config.host,
path : "/qcbin/authentication-point/authenticate",
method: "GET",
headers : {'Authorization': 'Basic '+new Buffer(config.alm_userName + ':' + config.alm_password).toString('base64')}
};
//authenticating the user into ALM
ALMConnect(options, 'header','', function(status, data){
if(status){
//get the LWSSO_Cookie from the header. This is the session cookie which will be used in all callouts to ALM.
if(data.headers["set-cookie"] != undefined ) {
extractDefects(data.headers["set-cookie"]);
}else{
console.log('Dagnabbit!! ERROR: Unable to login, check your username/password/serverURL.');
}
}else{
console.log('Dagnabbit!! ERROR: ' + JSON.stringify(data));
}
});
//Function to extract the defects for analysis.
function extractDefects(LWSSO_Cookie){
var queryParam = "{";
//add Release
queryParam += "detected-in-rel["+config.release+"];";
//add all your request parameters here. Its a little complicated initially, but you will get a hang of it.
// Make sure to use encodeURIComponents() for all the values in the query parameters.
queryParam+="}";
//get all the fields that you want to query. Lesser the fields smaller the XML returned, faster is the call.
var fields = config.defectFieldMapping.fieldArray.join(',');
var opt = {
host: config.host,
path: "/qcbin/rest/domains/"+config.domain+"/projects/"+config.project+"/defects?query="+queryParam+"&fields="+fields+"&page-size=max",
method:"GET",
headers: {"Cookie":LWSSO_Cookie}
};
ALMConnect(opt, 'data','',function(status,data){
if(status){
//write the defects to an XML file in local drive.
fs.writeFileSync('newDefect.xml',data);
//once you get the defectXML you can parse it into JSON and push it other databases like SFDC etc..
}else{
console.log('Dagnabbit!! ERROR: ' + JSON.stringify(data));
}
});
}
function ALMConnect(opt, responseType,requestBody, callback){
var request = https.request(opt, function(res){
res.setEncoding('utf8');
var XMLoutput='';
res.on('data',function(chunk){
XMLoutput+=chunk;
});
res.on('end',function(){
if(responseType=='data'){
callback(true,XMLoutput);
}else {
callback(true, res);
}
});
});
request.on('error',function(e){
callback(false,e);
});
if(opt.method=='POST' || opt.method == 'PUT'){
request.write(requestBody);
}
request.end();
}
@VishnuJin
Copy link

VishnuJin commented May 5, 2020

@sayantan2007
Hi bro,
Cross-origin resource sharing (CORS) means - the server(in our case ALM) expects your requests to come from another server/host and not from browser JavaScript like using FetchAPI or XMLHttpRequest. I tried something similar using Fetch in JavaScript and encountered similar error.
So i would suggest you to write your ALM rest calls in NodeJS, python, or any other server side programming languages or runtimes.
Hope it helps!!

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