同事代码忘了提交svn 结局崩溃了

272 阅读2分钟

秘密任务

因为某个前端同事电脑代码没有及时提交svn,然后第二天上班,发现电脑开机不了,因为这个项目的前端代码都是他写的,他就没有每天都提交到svn,最后他加班通宵把代码补齐了,当天公司就安排了一个任务给运维,说每天下班前,检查下每个人的代码有没有提交到svn,公司50多个人,这让运维有点头疼了,于是,运维找到我,问我有没有好办法,让他能不能偷偷懒,回去多撸几把王者!

提交到钉钉群的代码

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

java编译成class文件

svn 提交代码后 通知钉钉群

配置 Post-commit hook,运行java代码,推送到钉钉群

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 -classpath E:\java Request 钉钉机器人token %CONTENT%

参考

svn版本管理详解(www.kancloud.cn/i281151/svn…)

SVN提交后自动推送消息到钉钉群(www.cnblogs.com/jianxuanbin…)