WebHook broker that accepts notifications from multiple platforms and performs simple actions in response
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from flask import Flask, abort, request
  2. from lineandsinker.common import get_hook_key
  3. from lineandsinker.services import services
  4. for service in services.values():
  5. service.refresh()
  6. app = Flask(__name__)
  7. def handle_events(events):
  8. for event in events:
  9. if event["type"] == "git.push":
  10. services["jenkins"].build_job_by_scm_url(event["repo"]["urls"])
  11. if event["repo"]["public"]:
  12. services["reportbot"].announce(
  13. f"\002[git]\002 {event['user']} pushed {len(event['commits'])} commit{'s' if len(event['commits']) != 1 else ''} to {event['repo']['name']}: {event['compare_url']}"
  14. )
  15. for commit in event["commits"][:3]:
  16. services["reportbot"].announce(
  17. f"\002[git]\002 {commit['id']}: {commit['message'][:100]}"
  18. )
  19. elif event["type"] == "docker.push":
  20. services["reportbot"].announce(
  21. f"\002[registry]\002 New manifest pushed to {event['host']}/{event['repo']}:{event['tag']} by {event['user']}"
  22. )
  23. @app.route("/")
  24. def handle_index():
  25. return app.send_static_file("index.html")
  26. @app.route("/hooks/<service>/<path:identifier>/<hash>", methods=["GET", "POST"])
  27. def handle_hook(service, identifier, hash):
  28. app.logger.info(f"Received hook for {service} with identifier {hash}")
  29. expected_hash = get_hook_key(service, identifier)
  30. if hash != expected_hash:
  31. app.logger.info(f"Hash not valid. Expected: {expected_hash}, got {hash}")
  32. abort(403)
  33. if service not in services:
  34. app.logger.info(f"Unknown service {service}, known: {services.keys()}")
  35. abort(404)
  36. handle_events(services[service].accept_hook(identifier, request) or [])
  37. return "", 204
  38. if __name__ == "__main__":
  39. app.run("0.0.0.0")