WebHook broker that accepts notifications from multiple platforms and performs simple actions in response
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

main.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. elif event["type"] == "slack":
  24. services["reportbot"].announce(
  25. f"\002[{event['source']}]\002 {event['text']}"
  26. )
  27. @app.route("/")
  28. def handle_index():
  29. return app.send_static_file("index.html")
  30. @app.route("/hooks/<service>/<path:identifier>/<hash>", methods=["GET", "POST"])
  31. def handle_hook(service, identifier, hash):
  32. app.logger.info(f"Received hook for {service} with identifier {hash}")
  33. expected_hash = get_hook_key(service, identifier)
  34. if hash != expected_hash:
  35. app.logger.info(f"Hash not valid. Expected: {expected_hash}, got {hash}")
  36. abort(403)
  37. if service not in services:
  38. app.logger.info(f"Unknown service {service}, known: {services.keys()}")
  39. abort(404)
  40. handle_events(services[service].accept_hook(identifier, request) or [])
  41. return "", 204
  42. if __name__ == "__main__":
  43. app.run("0.0.0.0")