Today’s corporations use different software tools for different functions like inventory, payroll, utility, and so on. Server scripts allow organizations to fetch data from these third-party software or websites, and list the same in cards.
Business Scenario
Zylker uses Jira to track issues in their products. Sometimes, Zylker’s partners or clients might create a card in Qntrl to file a bug. To collate all issues in one place, Zylker wants to create an issue in Jira every time a card related to a bug is created in Qntrl.
Solution
We can create a specific orchestration to record issues and trigger a server script each time a new card is created in it.
-
Create a new orchestration to record issues in product and save the issue link from Jira.
-
Create a server script to file issues in Jira when cards are created in Qntrl's orchestration.
-
Create a card and fill in the issue details to test the server script.
Sample Configuration
Step 1: Create an orchestration
Create a new
orchestration titled 'Raise Issue' and add related custom fields to it.
-
Add a link field--Issue Link--to record the issue link from Jira.
Once the form is saved, proceed to design the blueprint, set permissions, and publish the orchestration.
Step 2: Code server scripts
Now, proceed to code the server script to send details to Jira.
-
Create a new server script and enter the name as 'File issue in Jira'.
-
Choose
Raise Issue
in the
Form
dropdown.
-
Choose
Job
as the
Execution Location
to execute the script.
-
Choose
After Create
as the
Execution Trigger
to execute this script whenever the card page loads.
-
Copy and paste the below script into your script editor and replace the parameter names.
-
You can use the ? icon at the top-right corner of the script editor to refer to parameter names.
-
Once the script is ready,
Publish
it.
-
function afterCreate(){
-
var globalVars = ZDK.Vars.getGlobalVariables();
-
var job = current.Job;
-
-
var jiraAuthBase64 = ZDK.Crypto.base64Encode(globalVars["JiraEmail"] + ":" + globalVars["JiraApiKey"]);
-
var payload = getJiraPayload(job.getTitle(), job.getDescription(), globalVars["JiraProjectKey"]);
-
-
var url = "https://" + globalVars["JiraDomain"] + "/rest/api/3/issue";
-
var headers = {
-
'Authorization': "Basic "+jiraAuthBase64,
-
'Accept': 'application/json',
-
'Content-Type': 'application/json'
-
}
-
var resp = ZDK.HTTP.request(
-
url,
-
"post",
-
headers,
-
"",
-
payload,
-
""
-
);
-
if (typeof resp == "string") {
-
resp = JSON.parse(resp);
-
}
-
console.log("Res = " + JSON.stringify(resp));
-
if (resp && resp.body) {
-
if (resp.status == 400) {
-
console.log("Jira issue creation failed, "+JSON.stringify(resp));
-
} else {
-
if (typeof resp.body == "string") {
-
resp.body = JSON.parse(resp.body);
-
}
-
var id = resp.body.id;
-
var key = resp.body.key;
-
var link = "https://" + globalVars["JiraDomain"] + "/browse/" + key;
-
job.setVariable("JiraId", id);
-
job.setVariable("JiraKey", key);
-
//Select issue link parameter here
-
job.setValue(current.Layout.Fields.<select issue link parameter here>.id, link);
-
console.log("Jira issue created, id = " + id + ", key = " + key);
-
}
-
} else {
-
}
-
-
}
-
-
-
function getJiraPayload(title, desc, project) {
-
if (!title) title = "";
-
if (!desc) desc = "";
-
var payload = {
-
"fields": {
-
"summary": title,
-
"issuetype": {
-
"id": "10004"
-
},
-
"project": {
-
"key": project
-
},
-
"description": {
-
"type": "doc",
-
"version": 1,
-
"content": [
-
{
-
"type": "paragraph",
-
"content": [
-
{
-
"text": desc,
-
"type": "text"
-
}
-
]
-
}
-
]
-
}
-
}
-
};
-
return JSON.stringify(payload);
-
}
Step 3: Add card
When the orchestration and server script are ready, we can test the script by creating a new
card.
-
Navigate to
Cards
and click
Add Card.
-
Select
Raise Issue
from the Form dropdown.
-
Enter the bug details.
-
Save
the card.
Check if an issue with the same details has been created in your Jira account.