美文网首页
gitlab PR 通知企业微信群nodejs快速实现

gitlab PR 通知企业微信群nodejs快速实现

作者: Amitabha2 | 来源:发表于2020-07-28 11:06 被阅读0次

nodejs+express+request

  • 环境搭建
  • index.js
const express = require('express')

const bodyParser = require('body-parser')
const request = require('request');

const app = express()
const port = 3000



app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

app.get('/', (req, res) => {

    res.send('Hello World!' + JSON.stringify(req.query))
})

app.post('/', (req, res, next) => {
    if (req.body.object_kind == "merge_request") {
        var actionMsg = "提交请求"
        var sendUser;
        var title;
        if (req.body.object_attributes.action=="open") {
            actionMsg = "提交请求";
        }
        if (req.body.object_attributes.action=="merge") {
            actionMsg = "已合并请求";
            sendUser = "@all";
        }

        if (req.body.object_attributes.action=="close") {
            actionMsg = "撤销合并请求";
            sendUser = "@all";
        }
        title = req.body.object_attributes.title;
        var source = req.body.object_attributes.source_branch;
        var target = req.body.object_attributes.target_branch;
        var lastUrl = req.body.object_attributes.last_commit.url;
        var lastMsg = req.body.object_attributes.last_commit.message||"无";
        var targetUserName = req.body.user.name;
        var targetUserLoginName = req.body.user.username;
        var mgDesc = req.body.object_attributes.description||"无";
        var mgUrl = req.body.object_attributes.url;
        var projectName = req.body.project.name;
        msgDesc = mgDesc.replace(/\"/g,"").replace(/\"\'/g,"");
        lastMsg = lastMsg.replace(/\"/g,"").replace(/\"\'/g,"");

        request.post(
            {url:'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=企业微信机器人key', 
                json: true,
                headers: {
                    "content-type": "application/json",
                },
                body: {
                    "msgtype": "markdown",
                    "markdown": {
                        "content": "请求合并["+ projectName +"]("+mgUrl+")\n"+
                            ">动作:<font color=\"warning\">" + actionMsg + "</font>\n"+
                            ">合并分支:<font color=\"comment\">"+ source + "</font> => <font color=\"comment\">"+ target + "</font>\n"+
                            ">发起人:"+targetUserName+"<font color=\"comment\">"+targetUserLoginName+"</font>\n"+
                            ">描述:<font color=\"comment\">" + mgDesc + "</font>\n"+
                            ">最后提交:[" + lastMsg + "]("+ lastUrl +")"
                    }
                }
        }, function (error, response, body) { 

        })
    }
    

    console.log(req.body)
    console.log(req.query);
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))
  • 使用forever start index.js后台运行
  • gitlab配置webhooks合并请求事件触发
  • 完成

相关文章

网友评论

      本文标题:gitlab PR 通知企业微信群nodejs快速实现

      本文链接:https://www.haomeiwen.com/subject/luxfrktx.html