1. 引言
本文将快速介绍如何通过 REST API 集成 JIRA 系统。我们将从环境配置到实际操作,逐步展示核心功能的实现方式。
2. Maven 依赖
首先需要在项目中添加 Atlassian 的公共 Maven 仓库:
<repository>
<id>atlassian-public</id>
<url>https://packages.atlassian.com/maven/repository/public</url>
</repository>
仓库配置完成后,添加以下核心依赖:
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client-core</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.atlassian.fugue</groupId>
<artifactId>fugue</artifactId>
<version>2.6.1</version>
</dependency>
⚠️ 最新版本请查阅 Maven Central 和 fugue
3. 创建 JIRA 客户端
连接 JIRA 需要三个关键参数:
username
:有效的 JIRA 用户名password
:对应用户的密码jiraUrl
:JIRA 实例的访问地址
通过这些参数初始化客户端:
MyJiraClient myJiraClient = new MyJiraClient(
"user.name",
"password",
"http://jira.company.com");
客户端构造函数实现:
public MyJiraClient(String username, String password, String jiraUrl) {
this.username = username;
this.password = password;
this.jiraUrl = jiraUrl;
this.restClient = getJiraRestClient();
}
核心方法 getJiraRestClient()
创建 REST 客户端实例:
private JiraRestClient getJiraRestClient() {
return new AsynchronousJiraRestClientFactory()
.createWithBasicHttpAuthentication(getJiraUri(), this.username, this.password);
}
✅ 当前使用基础认证,也支持 OAuth 等高级认证方式
URL 转换方法:
private URI getJiraUri() {
return URI.create(this.jiraUrl);
}
3.1. 创建新 Issue
创建 Issue 需要三个参数:
projectKey
:项目唯一标识(如 "PROJ")issueType
:问题类型(如 Task/Story)issueSummary
:问题标题
public String createIssue(String projectKey, Long issueType, String issueSummary) {
IssueRestClient issueClient = restClient.getIssueClient();
IssueInput newIssue = new IssueInputBuilder(
projectKey, issueType, issueSummary).build();
return issueClient.createIssue(newIssue).claim().getKey();
}
⚠️ IssueInput
可扩展更多字段(经办人、报告人、影响版本等)
3.2. 更新 Issue 描述
通过 Issue Key(如 "PROJ-123")更新描述:
public void updateIssueDescription(String issueKey, String newDescription) {
IssueInput input = new IssueInputBuilder()
.setDescription(newDescription)
.build();
restClient.getIssueClient()
.updateIssue(issueKey, input)
.claim();
}
读取更新后的描述:
public Issue getIssue(String issueKey) {
return restClient.getIssueClient()
.getIssue(issueKey)
.claim();
}
使用示例:
Issue issue = myJiraClient.getIssue(issueKey);
System.out.println(issue.getDescription());
3.3. 为 Issue 投票
使用 Issue 实例执行投票操作:
public void voteForAnIssue(Issue issue) {
restClient.getIssueClient()
.vote(issue.getVotesUri())
.claim();
}
验证投票数:
public int getTotalVotesCount(String issueKey) {
BasicVotes votes = getIssue(issueKey).getVotes();
return votes == null ? 0 : votes.getVotes();
}
❗ 需重新获取 Issue 实例以获取最新投票数
3.4. 添加评论
通过 Issue 实例添加评论:
public void addComment(Issue issue, String commentBody) {
restClient.getIssueClient()
.addComment(issue.getCommentsUri(), Comment.valueOf(commentBody));
}
获取所有评论:
public List<Comment> getAllComments(String issueKey) {
return StreamSupport.stream(getIssue(issueKey).getComments().spliterator(), false)
.collect(Collectors.toList());
}
✅ Comment.valueOf()
提供基础创建方式,高级场景可控制可见性等参数
3.5. 删除 Issue
通过 Issue Key 删除 Issue:
public void deleteIssue(String issueKey, boolean deleteSubtasks) {
restClient.getIssueClient()
.deleteIssue(issueKey, deleteSubtasks)
.claim();
}
⚠️ deleteSubtasks
参数控制是否同时删除子任务
4. 总结
本文实现了一个简单的 JIRA REST API Java 客户端,覆盖了核心操作流程:
- Issue 创建/更新/删除
- 投票与评论管理
- 基础认证集成
完整源码请参考 GitHub 仓库。实际使用时建议:
- 添加异常处理机制
- 考虑使用连接池优化性能
- 敏感信息(如密码)建议使用环境变量或密钥管理服务