Highlighted
Super Contributor.. Super Contributor..
Super Contributor..
109 views

Java script with REST request to external system (Jira)

Hi,

Sending system: SM
Receiving system: Atlassian Jira

I am building a REST request from SM to the external system Jira. The goal is to create a record in Jira.
I have managed to create the record using curl in a batch file:

Batch file contents:
curl -u "username:pass" -X "POST" https://jira.ourdomain.com/rest/api/latest/issue/ -H "Content-Type:application/json" -d @Json.txt

I can run the batch file from the command line. It will open a session to the remote Jira server and open a new Jira record. The -d @Json.txt will read the json.txt file and push that request to the Jira server

json.txt contents:

{

    "fields":

    {

        "project":

        {

            "key": "Project 344"

        },

        "summary": "LB dosent write to Event Log",

        "description": "Logging is not configured for the LB server",

        "issuetype":

        {

            "name": "Bug"

        }

    }

}

 

Now the question: have anyone done something similar but using java script in SM instead?
(I know this is build in when using SMAX but we are using SMA-SM)

Labels (4)
0 Likes
1 Reply
Trusted Contributor.. Trusted Contributor..
Trusted Contributor..

Re: Java script with REST request to external system (Jira)

It's somewhat complex to implement, but once you've done it once it's not that difficult.

You have to create the Json object in JavaScript like this:

var crearJira = {
"fields": {
"project": {
"key": "Project 344"
},
"summary": probsummaryFile.brief_description,
"description": lib.stanumUtils.array2text(probsummaryFile.action),

[...]

};

You then convert it into a format that doHTTPrequest can use:

var request = system.library.JSON2.toJSON(crearJira);

Then you call the rest Service:

try
{
var res = doHTTPRequest("POST", URL, arrHeaders, request, 15, 15, 15);
JSNres = system.library.JSON3.parse(res);
print("[Jira] Jira id: " + JSNres.id + " key: " + JSNres.key);

[...]

catch(error)
{

}

 

The arrHeaders object can be a bit tricky to construct, I wrote a function for that (you'll need to write your own getBase64Encode function) that accepts user and password to do basic authentication:

 

function getRequestHeaderAuth(User, Password) {

var authHeader = new Header();
authHeader.name = "Authorization";
authHeader.value = "Basic " + system.library.Utilities.getBase64Encode(User + ":" + Password);

var typeHeader = new Header();
typeHeader.name = "Content-Type";
typeHeader.value = "application/json; charset=utf-8";

var arrHeaders = new Array();
arrHeaders.push(authHeader);
arrHeaders.push(typeHeader);

return arrHeaders;
}

 

Regards

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.