The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ServiceNow Jira Attachment Integration

ashik1
Kilo Guru

Hi All,

We all know John's poc work on ServiceNow-jira integration is one of the best references for integrating these tools - poc link

We do come across a requirement to manage file transfer as well. Many people have posted solutions for the same but none worked (at least for me it didn't work as expected) so I have tried to enhance the code of others from when they have left and below code works for me.

Requirements
Mid Server, Powershell installed in mid

Process Flow

  • Pick a file from SNow --> convert it to base64 --> dump the file in mid server in its original format
  • pick a file from mid server --> upload it to Jira --> delete uploaded file using Powershell

Code

/********************************************************************************************************************/
/********************************* Code to transfer file to mid-server ****************************************/
/********************************************************************************************************************/

Script Type: Mid Server Script Include
Name: JiraIntegration
Script:

var JiraAttachment = Class.create();
JiraAttachment.prototype = {
initialize: function () {
/* Set up the Packages references */
this.File = Packages.java.io.File;
this.FileOutputStream = Packages.java.io.FileOutputStream;
this.HttpClient = Packages.org.apache.commons.httpclient.HttpClient;
this.UsernamePasswordCredentials = Packages.org.apache.commons.httpclient.UsernamePasswordCredentials;
this.AuthScope = Packages.org.apache.commons.httpclient.auth.AuthScope;
this.GetMethod = Packages.org.apache.commons.httpclient.methods.GetMethod;

/* Set up the parameters */
this.verbose = probe.getParameter("verbose");
this.filepath = probe.getParameter("filepath");
this.filename = probe.getParameter("filename");
this.encodedData = probe.getParameter('encodedData');
this.debug("verbose -- filepath -- filename : "+this.verbose + ' -- ' + this.filepath + ' -- ' + this.filename);
},

/* Function to create new file and write data in target path*/
saveToFile: function(targetPath)
{
this.debug("Initiating file save function");
var f = new this.File(targetPath); // create new file
var inputStream = this.encodedData;
var fout = new this.FileOutputStream(f);
this.StringUtil = Packages.com.glide.util.StringUtil;
var data = this.StringUtil.base64DecodeAsBytes(inputStream); // convert base64 to original format
fout.write(data); // write data to newly created file
fout.close();
inputStream.close();
result = "File successfully created : "+this.filepath+this.filename;
this.debug(result);
},

/* Function to debug in mid-server log*/
debug: function (m)
{
if (this.verbose == "true") {
ms.log("Jira Attachment: " + m);
}
},

/* Execute the Probe*/
execute: function()
{
var saveRes = this.saveToFile(this.filepath+this.filename);
return result;
},
type : "JiraAttachment"
};

/********************************************************************************************************************/

/* Background Script to trigger above script include*/

Script:
var gr = GlideRecord('sys_attachment');
gr.addQuery('sys_id', <sysid_of_attachment>);
gr.query();
if (gr.next())
{
var sa = new GlideSysAttachment();
var binData = sa.getBytes(gr);
var encData =GlideStringUtil.base64Encode(binData);
var jspr = new JavascriptProbe(<midserver_name>);
jspr.setName(<Any_name_of_js_probe>); // This can be any name 
jspr.setJavascript("var ddr = new JiraAttachment(); res= ddr.execute();");
jspr.addParameter("verbose","true");
jspr.addParameter("filepath", <path_to_save_file>)); // eg, D://ServiceNow//JiraAttachment// This is optional, if not added, file will be created at rool i.e midserver's agent folder
jspr.addParameter("filename",gr.file_name);
jspr.addParameter("encodedData",encData);
jspr.create();
gs.print('Completedeck Mid Server log');
}

/********************************************************************************************************************/
/********************************** Code to upload file to jira ****************************************/
/********************************************************************************************************************/
Script Type: PowerShell script in mid-server
Name: JiraIntegration.ps1
Script: 

#
# (c) Ashik Narayan
#
# This script is related to "ServiceNow-Jira Integration" in Service Now
# This script attach file to Jira issue and delete fiel at source path (windows machine)

Param([string]$jira_base_url, [string]$jira_issue, [string]$file_path, [string]$file_name)

$jira_base_url = $null
$jira_issue = $null
$file_path = $null
$file_name = $null

if(test-path env:\SNC_jira_base_url) {
$jira_base_url=$env:SNC_jira_base_url
}

if(test-path env:\SNC_jira_issue) {
$jira_issue=$env:SNC_jira_issue
}

