• Git操作自动触发企业微信机器人webhook


    [本文出自天外归云的博客园]

    背景

    在git做一些merge或push的操作,我们希望可以自动在企业微信群发送自定义的通知。

    服务代码

    这里选用php作为网络服务的开发语言,关键的代码如下(githook函数就是对应webhook的服务函数):

    <?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自动企业微信报告服务等githook服务
        // 请求路径: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(触发条件)-Add Web Hook(把自己的网络服务请求地址填上去,也就是上面的githook函数的请求地址):

    请求url带的参数key为企业微信机器人的webhook地址(在企业微信群创建企业微信机器人后即可看到该地址)。

    至此就可以在指定trigger被触发(比如有人进行了push操作)时,自动发送你服务函数中自定义的消息体到指定webhook的企业微信群。

    注意:git操作触发的消息内容在请求的post body中,而我们自己传的key在请求的get参数中。

  • 相关阅读:
    idea 中main 方法运行错误二
    shiro 忘记加密后的密码怎么办?
    idea 中 Caused by: java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener
    Date 类型的比较
    oracle中数据类型对应java类型[转载]
    mybitis面试题基础必考 Mybatis 中$与#的区别
    301
    201
    0
    Apache负载 均衡
  • 原文地址:https://www.cnblogs.com/LanTianYou/p/10395678.html
Copyright © 2020-2023  润新知