Browse Source

Make reportbot a service.

master
Chris Smith 5 years ago
parent
commit
e4bbd1396f
4 changed files with 35 additions and 24 deletions
  1. 4
    4
      README.md
  2. 9
    0
      lineandsinker/services/__init__.py
  3. 17
    0
      lineandsinker/services/reportbot.py
  4. 5
    20
      main.py

+ 4
- 4
README.md View File

@@ -59,11 +59,11 @@ start Jenkins jobs in response to other events.
59 59
 
60 60
 Environment variable | Description
61 61
 -------------------- | --------------------------------------------------------
62
- `LAS_REPORTBOT_ADDRESS` | The host:port combination to send messages to
63
- `LAS_REPORTBOT_PREFIX`  | The text to send to the socket before any messages (e.g. API key)
62
+ `LAS_REPORTBOT_URL` | The full URL to the report API endpoint
63
+ `LAS_REPORTBOT_KEY` | The authentication key to use
64
+ `LAS_REPORTBOT_CHANNEL` | The channel to send messages to
64 65
 
65
-LaS can send messages to a ReportBot instance (sending plain text over a TCP
66
-connection with a specified prefix).
66
+LaS can send messages to a ReportBot instance.
67 67
 
68 68
 ## Contributing
69 69
 

+ 9
- 0
lineandsinker/services/__init__.py View File

@@ -2,6 +2,7 @@ import os
2 2
 
3 3
 from .jenkins import Jenkins
4 4
 from .gitea import Gitea
5
+from .reportbot import ReportBot
5 6
 
6 7
 
7 8
 def gitea_factory():
@@ -16,4 +17,12 @@ def jenkins_factory():
16 17
     )
17 18
 
18 19
 
20
+def reportbot_factory():
21
+    return ReportBot(
22
+        os.environ["LAS_REPORTBOT_URL"],
23
+        os.environ["LAS_REPORTBOT_KEY"],
24
+        os.environ["LAS_REPORTBOT_CHANNEL"],
25
+    )
26
+
27
+
19 28
 factories = {"gitea": gitea_factory, "jenkins": jenkins_factory}

+ 17
- 0
lineandsinker/services/reportbot.py View File

@@ -0,0 +1,17 @@
1
+import requests
2
+
3
+from .service import Service
4
+
5
+
6
+class ReportBot(Service):
7
+    def __init__(self, url, key, channel):
8
+        super().__init__("reportbot")
9
+        self._url = url
10
+        self._key = key
11
+        self._channel = channel
12
+
13
+    def announce(self, message):
14
+        requests.get(
15
+            self._url,
16
+            {"key": self._key, "command": f"CM {self._channel}", "args": message},
17
+        )

+ 5
- 20
main.py View File

@@ -1,13 +1,10 @@
1
-import os
2 1
 import re
3
-import socket
4 2
 from functools import wraps
5 3
 
6 4
 from flask import Flask, abort, request, Response
7 5
 
8 6
 from lineandsinker.common import get_hook_key
9
-from lineandsinker.services import gitea_factory, jenkins_factory
10
-
7
+from lineandsinker.services import gitea_factory, jenkins_factory, reportbot_factory
11 8
 
12 9
 url_pattern = re.compile(
13 10
     "^/hooks/(?P<service>[^/]+)/(?P<identifier>.*)/(?P<key>[^/]+)$"
@@ -33,21 +30,9 @@ def authenticate(f):
33 30
     return wrapper
34 31
 
35 32
 
36
-def reportbot_announce(message):
37
-    try:
38
-        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
39
-            host = os.environ["LAS_REPORTBOT_ADDRESS"].split(":")
40
-            sock.connect((host[0], int(host[1])))
41
-            sock.sendall(
42
-                f"{os.environ['LAS_REPORTBOT_PREFIX']} {message}\n".encode("utf-8")
43
-            )
44
-            app.logger.info(f"Report bot response: {sock.recv(512)}")
45
-    except:
46
-        app.logger.exception("Unable to send report bot message")
47
-
48
-
49 33
 jenkins_service = jenkins_factory()
50 34
 gitea_service = gitea_factory()
35
+reportbot_service = reportbot_factory()
51 36
 repos = dict((name, [ssh, clone]) for name, ssh, clone in gitea_service.get_repos())
52 37
 jobs = list(jenkins_service.get_jobs())
53 38
 app = Flask(__name__)
@@ -82,11 +67,11 @@ def handle_hook_gitea(repo):
82 67
             compare = data["compare_url"]
83 68
             pusher = data["pusher"]["login"]
84 69
 
85
-            reportbot_announce(
70
+            reportbot_service.announce(
86 71
                 f"\002[git]\002 {pusher} pushed {commits} commit{'s' if commits != 1 else ''} to {repo}: {compare}"
87 72
             )
88 73
             for commit in data["commits"][:3]:
89
-                reportbot_announce(
74
+                reportbot_service.announce(
90 75
                     f"\002[git]\002 {commit['id'][:10]}: {commit['message'][:100]}"
91 76
                 )
92 77
 
@@ -106,7 +91,7 @@ def handle_docker_registry():
106 91
             tag = event["target"]["tag"]
107 92
             host = event["request"]["host"]
108 93
             user = event["actor"]["name"]
109
-            reportbot_announce(
94
+            reportbot_service.announce(
110 95
                 f"\002[registry]\002 New manifest pushed to {host}/{repo}:{tag} by {user}"
111 96
             )
112 97
 

Loading…
Cancel
Save