Programmer's Note

コード読み書きの備忘録。

JIRAの内容を取ってくるVBA

GPTさん作。

Option Explicit

Sub ExampleUsage()
    Dim issueKey As String
    Dim fieldName As String
    Dim fieldValue As Variant
    Dim strUsername As String
    Dim strApiToken As String
    Dim strURL As String

    ' Replace with your JIRA credentials and URL
    strUsername = "YOUR_USERNAME"
    strApiToken = "YOUR_API_TOKEN"
    strURL = "YOUR_JIRA_URL"

    issueKey = "YOUR_ISSUE_KEY"
    fieldName = "summary"

    fieldValue = GetJiraIssueData(issueKey, fieldName, strUsername, strApiToken, strURL)

    ' Write the result to Excel cells
    Cells(1, 1).Value = fieldName
    Cells(2, 1).Value = fieldValue
End Sub

Function GetJiraIssueData(issueKey As String, fieldName As String, strUsername As String, strApiToken As String, strURL As String) As Variant
    Dim objHTTP As Object
    Dim Json As Object

    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    
    ' Build the URL to access the JIRA REST API
    strURL = strURL & "/rest/api/2/issue/" & issueKey
    
    With objHTTP
        .Open "GET", strURL, False
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "Authorization", "Basic " & EncodeBase64(strUsername & ":" & strApiToken)
        .send
        Set Json = ParseJson(.responseText)
    End With

    ' Get the desired field from the JSON response and return its value
    If fieldName = "summary" Then
        GetJiraIssueData = Json("fields")("summary")
    ElseIf fieldName = "status" Then
        GetJiraIssueData = Json("fields")("status")("name")
    ElseIf fieldName = "assignee" Then
        GetJiraIssueData = Json("fields")("assignee")("displayName")
    End If
End Function


Function EncodeBase64(text As String) As String
    Dim arrData() As Byte
    arrData = StrConv(text, vbFromUnicode)

    Dim objXML As MSXML2.DOMDocument60
    Dim objNode As MSXML2.IXMLDOMElement
    
    Set objXML = New MSXML2.DOMDocument60
    Set objNode = objXML
    Set objNode = objXML.createElement("b64")

    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = arrData
    EncodeBase64 = objNode.Text
    
    Set objNode = Nothing
    Set objXML = Nothing
End Function