Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node + Github webhook for user activities

I will explain my problem easily:

I want to interact with the github webhooks to fetch when my user (or a logged user) click star on a repo (should be this hook event).

I have a simply server with node + express but I really don't understand how perform this. Could someone help me?

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
});
like image 416
Maurizio Battaghini Avatar asked May 22 '26 06:05

Maurizio Battaghini


1 Answers

A quick test for this would be to use ngrok to make a local port available from the outside :

ngrok http 8080

Then create the hook using the API using the url given by ngrok and your personal access token. You can also build the webhook manually in your repo hook section https://github.com/$USER/$REPO/settings/hooks/ (selecting watch events) :

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

launch an http server listening on the port exposed with the POST endpoint you have specified :

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)
})

launch it :

node server.js

The server will now receive starring events

For debugging you can see the sent requests from Github in the hooks section :

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

enter image description here

like image 129
Bertrand Martel Avatar answered May 23 '26 22:05

Bertrand Martel