Skip to content

Commit 89e9cc4

Browse files
authored
Merge pull request #72 from OpenRailAssociation/fix-badcredentials
2 parents 3b528ca + 1a99ab0 commit 89e9cc4

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

gh_org_mgr/_gh_org.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
GithubIntegration,
1616
UnknownObjectException,
1717
)
18+
from github.GithubException import BadCredentialsException
1819
from github.NamedUser import NamedUser
1920
from github.Organization import Organization
2021
from github.Repository import Repository
2122
from github.Team import Team
23+
from jwt.exceptions import InvalidKeyError
2224

2325
from ._gh_api import get_github_secrets_from_env, run_graphql_query
2426

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

70+
# amazonq-ignore-next-line
6871
def login(
6972
self, orgname: str, token: str = "", app_id: str | int = "", app_private_key: str = ""
7073
) -> None:
@@ -81,7 +84,11 @@ def login(
8184
logging.debug("Logging in via app %s", self.gh_app_id)
8285
auth = Auth.AppAuth(app_id=self.gh_app_id, private_key=self.gh_app_private_key)
8386
app = GithubIntegration(auth=auth)
84-
installation = app.get_org_installation(org=orgname)
87+
try:
88+
installation = app.get_org_installation(org=orgname)
89+
except InvalidKeyError:
90+
logging.critical("Invalid private key provided for GitHub App")
91+
sys.exit(1)
8592
self.gh = installation.get_github_for_installation()
8693
logging.debug("Logged in via app installation %s", installation.id)
8794

@@ -90,7 +97,11 @@ def login(
9097
elif self.gh_token:
9198
logging.debug("Logging in as user with PAT")
9299
self.gh = Github(auth=Auth.Token(self.gh_token))
93-
logging.debug("Logged in as %s", self.gh.get_user().login)
100+
try:
101+
logging.debug("Logged in as %s", self.gh.get_user().login)
102+
except BadCredentialsException:
103+
logging.critical("Invalid GitHub token provided")
104+
sys.exit(1)
94105
else:
95106
logging.error("No GitHub token or App ID+private key provided")
96107
sys.exit(1)

0 commit comments

Comments
 (0)