<%@LANGUAGE="VBSCRIPT"%> <% '===================================================== ' Build System Status Enquiry ' _json_buildStatus.asp ' Designed to be called via AJAX and to return ' JSON formatted data to dynamic page '===================================================== %> <% Option explicit ' Good idea to set when using redirect Response.Expires = 0 ' always load the page, dont store %> <% '------------ Variable Definition ------------- Dim parOpr Dim parPv_id Dim result Dim iJSON,jJSON, el, this, post, newitem Dim allIssues ' Basic Parameters parOpr = QStrPar("Opr") parPv_id = QStrPar("pv_id") result = -1 post = "None" ' Init the output JSON class ' Operations can add data ' Default data will be added at the end Dim oJSON Set oJSON = New aspJSON ' ' getIssues - Get issues for the current PV_ID and create a hash ' Sub getIssues Set allIssues = CreateObject ("Scripting.Dictionary") Dim rsTemp, sqlstr, key sqlstr = "SELECT iss_key FROM JIRA_ISSUES WHERE pv_id="& parPv_id Set rsTemp = OraDatabase.DbCreateDynaset( sqlstr, cint(0)) While ((NOT rsTemp.BOF) AND (NOT rsTemp.EOF)) key = rsTemp("iss_key") If NOT allIssues.Exists(key) Then allIssues.add key, 1 End If rsTemp.MoveNext WEnd rsTemp.Close() Set rsTemp = nothing End Sub ' ' Determine the operation to be performed ' If (parOpr = "getAllKeys") Then ' Examine the Jira subsystem and determine ALL available project keys ' Return an HTML string that is a box ' This is designed to be inserted into an element ' result = 0 ' Dim objXML, qry qry = "/rest/api/2/project" Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0") objXML.Open "GET", Application("JIRA_URL") & qry, false objXML.setRequestHeader "Authorization", "Basic " & Base64Encode(Application("JIRA_CREDENTIALS")) On Error Resume Next objXML.Send("") if Err.Number <> 0 then result = 1 oJSON.data("emsgSummary") = Err.Description On Error Goto 0 else On Error Goto 0 If objXML.status = 403 then result = 1 oJSON.data("emsgSummary") = "Jira Server Access Denied" ElseIf objXML.status <> 200 then result = 1 oJSON.data("emsgSummary") = objXML.responseText Else Set jJSON = New aspJSON jJSON.loadJSON( objXML.responseText ) oJSON.data("keyListCount") = jJSON.data.Count Dim str str = "" end if end if oJSON.data("keyList") = str ' ' getIssues ElseIf (parOpr = "getIssues") Then Dim project, projectList, issues, issueList, query, order, parCount, parStartAt getIssues() project = QStrPar("Project") issues = QStrPar("Issue") parStartAt = QStrPar("StartAt") parCount = QStrPar("Count") qry = "/rest/api/2/search" order = " ORDER BY key DESC" projectList = "" If project = "ALL" Then projectList = "" Else projectList = "project=" & project End If ' Convert comma or space seperated list into and 'IN' query issueList = "" Dim aList, entry, del If issues <> "ALL" AND issues <> "" AND project <> "ALL" Then issueList = issueList & " AND key in (" del = "" issues = Replace(issues ," ",",",1,-1) aList = Split(issues, ",") For Each entry in aList If entry <> "" Then issueList = issueList & del & project & "-" & entry del = "," End If Next issueList = issueList & ")" End If ' Create the JSON input via code ' Can do it as a string, as thats all we want set iJSON = New aspJSON With iJSON.data .Add "jql", projectList & issueList & order .Add "validateQuery", "false" .Add "startAt", CStr(parStartAt) .Add "maxResults", CStr(parCount) .Add "fields", iJSON.Collection() With .item("fields") .Add 0, "key" .Add 1, "summary" .Add 2, "status" .Add 3, "priority" .Add 4, "issuetype" End With End With post = iJSON.JSONoutput() Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0") objXML.Open "POST", Application("JIRA_URL") & qry , false objXML.setRequestHeader "Authorization", "Basic " & Base64Encode(Application("JIRA_CREDENTIALS")) objXML.setRequestHeader "Content-Type", "application/json" On Error Resume Next objXML.Send(post) if Err.Number <> 0 then result = 1 oJSON.data("emsgSummary") = Err.Description On Error Goto 0 else On Error Goto 0 If objXML.status = 400 then result = 1 Set jJSON = New aspJSON jJSON.loadJSON( objXML.responseText ) oJSON.data("emsgSummary") = jJSON.data("errorMessages").item(0) ElseIf objXML.status = 403 then result = 1 oJSON.data("emsgSummary") = "Jira Server Access Denied" ElseIf objXML.status <> 200 then result = 1 oJSON.data("emsgSummary") = objXML.responseText Else Set jJSON = New aspJSON jJSON.loadJSON( objXML.responseText ) ' Response.Write "
Pretty JSON display
"
'           Response.Write jJSON.JSONoutput()
'           Response.Write "
" ' oJSON.data("issueCount") = jJSON.data("total") oJSON.data("startAt") = jJSON.data("startAt") oJSON.data("maxResults") = jJSON.data("maxResults") Set oJSON.data("issues") = oJSON.Collection() ' Select the fields to be returned Dim fields 'On Error Resume Next For Each el In jJSON.data("issues") Set this = jJSON.data("issues").item(el) Set fields = this.item("fields") Set newitem = oJSON.AddToCollection(oJSON.data("issues")) newitem.add "key",this.item("key") newitem.add "url",Application("JIRA_URL") & "/browse/" & this.item("key") newitem.add "statusIcon",fields.item("status").item("iconUrl") newitem.add "status",fields.item("status").item("name") newitem.add "issuetypeIcon",fields.item("issuetype").item("iconUrl") newitem.add "issuetype",fields.item("issuetype").item("name") newitem.add "priorityIcon",fields.item("priority").item("iconUrl") newitem.add "priority",fields.item("priority").item("name") newitem.add "summary",fields.item("summary") If allIssues.Exists(this.item("key")) Then newitem.add "inuse",1 End If Next result = 0 end if end if ' ' insertIssues ' Insert Issues into the Release Manager entry ' ElseIf (parOpr = "insertIssues") Then issues = QStrPar("Issue") If parPv_id = "" OR issues = "" Then result = 1 oJSON.data("emsgSummary") = "Action called with invalid arguments." & parPv_id & issues Else Dim SqlQry, issArr , iss_id, rsQry issArr = Split ( issues, ",") objEH.ErrorRedirect = FALSE objEH.TryORA ( OraSession ) On Error Resume Next ' Import issues if they do not already exist For Each iss_id In issArr If Err.Number = 0 Then SqlQry = "SELECT * FROM JIRA_ISSUES WHERE pv_id=" & parPv_id & " AND iss_key='"& LTrim(iss_id) & "'" Set rsQry = OraDatabase.DbCreateDynaset( SqlQry, ORADYN_DEFAULT ) OraDatabase.ExecuteSQL SqlQry If ((rsQry.BOF) OR (rsQry.EOF)) Then SqlQry = " INSERT INTO JIRA_ISSUES ( pv_id, iss_key, date_time_stamp )" &_ " VALUES ( " & parPv_id & ", '"& LTrim(iss_id) &"', "& ORA_SYSDATETIME &")" OraDatabase.ExecuteSQL SqlQry If Err.Number = 0 Then SqlQry = "BEGIN Log_Action ( "& parPv_id &", 'jira_issue_added', "& objAccessControl.UserId &", 'Issue number: "& LTrim(iss_id) &"' ); END;" OraDatabase.ExecuteSQL SqlQry End If Set rsQry = nothing End If End If Next ' SQL error detection and reporting objEH.CatchORA ( OraSession ) If objEH.LastOraFailed Then result = 2 oJSON.data("error") = 1 oJSON.data("emsgSummary") = objEH.MessageSummary oJSON.data("emsgDetails") = objEH.MessageDetails oJSON.data("SqlQry") = SqlQry Else result = 0 End If End If Else oJSON.data("emsgSummary") = "Unknown operation requested:" & parOpr End If ' ' Create JSON data for the user ' Important fields ' result ' ' Debug fields ' Request (Array) ' 'Write single value oJSON.data("result") = result ' DEBUG: An array of the user provided requests oJSON.data("JiraPost") = post Set oJSON.data("Request") = oJSON.Collection() Set newitem = oJSON.AddToCollection(oJSON.data("Request")) Dim variableName for each variableName in Request.QueryString newitem.add variableName, Request.QueryString(variableName) next 'Return the object Response.Write oJSON.JSONoutput() Set oJSON = Nothing Call Destroy_All_Objects %>