Skip to content

Commit

Permalink
Add support for multiple GH repos
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Apr 18, 2024
1 parent 82c7c7d commit 423d5f0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 6 additions & 0 deletions sam/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import enum
import os
from zoneinfo import ZoneInfo

Expand All @@ -14,3 +15,8 @@
SENTRY_DSN = os.getenv("SENTRY_DSN")
GITHUB_ORG = os.getenv("GITHUB_ORG")
GITHUB_REPOSITORY = os.getenv("GITHUB_REPOSITORY")
GITHUB_REPOS = type(
enum.StrEnum,
"GitHubRepos",
{repo: repo for repo in os.getenv("GITHUB_REPOS", "").split(",")},
)
8 changes: 4 additions & 4 deletions sam/contrib/github/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GitHubAPIError(requests.HTTPError):

class AbstractGitHubAPIWrapper(abc.ABC): # pragma: no cover
@abc.abstractmethod
def create_issue(self, title, body):
def create_issue(self, title, body, repo):
return NotImplemented

@abc.abstractmethod
Expand All @@ -42,9 +42,9 @@ def __init__(self, token):
}
)

def create_issue(self, title, body):
def create_issue(self, title, body, repo):
response = self.post(
f"{self.endpoint}/repos/{config.GITHUB_ORG}/{config.GITHUB_REPOSITORY}/issues",
f"{self.endpoint}/repos/{repo}/issues",
json={"title": title, "body": body},
)
try:
Expand All @@ -64,7 +64,7 @@ def __enter__(self):
def __exit__(self, exc_type, exc_val, exc_tb):
pass

def create_issue(self, title, body):
def create_issue(self, title, body, repo):
return {
"title": title,
"body": body,
Expand Down
7 changes: 5 additions & 2 deletions sam/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def fetch_coworker_emails(**_context) -> str:
return json.dumps(profiles)


def create_github_issue(title: str, body: str) -> str:
def create_github_issue(title: str, body: str, repo: config.GITHUB_REPOS) -> str:
"""
Create an issue on GitHub with the given title and body.
Expand All @@ -170,9 +170,12 @@ def create_github_issue(title: str, body: str) -> str:
title: The title of the issue.
body: The body of the issue, markdown supported.
"""
if repo not in config.GITHUB_REPOS.__members__:
logger.warning("Invalid repo: %s", repo)
return "invalid repo"
with github.get_client() as api:
try:
response = api.create_issue(title, body)
response = api.create_issue(title, body, repo)
except github.GitHubAPIError:
logger.exception("Failed to create issue on GitHub")
return "failed to create issue"
Expand Down

0 comments on commit 423d5f0

Please sign in to comment.