Whi*_*cat 5 java apache jira jira-rest-java-api jira-rest-api
如何使用 Java 中的 JIRA API 获取附件列表?我不需要附件,我只想知道有哪些附件及其 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]
Run Code Online (Sandbox Code Playgroud)
我尝试从 HBASE-15062 获取信息的一个示例,它具有 api link。在 api 链接中没有显示任何附件,但在实际链接中有 4 个附件。
我知道 Jira api 调用api/2/issue/{issueIdOrKey}/attachments。
我尝试过:api/2/issue/12925031/attachments
我得到了响应HTTP Status 405 - Method Not Allowed这是否意味着我无法获取附件列表或者我只是错误地执行了请求?
如何更改我的 java 代码来获取附件?是否可以从 APACHE JIRA 获取附件?
据我所知,Atlassian JIRA 中存在一张针对您所经历的问题的票证:REST issues get no more shown attachments information。这篇评论中甚至还提到了 Apache 的 JIRA 案例。引用另一条评论:
附件在“问题视图”网页中可见,但在 REST 上不可见,因为该字段在默认屏幕中隐藏。
该错误已解决,但用户似乎不喜欢它并要求适当的修复:
上面的“解决方案”不是解决方案,而是此错误的解决方法。Atlassian 的正确解决方案是修复此错误,以便附件始终在 JSON 中列出。GUI 设置不可能影响 REST API JSON 输出 - 这显然是错误的!请重新打开并修复此问题。
我同意他们的观点,但这并没有改变这样一个事实:目前你干净利落地实现目标的唯一选择就是让 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"
}
]
},
(...)
]
}
}
Run Code Online (Sandbox Code Playgroud)
让我们考虑日志中 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"
}
Run Code Online (Sandbox Code Playgroud)
现在您已拥有下载附件的所有信息:
https://issues.apache.org/jira/secure/attachment/12780132/HBASE-15062-v0.patch。
希望有帮助,保重!
我想说詹尼斯的回答引导我找到了这个问题的答案。
添加参数的jannis?expand=changelog方法是获取附件的唯一方法。java调用需要改变我在这里getIssue()找到了如何添加参数,api信息在这里。
getIssue(String issueKey, ProgressMonitor progressMonitor)被使用:
Issue issue = jrc.getIssueClient().getIssue(lIssueKey, null);
Run Code Online (Sandbox Code Playgroud)
Issue issue = jrc.getIssueClient().getIssue(lIssueKey, Arrays.asList(IssueRestClient.Expandos.CHANGELOG), null);
Run Code Online (Sandbox Code Playgroud)
通过更改对 getIssue 的调用,应填充更改日志字段。然后,您可以使用此字段通过迭代更改日志来查看附件。我这样做了:
private null findAttachments(Issue issue) {
String FieldName = "Attachment";
Iterable<ChangelogGroup> changes = issue.getChangelog();
StringBuilder attachments = new StringBuilder();
StringBuilder attachmentsIDs = new StringBuilder();
for (ChangelogGroup change: changes){
//Multiple change items per group.
for(ChangelogItem item: change.getItems()){
if(item.getField().equals(FieldName)){
//Gets attachment name.
attachments.append((String) item.getToString());
//Gets attachment ID to download if needed.
attachmentsIDs.append((String) item.getTo());
}
}
}
//Do something with attachments here..
}
Run Code Online (Sandbox Code Playgroud)