likes
comments
collection
share

领导急了,那该如何在钉钉群中推送团队的过期任务?

作者站长头像
站长
· 阅读数 15

前言

最近部门要求大家下班前挪动jira中敏捷迭代的任务,但是团队中总有些同事忘记,然后第二天早上被通报批评,领导后面直接发话这些同事有可能成为年底的淘汰对象。我在想能否通过程序每天定时获取jira中的过期任务,然后通知到敏捷团队钉钉群中呢,这样团队成员就不会忘记了。

领导急了,那该如何在钉钉群中推送团队的过期任务?

Jira任务获取功能

jira是一个应用广发的项目管理工具,我们公司也使用这个工具去管理需求、任务、bug等。那么通过程序如何获取jira中的任务、bug信息呢?

  1. 引入jira-client依赖

我这边使用了一个开源的依赖包jira-client, maven坐标如下:

 <dependency>
    <groupId>net.rcarz</groupId>
    <artifactId>jira-client</artifactId>
    <version>0.5</version>
    <scope>compile</scope>
</dependency>

github地址:https://github.com/bobcarroll/jira-client

  1. 创建JiraClient的Bean

领导急了,那该如何在钉钉群中推送团队的过期任务?

@Configuration
@EnableConfigurationProperties({JiraAuthProperties.class, DingTalkProperties.class})
public class JiraConfig {

    @Autowired
    private JiraAuthProperties jiraAuthProperties;

    /**
     * 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账号和密码
  • 根据jira的地址和认证信息创建JiraClient
  1. 获取团队成员过期的任务和bug
/**
 * 获取所有过期的任务
 *
 * @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等

钉钉通知功能

我们现在已经从jira服务中获取到了哪些用户的任务过期了,那么该如何推送到钉钉群中呢?其实钉钉就开放了这样的能力,详细可以参考:https://open.dingtalk.com/document/robots/custom-robot-access

  1. 钉钉群中添加机器人

钉钉群设置中添加机器人,如下图所示,保存。

领导急了,那该如何在钉钉群中推送团队的过期任务?

  1. 编写推送代码
  • 钉钉配置处理

领导急了,那该如何在钉钉群中推送团队的过期任务?

@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() + "&timestamp={}&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);
    }
}

结果展示

现在我们已经完成了最核心的获取钉钉任务以及推送的功能,最后通过springboot的调度,每天临近下班,6点的时候每隔5分钟提醒大家要去处理jira中的过期任务。

领导急了,那该如何在钉钉群中推送团队的过期任务?

领导急了,那该如何在钉钉群中推送团队的过期任务?

总结

本文和大家简单分享了一种通过钉钉推送jira中过期任务的方法,其实我们还可以用它做更多的事情,比如统计缺陷数量,统计大家的周报等等。

本案例中的代码地址:https://github.com/alvinlkk/jira-job 欢迎关注个人公众号【JAVA旭阳】交流学习!