Intergrating ITSM and CSM with Azure DevOps Series: Create a Work Item in Azure DevOps

Nosey
Tera Contributor

Hello!

We were recently asked by a small DevOps team if we could integrate ServiceNow into their tools.

 

As part of this they wanted the ability to integrate ServiceNow ITSM and CSM with Azure DevOps so that issues and faults could be managed by the DevOps teams in their tool without having to log into ServiceNow.

 

ServiceNow an OOTB integration but with PPM which was not a good fit for us because we didn’t want or need to spin up PPM just to create a work item in Azure DevOps.

 

In a series of blogs, I am going to talk about how I built this integration and the challenges I faced

 

Creating a Workitem in Azure DevOps is super simple.

 

Azure has some great docs explaining the API’s Available

 

To send a request to Azure DevOps you will need to following information about the Azure DevOps project you want to communicate with

Organisation and project name can be see in the Azure DevOps URL

Nosey_0-1714546368344.png

 

 

Once you have all the required information creating a card in Azure DevOps driven by a business rule.

Spoiler
NOTE: Flow Designer does not work (or at least I could not get it to :D) because Azure DevOps authentication relies on a password but no username.

 

An example of code we can use to create a workitem type of bug can be seen below.

Azure DevOps authenticaiton requires that we send just the password. We achive this below by base64 encoding a colon followed by the PAT token we got from Azure DevOps. In the below code I have shown hard coding the password in the a script but in a real worl example we store the password in an encoded formate using a system property with the type of password

 

In the below code rather than creating a Rest Message we instantiate a RestMessageV2 class and set all the properties manually for two reasons.
1) we cant store the password in the sys_user table passwords must have a user name
2) we use this intergraiton now for a number of different Azure DevOps boards being able to pass in orgnames, project names and tokens keeps the code resuable

 

 

 

var workItemBody = [{
    "op": "add",
    "path": "/fields/System.Title",
    "from": null,
    "value": "Short Description from SN task"

  },
  {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.Common.Priority",
    "from": null,
    "value": "3"
  },
  {
    "op": "add",
    "path": "/fields/System.History",
    "from": null,
    "value": "A work note"
  }
]

try {
  var token = ":" + "{{TOKEN FROM ADO}}"
  var orgName = "orgName"
  var projectName = "projectName"
  var encodedToken = gs.base64Encode(token);
  var code;
  var returnBody;

  gs.info(encodedToken)



  var url = "https://dev.azure.com/" + orgName + "/" + projectName + "/_apis/wit/workitems/$bug?api-version=7.0"
  r = new sn_ws.RESTMessageV2();
  r.setHttpMethod('post');
  r.setEndpoint(url);
  r.setRequestBody(JSON.stringify(workItemBody));
  r.setRequestHeader("Content-Type", "application/json-patch+json");
  r.setRequestHeader("Authorization", "Basic " + encodedToken);
  var response = r.execute()
  returnBody = response.getBody();
  code = response.getStatusCode();

} catch (error) {
  gs.error("ADO v2_Util_Rest_Message - insertWorkItem " + error);
  gs.error("ADO v2_Util_Rest_Message - insertWorkItem " + code);
}

 

 

 

Next blog I will talk about pulling updates from Azure DevOps and keeping ADO and ServiceNow in sync


 

 

 

 

 

5 REPLIES 5

Mark Manders
Mega Patron

Maybe also post it as blog, not as question? That makes it easier to find when someone is looking for such a solution (I wouldn't look into questions, because I wouldn't expect it there).


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Rebecca_J
Tera Contributor

Hi,Where to find your next blog on this integration?Thanks.

By clicking on someone's name, you can see all of their contributions to the community. The next blog hasn't been written/published yet.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Nosey
Tera Contributor

Hello,

I was advised not to post blogs here never wrote anymore - I will get this content in a more appropriate forum and post the link here