當(dāng)前位置:首頁 > IT技術(shù) > 微信平臺 > 正文

Git操作自動觸發(fā)企業(yè)微信機器人webhook
2021-07-29 19:44:11

?

背景

在git做一些merge或push的操作,我們希望可以自動在企業(yè)微信群發(fā)送自定義的通知。

服務(wù)代碼

這里選用php作為網(wǎng)絡(luò)服務(wù)的開發(fā)語言,關(guān)鍵的代碼如下(githook函數(shù)就是對應(yīng)webhook的服務(wù)函數(shù)):

<?php
class tools extends CI_Controller
{
    function __construct()
    {
        parent::__construct(false);
        $this->load->helper('url');
        $dir = APPPATH . "config/conf";
        $confFile = "{$dir}/autotestconf.json";
        $this->load->library('conffile');
        $this->confData = $this->conffile->getConfData($confFile);
        $this->nav_top = $this->conffile->get_nav_top($this->confData);
        $this->load->database();
        $this->load->model("tools/tools_model");
    }

    // 代碼CodeReview自動企業(yè)微信報告服務(wù)等githook服務(wù)
    // 請求路徑:http://localhost/cloud/tools/githook
    function githook()
    {
        $key = $this->input->get('key');
        $post_data = file_get_contents("php://input");
        $post_data_std_class = json_decode($post_data);
        $curl = curl_init();
        if ($post_data_std_class->object_kind == "merge_request") {
            if ($post_data_std_class->object_attributes->target_branch != "master") {
                return;
            }
            $commitUrl = $post_data_std_class->object_attributes->url;
            $postFields = "{
    "msgtype": "text",
    "text": {
        "content": "" . $post_data_std_class->user->username . " " . $post_data_std_class->object_attributes->action . " Merge Request " . $commitUrl . "

From " . $post_data_std_class->object_attributes->source_branch . " To " . $post_data_std_class->object_attributes->target_branch . "
Title: " . $post_data_std_class->object_attributes->title . "
Description:
" . $post_data_std_class->object_attributes->description . ""
    }
}";
        } else if ($post_data_std_class->object_kind == "push") {
            $branch = substr($post_data_std_class->ref, 11);
            if ($branch != "master") {
                return;
            }
            $commitMessage = "【".$post_data_std_class->commits[0]->message."】";
            $http_url = substr($post_data_std_class->repository->git_http_url, 0, -4);
            $commitUrl = $http_url . "/commits/" . $branch;
            $postFields = "{
    "msgtype": "text",
    "text": {
        "content": "" . $post_data_std_class->user_name . " Push To " . $commitUrl . "

" . $commitMessage . " "
    }
}";
        }
        curl_setopt_array($curl, array(
            CURLOPT_URL => "http://in.qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" . $key,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $postFields,
            CURLOPT_HTTPHEADER => array(
                "Cache-Control: no-cache",
            ),
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            echo $response;
        }
    }
}

Git配置

在git項目Setting-Advanced Settings-Web Hooks中勾選Trigger(觸發(fā)條件)-Add Web Hook(把自己的網(wǎng)絡(luò)服務(wù)請求地址填上去,也就是上面的githook函數(shù)的請求地址):

Git操作自動觸發(fā)企業(yè)微信機器人webhook_webhook

請求url帶的參數(shù)key為企業(yè)微信機器人的webhook地址(在企業(yè)微信群創(chuàng)建企業(yè)微信機器人后即可看到該地址)。

至此就可以在指定trigger被觸發(fā)(比如有人進(jìn)行了push操作)時,自動發(fā)送你服務(wù)函數(shù)中自定義的消息體到指定webhook的企業(yè)微信群。

注意:git操作觸發(fā)的消息內(nèi)容在請求的post body中,而我們自己傳的key在請求的get參數(shù)中。

?

?
?
?
?

本文摘自 :https://blog.51cto.com/u

開通會員,享受整站包年服務(wù)立即開通 >