I am trying to create a ticket by using VBA, would like to get some tips from experts here. Thanks
Hi Li,
You can check this thread - VBA code to get issue from JIRA using REST API
Pavel
Pavel Junek:
Thank you for your response. I did read this thread and tried to follow the code. However, it is for GET mothod while I am lookiing for a POST method to create a new issue on Jira. I worte a sub and got Response status: 403 error. It seems there is a permission iissue.
Below is the code. Please take a look and provide me with your advce.
Thanks
Li Chai
%%%%%%%%%%%
Option Explicit
Sub CreateJiraTicket()' Define the Jira REST API URL and authentication detailsConst JiraUrl As String = "https://engsupport.juniper.net/rest/api/2/issue"Const JiraUser As String = "glouser"Const JiraPass As String = "glouser"
' Print the values of JiraUser and JiraPass for debuggingDebug.Print "JiraUser: " & JiraUserDebug.Print "JiraPass: " & JiraPass
' Define the Jira ticket dataDim IssueType As StringDim Summary As StringDim Description As StringDim Campus As StringDim Lab As StringDim Watcher As String
' Set the Jira ticket dataIssueType = "GLO Lab Support"Summary = "This is a test ticket please ignore."Description = "This is a test ticket please ignore."Campus = "Sunnyvale Building B"Lab = "San Francisco Lab (B.5.210)"Watcher = "glouser"
' Create an XMLHTTP objectDim http As ObjectSet http = CreateObject("MSXML2.XMLHTTP")
' Prepare the Jira REST API requestDebug.Print "Preparing request..."http.Open "POST", JiraUrl, Falsehttp.setRequestHeader "Content-Type", "application/json"http.setRequestHeader "Authorization", "Basic " & Base64Encode(JiraUser & ":" & JiraPass)
' Construct the Jira ticket JSON payloadDim payload As Stringpayload = "{""fields"": {""project"": {""key"": ""GLO""}, ""summary"": """ & Summary & """, ""watcher"": """ & Watcher & """, ""description"": """ & Description & """, ""issuetype"": {""name"": """ & IssueType & """}, ""customfield_10371"": {""value"": """ & Campus & """, ""child"": {""value"": """ & Lab & """}}}}"''' payload = "{""fields"": {""project"": {""key"": ""Project""}}}"
' Send the Jira REST API requesthttp.Send payloadDebug.Print "Sending request..."Debug.Print ("This is the payload: " & payload)Debug.Print ("This is the URL: " & JiraUrl)
' Check the response statusDebug.Print "Response status: " & http.StatusIf http.Status = 201 Then' Get the ticket key from the responseDim JSON As ObjectSet JSON = JsonConverter.ParseJson(http.responseText)Dim ticketKey As StringticketKey = JSON("key")
' Display the Jira ticket keyMsgBox "Jira ticket " & ticketKey & " created."''' Else''' ' Display an error message''' MsgBox "Failed to create Jira ticket. Response status: " & http.StatusEnd IfEnd Sub
Function Base64Encode(ByVal sText As String) As StringDim arrData() As BytearrData = StrConv(sText, vbFromUnicode)
Dim objXML As ObjectSet objXML = CreateObject("MSXML2.DOMDocument")
Dim objNode As ObjectSet objNode = objXML.createElement("b64")objNode.DataType = "bin.base64"objNode.nodeTypedValue = arrDataBase64Encode = objNode.Text
Set objNode = NothingSet objXML = NothingEnd Function
Unfortunately, I don't know how to program in VBA, so I can't advise you.
Pavel:
Thank you for your resposne. Don't worry. I have figured out and fixed the bug in the code.
Best regards,
Hi Li Chai,
What was the bug in the code? Was it a permission issue? I've used your code as a base but I get a 500 Internal Server error, and the JSON payload looks fine.
Thanks,
Ben
Ben:
Thank you for asking. This issue has been resolved. It turned out the problem was comes from Jira server configuration.
It looks like you're new here. Sign in or register to get started.