From 123043bea0a207676ed81bad905f90c18b152431 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Tue, 26 Jan 2021 21:36:35 -0500 Subject: [PATCH] Run scripts --- Dockerfile | 4 +- bot.py | 239 +++++++++++++++++++++++++------------------------- test-regex.py | 82 +++++++++-------- 3 files changed, 166 insertions(+), 159 deletions(-) diff --git a/Dockerfile b/Dockerfile index b6310f6..159187b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,9 @@ FROM fedora:33 - + LABEL maintainer="Hein-Pieter van Braam-Stewart " RUN dnf -y install python3-websocket-client python3-requests && \ - dnf clean all + dnf clean all COPY bot.py /root/bot.py WORKDIR /root diff --git a/bot.py b/bot.py index 3c63574..858c87c 100755 --- a/bot.py +++ b/bot.py @@ -9,32 +9,34 @@ import os import time import re -DEBUG=os.environ.get('BOT_DEBUG') -ROCKET_WS_URL=os.environ.get('ROCKET_WS_URL') -ROCKET_USERNAME=os.environ.get('ROCKET_USERNAME') -ROCKET_PASSWORD=os.environ.get('ROCKET_PASSWORD') -GITHUB_PROJECT=os.environ.get('GITHUB_PROJECT') -GITHUB_USERNAME=os.environ.get('GITHUB_USERNAME') -GITHUB_TOKEN=os.environ.get('GITHUB_TOKEN') -DEFAULT_AVATAR_URL=os.environ.get('DEFAULT_AVATAR_URL') -DEFAULT_REPOSITORY=os.environ.get('DEFAULT_REPOSITORY') -REPOSITORY_SHORTNAME_MAP=os.environ.get('REPOSITORY_SHORTNAME_MAP') +DEBUG = os.environ.get("BOT_DEBUG") +ROCKET_WS_URL = os.environ.get("ROCKET_WS_URL") +ROCKET_USERNAME = os.environ.get("ROCKET_USERNAME") +ROCKET_PASSWORD = os.environ.get("ROCKET_PASSWORD") +GITHUB_PROJECT = os.environ.get("GITHUB_PROJECT") +GITHUB_USERNAME = os.environ.get("GITHUB_USERNAME") +GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN") +DEFAULT_AVATAR_URL = os.environ.get("DEFAULT_AVATAR_URL") +DEFAULT_REPOSITORY = os.environ.get("DEFAULT_REPOSITORY") +REPOSITORY_SHORTNAME_MAP = os.environ.get("REPOSITORY_SHORTNAME_MAP") -RE_TAG_PROG = re.compile('([A-Za-z0-9_.-]+)?#(\d+)') -RE_URL_PROG = re.compile('(https?://)?github.com/([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)/(issues|pull)/(\d+)(\S*)') +RE_TAG_PROG = re.compile("([A-Za-z0-9_.-]+)?#(\d+)") +RE_URL_PROG = re.compile("(https?://)?github.com/([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)/(issues|pull)/(\d+)(\S*)") -SHORTNAME_MAP={} -for item in re.sub('\s+', ' ', REPOSITORY_SHORTNAME_MAP).split(' '): - split = item.split(':') +SHORTNAME_MAP = {} +for item in re.sub("\s+", " ", REPOSITORY_SHORTNAME_MAP).split(" "): + split = item.split(":") if len(split) != 2: continue SHORTNAME_MAP[split[0]] = split[1] + def debug_print(msg): if DEBUG: print(msg) + class Bot: def __init__(self): self.sever_id = None @@ -43,11 +45,13 @@ class Bot: self.token_expires = None self.id = None - self.ws = websocket.WebSocketApp(ROCKET_WS_URL, - on_message = lambda ws,msg: self.on_message(ws, msg), - on_error = lambda ws,msg: self.on_error(ws, msg), - on_close = lambda ws: self.on_close(ws), - on_open = lambda ws: self.on_open(ws)) + self.ws = websocket.WebSocketApp( + ROCKET_WS_URL, + on_message=lambda ws, msg: self.on_message(ws, msg), + on_error=lambda ws, msg: self.on_error(ws, msg), + on_close=lambda ws: self.on_close(ws), + on_open=lambda ws: self.on_open(ws), + ) def run(self): self.ws.run_forever() @@ -61,15 +65,15 @@ class Bot: "msg": "method", "method": "login", "id": "login", - "params" : [{ - "user": { - "username": ROCKET_USERNAME - }, - "password": { - "digest": hashlib.sha256(ROCKET_PASSWORD.encode('utf-8')).hexdigest(), - "algorithm": "sha-256", - }, - }], + "params": [ + { + "user": {"username": ROCKET_USERNAME}, + "password": { + "digest": hashlib.sha256(ROCKET_PASSWORD.encode("utf-8")).hexdigest(), + "algorithm": "sha-256", + }, + } + ], } self.send(login_msg) @@ -86,15 +90,12 @@ class Bot: "msg": "sub", "id": channel, "name": "stream-room-messages", - "params":[ - channel_id, - False - ] + "params": [channel_id, False], } self.send(subscribe_msg) def format_issue(self, repository, issue, add_issue_link): - headers = { 'User-Agent': 'Godot Issuebot by hpvb', } + headers = {"User-Agent": "Godot Issuebot by hpvb"} url = f"https://api.github.com/repos/{GITHUB_PROJECT}/{repository}/issues/{issue}" debug_print(f"GitHub API request: {url}") @@ -107,9 +108,9 @@ class Bot: issue = r.json() avatar_url = DEFAULT_AVATAR_URL - if 'avatar_url' in issue['user'] and issue['user']['avatar_url']: - avatar_url = issue['user']['avatar_url'] - if 'gravatar_id' in issue['user'] and issue['user']['gravatar_id']: + if "avatar_url" in issue["user"] and issue["user"]["avatar_url"]: + avatar_url = issue["user"]["avatar_url"] + if "gravatar_id" in issue["user"] and issue["user"]["gravatar_id"]: avatar_url = f"https://www.gravatar.com/avatar/{issue['user']['gravatar_id']}" is_pr = False @@ -121,44 +122,44 @@ class Bot: status = None closed_by = None - if 'pull_request' in issue and issue['pull_request']: + if "pull_request" in issue and issue["pull_request"]: is_pr = True debug_print(f"GitHub API request: {issue['pull_request']['url']}") - prr = requests.get(issue['pull_request']['url'], headers=headers, auth=(GITHUB_USERNAME, GITHUB_TOKEN)) + prr = requests.get(issue["pull_request"]["url"], headers=headers, auth=(GITHUB_USERNAME, GITHUB_TOKEN)) if prr.status_code == 200: pr = prr.json() - status = pr['state'] + status = pr["state"] - if 'merged_by' in pr and pr['merged_by']: - pr_merged_by = pr['merged_by']['login'] - if 'mergeable' in pr: - pr_mergeable = pr['mergeable'] - if 'merged' in pr: - pr_merged = pr['merged'] - if 'draft' in pr: - pr_draft = pr['draft'] - if 'requested_reviewers' in pr and pr['requested_reviewers']: + if "merged_by" in pr and pr["merged_by"]: + pr_merged_by = pr["merged_by"]["login"] + if "mergeable" in pr: + pr_mergeable = pr["mergeable"] + if "merged" in pr: + pr_merged = pr["merged"] + if "draft" in pr: + pr_draft = pr["draft"] + if "requested_reviewers" in pr and pr["requested_reviewers"]: reviewers = [] - for reviewer in pr['requested_reviewers']: - reviewers.append(reviewer['login']) - pr_reviewers = ', '.join(reviewers) - if 'requested_teams' in pr and pr['requested_teams']: + for reviewer in pr["requested_reviewers"]: + reviewers.append(reviewer["login"]) + pr_reviewers = ", ".join(reviewers) + if "requested_teams" in pr and pr["requested_teams"]: teams = [] - for team in pr['requested_teams']: + for team in pr["requested_teams"]: teams.append(f"team:{team['name']}") if pr_reviewers: - pr_reviewers += ' and ' - pr_reviewers += ', '.join(teams) + pr_reviewers += " and " + pr_reviewers += ", ".join(teams) else: - pr_reviewers = ', '.join(teams) + pr_reviewers = ", ".join(teams) else: - status = issue['state'] + status = issue["state"] - if status == 'closed': - if 'closed_by' in issue and issue['closed_by']: - closed_by = issue['closed_by']['login'] + if status == "closed": + if "closed_by" in issue and issue["closed_by"]: + closed_by = issue["closed_by"]["login"] issue_type = None @@ -168,7 +169,7 @@ class Bot: status = "PR merged" if pr_merged_by: status += f" by {pr_merged_by}" - elif status == 'closed': + elif status == "closed": status = "PR closed" elif not pr_merged: status = "PR open" @@ -181,23 +182,23 @@ class Bot: status += " [needs rebase]" if pr_reviewers: status += f" reviews required from {pr_reviewers}" - + else: issue_type = "Issue" status = f"Status: {status}" - if not pr_merged and closed_by and status == 'closed': + if not pr_merged and closed_by and status == "closed": status += f" by {closed_by}" retval = { "author_icon": avatar_url, - "author_link": issue['html_url'], + "author_link": issue["html_url"], "author_name": f"{repository.title()} [{issue_type}]: {issue['title']} #{issue['number']}", "text": status, } if not add_issue_link: - retval.pop('author_link', None) + retval.pop("author_link", None) return retval @@ -207,43 +208,43 @@ class Bot: links = [] # First replace all the full links that rocket.chat has detected with tags - if 'urls' in msg and msg['urls']: + if "urls" in msg and msg["urls"]: urls_to_keep = [] - for url in msg['urls']: + for url in msg["urls"]: debug_print(f"URL: {url['url']}") - match = re.search(RE_URL_PROG, url['url']) - if match and not match.group(6).startswith('#') and not match.group(6).startswith('/'): + match = re.search(RE_URL_PROG, url["url"]) + if match and not match.group(6).startswith("#") and not match.group(6).startswith("/"): - match = re.search(RE_URL_PROG, url['url']) + match = re.search(RE_URL_PROG, url["url"]) repository = match.group(3) issue = int(match.group(5)) - tag = f'{repository}#{issue}' + tag = f"{repository}#{issue}" debug_print(f"Replacing url {url['url']} with {tag}") - msg['msg'] = msg['msg'].replace(url['url'], tag) + msg["msg"] = msg["msg"].replace(url["url"], tag) continue urls_to_keep.append(url) - msg['urls'] = urls_to_keep + msg["urls"] = urls_to_keep # Then we replace all of the part-urls with tags as well - for match in re.finditer(RE_URL_PROG, msg['msg']): + for match in re.finditer(RE_URL_PROG, msg["msg"]): project = match.group(2) repository = match.group(3) issue = match.group(5) - tag = f'{repository}#{issue}' + tag = f"{repository}#{issue}" if project == GITHUB_PROJECT: - if (match.group(6).startswith('#') or match.group(6).startswith('/')) and not tag in msg['msg']: - msg['msg'] = msg['msg'].replace(match.group(0), f'{match.group(0)} ({tag})') + if (match.group(6).startswith("#") or match.group(6).startswith("/")) and not tag in msg["msg"]: + msg["msg"] = msg["msg"].replace(match.group(0), f"{match.group(0)} ({tag})") add_issue_link = False else: - msg['msg'] = msg['msg'].replace(match.group(0), tag) + msg["msg"] = msg["msg"].replace(match.group(0), tag) # Then finally add the metadata for all our tags debug_print("Scanning message for tags") - for match in re.finditer(RE_TAG_PROG, msg['msg']): + for match in re.finditer(RE_TAG_PROG, msg["msg"]): repository = match.group(1) issue = int(match.group(2)) @@ -257,7 +258,9 @@ class Bot: repository = DEFAULT_REPOSITORY if repository in SHORTNAME_MAP: - debug_print(f"Found repository: {repository} in shortname map. Replacing with {SHORTNAME_MAP[repository]}") + debug_print( + f"Found repository: {repository} in shortname map. Replacing with {SHORTNAME_MAP[repository]}" + ) repository = SHORTNAME_MAP[repository] debug_print(f"Message contains issue for {repository}") @@ -268,72 +271,69 @@ class Bot: if not len(links): return - - if not 'attachments' in msg: - msg['attachments'] = [] + + if not "attachments" in msg: + msg["attachments"] = [] # We may be editing, remove all the github attachments old_attachments = [] - for attachment in msg['attachments']: - if 'author_icon' in attachment: + for attachment in msg["attachments"]: + if "author_icon" in attachment: continue old_attachments.append(attachment) - msg['attachments'] = old_attachments + msg["attachments"] = old_attachments # Hack Hack, the clients won't update without a change to this field. Even if we add or remove attachments. - msg['msg'] = msg['msg'] + " " + msg["msg"] = msg["msg"] + " " # Add timestamp to all attachments. These are visible in the mobile client. for link in links: - link['ts'] = msg['ts'], + link["ts"] = (msg["ts"],) # Deduplicate links - [msg['attachments'].append(x) for x in links if x not in msg['attachments']] + [msg["attachments"].append(x) for x in links if x not in msg["attachments"]] - update_msg = { - "msg": "method", - "method": "updateMessage", - "id": "update-message", - "params": [ msg ] - } + update_msg = {"msg": "method", "method": "updateMessage", "id": "update-message", "params": [msg]} self.send(update_msg) def on_message(self, ws, message): decoded_msg = json.loads(message) debug_print("Incoming: " + decoded_msg.__repr__()) - if 'server_id' in decoded_msg: - self.server_id = decoded_msg['server_id'] + if "server_id" in decoded_msg: + self.server_id = decoded_msg["server_id"] - if 'msg' in decoded_msg: - msg = decoded_msg['msg'] - if msg == 'ping': - self.send({'msg': 'pong'}) + if "msg" in decoded_msg: + msg = decoded_msg["msg"] + if msg == "ping": + self.send({"msg": "pong"}) - if msg == 'connected': - self.session_id = decoded_msg['session'] + if msg == "connected": + self.session_id = decoded_msg["session"] debug_print(f"Got session: {self.session_id}") self.login() - if msg == 'result': - if decoded_msg['id'] == 'login': - self.id = decoded_msg['result']['id'] - self.token = decoded_msg['result']['token'] - self.token_expires = datetime.fromtimestamp(int(decoded_msg['result']['tokenExpires']['$date']) / 1000) + if msg == "result": + if decoded_msg["id"] == "login": + self.id = decoded_msg["result"]["id"] + self.token = decoded_msg["result"]["token"] + self.token_expires = datetime.fromtimestamp( + int(decoded_msg["result"]["tokenExpires"]["$date"]) / 1000 + ) debug_print(f"Loggedin: id: {self.id}, token: {self.token}, expires: {self.token_expires}") self.get_subscriptions() - if decoded_msg['id'] == 'subscriptions': - for subscription in decoded_msg['result']: - self.subscribe(subscription['name'], subscription['rid']) + if decoded_msg["id"] == "subscriptions": + for subscription in decoded_msg["result"]: + self.subscribe(subscription["name"], subscription["rid"]) - if msg == 'changed' and decoded_msg['collection'] == 'stream-room-messages': - for chat_msg in decoded_msg['fields']['args']: - if 'editedBy' in chat_msg and chat_msg['editedBy']['_id'] == self.id: + if msg == "changed" and decoded_msg["collection"] == "stream-room-messages": + for chat_msg in decoded_msg["fields"]["args"]: + if "editedBy" in chat_msg and chat_msg["editedBy"]["_id"] == self.id: continue - if re.search(RE_TAG_PROG, chat_msg['msg']) or re.search(RE_URL_PROG, chat_msg['msg']): + if re.search(RE_TAG_PROG, chat_msg["msg"]) or re.search(RE_URL_PROG, chat_msg["msg"]): debug_print("Sending message to be update") self.replace_issue_tags(chat_msg) @@ -343,18 +343,19 @@ class Bot: def on_close(self, ws): debug_print("Disconnected, reconnecting") ws.close() - #ws.run_forever() + # ws.run_forever() def on_open(self, ws): - connect_msg = { "msg": "connect", "version": "1", "support": ["1"] } + connect_msg = {"msg": "connect", "version": "1", "support": ["1"]} self.send(connect_msg) + if __name__ == "__main__": if DEBUG: websocket.enableTrace(True) while True: - try: + try: bot = Bot() bot.run() except Exception as e: diff --git a/test-regex.py b/test-regex.py index 848443e..c935ae2 100644 --- a/test-regex.py +++ b/test-regex.py @@ -2,48 +2,55 @@ import re -def makeurl(issue, repo = ''): - if not repo: - repo = 'godot' - return f'https://github.com/godotengine/{repo}/issues/{issue}' +def makeurl(issue, repo=""): + if not repo: + repo = "godot" + + return f"https://github.com/godotengine/{repo}/issues/{issue}" + tests = [ - { 'text': '#100', 'results' : [ makeurl(100) ] }, - { 'text': 'godot#100', 'results' : [ makeurl(100) ] }, - { 'text': 'issue-bot#100', 'results' : [ makeurl(100, 'issue-bot') ] }, - { 'text': '#100,text', 'results' : [ makeurl(100) ] }, - { 'text': '#100,#101,#102', 'results' : [ makeurl(100), makeurl(101), makeurl(102) ] }, - { 'text': 'text #100,#101,#102 text', 'results' : [ makeurl(100), makeurl(101), makeurl(102) ] }, - { 'text': 'text issue-bot#100,godot#101,collada-exporter#102 text', 'results' : [ makeurl(100, 'issue-bot'), makeurl(101), makeurl(102, 'collada-exporter') ] }, - { 'text': 'text #100', 'results' : [ makeurl(100) ] }, - { 'text': 'text #100 text', 'results' : [ makeurl(100) ] }, - { 'text': 'text #100 #101 text', 'results' : [ makeurl(100), makeurl(101) ] }, - { 'text': 'godot#100', 'results' : [ makeurl(100) ] }, - { 'text': 'text godot#100 text', 'results' : [ makeurl(100) ] }, - { 'text': 'godot#100 text', 'results' : [ makeurl(100) ] }, - { 'text': 'repo.name#100 text', 'results' : [ makeurl(100, 'repo.name') ] }, - { 'text': '(#100) text', 'results' : [ makeurl(100) ] }, - { 'text': '(repo#100) text', 'results' : [ makeurl(100, 'repo') ] }, - - { 'text': 'https://github.com/godotengine/issue-bot/issues/2', 'results': [ makeurl(2, 'issue-bot') ] }, - { 'text': 'https://github.com/godotengine/godot/pull/100', 'results': [ makeurl(100) ] }, - { 'text': 'http://github.com/godotengine/godot/pull/100', 'results': [ makeurl(100) ] }, - { 'text': 'github.com/godotengine/godot/pull/100', 'results': [ makeurl(100) ] }, - { 'text': 'https://github.com/godotengine/godot/pull/100#issuecomment-1', 'results': [ makeurl(100) ] }, - - { 'text': 'https://github.com/godotengine/godot-cpp/pull/373/checks?check_run_id=1802261888', 'results': [ makeurl(373, 'godot-cpp') ] }, - - { 'text': 'a long line of text with an url https://github.com/godotengine/godot/issues/100 and some tags #102 repo#103', 'results': [ makeurl(102), makeurl(103, 'repo'), makeurl(100) ] }, - - { 'text': 'just a bunch of text', 'results' : [ ] }, - { 'text': 'Bunch of ## nonsense ##sdf $$', 'results' : [ ] }, + {"text": "#100", "results": [makeurl(100)]}, + {"text": "godot#100", "results": [makeurl(100)]}, + {"text": "issue-bot#100", "results": [makeurl(100, "issue-bot")]}, + {"text": "#100,text", "results": [makeurl(100)]}, + {"text": "#100,#101,#102", "results": [makeurl(100), makeurl(101), makeurl(102)]}, + {"text": "text #100,#101,#102 text", "results": [makeurl(100), makeurl(101), makeurl(102)]}, + { + "text": "text issue-bot#100,godot#101,collada-exporter#102 text", + "results": [makeurl(100, "issue-bot"), makeurl(101), makeurl(102, "collada-exporter")], + }, + {"text": "text #100", "results": [makeurl(100)]}, + {"text": "text #100 text", "results": [makeurl(100)]}, + {"text": "text #100 #101 text", "results": [makeurl(100), makeurl(101)]}, + {"text": "godot#100", "results": [makeurl(100)]}, + {"text": "text godot#100 text", "results": [makeurl(100)]}, + {"text": "godot#100 text", "results": [makeurl(100)]}, + {"text": "repo.name#100 text", "results": [makeurl(100, "repo.name")]}, + {"text": "(#100) text", "results": [makeurl(100)]}, + {"text": "(repo#100) text", "results": [makeurl(100, "repo")]}, + {"text": "https://github.com/godotengine/issue-bot/issues/2", "results": [makeurl(2, "issue-bot")]}, + {"text": "https://github.com/godotengine/godot/pull/100", "results": [makeurl(100)]}, + {"text": "http://github.com/godotengine/godot/pull/100", "results": [makeurl(100)]}, + {"text": "github.com/godotengine/godot/pull/100", "results": [makeurl(100)]}, + {"text": "https://github.com/godotengine/godot/pull/100#issuecomment-1", "results": [makeurl(100)]}, + { + "text": "https://github.com/godotengine/godot-cpp/pull/373/checks?check_run_id=1802261888", + "results": [makeurl(373, "godot-cpp")], + }, + { + "text": "a long line of text with an url https://github.com/godotengine/godot/issues/100 and some tags #102 repo#103", + "results": [makeurl(102), makeurl(103, "repo"), makeurl(100)], + }, + {"text": "just a bunch of text", "results": []}, + {"text": "Bunch of ## nonsense ##sdf $$", "results": []}, ] -tag_prog = re.compile('([A-Za-z0-9_.-]+)?#(\d+)') -url_prog = re.compile('(https?://)?github.com/([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)/(issues|pull)/(\d+)(\S*)') +tag_prog = re.compile("([A-Za-z0-9_.-]+)?#(\d+)") +url_prog = re.compile("(https?://)?github.com/([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)/(issues|pull)/(\d+)(\S*)") for test in tests: - text = test['text'] + text = test["text"] result = [] for match in re.finditer(tag_prog, text): @@ -52,6 +59,5 @@ for test in tests: for match in re.finditer(url_prog, text): result.append(makeurl(match.group(5), match.group(3))) - if test['results'] != result: + if test["results"] != result: print(f'FAILED for {text}: expected {test["results"]} got: {result}') -