使用 jira api 通过 vba 向现有问题添加标签时收到 'Operation aborted error' 即使标签已成功添加

Receiving 'Operation aborted error' when adding label to existing issue using jira api via vba even though label is successfully added

我正在尝试通过 VBA 使用 JIRA REST API 为 JIRA 中的现有问题添加标签。我可以成功登录、获取会话 id/cookie、找到问题并为问题添加评论,但是当我尝试添加标签时,我在 Excel[=27= 中收到以下错误消息]

然而,有趣的是标签实际上是添加的

宏之前:

宏后:

我无法解释或弄清楚发生了什么。我已经搜索了 Internet(尤其是 Atlassian 论坛),并且遇到了一些页面,其中的示例表明我正在正确发送此请求,但我仍然收到此 运行-time 错误。在很多事情中,有一些是我 read/do 试图弄清楚的

所以,据此我应该能够使用"add"动词来添加标签

所以,这就是我所在的位置。我的问题很简单:

如何添加标签而不每次都收到运行次错误?如果我的宏每次为一个问题添加一个标签就停止,这似乎有点毫无意义

以下是我正在使用的适当代码片段。

JIRA 模块:

Option Explicit

Sub JIRA()
    Dim ASNumber As String, Supplier As String, IssueLink As String
    Dim myJIRA As New clsJIRARest
    With myJIRA
        .UserName = "******"
        .Password = "******"
        .URL = "http://jira.company.local:8080"
        If .Login = False Then Exit Sub
        .GetLastSummary
        IssueLink = .GetIssueLink '<-works fine
        ASNumber = .GetASNumber '<-works fine
        Supplier = .GetSupplierName '<-works fine
        .PostExportComment '<-Throws run-time error 'Operation aborted'
        .Logout
    End With

End Sub

clsJIRARestClass模块的适当部分:

Public Function Login() As Boolean

    Login = False

    With JiraAuth
        .Open "POST", sURL & "/rest/auth/1/session", False
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "Accept", "application/json"

        .send " {""username"" : """ & sJIRAUserID & """, ""password"" : """ & sJIRAPass & """}"" '*** HTTP-Request senden"

        sErg = .responseText

        If .Status = "200" Then
            sCookie = "JSESSIONID=" & Mid(sErg, 42, 32) & "; Path=/" & sPfad
            Login = True
        End If

    End With

End Function

Public Function GetLastSummary()

    Dim myRegEx As Object: Set myRegEx = CreateObject("vbscript.regexp")
    myRegEx.Global = True
    myRegEx.Pattern = "as\/([0-9]{4,5}).+?(?=\-)" '<-Working
    With JiraService
        .Open "GET", sURL & "/rest/api/2/search?jql=assignee=mhill+order+by+lastViewed&now&maxResults=5", False
       .setRequestHeader "Content-Type", "application/json"
       .setRequestHeader "Accept", "application/json"
       .setRequestHeader "Set-Cookie", sCookie '*** see Create a "Cookie"

        .send
        Set sRestAntwort = myRegEx.Execute(.responseText)
        Sup = sRestAntwort(0)
        ANum = sRestAntwort(0).Submatches(0)

        myRegEx.Pattern = "self"":""(.+?(?=""))"
        myRegEx.Global = False
        Set sRestAntwort = myRegEx.Execute(.responseText)
        sIssueLink = sRestAntwort(0).Submatches(0)
    End With

End Function

'The comment posts fine below, but the label is what causes the error
Public Function PostExportComment() As Boolean
    With JiraService
        .Open "POST", sIssueLink & "/comment", False
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "Accept", "application/json"
        .setRequestHeader "Set-Cookie", sCookie '*** see Create a "Cookie"
        Dim sExportComment As String: sExportComment = "Full Export Requested - Awaiting Download Email"
        .send " {""body"" : """ & sExportComment & """}"" '*** HTTP-Request senden"
'
'        PostExportComment = IIf(.Status = "201", True, False)

        .Open "PUT", sIssueLink, False
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "Accept", "application/json"
        .setRequestHeader "Set-Cookie", sCookie '*** see Create a "Cookie"
        Dim sExportingLabel As String: sExportingLabel = "Exporting"
        '===============Errors on next line==================
        .send " { ""update"": { ""labels"": [{""add"": """ & sExportingLabel & """}] } }"" '*** HTTP-Request senden" '(See below photo for error message)
        Debug.Print .Status & "|" & .statusText
    End With
End Function

他们似乎为此提供了特定的错误代码。这很好,因为您可以忽略 那个 并仍然处理其他可能的错误 - 如果该错误编号不是 特定的 [=21] =] action,然后让自己成为一个局部变量来跟踪错误发生的位置:

    On Error GoTo ErrHandler

    '...

        currentStep = "PostExportComment"
        .PostExportComment '<-Throws run-time error 'Operation aborted'
        currentStep = "Logout"
        .Logout
    End With

CleanExit:
    'clean up here
    Exit Sub
ErrHandler:
    '"Operation aborted" error is a false positive, see https://jira.atlassian.com/browse/JRA-27929
    If currentStep = "PostExportComment" And Err.Number = -2147467260 Then
        Resume Next 
    Else
        'handle other possible runtime errors here
        Resume CleanExit
    End If

...或更好,将第 3 方 API 包装在 class 模块中,并在您自己的 PostExportComment:

包装器中巧妙地处理该错误
Private wrapped As My3rdPartyThing

Public Enum MeaningfulError
    ERR_FoobarNotInitialized = vbObjectError + 42
    ERR_WrongCredentials
    ERR_Whatever
End Enum

Private Sub Class_Initialize()
    Set wrapped = New My3rdPartyThing
End Sub

Private Sub Class_Terminate()
    Set wrapped = Nothing
End Sub

'wraps the "GetFoo" API method
Public Function GetFoo(ByVal bar As Long) As Something
    On Error GoTo ErrHandler

    Set GetFoo = wrapped.GetFoo(bar)

    Exit Function
ErrHandler:
    If Err.Number = 12345 Then 
        Err.Raise ERR_FoobarNotInitialized
    Else
        Err.Raise ERR_Whatever
    End If
End Sub