Browse Source

Handle gitea hooks, trigger jenkins.

master
Chris Smith 6 years ago
parent
commit
356a4581ce
1 changed files with 37 additions and 8 deletions
  1. 37
    8
      main.py

+ 37
- 8
main.py View File

@@ -4,27 +4,32 @@ import jenkins
4 4
 import requests
5 5
 import os
6 6
 from bs4 import BeautifulSoup
7
-from flask import Flask
7
+from flask import Flask, abort
8 8
 
9 9
 BASE_URL = os.environ["LAS_BASE_URL"]
10 10
 SECRET = os.environ["LAS_SECRET"]
11 11
 
12
-server = jenkins.Jenkins(
12
+jenkins_server = jenkins.Jenkins(
13 13
     os.environ["LAS_JENKINS_URL"],
14 14
     username=os.environ["LAS_JENKINS_USER"],
15 15
     password=os.environ["LAS_JENKINS_PASSWORD"],
16 16
 )
17 17
 
18 18
 
19
-def get_hook_url(service, identifier):
19
+def get_hook_key(service, identifier):
20 20
     nonce = (service + SECRET + identifier).encode("ascii")
21
-    token = hashlib.sha256(nonce).hexdigest()
22
-    return f"{BASE_URL}hooks/{service}/{identifier}/{token}"
21
+    return hashlib.sha256(nonce).hexdigest()
22
+
23
+
24
+def get_hook_url(service, identifier):
25
+    return f"{BASE_URL}hooks/{service}/{identifier}/{get_hook_key(service, identifier)}"
23 26
 
24 27
 
25 28
 def get_jenkins_jobs():
26
-    for job in server.get_all_jobs():
27
-        config = BeautifulSoup(server.get_job_config(job["fullname"]), features="xml")
29
+    for job in jenkins_server.get_all_jobs():
30
+        config = BeautifulSoup(
31
+            jenkins_server.get_job_config(job["fullname"]), features="xml"
32
+        )
28 33
         for git_config in config.find_all("scm", class_="hudson.plugins.git.GitSCM"):
29 34
             branch_spec = git_config.find("branches").find("name").text
30 35
             yield job["fullname"], branch_spec, git_config.find("url").text
@@ -71,6 +76,8 @@ def get_gitea_repos():
71 76
         yield repo["full_name"], repo["ssh_url"], repo["clone_url"]
72 77
 
73 78
 
79
+repos = dict((name, [ssh, clone]) for name, ssh, clone in get_gitea_repos())
80
+jobs = list(get_jenkins_jobs())
74 81
 app = Flask(__name__)
75 82
 
76 83
 
@@ -79,4 +86,26 @@ def handle_index():
79 86
     return app.send_static_file("index.html")
80 87
 
81 88
 
82
-app.run('0.0.0.0')
89
+@app.route("/hooks/gitea/<path:repo>/<hash>")
90
+def handle_hook_gitea(repo, hash):
91
+    print(f"Received hook for repo {repo} with has {hash}")
92
+    expected_hash = get_hook_key("gitea", repo)
93
+    if hash != expected_hash:
94
+        print(f"Hash mismatch: expected {expected_hash}")
95
+        abort(403)
96
+
97
+    if repo not in repos:
98
+        print(f"Repository not found. Known repos: {repos.keys()}")
99
+        abort(404)
100
+
101
+    urls = repos[repo]
102
+    for name, spec, url in jobs:
103
+        if url in urls:
104
+            # TODO: Check branches
105
+            print(f"Found matching job: {name} with URL {url}")
106
+            jenkins_server.build_job(name)
107
+
108
+    return "", 204
109
+
110
+
111
+app.run("0.0.0.0")

Loading…
Cancel
Save