jira rest api方式创建bug并且提交附件

除了常用的项目jar包之外,jira所需的3个核心jar包:jms-1.1.jar、jmxri.jar、jmxtools.jar

具体代码下载地址如:https://download.csdn.net/download/liqi_q/10304356

长用接口API如下:

jira_api_project http://tbjira.sinas.com/issue/rest/api/2/project
jira_api_versions http://tbjira.sinas.com/issue/rest/api/2/project/{key}/versions
jira_api_components http://tbjira.sinas.com/issue/rest/api/2/project/{key}/components
jira_api_products http://tbjira.sinas.com/issue/rest/mailruselectsrv/1.0/mailruselectsrv/getinfo?cfid=11700&project={key}
jira_api_newissue http://tbjira.sinas.com/issue/rest/api/2/issue
jira_api_issue_search http://tbjira.sinas.com/issue/rest/api/2/search
jira_api_editissue http://tbjira.sinas.com/issue/rest/api/2/issue/{key}
jira_api_getissueinfo http://tbjira.sinas.com/issue/rest/api/2/issue/{key}
jira_api_createmeta https://tbjira.sinas.com/issue/rest/api/2/issue/createmeta?projectKeys={key}&issuetypeNames=Bug&expand=projects.issuetypes.fields
jira_api_addcomment http://tbjira.sinas.com/issue/rest/api/2/issue/{key}/comment
jira_api_getcomment http://tbjira.sinas.com/issue/rest/api/2/issue/{key}/comment
jira_api_getissuelinktype http://tbjira.sinas.com/issue/rest/api/2/issueLinkType 
jira_api_addissuelink http://tbjira.sinas.com/issue/rest/api/2/issueLink
jira_api_getbugstatus http://tbjira.sinas.com/issue/rest/api/2/issue/{key}/transitions
jira_api_updateissuestatus http://tbjira.sinas.com/issue/rest/api/2/issue/{key}/transitions
jira_api_deleteissuelink http://tbjira.sinas.com/issue/rest/api/2/issueLink/{key}
jira_api_remotelink http://tbjira.sinas.com/issue/rest/api/2/issue/{key}/remotelink

封装调用工具

package ltms.ws.tools;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.DatatypeConverter;
import ltms.bug.model.JiraBug;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;


