WebHook broker that accepts notifications from multiple platforms and performs simple actions in response
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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. 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"][::-1][:3]:
  16. line = commit["message"].split("\n")[0][:100]
  17. services["reportbot"].announce(
  18. f"\002[git]\002 {commit['id']}: {line}"
  19. )
  20. elif event["type"] == "docker.push":
  21. services["reportbot"].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["reportbot"].announce(
  26. f"\002[{event['source']}]\002 {event['text']}"
  27. )
  28. @app.route("/")
  29. def handle_index():
  30. return app.send_static_file("index.html")
  31. @app.route("/hooks/<service>/<path:identifier>/<hash>", methods=["GET", "POST"])
  32. def handle_hook(service, identifier, hash):
  33. app.logger.info(f"Received hook for {service} with identifier {hash}")
  34. expected_hash = get_hook_key(service, identifier)
  35. if hash != expected_hash:
  36. app.logger.info(f"Hash not valid. Expected: {expected_hash}, got {hash}")
  37. abort(403)
  38. if service not in services:
  39. app.logger.info(f"Unknown service {service}, known: {services.keys()}")
  40. abort(404)
  41. handle_events(services[service].accept_hook(identifier, request) or [])
  42. return "", 200
  43. if __name__ == "__main__":
  44. app.run("0.0.0.0")