当前位置: 首页 > 工具软件 > JIRA-Client > 使用案例 >

JAVA中通过JIRA REST Client 使用jira

徐嘉谊
2023-12-01

首先创建springboot工程,使用maven进行构建,pom依赖如下:

  <dependency>
          <groupId>com.atlassian.jira</groupId>
          <artifactId>jira-rest-java-client-core</artifactId>
          <version>5.1.6</version>
  </dependency>
  <dependency>
          <groupId>io.atlassian.fugue</groupId>
          <artifactId>fugue</artifactId>
          <version>4.7.2</version>
          <scope>provided</scope>
  </dependency>

加入依赖以后可以通过asynchronousJiraRestClientFactory进行登陆获取认证,本次使用用户名密码的方式进行校验,也可以使用其他 的方式进行校验(例如token的方式),登陆验证以后获取到jiraRestClient进行后续的操作。

public JiraRestClient login_jira(){
    AsynchronousJiraRestClientFactory asynchronousJiraRestClientFactory = new AsynchronousJiraRestClientFactory();
    JiraRestClient jiraRestClient = asynchronousJiraRestClientFactory.createWithBasicHttpAuthentication(URI.create(jira地址), 用户名,密码);
    return jiraRestClient;
}

通过查看接口可以看到可以获取到多种操作类型的client。

public interface JiraRestClient extends Closeable {
    IssueRestClient getIssueClient(); //可以进行issue相关的操作
 
    SessionRestClient getSessionClient();
 
    UserRestClient getUserClient(); //jira用户相关的操作
 
    GroupRestClient getGroupClient();
 
    ProjectRestClient getProjectClient(); //工程相关的操作
 
    ComponentRestClient getComponentClient();
 
    MetadataRestClient getMetadataClient();
 
    SearchRestClient getSearchClient();
 
    VersionRestClient getVersionRestClient();
 
    ProjectRolesRestClient getProjectRolesRestClient();
 
    AuditRestClient getAuditRestClient();
 
    MyPermissionsRestClient getMyPermissionsRestClient();
 
    void close() throws IOException;
}

示例1:获取对应的issue信息

/**
* 获取并返回指定的Issue对象
*/
public static Issue get_issue(String issueNum, String username, String password) throws URISyntaxException {
    try {
        final JiraRestClient restClient = login_jira(username, password);
        final NullProgressMonitor pm = new NullProgressMonitor();
        final Issue issue = restClient.getIssueClient().getIssue(issueNum, pm);

        //获取jira备注
        List<String> comments = new ArrayList<String>();
        for (Comment comment : issue.getComments()) {
            comments.add(comment.getBody().toString());
        }
            
        //获取创建时间
        DateTime createTime = issue.getCreationDate();
        
        //获取描述部分
        String description = issue.getDescription();

        //获取标题
        String summary = issue.getSummary();

        //获取报告人的名字
        issue.getReporter().getDisplayName();

        //获取jira状态
        String status =  issue.getStatus().getName();

        //获取jira类型
        String type = issue.getIssueType().getName();

        //获取模块
        ArrayList<String> arrayList = new ArrayList<String>();
        Iterator<BasicComponent> basicComponents = issue.getComponents().iterator();
        while (basicComponents.hasNext()) {
            String moduleName = basicComponents.next().getName();
            arrayList.add(moduleName);
        }

        //获取自定义字段,假设自定义字段的名称是“前端”
        ArrayList<String> qianduanList = new ArrayList<String>();
        Json json = Json.read(issue.getFieldByName("前端").getValue().toString());
        Iterator<Json> qianduans = json.asJsonList().iterator();
        while (qianduans.hasNext()) {
            Json qianduan = Json.read(qianduans.next().toString());
            String qianduanName = qianduan.at("displayName").toString();
            qianduanList.add(qianduanName);
        }

        //获取经办人
        ArrayList<String> assigneesNames = new ArrayList<String>();
        Json json = Json.read(issue.getFieldByName("分派给").getValue().toString());
        Iterator<Json> assignees = json.asJsonList().iterator();
        while (assignees.hasNext()) {
            Json assignee = Json.read(assignees.next().toString());
            String assigneeName = assignee.at("displayName").toString();
            assigneesNames.add(assigneeName);
        }

        return issue;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

示例2:查询Jira数据

JiraRestClient jiraRestClient = JiraTest.loginJira();

List<Issue> issues = new ArrayList<>();

//获取所有符合条件的issue
int start = 0;
int maxPerPage = 50;  /* batch size (max 50) */
int total = 0;
do {
    SearchResult result = jiraRestClient.getSearchClient().searchJql(jql, maxPerPage, start, new HashSet<String>())
                .claim();
    String s = JSONObject.toJSONString(result);
    total = result.getTotal();
    start += maxPerPage;
    result.getIssues().iterator().forEachRemaining(issues::add);
} while (total > start);

//分析所有issue
for(Issue issue:issues){
    System.out.println("issue的标题:"+issue.getSummary());
}

 类似资料: