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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import hashlib
  2. import re
  3. import socket
  4. from functools import wraps
  5. import jenkins
  6. import requests
  7. import os
  8. from bs4 import BeautifulSoup
  9. from flask import Flask, abort, request, Response
  10. BASE_URL = os.environ["LAS_BASE_URL"]
  11. SECRET = os.environ["LAS_SECRET"]
  12. jenkins_server = jenkins.Jenkins(
  13. os.environ["LAS_JENKINS_URL"],
  14. username=os.environ["LAS_JENKINS_USER"],
  15. password=os.environ["LAS_JENKINS_PASSWORD"],
  16. )
  17. def get_hook_key(service, identifier):
  18. nonce = (service + SECRET + identifier).encode("ascii")
  19. return hashlib.sha256(nonce).hexdigest()
  20. def get_hook_url(service, identifier):
  21. return f"{BASE_URL}hooks/{service}/{identifier}/{get_hook_key(service, identifier)}"
  22. def authenticate(f):
  23. @wraps(f)
  24. def wrapper(*args, **kwargs):
  25. match = re.match(
  26. "^/hooks/(?P<service>[^/]+)/(?P<identifier>.*)/(?P<key>[^/]+)$",
  27. request.path,
  28. )
  29. if not match:
  30. return Response("Bad request", 400)
  31. expected_key = get_hook_key(match.group("service"), match.group("identifier"))
  32. if expected_key != match.group("key"):
  33. app.logger.info(
  34. f"Bad request to {request.path}: expected key {expected_key}"
  35. )
  36. return Response("Invalid key", 403)
  37. return f(*args, **kwargs)
  38. return wrapper
  39. def get_jenkins_jobs():
  40. for job in jenkins_server.get_all_jobs():
  41. config = BeautifulSoup(
  42. jenkins_server.get_job_config(job["fullname"]), features="xml"
  43. )
  44. for git_config in config.find_all("scm", class_="hudson.plugins.git.GitSCM"):
  45. branch_spec = git_config.find("branches").find("name").text
  46. yield job["fullname"], branch_spec, git_config.find("url").text
  47. def gitea_request(method, api_path, **kwargs):
  48. if "params" not in kwargs:
  49. kwargs["params"] = {}
  50. kwargs["params"]["access_token"] = os.environ["LAS_GITEA_TOKEN"]
  51. return requests.request(
  52. method, f"{os.environ['LAS_GITEA_URL']}api/v1/{api_path}", **kwargs
  53. )
  54. def maybe_install_gitea_hook(project):
  55. hook_url = get_hook_url("gitea", project)
  56. path = f"repos/{project}/hooks"
  57. hooks = gitea_request("get", path).json()
  58. if hook_url not in [hook["config"]["url"] for hook in hooks]:
  59. body = {
  60. "active": True,
  61. "config": {"content_type": "json", "url": hook_url},
  62. "events": [
  63. "create",
  64. "delete",
  65. "fork",
  66. "push",
  67. "issues",
  68. "issue_comment",
  69. "pull_request",
  70. "repository",
  71. "release",
  72. ],
  73. "type": "gitea",
  74. }
  75. gitea_request("post", path, json=body).json()
  76. def get_gitea_repos():
  77. repos = gitea_request("get", f"user/repos").json()
  78. for repo in repos:
  79. maybe_install_gitea_hook(repo["full_name"])
  80. yield repo["full_name"], repo["ssh_url"], repo["clone_url"]
  81. def reportbot_announce(message):
  82. try:
  83. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
  84. host = os.environ["LAS_REPORTBOT_ADDRESS"].split(":")
  85. sock.connect((host[0], int(host[1])))
  86. sock.sendall(
  87. f"{os.environ['LAS_REPORTBOT_PREFIX']} {message}\n".encode("utf-8")
  88. )
  89. app.logger.info(f"Report bot response: {sock.recv(512)}")
  90. except Exception:
  91. app.logger.exception("Unable to send report bot message")
  92. repos = dict((name, [ssh, clone]) for name, ssh, clone in get_gitea_repos())
  93. jobs = list(get_jenkins_jobs())
  94. app = Flask(__name__)
  95. @app.route("/")
  96. def handle_index():
  97. return app.send_static_file("index.html")
  98. @app.route("/hooks/gitea/<path:repo>/<hash>", methods=["POST"])
  99. @authenticate
  100. def handle_hook_gitea(repo):
  101. app.logger.info(f"Received hook for repo {repo}")
  102. if repo not in repos:
  103. app.logger.info(f"Repository not found. Known repos: {repos.keys()}")
  104. abort(404)
  105. if request.headers.get("X-Gitea-Event") == "push":
  106. urls = repos[repo]
  107. for name, spec, url in jobs:
  108. if url in urls:
  109. # TODO: Check branches
  110. app.logger.info(f"Found matching job: {name} with URL {url}")
  111. jenkins_server.build_job(name)
  112. data = request.get_json()
  113. if not data["repository"]["private"]:
  114. repo = data["repository"]["full_name"]
  115. commits = len(data["commits"])
  116. compare = data["compare_url"]
  117. pusher = data["pusher"]["login"]
  118. reportbot_announce(
  119. f"\002[git]\002 {pusher} pushed {commits} commit{'s' if commits != 1 else ''} to {repo}: {compare}"
  120. )
  121. for commit in data["commits"][:3]:
  122. reportbot_announce(
  123. f"\002[git]\002 {commit['id'][:10]}: {commit['message'][:100]}"
  124. )
  125. return "", 204
  126. @app.route("/hooks/docker/registry/<hash>", methods=["GET", "POST"])
  127. @authenticate
  128. def handle_docker_registry():
  129. for event in request.get_json()["events"]:
  130. if (
  131. event["action"] == "push"
  132. and "vnd.docker.distribution.manifest" in event["target"]["mediaType"]
  133. and "tag" in event["target"]
  134. ):
  135. repo = event["target"]["repository"]
  136. tag = event["target"]["tag"]
  137. host = event["request"]["host"]
  138. user = event["actor"]["name"]
  139. reportbot_announce(
  140. f"\002[registry]\002 New manifest pushed to {host}/{repo}:{tag} by {user}"
  141. )
  142. return "", 204
  143. app.run("0.0.0.0")