简介:通过Jira网络钩子和钉钉自定义机器人,实现Jira任务的自定义格式指派通知
一、前提
Jira Software、钉钉群、RESTful服务、LDAP服务
二、流程图
三、对接步骤
1、创建项目群,把相关人员拉入群
2、钉钉群的智能群助手里添加自定义机器人
3、设置机器人,安全设置里建议勾选IP地址段,将RESTful服务所在的机器IP填入
4、注意创建完成后的Webhook地址,RESTful服务用这个地址发送通知消息,后面步骤要用上
5、RESTful服务新增一个POST接口,这里可以自定义通知内容,部署到服务器
1 @ApiOperation(value = "JIRA网络钩子") 2 @PostMapping("/jiraWebHook") 3 @ResponseBody 4 public Result jiraWebHook(HttpServletRequest request) { 5 BufferedReader br = null; 6 String str, wholeStr = ""; 7 try { 8 br = request.getReader(); 9 10 while ((str = br.readLine()) != null) {11 wholeStr += str;12 }13 log.info("jiraWebHook body: {}", wholeStr);14 } catch (Exception e) {15 log.error("jiraWebHook error", e);16 } finally {17 if (br != null) {18 try {19 br.close();20 } catch (Exception e) {21 }22 }23 }24 return Result.success(JiraWebHookUtil.hook(JSONObject.parseObject(wholeStr, JiraPostData.class)));25 }
View Code
1 @Slf4j 2 public class JiraWebHookUtil { 3 4 private static final String DING_TALK_ROBOT_URL = "https://oapi.dingtalk.com/robot/send" + 5 "?access_token=xxxxxx"; 6 private static final String JIRA_BASE_URL = "http://xxx.xxx.xxx.xxx:38080/browse/"; 7 private static RestTemplate restTemplate = new RestTemplate(); 8 9 public static boolean hook(JiraPostData postData){ 10 try { 11 if(postData != null){ 12 if(StringUtils.equals(postData.getWebhookEvent(), "jira:issue_updated")){ 13 //更新事件 14 List items = postData.getChangelog().getItems(); 15 String toUserId = Strings.EMPTY; 16 String fromUserId = Strings.EMPTY; 17 for(Item item : items){ 18 if(StringUtils.equals(item.getField(), "assignee")){ 19 toUserId = item.getTo(); 20 fromUserId = item.getFrom(); 21 break; 22 } 23 } 24 if(StringUtils.isNotBlank(toUserId) && StringUtils.isNotBlank(fromUserId)){ 25 String toMobile = getUserMobile(toUserId); 26 String fromMobile = getUserMobile(fromUserId); 27 if(StringUtils.isNotBlank(toMobile) && StringUtils.isNotBlank(fromMobile)){ 28 String issueKey = postData.getIssue().getKey(); 29 String issueSummary = postData.getIssue().getFields().getSummary(); 30 DingTalkMarkdownMessage msg = new DingTalkMarkdownMessage(); 31 msg.setMsgtype("markdown"); 32 Markdown markdown = new Markdown(); 33 markdown.setTitle(postData.getIssue().getKey()); 34 markdown.setText("### @" + fromMobile + " 【转移任务给】@" + toMobile 35 + "\n>任务编号:[" + issueKey + "](" + JIRA_BASE_URL + issueKey + ")" 36 + " \n任务标题:" + issueSummary); 37 msg.setMarkdown(markdown); 38 At at = new At(); 39 at.setAtMobiles(Arrays.asList(toMobile, fromMobile)); 40 msg.setAt(at); 41 return sendMsgToDingTalk(JSONObject.to.........