Skip to content

Commit

Permalink
Workaround upstream PyGithub/PyGithub#2079
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Jan 5, 2022
1 parent 9f992e8 commit 87d4473
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions tcms_github_app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,50 @@ def get_installation(self, inst_id):
)


class PatchGithubIntegration(github.GithubIntegration):
def get_access_token(self, installation_id, user_id=None):
"""
Workaround for KIWI-TCMS-HD,
https://sentry.io/organizations/kiwitcms/issues/2835963408
Remove when https://github.com/PyGithub/PyGithub/pull/2079 gets merged!
"""
body = {}
if user_id:
body = {"user_id": user_id}
response = requests.post(
f"{self.base_url}/app/installations/{installation_id}/access_tokens",
headers={
"Authorization": f"Bearer {self.create_jwt()}",
"Accept": Consts.mediaTypeIntegrationPreview,
"User-Agent": "PyGithub/Python",
},
json=body,
)

if response.status_code == 201:
return InstallationAuthorization.InstallationAuthorization(
requester=None, # not required, this is a NonCompletableGithubObject
headers={}, # not required, this is a NonCompletableGithubObject
attributes=response.json(),
completed=True,
)
elif response.status_code == 403:
raise GithubException.BadCredentialsException(
status=response.status_code, data=response.text,
headers=response.headers,
)
elif response.status_code == 404:
raise GithubException.UnknownObjectException(
status=response.status_code, data=response.text,
headers=response.headers,
)
raise GithubException.GithubException(
status=response.status_code, data=response.text,
headers=response.headers,
)


def find_token_from_app_inst(gh_app, installation):
"""
Find an installation access token for this app:
Expand All @@ -94,8 +138,8 @@ def find_token_from_app_inst(gh_app, installation):


def github_rpc_from_inst(installation):
gh_app = github.GithubIntegration(settings.KIWI_GITHUB_APP_ID,
settings.KIWI_GITHUB_APP_PRIVATE_KEY)
gh_app = PatchGithubIntegration(settings.KIWI_GITHUB_APP_ID,
settings.KIWI_GITHUB_APP_PRIVATE_KEY)

token = find_token_from_app_inst(gh_app, installation)
return KiwiTCMSGithub(token)
Expand Down

0 comments on commit 87d4473

Please sign in to comment.