编程语言
首页 > 编程语言> > 如何通过java检索附件列表JIRA rest api?

如何通过java检索附件列表JIRA rest api?

作者:互联网

如何使用JIRA API和Java获取附件列表?我不需要附件我只想知道附件是什么,以及他们的ID.
我遵循了使用查询来解决问题的基础知识,但我遇到了问题,但没有问题.
我的代码:

[String searchQuery = "(created >= '2015/12/1' ) AND (created < '2016/01/1')" 
JiraAuthentication jiraAuth;
JiraRestClient arc;
// Get JiraAuthentication instance to create basic HTTP authentication
// string
jiraAuth = JiraAuthentication.getInstance();

// Make sure you use a new instance
jiraAuth.initInstance();
jiraAuth = JiraAuthentication.getInstance();

// Get the JIRA Rest Client from the basic HTTP authentication
jrc = jiraAuth.getJrc();

// Receive search results based on query
SearchRestClient searchClient = jrc.getSearchClient();
// limit results to 50
SearchResult result = searchClient.searchJql(searchQuery, 50,0, null);
Iterator<BasicIssue> itIssue = result.getIssues().iterator();

// pull information on individual issues.
while (itIssue.hasNext()) {
   BasicIssue lBasicIssue = (BasicIssue) itIssue.next();
   String lIssueKey = lBasicIssue.getKey();
   Issue issue = jrc.getIssueClient().getIssue(lIssueKey, null);
   //This line should print the list of attachments. but everything is always null.
   System.out.println(issue.getAttachements());
}][1]

我试图获取信息的一个例子来自HBASE-15062,它有api link.在api链接中没有显示任何附件,但在实际链接中有4个附件.

我知道Jira api调用api / 2 / issue / {issueIdOrKey} / attachments.

我试过了:api/2/issue/12925031/attachments
我收到响应HTTP状态405 – 方法不允许这是否意味着我无法获取附件列表或我只是做错了请求?

如何更改我的java代码以获取附件?
是否可以从APACHE JIRA获得附件?

解决方法:

从我所看到的Atlassian JIRA中有一张票,你可以体验到:REST issue get no longer shows attachments information.在this comment中甚至提到了Apache的JIRA案例.引用another comment

Attachments are visible in Issue View webpage, but not visible on REST while the field is hidden from the default screen.

该错误已经解决,但用户似乎并不喜欢它和demand a proper fix

The “Resolution” above is not a resolution but a workaround for this bug. A proper resolution is for Atlassian to fix this bug so that attachments are always listed in the JSON. There is no way a GUI setting should affect the REST API JSON output – that’s clearly wrong! Please reopen and fix this issue.

我同意他们的观点,但这并没有改变这样一个事实,即目前你干净利落地实现目标的唯一选择就是让Apache采取行动.无论如何,我似乎有一个解决方法.

解决方法

我不知道您正在使用的客户端工具,因此我将使用纯REST方法描述解决方法.将附件添加到问题时,会在故障单的更改日志中记录该事件:

https://issues.apache.org/jira/rest/api/2/issue/HBASE-15062?expand=changelog

{
   (...),          
   "changelog": {
      "startAt":0,
      "maxResults":8,
      "total":8,
      "histories":[
         (...),
         {                 
            "id":"15174314",
            "author":{...},
            "created":"2015-12-31T15:55:43.318+0000",
            "items":[  
               {  
                  "field":"Attachment",
                  "fieldtype":"jira",
                  "from":null,
                  "fromString":null,
                  "to":"12780132",
                  "toString":"HBASE-15062-v0.patch"
               }
            ]
         },
         {  
            "id":"15174320",
            "author":{...},
            "created":"2015-12-31T16:06:03.165+0000",
            "items":[  
               {  
                  "field":"Attachment",
                  "fieldtype":"jira",
                  "from":null,
                  "fromString":null,
                  "to":"12780133",
                  "toString":"HBASE-15062-v1.patch"
               }
            ]
         },             
         {  
            "id":"15184002",
            "author":{...},
            "created":"2016-01-04T08:04:50.969+0000",
            "items":[  
               {  
                  "field":"Attachment",
                  "fieldtype":"jira",
                  "from":null,
                  "fromString":null,
                  "to":"12780273",
                  "toString":"HBASE-15062-v1.patch"
               }
            ]
         },
         {  
            "id":"15187193",
            "author":{...},
            "created":"2016-01-05T21:30:48.977+0000",
            "items":[  
               {  
                  "field":"Attachment",
                  "fieldtype":"jira",
                  "from":null,
                  "fromString":null,
                  "to":"12780629",
                  "toString":"HBASE-15062-v1.patch"
               }
            ]
         },
         (...)
      ]
   }
}

让我们考虑一下id为12780132的日志中的第一个附件.了解id后,您可以在API中查询附件的详细信息:

https://issues.apache.org/jira/rest/api/2/attachment/12780132

{  
   "self":"https://issues.apache.org/jira/rest/api/2/attachment/12780132",
   "filename":"HBASE-15062-v0.patch",
   "author":{  
      "self":"https://issues.apache.org/jira/rest/api/2/user?username=asamir",
      "key":"asamir",
      "name":"asamir",
      "avatarUrls":{  
         "48x48":"https://issues.apache.org/jira/secure/useravatar?avatarId=10452",
         "24x24":"https://issues.apache.org/jira/secure/useravatar?size=small&avatarId=10452",
         "16x16":"https://issues.apache.org/jira/secure/useravatar?size=xsmall&avatarId=10452",
         "32x32":"https://issues.apache.org/jira/secure/useravatar?size=medium&avatarId=10452"
      },
      "displayName":"Samir Ahmic",
      "active":true
   },
   "created":"2015-12-31T15:55:43.304+0000",
   "size":2173,
   "mimeType":"text/x-patch",
   "properties":{  

   },
   "content":"https://issues.apache.org/jira/secure/attachment/12780132/HBASE-15062-v0.patch"
}

现在您拥有了下载附件的所有信息:

https://issues.apache.org/jira/secure/attachment/12780132/HBASE-15062-v0.patch.

希望它有所帮助,保重!

标签:apache,java,jira,jira-rest-api,jira-rest-java-api
来源: https://codeday.me/bug/20190609/1201786.html