用于用户活动的 Node + Github webhook

Mau*_*ini 3 javascript github node.js github-api

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

我想与 github webhooks 交互以在我的用户(或登录用户)单击 repo 上的 star(应该是这个钩子事件)时获取。

我有一个带有 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
});
Run Code Online (Sandbox Code Playgroud)

Ber*_*tel 5

对此的快速测试是使用ngrok从外部提供本地端口:

ngrok http 8080
Run Code Online (Sandbox Code Playgroud)

然后使用urlngrok 给定的 API和您的个人访问令牌创建挂钩。你也可以在你的 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
Run Code Online (Sandbox Code Playgroud)

启动一个 http 服务器,侦听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)
})
Run Code Online (Sandbox Code Playgroud)

启动它:

node server.js
Run Code Online (Sandbox Code Playgroud)

服务器现在将接收主演事件

对于调试,您可以在 hooks 部分查看来自 Github 的发送请求:

https://github.com/$USER/$REPO/settings/hooks/
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明