代码之家  ›  专栏  ›  技术社区  ›  Maurizio Battaghini

用于用户活动的节点+Github webhook

  •  3
  • Maurizio Battaghini  · 技术社区  · 8 年前

    我会很容易地解释我的问题:

    我想在我的用户(或登录用户)单击回购协议上的star时与github webhooks进行交互以获取(应该是这样的) hook event

    我有一个简单的带有node+express的服务器,但我真的不知道如何执行此操作。有人能帮我吗?

    const chalk = require('chalk');
    const express = require('express');
    const serverConfig = require('./config/server.config');
    
    const app = express();
    
    const port = process.env.PORT || serverConfig.port;
    
    console.log(chalk.bgGreen(chalk.black('###   Starting server...   ###'))); // eslint-disable-line
    
    app.listen(port, () => {
      const uri = `http://localhost:${port}`;
      console.log(chalk.red(`> Listening ${chalk.white(serverConfig.env)} server at: ${chalk.bgRed(chalk.white(uri))}`)); // eslint-disable-line
    });
    
    1 回复  |  直到 8 年前
        1
  •  5
  •   Bertrand Martel    8 年前

    对此的快速测试是使用 ngrok

    ngrok http 8080
    

    然后使用API和 url 由ngrok和您的个人访问令牌提供。您还可以在repo hook部分手动构建webhook https://github.com/ $USER/$REPO/settings/hooks/(选择) watch 事件):

    curl "https://api.github.com/repos/bertrandmartel/speed-test-lib/hooks" \
         -H "Authorization: Token YOUR_TOKEN" \
         -d @- << EOF
    {
      "name": "web",
      "active": true,
      "events": [
        "watch"
      ],
      "config": {
        "url": "http://e5ee97d2.ngrok.io/webhook",
        "content_type": "json"
      }
    }
    EOF
    

    POST 您指定的端点:

    const express = require('express')
    const bodyParser = require('body-parser')
    const app = express()
    const port = 8080;
    
    app.use(bodyParser.json());
    
    app.post('/webhook', function(req, res) {
        console.log(req.body);
        res.sendStatus(200);
    })
    
    app.listen(port, function() {
        console.log('listening on port ' + port)
    })
    

    启动它:

    node server.js
    

    服务器现在将接收明星活动

    为了进行调试,您可以在hooks部分中看到Github发送的请求:

    https://github.com/$USER/$REPO/settings/hooks/
    

    enter image description here