11import logging
2+ from typing import Any
23
34from sentry .constants import ObjectStatus
45from sentry .integrations .services .integration import integration_service
1819from sentry .taskworker .config import TaskworkerConfig
1920from sentry .taskworker .namespaces import integrations_control_tasks
2021from sentry .taskworker .retry import Retry
21- from sentry .utils import metrics
2222
2323logger = logging .getLogger (__name__ )
2424
@@ -58,27 +58,11 @@ def link_all_repos(
5858 integration_id = integration_id , status = ObjectStatus .ACTIVE
5959 )
6060 if not integration :
61- # TODO: Remove this logger in favor of context manager
62- logger .error (
63- "%s.link_all_repos.integration_missing" ,
64- integration_key ,
65- extra = {"organization_id" : organization_id },
66- )
67- metrics .incr ("github.link_all_repos.error" , tags = {"type" : "missing_integration" })
6861 lifecycle .record_failure (str (LinkAllReposHaltReason .MISSING_INTEGRATION ))
6962 return
7063
7164 rpc_org = organization_service .get (id = organization_id )
7265 if rpc_org is None :
73- logger .error (
74- "%s.link_all_repos.organization_missing" ,
75- integration_key ,
76- extra = {"organization_id" : organization_id },
77- )
78- metrics .incr (
79- f"{ integration_key } .link_all_repos.error" ,
80- tags = {"type" : "missing_organization" },
81- )
8266 lifecycle .record_failure (str (LinkAllReposHaltReason .MISSING_ORGANIZATION ))
8367 return
8468
@@ -93,26 +77,31 @@ def link_all_repos(
9377 lifecycle .record_halt (str (LinkAllReposHaltReason .RATE_LIMITED ))
9478 return
9579
96- metrics .incr (f"{ integration_key } .link_all_repos.api_error" )
9780 raise
9881
9982 integration_repo_provider = get_integration_repository_provider (integration )
10083
101- # If we successfully create any repositories, we'll set this to True
102- success = False
103-
84+ repo_configs : list [dict [str , Any ]] = []
85+ missing_repos = []
10486 for repo in repositories :
10587 try :
106- config = get_repo_config (repo , integration_id )
107- integration_repo_provider .create_repository (
108- repo_config = config , organization = rpc_org
109- )
110- success = True
88+ repo_configs .append (get_repo_config (repo , integration_id ))
11189 except KeyError :
112- continue
113- except RepoExistsError :
114- metrics .incr ("sentry.integration_repo_provider.repo_exists" )
90+ missing_repos .append (repo )
11591 continue
11692
117- if not success :
118- lifecycle .record_halt (str (LinkAllReposHaltReason .REPOSITORY_NOT_CREATED ))
93+ try :
94+ integration_repo_provider .create_repositories (
95+ configs = repo_configs , organization = rpc_org
96+ )
97+ except RepoExistsError as e :
98+ lifecycle .record_halt (
99+ str (LinkAllReposHaltReason .REPOSITORY_NOT_CREATED ),
100+ {"missing_repos" : e .repos , "integration_id" : integration_id },
101+ )
102+
103+ if missing_repos :
104+ lifecycle .record_halt (
105+ str (LinkAllReposHaltReason .REPOSITORY_NOT_CREATED ),
106+ {"missing_repos" : missing_repos , "integration_id" : integration_id },
107+ )
0 commit comments