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 2.0KB

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