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 格式