GitHub配置WebHook

2021年第一篇博客,给大家分享一个:
作为程序猿让老板每天都知道你写了个什么bug
作为leader 及时知道团队工作进度的好方法

GitHub配置WebHook

配置:

选择需要配置Webhook的项目,点击setting 找到webhook,并且配置以下参数:

  • Payload URL:接受请求的地址(内网穿透工具参考
    https://ngrok.com/download)
  • Content type:请求参数的类型(application/json)
  • Secret:配置密钥(建议生成随机8-16位的字符)
  • 触发事件:勾选 just the push event(仅push事件触发)
    在这里插入图片描述

测试:

Push代码后,可看到以下触发记录
在这里插入图片描述

验签:

代码可参考如下地址:

**
 * 将一个字节转化成十六进制形式的字符串
 *
 * @param b 字节数组
 * @return 字符串
 */
private static String byteToHexString(byte b) {
    int ret = b;
    if (ret < 0) {
        ret += 256;
    }
    int m = ret / 16;
    int n = ret % 16;
    return hexDigits[m] + hexDigits[n];
}

/**
 * 转换字节数组为十六进制字符串
 *
 * @param bytes 字节数组
 * @return 十六进制字符串
 */
private static String byteArrayToHexString(byte[] bytes) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < bytes.length; i++) {
        sb.append(byteToHexString(bytes[i]));
    }
    return sb.toString();
}

/**
 * HmacSHA256 加密
 * @param message
 * @param secret
 * @return
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
private static byte[] computeSha256(String message, String secret)
        throws NoSuchAlgorithmException, InvalidKeyException {
    byte[] secretKey = secret.getBytes();
    SecretKeySpec signingKey = new SecretKeySpec(secretKey, "HmacSHA256");
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(signingKey);
    byte[] stringBytes = message.getBytes(Charset.forName("UTF-8"));
    byte[] hashedValue = mac.doFinal(stringBytes);
    return hashedValue;

}

/**
 * github 请求签名
 * @param data
 * @param secret
 * @return
 * @throws InvalidKeyException
 * @throws NoSuchAlgorithmException
 */
public static String githubSign(String data,String secret) throws InvalidKeyException, NoSuchAlgorithmException {
    byte[] hashedValue = computeSha256(data,secret);
    return byteArrayToHexString(hashedValue);
}

注:将Map转换成JSON的时候,要保留空值
Java示例:

String toJSON = JSON.toJSONString(map, SerializerFeature.WRITE_MAP_NULL_FEATURES, SerializerFeature.QuoteFieldNames);

通知:

  • 添加企业微信机器人
  • 点击群聊,右键添加群机器人
  • 获取需要发送通知的地址
**
 * 企业微信群机器人
 * @param sendPath 企业微信机器人地址
 * @param type 文档类型:markdown、text ...
 * @param msg 发送消息
 */
public String sendWeChatRobotMsg(String sendPath, String type, String msg) {
    JSONObject content = new JSONObject();
    content.put("content", msg);

    JSONObject reqBody = new JSONObject();
    reqBody.put("msgtype", type);
    reqBody.put(type, content);

    String result = OkHttpUtils.doJsonPost(sendPath, reqBody);
    return result;
}

注:具体正文内容,可自由选择,也可采用markdown 格式

效果:

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值