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 .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 .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)

  • 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