Skip to content

Instantly share code, notes, and snippets.

@jungleeforce
Last active January 19, 2021 06:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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(jungleeforce@gmail.com)>
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();
}
@johnysv
Copy link

johnysv commented Mar 28, 2019

Below is my url and cred (see image). The config values I have changed. Should I change the path from "/qcbin/authentication-point/authenticate" to "/qcbin/start_a.jsp" ?

Also please let me know , what will be in the below line (defectFieldMapping), coz I m getting error here

var fields = config.defectFieldMapping.fieldArray.join(',');

image

@VishnuJin
Copy link

VishnuJin commented Dec 8, 2019

@jungleeforce
Bro, You almost saved my day !!
i have already done the same in Python with Requests..

Now i'm trying to do one for HP Performance center (Load Runner) in NodeJS and i got struct in setting the cookies(In Python requests lib i used to strip the LWSSO and QCSession from cookies and pass it as args to the request calls) i tried the same in Nodejs https and nothing seemed to work. Somehow i did not even think of trying to pass the received set-cookies without parsing them until i saw your post .
Thanks

@VishnuJin
Copy link

VishnuJin commented Dec 8, 2019

Hi @johnysv
(i dont know if you have figured it out already)
You should not change the to "/qcbin/start_a.jsp"

For authentication you should use - "/qcbin/authentication-point/authenticate"
similarly to choose domain and project you should use - "/qcbin/rest/domain/urDomainName/projects/urPrjName" (you should also suffix th e endpoint with what you are looking for line defects,tests ect)
in your case you should be using something like - "https://alm.rb.ipdesign.info/qcbin/rest/domain/THREEUK_REBUS/project/Three_REBUS_Program/defects"

Hope it helps !!

@sayantan2007
Copy link

sayantan2007 commented May 5, 2020

I am trying this in angular 8. Always print from line no 48. The reason behind that, my url is blocked by CORD policy. No Access-control-Allow-Origin header is present. Please suggest

@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