Skip to content

Commit

Permalink
🔥 Drop tracking installs in gh app obj
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz committed Jan 10, 2020
1 parent d7fdff8 commit 71f4fd7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
2 changes: 1 addition & 1 deletion octomachinery/app/server/machinery.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def run_forever(config):
config.github,
http_session=aiohttp_client_session,
)
await github_app.pre_fetch_installs()
await github_app.log_installs_list()
await _prepare_github_app(github_app)
await _launch_web_server_and_wait_until_it_stops(
config.server, github_app,
Expand Down
33 changes: 5 additions & 28 deletions octomachinery/github/api/app_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,50 +49,37 @@ def __attrs_post_init__(self):
' SIGNATURE VERIFICATION WILL BE ENFORCED'
if webhook_secret else '',
)
# pylint: disable=attribute-defined-outside-init
self._installations = defaultdict(dict)

async def event_from_request(self, request):
"""Get an event object out of HTTP request."""
event = Event.from_http(
return Event.from_http(
request.headers,
await request.read(),
secret=self._config.webhook_secret,
)
await self.pre_process_webhook_event(event)
return event

async def pre_process_webhook_event(self, event):
"""Get an event object out of HTTP request."""
action = event.data.get('action')
if event.event in GH_INSTALL_EVENTS and action == 'created':
await self.add_installation(event)

async def pre_fetch_installs(self) -> 'GitHubApp':
async def log_installs_list(self) -> None:
"""Store all installations data before starting."""
# pylint: disable=attribute-defined-outside-init
try:
self._installations = await self.get_installations()
installations = await self.get_installations()
except ClientConnectorError as client_error:
logger.info('It looks like the GitHub API is offline...')
logger.error(
'The following error has happened while trying to grab '
'installations list: %s',
client_error,
)
self._installations = defaultdict(dict)
return

logger.info('This GitHub App is installed into:')
# pylint: disable=protected-access
for install_id, install_val in self._installations.items():
for install_id, install_val in installations.items():
logger.info(
'* Installation id %s (installed to %s)',
install_id,
install_val._metadata.account['login'],
)

return self

@property
def gh_jwt(self):
"""Generate app's JSON Web Token, valid for 60 seconds."""
Expand All @@ -110,16 +97,6 @@ def api_client(self): # noqa: D401
user_agent=self._config.user_agent,
)

async def add_installation(self, event):
"""Retrieve an installation creds from store."""
install = event.data['installation']
install_id = install['id']
self._installations[install_id] = GitHubAppInstallation(
GitHubAppInstallationModel(**install),
self,
)
return self._installations[install_id]

async def get_installation(self, event):
"""Retrieve an installation creds from store."""
if 'installation' not in event.data:
Expand Down

0 comments on commit 71f4fd7

Please sign in to comment.