Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Change github objects in set to only strings #67

Merged
merged 2 commits into from
Jan 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import os

from github import Github
from github.Repository import Repository

from github.GithubException import UnknownObjectException
from thoth.common import OpenShift
Expand All @@ -48,9 +47,10 @@ class Schedule:
def __init__(self, github: Github, organizations: List[str] = None, repositories: List[str] = None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just set the default for organizations and repositores to be [] here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python is known to work unexpectedly here: if you use mutable as a default parameter, it can be modified later, which would affect all the other subsequent calls.

See here e.g.: https://web.archive.org/web/20200221224620/http://effbot.org/zone/default-values.htm

Therefore, for a better practice, I use None and then check, instead of mutables as default (even though they are not modified in body).

"""Initialize with github, orgs and repos optional."""
self.gh = github
self.orgs = organizations
self.repos = repositories
self.github_repos: Set[Repository] = set()
self.orgs = organizations if organizations else []
self.repos = repositories if repositories else []

self.checked_repos: Set[str] = set()

self._initialize_repositories_from_organizations()
self._initialize_repositories_from_raw()
Expand All @@ -68,7 +68,7 @@ def _initialize_repositories_from_organizations(self):
if repo.full_name in self.repos:
self.repos.remove(repo.full_name)

self.github_repos.add(repo)
self.checked_repos.add(repo.full_name)

except UnknownObjectException:
_LOGGER.error("organization %s was not recognized by GitHub API", org)
Expand All @@ -83,31 +83,33 @@ def _initialize_repositories_from_raw(self):
_LOGGER.info("repository %s is archived, therefore skipped", repo.full_name)
continue

self.github_repos.add(gh_repo)
self.checked_repos.add(gh_repo.full_name)

except UnknownObjectException:
_LOGGER.error("Repository %s was not recognized by GitHub API", repo)

def schedule_for_mi_analysis(self) -> None:
"""Schedule workflows for mi analysis."""
for repo in self.github_repos:
workflow_id = OpenShift().schedule_mi_workflow(repository=repo.full_name)
for repo in self.checked_repos:
workflow_id = OpenShift().schedule_mi_workflow(repository=repo)
_LOGGER.info("Scheduled mi with id %r", workflow_id)

def schedule_for_kebechet_analysis(self):
"""Schedule workflows for kebechet analysis."""
for repo in self.github_repos:
workflow_id = OpenShift().schedule_mi_workflow(repository=repo.full_name, entities=KEBECHET_ENTITIES)
for repo in self.checked_repos:
workflow_id = OpenShift().schedule_mi_workflow(repository=repo, entities=KEBECHET_ENTITIES)
_LOGGER.info("Scheduled mi-kebechet analysis with id %r", workflow_id)


def main():
"""MI-Scheduler entrypoint."""
gh = Github(login_or_token=GITHUB_ACCESS_TOKEN)

# regular mi schedule
repos, orgs = OpenShift().get_mi_repositories_and_organizations()
Schedule(gh, orgs, repos).schedule_for_mi_analysis()
Schedule(gh, organizations=orgs, repositories=repos).schedule_for_mi_analysis()

# kebechet schedule
graph = GraphDatabase()
graph.connect()
kebechet_repos = graph.get_active_kebechet_github_installations_repos()
Expand Down