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

釘釘設置機器人配置

一、進入配置機器人入口

二、添加機器人





三、測試WebHook請求

本人使用Postman進行測試

java

四、配置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調用,傳入信息便可。web

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) {
                }
            }
        }
    }
}

結果以下:
json

相關文章
相關標籤/搜索