Skip to content

Commit

Permalink
ref(autofix): Move repo provider processing + validation logic to mod…
Browse files Browse the repository at this point in the history
…el (#281)

Move repo validation logic to where it should be, so we don't get any leftover unprocessed repo provider values
  • Loading branch information
jennmueng authored Mar 3, 2024
1 parent a279695 commit 196b915
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
12 changes: 12 additions & 0 deletions src/seer/automation/autofix/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ class RepoDefinition(BaseModel):
owner: str
name: str

@field_validator("provider", mode="after")
@classmethod
def validate_provider(cls, provider: str):
cleaned_provider = provider
if provider.startswith("integrations:"):
cleaned_provider = provider.split(":")[1]

if cleaned_provider != "github":
raise ValueError(f"Provider {cleaned_provider} is not supported.")

return cleaned_provider

def __hash__(self):
return hash((self.provider, self.owner, self.name))

Expand Down
5 changes: 2 additions & 3 deletions src/seer/automation/codebase/codebase_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,20 @@ def has_repo_been_indexed(organization: int, project: int, repo: RepoDefinition)
def from_repo_definition(
cls, organization: int, project: int, repo: RepoDefinition, run_id: uuid.UUID
):
provider = RepoClient.process_repo_provider(repo.provider)
db_repo_info = (
Session()
.query(DbRepositoryInfo)
.filter(
DbRepositoryInfo.organization == organization,
DbRepositoryInfo.project == project,
DbRepositoryInfo.provider == provider,
DbRepositoryInfo.provider == repo.provider,
DbRepositoryInfo.external_slug == f"{repo.owner}/{repo.name}",
)
.one_or_none()
)
if db_repo_info:
repo_info = RepositoryInfo.from_db(db_repo_info)
repo_client = RepoClient(provider, repo.owner, repo.name)
repo_client = RepoClient(repo.provider, repo.owner, repo.name)
return cls(organization, project, repo_client, repo_info, run_id)

return None
Expand Down
6 changes: 0 additions & 6 deletions src/seer/automation/codebase/repo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ def __init__(
self.repo_owner = repo_owner
self.repo_name = repo_name

@staticmethod
def process_repo_provider(provider: str) -> str:
if provider.startswith("integrations:"):
return provider.split(":")[1]
return provider

def get_default_branch(self):
return self.repo.default_branch

Expand Down
14 changes: 12 additions & 2 deletions tests/automation/autofix/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,19 @@ def test_repo_definition_uniqueness(self):

def test_multiple_repos(self):
repo_def1 = RepoDefinition(provider="github", owner="seer", name="automation")
repo_def2 = RepoDefinition(provider="bitbucket", owner="seer", name="automation-tools")
repo_def2 = RepoDefinition(provider="github", owner="seer", name="automation-tools")
self.assertNotEqual(hash(repo_def1), hash(repo_def2))

def test_repo_with_provider_processing(self):
repo_def = RepoDefinition(provider="integrations:github", owner="seer", name="automation")
self.assertEqual(repo_def.provider, "github")
self.assertEqual(repo_def.owner, "seer")
self.assertEqual(repo_def.name, "automation")

def test_repo_with_invalid_provider(self):
with self.assertRaises(ValidationError):
RepoDefinition(provider="invalid_provider", owner="seer", name="automation")

def test_repo_with_none_provider(self):
repo_dict = {"provider": None, "owner": "seer", "name": "automation"}
with self.assertRaises(ValidationError):
Expand Down Expand Up @@ -146,7 +156,7 @@ def test_autofix_request_with_duplicate_repos(self):

def test_autofix_request_with_multiple_repos(self):
repo_def1 = RepoDefinition(provider="github", owner="seer", name="automation")
repo_def2 = RepoDefinition(provider="bitbucket", owner="seer", name="automation-tools")
repo_def2 = RepoDefinition(provider="github", owner="seer", name="automation-tools")
issue_details = IssueDetails(id=789, title="Test Issue", events=[SentryEvent(entries=[])])
autofix_request = AutofixRequest(
organization_id=123,
Expand Down
5 changes: 0 additions & 5 deletions tests/automation/codebase/test_repo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,3 @@ def test_repo_client_rejects_unsupported_provider(self, mock_get_github_auth):
RepoClient(
repo_provider="unsupported_provider", repo_owner="test_owner", repo_name="test_repo"
)

def test_repo_client_process_repo_provider(self):
assert RepoClient.process_repo_provider("github") == "github"
assert RepoClient.process_repo_provider("integrations:github") == "github"
assert RepoClient.process_repo_provider("integrations:bitbucket") == "bitbucket"

0 comments on commit 196b915

Please sign in to comment.