Skip to content

Commit c05401e

Browse files
cmelonealecbcs
andauthored
make comment bot account optional (llnl#213)
* make comment bot account optional users may not want to set up a github account for tagging in comments. this makes `HC_GH_BOT_USER` optional, allowing a user to tag hubcast with the `/hubcast` prefix. * rewrite env_get to support optional values * flip order of conditional Co-authored-by: Alec Scott <hi@alecbcs.com> --------- Co-authored-by: Alec Scott <hi@alecbcs.com>
1 parent 76e5e22 commit c05401e

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

src/hubcast/config.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
from typing import Optional
32

43

54
class ConfigError(Exception):
@@ -23,8 +22,10 @@ def __init__(self):
2322
self.ldap_map_output = env_get("HC_LDAP_MAP_OUTPUT")
2423
self.ldap_map_scope = env_get("HC_LDAP_MAP_SCOPE")
2524
# optional settings: if None, the mapper will behave accordingly
26-
self.ldap_map_bind_dn = os.environ.get("HC_LDAP_MAP_BIND_DN")
27-
self.ldap_map_bind_password = os.environ.get("HC_LDAP_MAP_BIND_PASSWORD")
25+
self.ldap_map_bind_dn = env_get("HC_LDAP_MAP_BIND_DN", optional=True)
26+
self.ldap_map_bind_password = env_get(
27+
"HC_LDAP_MAP_BIND_PASSWORD", optional=True
28+
)
2829

2930
self.gh = GitHubConfig()
3031
self.gl = GitLabConfig()
@@ -35,7 +36,7 @@ def __init__(self):
3536
self.app_id = env_get("HC_GH_APP_IDENTIFIER")
3637
self.privkey = env_get("HC_GH_PRIVATE_KEY")
3738
self.webhook_secret = env_get("HC_GH_SECRET")
38-
self.bot_user = env_get("HC_GH_BOT_USER")
39+
self.bot_user = env_get("HC_GH_BOT_USER", optional=True)
3940

4041

4142
class GitLabConfig:
@@ -47,9 +48,24 @@ def __init__(self):
4748
self.callback_url = env_get("HC_GL_CALLBACK_URL")
4849

4950

50-
def env_get(key: str, default: Optional[str] = None) -> str:
51-
value = os.environ.get(key) or default
52-
if not value:
53-
raise ConfigError(f"Required environment variable not found: {key}")
51+
def env_get(key: str, default=None, optional: bool = False):
52+
"""
53+
Retrieve environment variables.
54+
55+
Attributes:
56+
----------
57+
key: str
58+
The environment variable key to retrieve.
59+
default: any, optional
60+
The default value to return if the environment variable is not set.
61+
optional: bool, optional
62+
If True, the default value is returned when the variable is not found. If False
63+
and the variable is not found, a ConfigError is raised.
64+
"""
5465

55-
return value
66+
try:
67+
return os.environ[key]
68+
except KeyError:
69+
if optional:
70+
return default
71+
raise ConfigError(f"Required environment variable not found: {key}")

src/hubcast/web/comments.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
def help_message(bot_user: str):
1+
def help_message(bot_caller: str):
22
return f"""
33
You can interact with me in many ways!
44
5-
- `@{bot_user} help`: see this message
6-
- `@{bot_user} approve`: sync this pull request with the destination repository and trigger a new pipeline
7-
- `@{bot_user} run pipeline`: request a new run of the GitLab CI pipeline for any reason
8-
- `@{bot_user} restart failed jobs`: restart any failed jobs in the latest CI pipeline
5+
- `{bot_caller} help`: see this message
6+
- `{bot_caller} approve`: sync this pull request with the destination repository and trigger a new pipeline
7+
- `{bot_caller} run pipeline`: request a new run of the GitLab CI pipeline for any reason
8+
- `{bot_caller} restart failed jobs`: restart any failed jobs in the latest CI pipeline
99
1010
If you are an outside contributor to this repository, a maintainer will need to approve and run pipelines on your behalf.
1111

src/hubcast/web/github/routes.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,15 @@ async def respond_comment(event, gh, gl, gl_user, *arg, **kwargs):
298298
response = None
299299
plus_one = False
300300

301-
if re.search(f"@{gh.bot_user} help", comment, re.IGNORECASE):
302-
response = comments.help_message(gh.bot_user)
301+
if gh.bot_user:
302+
bot_caller = f"@{gh.bot_user}"
303+
else:
304+
bot_caller = "/hubcast"
305+
306+
if re.search(f"{bot_caller} help", comment, re.IGNORECASE):
307+
response = comments.help_message(bot_caller)
303308

304-
elif re.search(f"@{gh.bot_user} approve", comment, re.IGNORECASE):
309+
elif re.search(f"{bot_caller} approve", comment, re.IGNORECASE):
305310
# syncs PR changes to the destination on behalf of the commenter
306311
# this does not handle PR deletions, those will need to be manually cleaned by project maintainers
307312
pull_request_id = event.data["issue"]["number"]
@@ -313,7 +318,7 @@ async def respond_comment(event, gh, gl, gl_user, *arg, **kwargs):
313318
plus_one = True
314319

315320
elif re.search(
316-
f"@{gh.bot_user} (re[-]?)?(run|start) pipeline", comment, re.IGNORECASE
321+
f"{bot_caller} (re[-]?)?(run|start) pipeline", comment, re.IGNORECASE
317322
):
318323
pull_request_id = event.data["issue"]["number"]
319324
pull_request = await gh.get_pr(pull_request_id)
@@ -344,7 +349,7 @@ async def respond_comment(event, gh, gl, gl_user, *arg, **kwargs):
344349
response = "I had a problem starting the pipeline."
345350

346351
elif re.search(
347-
f"@{gh.bot_user} restart failed(?:[- ]?jobs)?", comment, re.IGNORECASE
352+
f"{bot_caller} restart failed(?:[- ]?jobs)?", comment, re.IGNORECASE
348353
):
349354
pull_request_id = event.data["issue"]["number"]
350355
pull_request = await gh.get_pr(pull_request_id)

0 commit comments

Comments
 (0)