Add REPOSITORY_SHORTNAME_MAP feature

This commit is contained in:
Hein-Pieter van Braam-Stewart
2021-01-27 15:14:03 +01:00
parent 9c6f01a610
commit e225ff3631
2 changed files with 18 additions and 1 deletions

View File

@@ -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"
```

13
bot.py
View File

@@ -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)