Skip to content

Commit

Permalink
Merge pull request #72 from OpenRailAssociation/fix-badcredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmehl authored Jan 30, 2025
2 parents 3b528ca + 1a99ab0 commit 89e9cc4
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions gh_org_mgr/_gh_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
GithubIntegration,
UnknownObjectException,
)
from github.GithubException import BadCredentialsException
from github.NamedUser import NamedUser
from github.Organization import Organization
from github.Repository import Repository
from github.Team import Team
from jwt.exceptions import InvalidKeyError

from ._gh_api import get_github_secrets_from_env, run_graphql_query

Expand Down Expand Up @@ -65,6 +67,7 @@ def _sluggify_teamname(self, team: str) -> str:
# supported, or multiple spaces etc.
return team.replace(" ", "-")

# amazonq-ignore-next-line
def login(
self, orgname: str, token: str = "", app_id: str | int = "", app_private_key: str = ""
) -> None:
Expand All @@ -81,7 +84,11 @@ def login(
logging.debug("Logging in via app %s", self.gh_app_id)
auth = Auth.AppAuth(app_id=self.gh_app_id, private_key=self.gh_app_private_key)
app = GithubIntegration(auth=auth)
installation = app.get_org_installation(org=orgname)
try:
installation = app.get_org_installation(org=orgname)
except InvalidKeyError:
logging.critical("Invalid private key provided for GitHub App")
sys.exit(1)
self.gh = installation.get_github_for_installation()
logging.debug("Logged in via app installation %s", installation.id)

Expand All @@ -90,7 +97,11 @@ def login(
elif self.gh_token:
logging.debug("Logging in as user with PAT")
self.gh = Github(auth=Auth.Token(self.gh_token))
logging.debug("Logged in as %s", self.gh.get_user().login)
try:
logging.debug("Logged in as %s", self.gh.get_user().login)
except BadCredentialsException:
logging.critical("Invalid GitHub token provided")
sys.exit(1)
else:
logging.error("No GitHub token or App ID+private key provided")
sys.exit(1)
Expand Down

0 comments on commit 89e9cc4

Please sign in to comment.