public class TestAir {
public static void main(String[] args) throws Exception {
// String requestUrl="http://tbjira.sinas.com:8081/issue/login.jsp";
// Map<String,String> loginMap = new HashMap<String,String>();
// loginMap.put("os_username", "username");
// loginMap.put("os_password", "passwd");
//  
// String requestParma =  JSON.toJSONString(loginMap);
// System.out.println(requestParma);
// String ss = sendPost2(requestUrl, requestParma);
// System.out.println(ss);
remoteLink();
}
 
public static void remoteLink() throws Exception{
String apiUrl = "http://tbjira.sinas.com:8081/issue/rest/api/2/issue/{key}/remotelink";
// 替换参数
apiUrl = apiUrl.replace("{key}", "TLAUTOTEST-467");

// 生成JSON格式参数
Map rootMap = new HashMap();
Map obj = new HashMap();
obj.put("url", "http://fanyi.bdstatic.com/static/translation/img/header/logo_cbfea26.png");
obj.put("title", "5555");
obj.put("summary", "asummay");

rootMap.put("object", obj);
String paramsStr = JSONUtils.toJSONString(rootMap);
String strResult =sendPost2(apiUrl, paramsStr);
com.alibaba.fastjson.JSONObject jsonObj = JSON.parseObject(strResult);
System.out.println( jsonObj.getString("id") +"-------"+ jsonObj.getString("self"));
 
}
public static String createIssues() throws Exception{
JiraBug jiraBug = new JiraBug();
       jiraBug.setProjectName("TLAUTOTEST");
       jiraBug.setJiraProject("TLAUTOTEST");
       jiraBug.setSummary("5555555555555");
       jiraBug.setDescription("5555555555");
       jiraBug.setHwStatus("10148");
       jiraBug.setComponent("10609");
       jiraBug.setBugType("10123");
       jiraBug.setBugRange("10125");
       jiraBug.setTestType("10215");
       jiraBug.setImportance("10103");
       jiraBug.setVersion(15736);
       jiraBug.setProducts("10313");
 
   String nsssjons = newIssueToJson(jiraBug);
   nsssjons = "{\"fields\":{\"summary\":\"阿斯蒂芬\",\"issuetype\":{\"name\":\"Bug\"},\"components\":[{\"id\":\"10609\"}],\"project\":{\"key\":\"TLAUTOTEST\"},\"description\":\"嗯嗯嗯\",\"customfield_10121\":{\"id\":\"10215\"},\"priority\":{\"name\":\"Highest\"},\"customfield_10100\":{\"id\":\"10100\"},\"customfield_10200\":{\"id\":\"10313\"},\"customfield_10102\":{\"id\":\"10123\"},\"customfield_10103\":{\"id\":\"10125\"},\"customfield_10107\":{\"id\":\"10148\"},\"versions\":[{\"id\":\"10215\"}]}}";
       String rsults = sendPost2("http://tbjira.sinas.com:8081/issue/rest/api/2/issue",nsssjons);
       System.out.println("添加bug任务返回结果:"+rsults);
       return rsults;
}
//HttpRest Post方式请求接口
public static String sendPost2(String uri,String jsonObj) throws Exception{  
        String resStr = null; 
        int statusCode=0;
         
        HttpClient htpClient = new HttpClient();  
        //PutMethod putMethod = new PutMethod(uri);  
        PostMethod postMethod = new PostMethod(uri);
        try{  
       
        postMethod.addRequestHeader( "Content-Type","application/json" );  
        postMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");  
            
            // 获取验证用户信息
        String strAuth = "username:passwd";
            String encoding =DatatypeConverter.printBase64Binary(strAuth.getBytes("UTF-8")); 
            postMethod.addRequestHeader("Authorization", "Basic " +encoding); 


            postMethod.setRequestBody( jsonObj );  
            statusCode = htpClient.executeMethod( postMethod );  
            System.out.println(statusCode);
            byte[] responseBody = postMethod.getResponseBody();           
            resStr = new String(responseBody,"UTF-8");  
            System.out.println(resStr);
            if(statusCode != 201){  
                return null;  
            } 
        }catch(Exception e){  
            e.printStackTrace();  
            throw e;
        }finally{  
        postMethod.releaseConnection();  
        }  
        return resStr;  
    } 
//HttpRest Get方式请求接口
public static String sendGet(String url,String param) throws Exception{
String result = "";

        BufferedReader in = null;
        try {
        // 获取验证用户信息
            String strAuth = "username:passwd";
       String encoding =DatatypeConverter.printBase64Binary(strAuth.getBytes("UTF-8"));
       
       String urlNameString = url;
       if (null!=param && param.length()>0){
        urlNameString = url + "?" + param;
       }
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.setRequestProperty("Authorization", "Basic " +encoding); 
           
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            /*for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }*/
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(),"utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            throw e;
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
}


    //构建提交issue的json格式数据
public static  String newIssueToJson(JiraBug jiraBug){
Map mapTemp= new HashMap();
Map mapRoot = new HashMap();
// fields 节点
mapRoot.put("fields", new HashMap());
// fields - project 节点 【项目,必选】
toMap(mapRoot.get("fields")).put("project", new HashMap());
// fields - project - key 节点
toMap(toMap(mapRoot.get("fields")).get("project")).put("key", jiraBug.getProjectName());//jiraBug.getJiraProject()
// fields - summary  节点
toMap(mapRoot.get("fields")).put("summary", jiraBug.getSummary());
 
// fields - issuetype 节点 【问题类型,必选】
toMap(mapRoot.get("fields")).put("issuetype", new HashMap());
// fields - issuetype - name 节点
toMap(toMap(mapRoot.get("fields")).get("issuetype")).put("name", "Bug"); // 默认值
 
// fields - issuetype 节点 【问题类型,必选】
toMap(mapRoot.get("fields")).put("priority", new HashMap());
// fields - issuetype - name 节点
toMap(toMap(mapRoot.get("fields")).get("priority")).put("name", "Highest");

toMap(mapRoot.get("fields")).put("customfield_10107", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10107")).put("id", jiraBug.getHwStatus());
// fields - customfield_10901  节点 【Test Type,必选】
toMap(mapRoot.get("fields")).put("customfield_10121", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10121")).put("id", jiraBug.getTestType());

toMap(mapRoot.get("fields")).put("customfield_10100", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10100")).put("id", jiraBug.getImportance());

toMap(mapRoot.get("fields")).put("customfield_10200", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10200")).put("id", jiraBug.getProducts());
 
toMap(mapRoot.get("fields")).put("customfield_10102", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10102")).put("id", jiraBug.getBugType());

toMap(mapRoot.get("fields")).put("customfield_10103", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10103")).put("id", jiraBug.getBugRange());

List lstComponents = new ArrayList();
lstComponents.add(new HashMap());
toMap(mapRoot.get("fields")).put("components", lstComponents);
toMap(toList(toMap(mapRoot.get("fields")).get("components")).get(0)).put("id", jiraBug.getComponent());


List versionList = new ArrayList();
versionList.add(new HashMap());
toMap(mapRoot.get("fields")).put("versions", versionList);
toMap(toList(toMap(mapRoot.get("fields")).get("versions")).get(0)).put("id", jiraBug.getVersion()+"");
 
// fields - labels 节点 【描述】
toMap(mapRoot.get("fields")).put("description", jiraBug.getDescription());

// 生成Json字符串
return JSON.toJSONString(mapRoot);
}

public static  Map toMap(Object o){
return (Map)o;
}

public static List toList(Object o){
return (List)o;
}

}


实体类:

package com.cn.jira.entity;

import java.util.Calendar;


public class JiraBug  implements java.io.Serializable {

   private String bugId;

   private String bugKey;

   private String projectName;

   private String jiraProject;

   private String hunter;

   private String affectsVersion;

   private Calendar created;

   private String component;

   private String bugRange;

   private String repetitionProbability;

   private String bugType;

   private String testType;

   private String importance;

   private String importanceType;

   private String hwStatus;

   private String ueImpactLevel;

   private String appVersion;

   private String coreVersion;

   private String caseId;

   private String city;

   private String products;

   private String needVerifyProducts;

   private String summary;

   private String description;

   private String ueas;

   private String comment;

   private String status;

   private String jfName;
   private String jfPath;

   public String getJfName() {
      return jfName;
   }

   public String getJfPath() {
      return jfPath;
   }

   public void setJfName(String jfName) {
      this.jfName = jfName;
   }

   public void setJfPath(String jfPath) {
      this.jfPath = jfPath;
   }

   /**
    * @return the bugKey
    */
   public String getBugKey() {
      return bugKey;
   }

   /**
    * @param bugKey the bugKey to set
    */
   public void setBugKey(String bugKey) {
      this.bugKey = bugKey;
   }

   public String getComment() {
      return comment;
   }

   public void setComment(String comment) {
      this.comment = comment;
   }

   public String getBugId() {
      return bugId;
   }

   public void setBugId(String bugId) {
      this.bugId = bugId;
   }

   public String getProjectName() {
      return projectName;
   }

   public void setProjectName(String projectName) {
      this.projectName = projectName;
   }

   public String getJiraProject() {
      return jiraProject;
   }

   public void setJiraProject(String jiraProject) {
      this.jiraProject = jiraProject;
   }

   public String getHunter() {
      return hunter;
   }

   public void setHunter(String hunter) {
      this.hunter = hunter;
   }

   public String getAffectsVersion() {
      return affectsVersion;
   }

   public void setAffectsVersion(String affectsVersion) {
      this.affectsVersion = affectsVersion;
   }

   public Calendar getCreated() {
      return created;
   }

   public void setCreated(Calendar created) {
      this.created = created;
   }

   public String getComponent() {
      return component;
   }

   public void setComponent(String component) {
      this.component = component;
   }

   public String getBugRange() {
      return bugRange;
   }

   public void setBugRange(String bugRange) {
      this.bugRange = bugRange;
   }

   public String getRepetitionProbability() {
      return repetitionProbability;
   }

   public void setRepetitionProbability(String repetitionProbability) {
      this.repetitionProbability = repetitionProbability;
   }

   public String getBugType() {
      return bugType;
   }

   public void setBugType(String bugType) {
      this.bugType = bugType;
   }

   public String getTestType() {
      return testType;
   }

   public void setTestType(String testType) {
      this.testType = testType;
   }

   public String getImportance() {
      return importance;
   }

   public void setImportance(String importance) {
      this.importance = importance;
   }

   public String getImportanceType() {
      return importanceType;
   }

   public void setImportanceType(String importanceType) {
      this.importanceType = importanceType;
   }

   public String getHwStatus() {
      return hwStatus;
   }

   public void setHwStatus(String hwStatus) {
      this.hwStatus = hwStatus;
   }

   public String getUeImpactLevel() {
      return ueImpactLevel;
   }

   public void setUeImpactLevel(String ueImpactLevel) {
      this.ueImpactLevel = ueImpactLevel;
   }

   public String getAppVersion() {
      return appVersion;
   }

   public void setAppVersion(String appVersion) {
      this.appVersion = appVersion;
   }

   public String getCoreVersion() {
      return coreVersion;
   }

   public void setCoreVersion(String coreVersion) {
      this.coreVersion = coreVersion;
   }

   public String getCaseId() {
      return caseId;
   }

   public void setCaseId(String caseId) {
      this.caseId = caseId;
   }

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public String getProducts() {
      return products;
   }

   public void setProducts(String products) {
      this.products = products;
   }

   public String getNeedVerifyProducts() {
      return needVerifyProducts;
   }

   public void setNeedVerifyProducts(String needVerifyProducts) {
      this.needVerifyProducts = needVerifyProducts;
   }

   public String getSummary() {
      return summary;
   }

   public void setSummary(String summary) {
      this.summary = summary;
   }

   public String getDescription() {
      return description;
   }

   public void setDescription(String description) {
      this.description = description;
   }

   /**
    * @return the ueas
    */
   public String getUeas() {
      return ueas;
   }

   /**
    * @param ueas the ueas to set
    */
   public void setUeas(String ueas) {
      this.ueas = ueas;
   }

   /**
    * @return the status
    */
   public String getStatus() {
      return status;
   }

   /**
    * @param status the status to set
    */
   public void setStatus(String status) {
      this.status = status;
   }

}

### 回答1: JIRA REST API是指JIRA软件提供的一组可以通过HTTP请求进行访问的API,开发人员可以使用这些API来与JIRA进行交互,以便于对其进行自动化管理或者扩展其功能。 通过JIRA REST API,可以对JIRA上的数据进行增删改查,包括用户、问题、工作流等,可以在外部系统中操作JIRA,实现自动化集成。此外,JIRA REST API还可以用于获取有关问题、项目和其他相关数据的报告。 JIRA REST API的意义在于,它允许开发人员在自己的应用程序中使用JIRA的数据,提高工作效率和协作效率,使得更多的业务流程可以自动化。 ### 回答2: JIRA REST API是一种用于与JIRA软件进行交互的编程接口,它允许开发人员通过发送HTTP请求来执行各种操作,如创建、更新、删除和检索JIRA项目、问题、评论等。 JIRA REST API的意义在于它提供了一种灵活、可扩展的方式来集成和扩展JIRA软件。通过使用这个API,我们可以通过编程方式自动化执行JIRA操作,而不需要手动操作JIRA界面。这可以显著提高工作效率,减少人工操作的错误和重复性工作,同时提供更好的系统可管理性。 JIRA REST API的应用范围非常广泛。例如,开发人员可以使用API自动创建和更新JIRA任务,将其与其他系统集成,如持续集成工具、版本控制系统等。同时,通过API,开发人员可以从JIRA中获取问题和项目的详细信息,并将其用于数据分析、报告生成等用途。 另外,除了开发人员,普通用户也可以通过JIRA REST API来实现自己的定制需求。他们可以使用API创建和更新问题,管理自己的任务和项目等。这为用户提供了更多灵活性和便利性,使他们能够根据自己的工作需要进行个性化配置和管理。 总之,JIRA REST API是一个重要的工具,它能够帮助开发人员和用户与JIRA软件进行交互,实现自动化、集成和定制化的需求。它可以提高工作效率,减少错误和重复性工作,并提供更好的系统可管理性。 ### 回答3: JIRA REST APIJIRA软件提供的一种用于与其他应用程序集成的接口,通过发送HTTP请求和接收JSON响应,可以实现对JIRA软件的数据和功能进行操作和访问。 JIRA REST API的意义主要体现在以下几个方面: 1. 数据交互:通过REST API,可以方便地将JIRA软件中的数据与其他应用程序进行交互和共享。例如,可以根据需要从JIRA获取任务数据,将其与其他系统中的数据进行关联,实现项目管理、任务分配等功能。 2. 自动化操作:利用JIRA REST API,可以编写脚本或开发自动化工具,实现对JIRA的自动化操作。例如,可以编写Python脚本来自动创建JIRA任务、更新任务状态等,节省了手动操作的时间与精力。 3. 扩展功能:通过JIRA REST API,开发人员可以在JIRA软件的基础上构建自己的应用程序或插件,扩展JIRA的功能。例如,可以开发一个自定义的JIRA报表工具,根据项目需求生成特定的报表。 4. 效率提升:利用JIRA REST API可以方便地集成其他工具或系统,实现数据的互通与共享。这可以帮助团队成员提高工作效率,减少重复性工作的产生。 总而言之,JIRA REST API的出现使得JIRA软件在与其他系统集成和扩展功能方面变得更加灵活和强大,帮助用户更好地管理项目和任务,提高工作效率。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值