-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbackfills.py
69 lines (60 loc) · 2.37 KB
/
backfills.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import logging
import shared.torngit as torngit
from asgiref.sync import async_to_sync
from sqlalchemy.orm.session import Session
from database.models.core import GithubAppInstallation, Repository
log = logging.getLogger(__name__)
# GH App Backfills
# Looping and adding all repositories in the installation app
def add_repos_service_ids_from_provider(
db_session: Session,
ownerid: int,
owner_service: torngit.base.TorngitBaseAdapter,
gh_app_installation: GithubAppInstallation,
):
# TODO: Convert this to the generator function
repos = async_to_sync(owner_service.list_repos_using_installation)()
if repos:
# Fetching all repos service ids we have for that owner in the DB
repo_service_ids_in_db = [
repo.service_id
for repo in db_session.query(Repository.service_id)
.filter_by(ownerid=ownerid)
.all()
]
# Add service ids from provider that we have DB records for to a list
new_repo_service_ids = []
for repo in repos:
repo_data = repo["repo"]
service_id = repo_data["service_id"]
if service_id and service_id in repo_service_ids_in_db:
new_repo_service_ids.append(service_id)
log.info(
"Added the following repo service ids to this gh app installation",
extra=dict(
ownerid=ownerid,
installation_id=gh_app_installation.installation_id,
new_repo_service_ids=new_repo_service_ids,
),
)
gh_app_installation.repository_service_ids = new_repo_service_ids
db_session.commit()
# Check if gh selection is set to all and act accordingly
def maybe_set_installation_to_all_repos(
db_session: Session,
owner_service,
gh_app_installation: GithubAppInstallation,
):
remote_gh_app_installation = async_to_sync(owner_service.get_gh_app_installation)(
installation_id=gh_app_installation.installation_id
)
repository_selection = remote_gh_app_installation.get("repository_selection", "")
if repository_selection == "all":
gh_app_installation.repository_service_ids = None
db_session.commit()
log.info(
"Selection is set to all, no installation is needed",
extra=dict(ownerid=gh_app_installation.ownerid),
)
return True
return False