Skip to end of banner
Go to start of banner

Running Scripts Via REST

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

We've encountered an issue exporting this macro. Please try exporting this page again later.

In certain cases, it is necessary to run SIL Scripts from an outside source. 

Background

In order to run SIL Scripts via REST, use the SIL Common REST Service.

Basic Steps

When creating scripts for use with REST, follow these steps:

  1. Create the script with hard-coded values and make sure it works the way you want it to.

  2. Replace the hard-coded values with variables that accept arguments (the argv variable) when run from an outside source.

  3. (optional) Test the script using the call() routine. This is an important step to test the script without the complications of a network.

  4. Make modifications to the example curl script to test running the script.

I will break down each step with an example script that uses the createIssue() routine.

Create a Script with Hard-Coded Values

Create a script and hard-code the values for easy implementation. Test that the script works as expected.

string newIssueKey = createIssue(
    "DEMO", 
    "", 
    "Task", 
    "Summary goes here"
);
 
if (isNotNull(newIssueKey)){
    printInFile("debug.txt", "Issue " + newIssueKey + " was created.");
} else {
    printInFile("debug.txt", "An issue was not created");
}

Replace the Hard-Coded Values with Variables

In the script below, note how the variables are configured in such a way as to accept arguments from the "argv" variable. We will use argv to pass variables when we run the script from an outside script or REST call.

string project = argv[0];
string parent = argv[1];
string issueType = argv[2];
string summary = argv[3];
 
string newIssueKey = createIssue(
    project, 
    parent, 
    issueType, 
    summary
);
 
if (isNotNull(newIssueKey)){
    printInFile("debug.txt", "Issue " + newIssueKey + " was created.");
} else {
    printInFile("debug.txt", "An issue was not created");
}

Test the Script Using the Call() Routine

In a separate script, run the script using the call() routine. Notice how the number of elements in the array matches the arguments used by argv in the above script. The arguments used should match all elements accessed in the script being called. 

string project = "DEMO";
string parent = "";
string issueType = "Task";
string summary = "summary goes here";
 
 
call("", "createIssue.sil", {project, parent, issueType, summary});

If everything goes well, you will see something like the following in the “debug.txt” folder under the silprograms folder (remember to click on the “Refresh” button to view changes after the script runs).

Issue DEMO-49 was created.

Test the Script Using Curl

Below is an example curl script with the parts to be replaced in all capital letters for readability. 

curl -v -u "<USERNAME:PASSWORD>" \
--request POST \
--url "http://<BASE_URL>/rest/keplerrominfo/refapp/latest/async-script/runScript" \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data '{ 
  "source": {
    "type": "FILE", "code": "<PATH/TO/FILE.SIL>"
  },
    "args": [<"ARGUMENTS", "FOR", "ARGV", "GO HERE">]
  }'


For an example of an working curl script, see the the script below:

curl -v -u "admin:admin" \
--request POST \
--url "http://localhost:8080/rest/keplerrominfo/refapp/latest/async-script/runScript" \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data '{ 
  "source": {
    "type": "FILE", "code": "createIssue.sil"
  },
    "args": ["EX", "", "Task", "summary goes here"]
  }'

If everything goes well after running the curl command, you will see something like the following in the “debug.txt” folder under the silprograms folder (again, remember to click on the “Refresh” button to view changes after the script runs).

Issue DEMO-49 was created.

Test the Script Using a SIL Script

Here is the same call as using curl, but using httpPost() instead.

string [] args = {"EX", "", "Task", "summary goes here"};
 
string rawData =    "{" +
                        "\"source\": {" +
                        "\"type\": \"FILE\", \"code\": \"Remote/createIssue.sil\"" +
                        "}," +
                        "\"args\": [\"" + replace(args, "|", "\",\"") + "\"]" +
                    "}";
                     
// runnerLog(rawData);
 
HttpRequest request;
request.headers += httpCreateHeader("Content-Type", "application/json");
request.headers += httpBasicAuthHeader("admin", "admin");
 
string jsonResponse = httpPost("http://localhost:8080/rest/keplerrominfo/refapp/latest/async-script/runScript", request, rawData);
 
number statusCode = httpGetStatusCode();
if (statusCode >= 200 && statusCode < 300) {
    runnerLog("SUCCESS");
}
else {
    string msg = trim(statusCode) + ":" + httpGetErrorMessage() + ":" + httpGetReasonPhrase();
    runnerLog(msg);
}
 
runnerLog(jsonResponse);

Additional Help

Need help implementing this script? Talk to me directly to me by clicking on the bot on this page.

Filter by label

There are no items with the selected labels at this time.

  • No labels