通过jira Api注册webHook(钩子),触发事件后接收jira回调信息

需要在jira上注册一个钩子,当issue状态变更,或者新增issue等操作时,接收jira回调信息,触发系统其他操作。
参考文档:https://developer.atlassian.com/server/jira/platform/webhooks/

一、注册webHook

请求url:/rest/webhooks/1.0/webhook,发送post请求,需要如下信息:
name–是创建的hook的名字(必须有);
url–事件触发后回调的地址(必须有);
events–哪些事件会触发hook,事件很多,详见文档;
filters–过滤条件,使用jql语法;excludebody–是否不需要回调信息(hook回调,会包含相关信息,true就是不需要信息)。
jql语法文档:https://support.atlassian.com/jira-service-desk-cloud/docs/advanced-search-reference-jql-fields/?_ga=2.124421194.1703533170.1565329940-469854552.1550548587#Advancedsearching-fieldsreference-fields

{
"name": "my first webhook via rest",
"url": "http://www.example.com/webhooks",
"events": [
  "jira:issue_created",
  "jira:issue_updated"
],
"filters": {
	"issue-related-events-section": "Project = JRA AND resolution = Fixed"
},
"excludeBody" : false
}

java调用,需要的依赖可见上篇文章使用Java client对接jira api

  public static void registerHook() throws UnirestException {

        //接收jira创建、更新,注释新增、更新的事件回调
        List<String> events = Lists.newArrayList("jira:issue_created", "jira:issue_updated","comment_created"
        ,"comment_updated");

        Map<String, Object> map = new HashMap<>();
        //jira回调的你的应用程序的地址
        map.put("url", "http://you.app.com/webhooks");
        map.put("name", "this is a test hook new");
        map.put("events", events);
        //过滤条件,也就是项目key为“ADTEST”的项目发生上面注册的事件才会回调
        map.put("filters", Pair.of("issue-related-events-section","project = ADTEST "));
        logger.info(JSON.toJSONString(map));
        //账号需要有可以注册hook的权限
        HttpResponse<JsonNode> response = Unirest.post(url + "/rest/webhooks/1.0/webhook")
                .basicAuth("user", "pwd")
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(map))
                .asJson();
        System.out.println(response.getBody());
    }

注册成功后,会返回注册的hook的信息。我们也可以通过webhook id去查询信息。

 public static void getHook(String hookId) throws UnirestException {
        HttpResponse<String> response = Unirest.get(url + "/rest/webhooks/1.0/webhook/" + hookId)
                .basicAuth("user", "pwd")
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .asString();
        System.out.println(JSON.toJSONString(response));

    }

同样的,也有更新删除的方法。

 public static void updateHook(String hookId) throws UnirestException {
        Map<String, Object> map = new HashMap<>();
        map.put("url", "http://you.app.com/webhooks");
//        map.put("filters", Pair.of("issue-related-events-section","issueKey = ADTEST-4"));
        logger.info(JSON.toJSONString(map));
        //发送put请求,更改回调的地址
        HttpResponse<JsonNode> response = Unirest.put(url + "/rest/webhooks/1.0/webhook/" + hookId)
                .basicAuth("user", "pwd")
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(map))
                .asJson();

        System.out.println(response.getBody());
    }
    
    public static void deleteHook(String hookId) throws UnirestException {
        HttpResponse<JsonNode> response = Unirest.delete(url + "/rest/webhooks/1.0/webhook/"+hookId)
                .basicAuth("user", "pwd")
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .asJson();
        System.out.println(response.getBody());
    }

二、接收回调信息

创建一个controller,用HttpServletRequest接受请求,回调信息在BufferedReader里面,读取BufferedReader的信息就可以了。

package com.stwl.jira.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author 
 * @date 2020/4/27 19:27
 */
@RestController
@RequestMapping("")
public class JiraHookController {

    @RequestMapping("/webhooks")
    public String issueChangeHook(HttpServletRequest request) throws IOException {
        System.out.println("===============================");

        BufferedReader reader = null;
        try {
            reader = request.getReader();
            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            System.out.println(result.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            reader.close();
        }
        return null;
    }
}

当我们的jira触发注册的事件后,就会回调上面的http接口,然后获取信息后做我们自己需要的逻辑就可以了。

你可能感兴趣的:(学习笔记,问题解析)