Skip to content

Commit b09bb23

Browse files
committed
1 parent 9f992e8 commit b09bb23

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

tcms_github_app/utils.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,53 @@ def get_installation(self, inst_id):
7474
)
7575

7676

77+
class PatchGithubIntegration(github.GithubIntegration):
78+
def get_access_token(self, installation_id, user_id=None):
79+
"""
80+
Workaround for KIWI-TCMS-HD,
81+
https://sentry.io/organizations/kiwitcms/issues/2835963408
82+
83+
Remove when https://github.com/PyGithub/PyGithub/pull/2079 gets merged!
84+
"""
85+
import requests # pylint: disable=import-outside-toplevel
86+
from github import Consts, GithubException, InstallationAuthorization # pylint: disable=import-outside-toplevel
87+
88+
body = {}
89+
if user_id:
90+
body = {"user_id": user_id}
91+
response = requests.post(
92+
f"{self.base_url}/app/installations/{installation_id}/access_tokens",
93+
headers={
94+
"Authorization": f"Bearer {self.create_jwt()}",
95+
"Accept": Consts.mediaTypeIntegrationPreview,
96+
"User-Agent": "PyGithub/Python",
97+
},
98+
json=body,
99+
)
100+
101+
if response.status_code == 201: # pylint: disable=no-else-return
102+
return InstallationAuthorization.InstallationAuthorization(
103+
requester=None, # not required, this is a NonCompletableGithubObject
104+
headers={}, # not required, this is a NonCompletableGithubObject
105+
attributes=response.json(),
106+
completed=True,
107+
)
108+
elif response.status_code == 403:
109+
raise GithubException.BadCredentialsException( # pylint: disable=no-member
110+
status=response.status_code, data=response.text,
111+
headers=response.headers,
112+
)
113+
elif response.status_code == 404:
114+
raise GithubException.UnknownObjectException( # pylint: disable=no-member
115+
status=response.status_code, data=response.text,
116+
headers=response.headers,
117+
)
118+
raise GithubException.GithubException( # pylint: disable=no-member
119+
status=response.status_code, data=response.text,
120+
headers=response.headers,
121+
)
122+
123+
77124
def find_token_from_app_inst(gh_app, installation):
78125
"""
79126
Find an installation access token for this app:
@@ -94,8 +141,8 @@ def find_token_from_app_inst(gh_app, installation):
94141

95142

96143
def github_rpc_from_inst(installation):
97-
gh_app = github.GithubIntegration(settings.KIWI_GITHUB_APP_ID,
98-
settings.KIWI_GITHUB_APP_PRIVATE_KEY)
144+
gh_app = PatchGithubIntegration(settings.KIWI_GITHUB_APP_ID,
145+
settings.KIWI_GITHUB_APP_PRIVATE_KEY)
99146

100147
token = find_token_from_app_inst(gh_app, installation)
101148
return KiwiTCMSGithub(token)

0 commit comments

Comments
 (0)