Open In App

Google Cloud Platform – Designing an Issues Notification System using Cloud Run

Improve
Improve
Like Article
Like
Save
Share
Report

Problem Statement: Consider your run a Software company. And you have a huge client list who frequently use your software. You host your software on Github so that its users can send information regarding bugs in them. You being a lead developer in the team, how will you design a system that notifies the development team on Slack whenever an issue is submitted by a user on GitHub?

GitHub is one of the many services that provide event-driven Webhooks. This means is that if someone files an issue, Github will send a request to a URL specified by the developer. To ensure that if someone files an issue in GitHub, the developer team is notified and the issue isn’t lost in their inbox.

We can use Cloud Run to host the new Webhook target. This means developers won’t have to worry about servers, and they can use the language and framework that they are comfortable with.

The first thing you have to worry about is authorization. It would look somewhat like below:

Python3




@app.route("/" , methods = [ "POST" ])
 
def index():
  make_request = request
  signature = make_request.headers.get("X-Hub-Signature")
  data = make_request.data
   
  assert verify_signature(signature, data)


A Webhook target is a public URL, and anyone with the URL could send anything to it. To ensure you are only processing valid requests, you can make use of a shared secret. You will need to create a secret key and put one copy in the GitHub configuration and store a copy securely with the Cloud Secret Manager. These secrets would look something like below depending upon your need:

$ SECRET = "$(python3 -c 'import secrets
    print(secrets.token_hex(20))')"
    
$ echo - $SECRET | cloud beta secrets
create github-secret
--replication-policy = automatic
--data-file=-

Every request from GitHub is signed, so you can use the secret to validate the signature and only process the valid requests. A generics signature verification function is given below, it may change depending upon your verification requirements:

Python3




def verify_signature(signature, data):
  expected = "sha1="
  try:
    secret = get_secret(PROJECT, SECRET, "1")
    hashed = hmac.new(secret, data, sha1)
    expected += hashed.hexdigest()
     
  except Exception as error:
    print("ERROR")
     
  return hmac.compare_digest (signature)


Now development team will be able to securely receive events from GitHub, and a developer can use these events to send messages to Slack using the Slack API. A developer at this will write a function to send a message to the team’s Slack channel every time a valid request from GitHub is received. A sample function would look like below:

Python3




def slack_message( issue_title, issue_url):
  message = format_msg(issue_title, issue_url)
  Request = urllib.request.Request( SLACK_URL, data = message)
  Request.add_header("Content-Type", "application/json")
  urllib.request.urlopen(Request)


Your team will never miss another issue. To measure how long it takes the team to close an issue. You can add one more function to the Webhook to stream any issue events to BigQuery so it can easily create metrics around the team’s issue performance. The full Webhook target is now creating notifications, triggering an action, and streaming data into BigQuery.



Last Updated : 21 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads