jira创建条目rest实用脚本

最近在搞crash崩溃分析,直接把解析到的信息录入jira系统进行跟踪;

经历了多次碰壁后终于调通,现记录一下

实用json请求脚本如下:

{
                "fields":{
                        "project":{
                                "id":"10945"
                        },
                        "issuetype":{
                                "id":"10103"
                        },
                        "summary":"%s",
                        "description":"%s",
                        "components":[
                                {
                                        "id":"16001"
                                }
                        ],
                        "versions":[
                                {
                                        "id":"16600"
                                }
                        ],
                        "customfield_12108":{
                                "id":"12972"
                        },
                        "customfield_12109":{
                                "id":"12974"
                        },
                        "priority":{
                                "id":"10102"
                        },
                        "security":{
                                "id":"10600"
                        },
                        "assignee":{
                                "name":"%s"
                        }
                }
        }

两个注意点:

1、代码里%s  是我这边他要替换的字符串;

2、上面的id值需要自己抓包来确定,每个project各id值是不一样的,

      一般抓包工具是Charles和Fiddler;

jira建单官方示例如下:

The Jira Cloud platform REST API

// The payload definition using the Jackson library
JsonNodeFactory jnf = JsonNodeFactory.instance;
ObjectNode payload = jnf.objectNode();
{
  ObjectNode fields = payload.putObject("fields");
  {
    ObjectNode assignee = fields.putObject("assignee");
    {
      assignee.put("id", "5b109f2e9729b51b54dc274d");
    }
    ArrayNode components = fields.putArray("components");
    ObjectNode components0 = components.addObject();
    {
      components0.put("id", "10000");
    }
    fields.put("customfield_10000", "09/Jun/19");
    fields.put("customfield_20000", "06/Jul/19 3:25 PM");
    ArrayNode customfield_30000 = fields.putArray("customfield_30000");
    customfield_30000.add("10000");
    customfield_30000.add("10002");
    fields.put("customfield_40000", "Occurs on all orders");
    fields.put("customfield_50000", "Could impact day-to-day work.");
    fields.put("customfield_60000", "jira-software-users");
    ArrayNode customfield_70000 = fields.putArray("customfield_70000");
    customfield_70000.add("jira-administrators");
    customfield_70000.add("jira-software-users");
    ObjectNode customfield_80000 = fields.putObject("customfield_80000");
    {
      customfield_80000.put("value", "red");
    }
    fields.put("description", "Order entry fails when selecting supplier.");
    fields.put("duedate", "2019-03-11");
    fields.put("environment", "UAT");
    ArrayNode fixVersions = fields.putArray("fixVersions");
    ObjectNode fixVersions0 = fixVersions.addObject();
    {
      fixVersions0.put("id", "10001");
    }
    ObjectNode issuetype = fields.putObject("issuetype");
    {
      issuetype.put("id", "10000");
    }
    ArrayNode labels = fields.putArray("labels");
    labels.add("bugfix");
    labels.add("blitz_test");
    ObjectNode parent = fields.putObject("parent");
    {
      parent.put("key", "PROJ-123");
    }
    ObjectNode priority = fields.putObject("priority");
    {
      priority.put("id", "20000");
    }
    ObjectNode project = fields.putObject("project");
    {
      project.put("id", "10000");
    }
    ObjectNode reporter = fields.putObject("reporter");
    {
      reporter.put("id", "5b10a2844c20165700ede21g");
    }
    ObjectNode security = fields.putObject("security");
    {
      security.put("id", "10000");
    }
    fields.put("summary", "Main order flow broken");
    ObjectNode timetracking = fields.putObject("timetracking");
    {
      timetracking.put("originalEstimate", "10");
      timetracking.put("remainingEstimate", "5");
    }
    ArrayNode versions = fields.putArray("versions");
    ObjectNode versions0 = versions.addObject();
    {
      versions0.put("id", "10000");
    }
  }
  ObjectNode update = payload.putObject("update");
  {
    ArrayNode worklog = update.putArray("worklog");
    ObjectNode worklog0 = worklog.addObject();
    {
      ObjectNode add = worklog0.putObject("add");
      {
        add.put("started", "2019-07-05T11:05:00.000+0000");
        add.put("timeSpent", "60m");
      }
    }
  }
}

// Connect Jackson ObjectMapper to Unirest
Unirest.setObjectMapper(new ObjectMapper() {
   private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper
           = new com.fasterxml.jackson.databind.ObjectMapper();

   public  T readValue(String value, Class valueType) {
       try {
           return jacksonObjectMapper.readValue(value, valueType);
       } catch (IOException e) {
           throw new RuntimeException(e);
       }
   }
   //这里可以打印自己请求的json

   public String writeValue(Object value) {
       try {
           return jacksonObjectMapper.writeValueAsString(value);
       } catch (JsonProcessingException e) {
           throw new RuntimeException(e);
       }
   }
});

// This code sample uses the  'Unirest' library:
// http://unirest.io/java.html
HttpResponse response = Unirest.post("https://your-domain.atlassian.net/rest/api/2/issue")
  .basicAuth("[email protected]", "")
  .header("Accept", "application/json")
  .header("Content-Type", "application/json")
  .body(payload)
  .asJson();

System.out.println(response.getBody());

这里可以打印自己发送的json脚本

   //这里可以打印自己请求的json

   public String writeValue(Object value) {
       System.out.println("writeValue=====" + value);
       try {
           return jacksonObjectMapper.writeValueAsString(value);
       } catch (JsonProcessingException e) {
           throw new RuntimeException(e);
       }
   }

在这里给自己做个笔记

你可能感兴趣的:(http,jira)