2014-11-21 158 views
0

在jira rest api中,我們正在使用轉換API。 通過在/ rest/api/2/issue/{issueIdOrKey}上發佈/轉換具有轉換ID和註釋以及其他字段的url,我們可以發佈評論和其他字段進行狀態轉換。Jira rest api,添加轉換附件

{ 
    "fields" : {"summary": "Test update 5"}, 
    "transition": { "id": "4"}, 
"update": { 
     "comment": [ 
     { 
      "add": { 
       "body": "It is time to finish this task" 
      } 
     } 
     ] 
    } 

} 

最近我們才知道jira也有附件驗證。意味着我需要添加附件,如果我做過渡。我們正在尋找如何使用rest api在轉換過程中添加附件。

任何幫助將非常感激。

在此先感謝。

回答

1

不確定在轉換過程中它是否可以正常工作,但以下是如何添加附件。

https://docs.atlassian.com/jira/REST/latest/#d2e88

我不得不作出2個電話 - 首先創建問題,然後又POST調用與截圖更新它,因爲沒有辦法在建立呼叫添加附件都沒有。

0

我不確定是否有過渡期間添加附件(我從來沒有這樣做過),但我認爲可能會杵套。以下是將附件添加到JIRA的代碼,您可以使用JRJC的Transition API並在下面的代碼中應用/添加一些邏輯來完成工作。

關注鏈接[Listing All JIRA Transitions via API 基於轉換更新狀態,希望你也可以從這裏得到一些東西。

public void addAttachment(IssueRestClient issueRestClient, 
     VersionOne versionOne, Issue issue, Epics epic) { 

    try { 

     URI attachmentsUri = new URI(
       applicationProperties.get(Constants.JIRAURL) 
         + "/rest/api/2/issue/" + issue.getKey() 
         + "/attachments"); 
     Iterable<Attachment> attachments = issue.getAttachments(); 
     Set<String> existingAttachments = new TreeSet<String>(); 
     String _jiraUser = applicationProperties.get(Constants.JIRAUSER); 
     String _jiraPwd = applicationProperties.get(Constants.JIRAPWD); 
     String auth = new String(Base64.encode(_jiraUser + ":" + _jiraPwd)); 
     Set<String> files = new TreeSet<String>(); 

     for (Attachment attachment : attachments) { 
      for (VAttachements vAttachement : epic 
        .getAttachement()) { 
       files.add(vAttachement.getFileName()); 

      } 
      existingAttachments.add(attachment.getFilename()); 
     } 

     for (VAttachements vAttachement : epic.getvAttachement()) { 

      if (!(existingAttachments.contains(vAttachement.getFileName()))) { 

       Promise<Void> attachmentResult = issueRestClient 
         .addAttachment(attachmentsUri, 
           vAttachement.getInputStream(), 
           vAttachement.getFileName()); 
       attachmentResult.claim(); 
       Constants.REPORT.info(attachmentResult.isDone()); 

      } 

     } 
     for (Attachment attachment : attachments) { 
      for (String checkAttachment : existingAttachments) { 
       if (!files.contains(checkAttachment)) 
        deleteJiraAttachment(attachment, auth, issue, 
          checkAttachment); 

      } 
     } 

    } catch (Exception e) { 
     Constants.ERROR.info(Level.INFO, e); 

    } 
} 

,尤其是圓形的Epics是一個POJO類,其中包含在附件吉拉被添加,通過吸氣/ setter方法。

private void deleteJiraAttachment(Attachment attachment, String auth, 
     Issue issue, String jiraFilename) { 

    URI attachmentURL = attachment.getSelf(); 

    int status; 
    try { 
     if (jiraFilename.equalsIgnoreCase(attachment.getFilename())) { 
      status = invokeDeleteMethod(auth, String.valueOf(attachmentURL)); 

      if (status == 204) { 
       Constants.REPORT.info("Attachment deleted from Issue" 
         + issue.getKey()); 
      } else if (status == 403) { 
       System.out 
         .println("attachments for Issue\t " 
           + issue.getKey() 
           + " is disabled or you don't have permission to remove"); 
      } else if (status == 404) { 
       Constants.REPORT.info("No attachment is not found for" 
         + issue.getKey()); 
      } 
     } 
    } catch (AuthenticationException | ClientHandlerException e) { 
     Constants.ERROR.info(Level.INFO, e); 

    } 

} 

private static int invokeDeleteMethod(String auth, String url) 
     throws AuthenticationException, ClientHandlerException { 

    Client client = Client.create(); 
    WebResource webResource = client.resource(url); 
    ClientResponse response = webResource 
      .header("Authorization", "Basic " + auth) 
      .type("application/json").accept("application/json") 
      .delete(ClientResponse.class); 
    int statusCode = response.getStatus(); 
    if (statusCode == 401) { 
     throw new AuthenticationException("Invalid Username or Password"); 
    } 
    return statusCode;