if(test-path env:\SNC_file_path) {
$file_path=$env:SNC_file_path
}

if(test-path env:\SNC_file_name) {
$file_name=$env:SNC_file_name
}

$wc = new-object System.Net.WebClient
#$wc.Headers.Add("Authorization", "Basic $([System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $<jira_user>, $<jira_password>))))")
#$wc.Headers.Add("Authorization", "<credentials_of_jira_account_in_basic>") //  after your testing, convert the credentials of jira account to basic authentication and replace with above line
$wc.Headers.Add("X-Atlassian-Token", "nocheck")
$wc.UploadFile($jira_base_url+"/rest/api/2/issue/"+$jira_issue+"/attachments", $file_path+$file_name)

Write-Host "Jira Attachment: Attachment Successful" $file_name

Remove-Item $file_path$file_name

Write-Host "Jira Attachment: File deleted successfully" $file_name

/********************************************************************************************************************/

/* Background Script to trigger above powershell script*/

Script: 
var payload = new GlideXMLDocument('parameters');
this.attachmentAddParametersToPayload(payload, 'skip_sensor', true);
this.attachmentAddParametersToPayload(payload, 'probe_name', 'Jira - MidServerToJira'); // this can be any name
this.attachmentAddParametersToPayload(payload, 'script.ps1', <path_of_powershell_script>)); // eg.D://ServiceNow//Script//JiraAttachment.ps1
this.attachmentAddParametersToPayload(payload, 'powershell_param_jira_base_url', <jira_instance_url>));
this.attachmentAddParametersToPayload(payload, 'powershell_param_jira_issue', <jira_issue>);
this.attachmentAddParametersToPayload(payload, 'powershell_param_file_path', <path_of_file_to_upload>); eg. D://ServiceNow//JiraAttachment//
this.attachmentAddParametersToPayload(payload, 'powershell_param_file_name', <name_of_file>); eg. abc.jpg
this.attachmentCreateECCEntry(payload.toString());

function attachmentAddParametersToPayload(payload, name, value) {
var element = payload.createElement('parameter');
element.setAttribute('name', name);
element.setAttribute('value', value);
},

function attachmentCreateECCEntry(payload) {
var eccgr = new GlideRecord('ecc_queue');
eccgr.initialize();
eccgr.agent = 'mid.server.'+<name_of_midserver>);
eccgr.topic = 'Powershell';
eccgr.name = 'Jira - MidServerToJira'; // this can be any name
eccgr.payload = payload;
eccgr.queue = 'output';
eccgr.state = 'ready';
this.outputq = eccgr.insert();
}

17 REPLIES 17

harishdasari
Tera Guru

Hi Ashik,

where should I copy and paste the "PowerShell script in mid-server".

could you please help, where I have to paste the powershell script.

Thank you.

 

Hello Harish,

Have you installed your mid-server in windows machine, if yes nothing specific paste anywhere in the  drive (C:/ or D:/), but make sure you use double slash (//) or just use bask slash (\)

arijitwarm32
Kilo Contributor

Hi Ashik ,

 

Can you kindly explain the below code from Powershell ? I am not able to understand what we need to do here :-

#$wc.Headers.Add("Authorization", "Basic $([System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $<jira_user>, $<jira_password>))))")

Do we need to put the $username , $password where username is the username and password is the password for the service account on JIRA ? Have you commented the Power shell script using # ( I have no idea on Powershell )


#$wc.Headers.Add("Authorization", "<credentials_of_jira_account_in_basic>") //  after your testing, convert the credentials of jira account to basic authentication and replace with above line

So what do we need to do here , I am sorry but we added authorization in the line above .. Kindly explain this one , I really need this very urgently .

Also I am not giving any path for the file , I mean hopefully it would get stored in the agent folder .. so in order to execute the powershell from ServiceNow what would be the file path ?

 

Hello Arijit,

#1

#$wc.Headers.Add("Authorization", "Basic $([System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $<jira_user>, $<jira_password>))))")

I added above line just to explain how authorization works, basically above line pickes up your jira service account user name and password in plain string format and converts to basic authentication.

But it's not good practice to store username and password in plain text format so I replaced above line with below one,

 $wc.Headers.Add("Authorization", "<credentials_of_jira_account_in_basic>") (I used postman to convert my plain credentials to basic authentication)

#2

Okay if you not providing path while dumping file into mid-server check if the file is created in agent folder. If file creation is successful, then just copy the path navigating till your agent folder (like, C:\\Program Files (x86)\\ServiceNow\\Mid-server\\agent\\)