[釘釘通知系列]SVN提交后自動推送消息到釘釘群


釘釘設置機器人配置

1、進入配置機器人入口

2、添加機器人





3、測試WebHook請求

本人使用Postman進行測試

4、配置SVN


4.1 配置 Pre-commit hook

  • 設置提交內容必須包含注釋
  • 配置參數
@echo off
setlocal
set REPOS=%1
set TXN=%2
rem check that logmessage contains at least 10 characters
svnlook log %REPOS% -t %TXN% | findstr "....." > nul
if %errorlevel% gtr 0 goto err
exit 0
:err
echo 上傳失敗!請添加注釋. 注釋長度至少為5個字符. Commit aborted! 1>&2
exit 1

4.2 配置 Post-commit hook

set REPOS=%1
set REV=%2
set tttt=%date:~0,10% %time:~0,8%
for /f "tokens=1,2 delims=:" %%a in ('svnlook author -r %REV% %REPOS%') do (
    if not defined AUTHOR set AUTHOR=%%a
)
for /f "tokens=1,2 delims=:" %%a in ('svnlook dirs-changed %REPOS%') do (
    if not defined CHANGEDDIRS set CHANGEDDIRS=%%a
)
for /f "tokens=1,2 delims=:" %%a in ('svnlook log -r %REV% %REPOS%') do (
    if not defined MESSAGE set MESSAGE=%%a
)
set CONTENT="提交時間:%tttt% \n提交版本:%REV% \n作者:%AUTHOR%\n提交備注:%MESSAGE%\n修改目錄:%CHANGEDDIRS% "
java -cp D:\svnHook.jar com.wolf.util.Request 釘釘令牌 %CONTENT%

5 配置Java請求文件

由於釘釘提供的接口是https協議,curl需要支持https,因此通過java代碼發起Post請求,打包成可運行的jar,然后用post-commit hook調用,傳入信息即可。

package com.wolf.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class Request {
	public static void main(String[] args) throws Exception {
		String token = args[0];
		String content = args[1];
		content = "{\"msgtype\": \"text\",\"text\": {\"content\": \""+content+"\"}}";
		httpsRequest("https://oapi.dingtalk.com/robot/send?access_token="+token, "POST", content);
		System.out.println("OK");
		System.exit(0);
	}

	/**
	 * 發送https請求
	 */
	public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) throws Exception {
		HttpsURLConnection conn = null;
		BufferedReader bufferedReader = null;
		try {
			URL url = new URL(requestUrl);
			conn = (HttpsURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod(requestMethod);
			conn.setRequestProperty("content-type", "application/json");
			if (null != outputStr) {
				OutputStream outputStream = conn.getOutputStream();
				outputStream.write(outputStr.getBytes("utf-8"));
				outputStream.close();
			}
			bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			String str = null;
			StringBuffer buffer = new StringBuffer();
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			return buffer.toString();
		} catch (Exception e) {
			throw e;
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
				}
			}
		}
	}
}

結果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM