Node + Github Webhook 用于用户活动

3
我将简单地解释我的问题:
我想要与GitHub Webhooks交互,以便在我的用户(或已登录的用户)单击存储库上的星标时获取信息(应该是此 钩子事件)。
我有一个使用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个回答

5

一个快速测试的方法是使用ngrok将本地端口暴露给外部:

ngrok http 8080

然后使用ngrok提供的url和您的个人访问令牌,使用API创建钩子。您还可以在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

启动一个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)
})

启动它:

node server.js

服务器现在将接收主角事件。

为了调试,您可以在钩子部分查看来自Github的已发送请求:

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

enter image description here


谢谢 @Bertrand Martel,但这会将挂钩锁定到一个 repo 上,对吧?我想要“挂钩”我的用户活动。当我给一个随机的 repo 点赞时,我想要触发这个钩子。 - Maurizio Battaghini
1
从 API Webhook 页面 中可以看到:“每个 Webhook 都可以安装在一个组织或特定仓库上”,因此我认为没有针对个人用户事件的该类特性可用。即使是 PubSubHubbub 服务器订阅也只处理仓库事件。 - Bertrand Martel
所以,基本上我不能监控我的用户在做什么,对吗?如果我把我的用户转化为一个组织,会怎样呢? - Maurizio Battaghini
据我所知,组织无法为仓库点赞/关注用户,但您仍然可以轮询user events api - Bertrand Martel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接