Skip to content

Latest commit

 

History

40 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project goal

The goal of that project is to handle releases from github webhook and send it to a Gchat channel.

That project is mainly inspired from worker-slack-bot.

How to deploy?

Clone the repo and use wrangler publish. If you do not know wrangler please follow the guide here.

Deploy to Cloudflare Workers

Then follow requirements described for the deployment option you want to go to:

Additionaly you can follow logging instruction here to monitor any error raised by your workers.

How it works?

🔗 Deployment as a webhook

That deployment means you need to enter the webhook URL in Github that point to your Cloudflare Workers. You are leveraging the path /webhook and all the query string parameters expected by thta deployment (described below). In that mode, the Cloudflare Workers is a middelware to adapt the webhhok request sent by github into a webhook message understood by Google Chat.

Requirements in webhook mode:

The Workers has a path /webhook that need to be used. In addition to that the Workers is using the google webhook schema. The current logic is used: https://chat.googleapis.com/v1/spaces/<SPACE-ID>/messages?key=<KEY>&token=<TOKEN>

The SPACE-ID, KEY and TOKEN has to be sent in order to handle the destination correctly

The following params are available:

  • space: defined the space-id destination in Google.
  • key: defined the key for the Gchat destination.
  • token: defined the token for the Gchat destination

Here an example of the final URL you have to configure in github to get webhook in GChat: https://github2gchat-webhook.<subdomain>.workers.dev/webhook?space=<SPACE-ID>&key=<KEY>&token=<TOKEN>

🔎 Deployment as a monitoring mode

The monitoring mode leverage KV storage in Cloudflare and Workers Cron jobs in order to monitor specific RSS feed and post any update in a Gchat via a webhooks.

Requirements in webhook mode:

In order to use that mode, you have to:

  1. Configure a KV store and bind it to your workers: wrangler kv:namespace create KV
  2. Configure the structure use by the Workers in order to monitor an RSS feed. Here an example: wrangler kv:key put --binding=KV "webhooks" '[{"name": "Terraform Provider Cloudflare", "source": "https://github.com/cloudflare/terraform-provider-cloudflare/releases.atom", "destinations": ["https://chat.googleapis.com/v1/spaces/<SPACE_ID>/messages?key=<KEY>&token=<TOKEN>"]}]' Please not that the url should not be URL encoded, menaing you need to remove any excape characters like \ and replace %3D by = for example. More info here. Otherwise the escape charactere is breaking the JSON object.
  3. Make sure you are using htmlparser2 dependency. Run npm install.
  4. (if needed) Change the default Cron triggers (set to every minutes in the wrangler.toml file) as described in the docs here

The following structure has to be defined in the KV store:

  • KV key: webhooks
  • KV Value: an Array containing a JSON object with:
    • name: name of the webhook
    • source: RSS feed to monitor in my example I am taking RSS feed directly from github.
    • destinations: Array of Google Chat webhook on which any update are posted.

Note: There is a static test in order to removed scheduled events part of the cloudflare incident. If you want to received those events you have to create a new webhook name with scheduled inside.

RSS feed example supported by Github:

/* Repo releases */

https://github.com/:owner/:repo/releases.atom

/* Repo commits */

https://github.com/:owner/:repo/commits.atom

/* Private feed (You can find Subscribe to your news feed in dashboard page after login) */

https://github.com/:user.private.atom?token=:secret

/* Repo tags */

https://github.com/:user/:repo/tags.atom

/* User activity */

https://github.com/:user.atom

🔔 Logging via Sentry

The project use Sentry as logging endpoint for every errors.

Requirements for logging:

To redirect those error correctly you need to set-up Sentry_Key and Sentry_ID (if not specify no logging would be done). The steps are the following:

  • wrangler secret put Sentry_Key: Defined your Sentry Key
  • wrangler secret put Sentry_ID: Defined your Sentry ID

Test locally

If you want to test locally, you have to follow the steps below:

  1. Create a file .dev.vars at the root with the variable Sentry_Key and Sentry_ID.
  2. Run the project with wrangler dev

Final note

The current version of the webhook mode only handle released as an action as per the github doc here.

When you add the webhook (in webhook mode), the Workers handles ping event and write a message in the destination chat channel.

Enjoy! 🎉

About

Cloudflare Workers to handle github webhook to GChat webhook

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages