<dependency> <groupId>net.rcarz</groupId> <artifactId>jira-client</artifactId> <version>0.5</version> <scope>compile</scope> </dependency>github地址:https://github.com/bobcarroll/jira-client
@Configuration @EnableConfigurationProperties({JiraAuthProperties.class, DingTalkProperties.class}) public class JiraConfig { @Autowired private JiraAuthProperties jiraAuthProperties; /** *堆代码 duidaima.com * jira client的bean * @return */ @Bean public JiraClient createJiraClient() { BasicCredentials creds = new BasicCredentials(jiraAuthProperties.getUsername(), jiraAuthProperties.getPassword()); JiraClient jiraClient = new JiraClient(jiraAuthProperties.getUrl(), creds); return jiraClient; } }.BasicCredentials对象中传入jira账号和密码
/** * 获取所有过期的任务 * * @param endDate 截止时间 * @return * @throws JiraException */ public List<Issue> getAllExpireTasks(Date endDate) throws JiraException { List<String> allUserNames = EmployeeManager.getAllUserNames(); String endDateStr = DateUtil.format(endDate, DATE_FORMATE); String userTasksJql = getUserExpireTasksJql(allUserNames, endDateStr); Issue.SearchResult searchResult = jiraClient.searchIssues(userTasksJql, 100000); return searchResult.issues; } /** * 获取用户过期的查询语句 * * @param users * @param dueDateStr * @return */ public static String getUserExpireTasksJql(List<String> users, String dueDateStr) { String jqlTmpl = "resolution = Unresolved and due <= {} AND assignee in ({}) ORDER BY priority DESC"; return StrUtil.format(jqlTmpl, dueDateStr, CollUtil.join(users, ",")); }.getUserExpireTasksJql() 方法中拼接过期任务的jira查询语句
.传入对应的查询语句,调用jiraClient.searchIssues()从jira服务端中查询对应的jira任务和bug等
@ConfigurationProperties(prefix = "dingtalk.auth") @Data public class DingTalkProperties { private String url; private String secretKey; }.根据webhook和加签生成钉钉最终的推送地址
private String genDingTalkUrl() { try { Long timestamp = System.currentTimeMillis(); String stringToSign = timestamp + "\n" + dingTalkProperties.getSecretKey(); Mac mac = null; mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(dingTalkProperties.getSecretKey().getBytes("UTF-8"), "HmacSHA256")); byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8"); String urlTmpl = dingTalkProperties.getUrl() + "×tamp={}&sign={}"; return StrUtil.format(urlTmpl, timestamp, sign); } catch (Exception e) { log.error("getDingTalkUrl error", e); } return null; }.markdown的格式推送内容
/** * 发送markdown内容 * @param title 标题 * @param content 内容 * @param mobiles 手机号,需要@的人 */ public void sendMarkDown(String title, String content, List<String> mobiles) { String url = genDingTalkUrl(); DingTalkClient client = new DefaultDingTalkClient(url); OapiRobotSendRequest req = new OapiRobotSendRequest(); req.setMsgtype("markdown"); OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); markdown.setTitle(title); markdown.setText(content); req.setMarkdown(markdown); OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); at.setAtMobiles(mobiles); req.setAt(at); try { OapiRobotSendResponse rsp = client.execute(req, ""); if(!rsp.isSuccess()) { log.error("rsp error, errorCode: [{}], errorMsg: [{}]", rsp.getErrcode(), rsp.getErrmsg()); }; } catch (ApiException e) { log.error("sendText error", e); } }结果展示
总结