Problem
Today I tried to write a GitHub Integration. The integration application receives commit messages from a registered repo and does some cool stuff. The messages are delivered via a webhook, registered with GitHub. To avoid parsing the messages myself, I found the package express-github-webhook by searching npms.io. The code for my server is very simple
1 | // requires skipped for brevity |
Great! To test how this is working out, I have started the server at
localhost:6001
and tunneled the messages from external temp address using
ngrok
service. For example the external website is relaying all messages
https://a1c5dd12.ngrok.io -> localhost:6001
.
Yet the server displayed an error as soon as I clicked "register", because GitHub tries to deliver a test message.
1 | listening on port 6001 |
Hmm, what is happening?
Investigation
Opening the offending 3rd party module file at that line shows the following code
1 | // parse payload |
Ok, but what is the payload in the GitHub test message? Let us print the
payloadData
before using it. I changed the code in
/node_modules/express-github-webhook/index.js
to print the object before
trying to access payloadData.repository
. Then I triggered the test message
POST again from GitHub UI.
1 | let payloadData = req.body; |
1 | listening on port 6001 |
There is no repository
field in the payload for the "ping" event!
Let us fix the crashing code.
1 | let payloadData = req.body; |
Trying delivering the event again - and GitHub is reporting success!
Fixing the problem
What do we do now? We need to deploy the integration server, so somehow we need to use the patched version right now. Luckily this is extremely easy with GitHub and NPM.
- Fork the original GitHub repo
Gisonrg/express-github-webhook
to my own bahmutov/express-github-webhook. - Fix the code in the fork. The change is so small, we can even use the
online editor. I describe my commits using semantic convention. This code got the commit SHA
1
git commit -m "fix(parsing): do not assume there is a repository"
e84170...
- Point the
express-github-webhook
NPM dependency at my GitHub repository. We can even leave the previous NPM registry version there, since the second key value overwrites the previous ones (it is how I write comments in my JSON files)1
2
3
4
5
6{
"dependencies": {
"express-github-webhook": "^1.0.5",
"express-github-webhook": "bahmutov/express-github-webhook"
}
} - Run the install and notice that we get our patch now. If we really wanted you could even specify particular commit SHA, like
1
2
3$ npm i
[email protected]
└── [email protected] (git://github.com/bahmutov/express-github-webhook.git#e84170fb7b1298fe033aa860595ee19ef6479a2e)"express-github-webhook": "bahmutov/express-github-webhook#e84170"
to be more precise and avoid accidentally using latest. - Test the GitHub "ping" event again - still works!
- Be a good Open Source Citizen and create a Pull Request from our patched repository back to the original one, you can see my pull request here Gisonrg/express-github-webhook/pull/2
And that's how Open Source and NPM make it simple to find, use and fix all the code around us.