gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 00:28:44 24 4
gpt4 key购买 nike

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

我想与 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com