From e225ff36311114db7d6650659b08316f3fa34a3a Mon Sep 17 00:00:00 2001 From: Hein-Pieter van Braam-Stewart Date: Wed, 27 Jan 2021 15:14:03 +0100 Subject: [PATCH] Add REPOSITORY_SHORTNAME_MAP feature --- README.md | 6 +++++- bot.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fd86958..acf8061 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,15 @@ Simple bot to ferry between rocket.chat and Github. If you want to run one for y What does it do? It will add issue/pr links to messages that mention a valid issue. Either by typing `#1335` to go to the default repository or `repository#345` for a particular one. The bot will edit the message in question and add links, description, and status to the message. +Shortnames can be configured which could for instance map `proposals#1` to `godot-proposals#1` for commonly used repositories. + ![screenshot](screenshot.png?raw=true "Screenshot") ## Environment variables The following environment variables are supported: - * `BOT_DEBUG` - Turn on exessive debug messages when set + * `BOT_DEBUG` - *optional* Turn on exessive debug messages when set + * `REPOSITORY_SHORTNAME_MAP` - *optional* A space separated list form of `shortname:fullname ...` with repository shorthands and full names * `DEFAULT_AVATAR_URL` - *required* url to some image if gh can't provide an avatar * `DEFAULT_REPOSITORY` - *required* default repository to search if a 'bare' issue # gets sent * `ROCKET_WS_URL` - *required* url to the rocket.chat server (wss://chat.godotengine.org/websocket) @@ -42,4 +45,5 @@ ROCKET_USERNAME=github ROCKET_PASSWORD=supersecret GITHUB_USERNAME=hpvb GITHUB_TOKEN=verysecret +REPOSITORY_SHORTNAME_MAP="assetlib:godot-asset-library demos:godot-demo-projects docs:godot-docs proposals:godot-proposals" ``` diff --git a/bot.py b/bot.py index 8e54beb..d25b65c 100755 --- a/bot.py +++ b/bot.py @@ -18,10 +18,19 @@ 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('github.com/([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)/(issues|pulls)/(\d+)\S*') +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) @@ -237,6 +246,10 @@ class Bot: if not repository: repository = DEFAULT_REPOSITORY + if repository in SHORTNAME_MAP: + 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}") link = self.format_issue(repository, issue)