二十八分鍾,帶你用gitlab向企業微信發出靈魂拷問
例如:
是不是想着想着,就下班了呢?哈哈。由於像靈魂拷問這樣的神問題數量有限,
下面也有播報新聞版的,這樣就不怕提交次數多了看見重復的拷問~
材料准備
gitlab一個,企業微信一個,能隨意部署應用的機器一台(用來翻譯webhook)。
WebHook
webhook拿大白話來說就是事后事件。當一個動作完成后,鈎子函數也好,事件下發也好,
總而言之,就是讓我這個動作做完了,要通知告訴下一個人。例如當你測試給你提了一個新bug的時候,可以利用webhook發送消息給oa去@你處理。當有人提交代碼之后,可以集成Jenkins自動構建。
如何在gitlab配置WebHook
-
本文以我使用的GitLab Community Edition 11.8.1 版本為例,介紹如何配置一個當代碼push完成后的webhook事件
-
打開integrations,在URL中配置好webhook地址,和需要觸發推送的分支。一般dev分支提交很頻繁,沒有必要推送消息。可以根據實際情況動態選擇。
-
格式轉換,gitlab的push webhook格式如下,和企業微信api能接收的肯定是不一樣的。所以需要在中間做一層轉換,或者加上一些自定義的東西。在后面會講到。
{
"object_kind":"push",
"event_name":"push",
"before":"1295d86467192337c379bc39e8fdc296c774a2bd",
"after":"30d073745fba51fe1858caf0859abffa401955eb",
"ref":"refs/heads/test",
"checkout_sha":"30d073745fba51fe1858caf0859abffa401955eb",
"message":null,
"user_id":19,
"user_name":"xiaoxiong",
"user_username":"xiaoxiong",
"user_email":"",
"user_avatar":"http://gitlab.***.local/uploads/-/system/user/avatar/19/avatar.png",
"project_id":65,
"project":{
"id":65,
"name":"OMS",
"description":"***",
"web_url":"http://gitlab.***.local/product-and-sales-group/oms",
"avatar_url":null,
"git_ssh_url":"ssh://git@gitlab.***.local:30022/product-and-sales-group/oms.git",
"git_http_url":"http://gitlab.***.local/product-and-sales-group/oms.git",
"namespace":"product-and-sales-group",
"visibility_level":0,
"path_with_namespace":"product-and-sales-group/oms",
"default_branch":"test",
"ci_config_path":null,
"homepage":"http://gitlab.***.local/product-and-sales-group/oms",
"url":"ssh://git@gitlab.***.local:30022/product-and-sales-group/oms.git",
"ssh_url":"ssh://git@gitlab.***.local:30022/product-and-sales-group/oms.git",
"http_url":"http://gitlab.***.local/product-and-sales-group/oms.git"
},
"commits":[
{
"id":"30d073745fba51fe1858caf0859abffa401955eb",
"message":"代碼優化
",
"timestamp":"2021-04-05T03:30:34Z",
"url":"http://gitlab.***.local/product-and-sales-group/oms/commit/30d073745fba51fe1858caf0859abffa401955eb",
"author":{
"name":"xiaoxiong",
"email":"xiaoxiong@***.com"
},
"added":[
],
"modified":[
"oms-service/src/main/java/com/****/oms/OmsServiceApp.java"
],
"removed":[
]
}
],
"total_commits_count":1,
"push_options":[
],
"repository":{
"name":"OMS",
"url":"ssh://git@gitlab.***.local:30022/product-and-sales-group/oms.git",
"description":"***",
"homepage":"http://gitlab.***.local/product-and-sales-group/oms",
"git_http_url":"http://gitlab.***.local/product-and-sales-group/oms.git",
"git_ssh_url":"ssh://git@gitlab.***.local:30022/product-and-sales-group/oms.git",
"visibility_level":0
}
}
其他的事件參數可以參見gitlab官方文檔:https://docs.gitlab.com/ee/user/project/integrations/webhooks.html
如何在企業微信中添加一個機器人
和微信一樣,可以先拉兩個人建一個群,然后再把他們兩個踢掉。這樣,你就得到了一個只有你一個人的群了。然后就可以右鍵群聊,添加機器人,進行隨意的測試了~
ok,現在兩邊的webhook都已經探索了一遍,現在要做的就是寫一個中間翻譯人的應用了。讓gitlab和企業微信能夠聽懂雙方的對話~
webhook翻譯應用
-
利用idea自動生成一個SpringBoot應用,雖然有點大材小用,但是方便,快捷~
整個應用的結構如圖,就兩個類,一個controller監聽gitlab的消息,一個HttpUtil工具類用來請求新聞接口,獲取新聞簡訊給每一次的push消息。
-
本來是手動搬運了一堆靈魂拷問來生成消息的,但是小伙伴們的代碼提交三下五除二就把這些靈魂拷問給用完了。然后找了幾個笑話的api,發現都不太好笑,不好笑也就算了,有的還要收費~最后用的是天行數據的IT資訊API,更新不是很及時,但也夠用。
try {
if (newsQueue.isEmpty()) {
String newsrst = HttpClientUtil.doGet("http://api.tianapi.com/it/index?key=******&num=50");
JSONObject newsjson = JSON.parseObject(newsrst);
for (Object object : newsjson.getJSONArray("newslist")) {
String news = String.format("[%s](%s)",((JSONObject)object).getString("title"),((JSONObject)object).getString("url"));
newsQueue.add(news);
}
}
head = "【" + name + "】播報了一條新聞:" + newsQueue.poll();
} catch (Exception e) {
head = "【" + name + "】";
e.printStackTrace();
}
每次放50條新聞到隊列里面,當用完了再去取新的新聞。這樣新聞就不會重復了。(api接口的新聞更新有點少,如果提交次數過多,新聞更新會跟不上提交代碼的速度。這樣就需要多調幾個api,多拿幾個新聞源了。另外如果提交次數過少,也會造成新聞更新不及時。要根據實際情況來適配~)
最終效果,實現提交代碼自動push消息到企業微信
需要獲取翻譯應用源代碼的小伙伴,可以關注公眾號【小熊的進階之路】回復“webhook”即可獲取;