javascript - 用于用户事件的 Node + Github webhook

标签 javascript node.js github github-api

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

我想与 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
});

最佳答案

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

ngrok http 8080

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

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

服务器现在将接收星标事件

为了调试,你可以在 hooks 部分看到来自 Github 的发送请求:

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

enter image description here

关于javascript - 用于用户事件的 Node + Github webhook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46957746/

相关文章:

PHP:Websocket 和 html5

Node.js Express : BodyParser Syntax

laravel - 无法以编程方式在 GitHub 中创建标签 - ISO8601 时间戳无效

java - 从 Selenium 测试 onbeforeunload 事件

node.js - 类型错误 : require(. ..) 不是 SendGrid NodeJS 中的函数

javascript - 如何通过命令行告诉 Jest 不要运行某个测试文件,而是运行所有其他文件?

git - 有没有办法使用逻辑 AND 在 GitHub 操作中组合推送标准?

git - 删除目标分支后,现有 pull 请求会发生什么情况?

javascript - 如何使用 javascript 读取包含(管道分隔)值的 .psv 文件数据

javascript - Async/Await 函数永远不会通过 Dispatch 解决