Skip to content

Commit cb336d0

Browse files
authored
chore(github): Allow forks to be added as repositories (#92435)
Default to allowing forks to be searched. Equivalent to adding it to your query. <img width="320" alt="image" src="https://github.com/user-attachments/assets/63300a42-e713-4a36-8060-d74abae5d550" /> <img width="299" alt="image" src="https://github.com/user-attachments/assets/94ce73b0-8693-4963-9d70-5d161508b1f4" />
1 parent eb735e0 commit cb336d0

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

src/sentry/integrations/github/integration.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ class GithubInstallationInfo(TypedDict):
160160

161161

162162
def build_repository_query(metadata: Mapping[str, Any], name: str, query: str) -> bytes:
163+
"""
164+
Builds a query for the GitHub Search API. Always includes both forks and original repositories.
165+
Test out your query updates here: https://github.com/search/advanced
166+
"""
163167
account_type = "user" if metadata["account_type"] == "User" else "org"
164-
return f"{account_type}:{name} {query}".encode()
168+
return f"fork:true {account_type}:{name} {query}".encode()
165169

166170

167171
def error(

tests/sentry/integrations/github/test_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def test_get_repositories_search_param(self):
586586
with self.tasks():
587587
self.assert_setup_flow()
588588

589-
querystring = urlencode({"q": "org:Test Organization ex"})
589+
querystring = urlencode({"q": "fork:true org:Test Organization ex"})
590590
responses.add(
591591
responses.GET,
592592
f"{self.base_url}/search/repositories?{querystring}",

tests/sentry/integrations/github/test_search.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from datetime import datetime, timedelta
22
from unittest.mock import patch
3+
from urllib.parse import urlencode
34

45
import responses
56
from django.urls import reverse
67

8+
from sentry.integrations.github.integration import build_repository_query
79
from sentry.integrations.models.organization_integration import OrganizationIntegration
810
from sentry.integrations.source_code_management.metrics import SourceCodeSearchEndpointHaltReason
911
from sentry.integrations.types import EventLifecycleOutcome
@@ -38,6 +40,17 @@ def _create_integration(self):
3840
},
3941
)
4042

43+
def _build_repo_query_path(self, *, query: str) -> str:
44+
return f"{self.base_url}/search/repositories?" + urlencode(
45+
{
46+
"q": build_repository_query(
47+
metadata=self.integration.metadata,
48+
name=self.integration.name,
49+
query=query,
50+
).decode("utf-8")
51+
}
52+
)
53+
4154
def setUp(self):
4255
super().setUp()
4356
self.integration = self._create_integration()
@@ -112,7 +125,7 @@ def test_finds_external_issue_results_with_id(self):
112125
def test_finds_repo_results(self, mock_record):
113126
responses.add(
114127
responses.GET,
115-
self.base_url + "/search/repositories?q=org:test%20ex",
128+
self._build_repo_query_path(query="ex"),
116129
json={
117130
"items": [
118131
{"name": "example", "full_name": "test/example"},
@@ -144,7 +157,7 @@ def test_finds_repo_results(self, mock_record):
144157
def test_repo_search_validation_error(self, mock_record):
145158
responses.add(
146159
responses.GET,
147-
self.base_url + "/search/repositories?q=org:test%20nope",
160+
self._build_repo_query_path(query="nope"),
148161
json={
149162
"message": "Validation Error",
150163
"errors": [{"message": "Cannot search for that org"}],
@@ -185,9 +198,7 @@ def test_finds_no_external_issues_results(self):
185198

186199
@responses.activate
187200
def test_finds_no_project_results(self):
188-
responses.add(
189-
responses.GET, self.base_url + "/search/repositories?q=org:test%20nope", json={}
190-
)
201+
responses.add(responses.GET, self._build_repo_query_path(query="nope"), json={})
191202
resp = self.client.get(self.url, data={"field": "repo", "query": "nope"})
192203

193204
assert resp.status_code == 200
@@ -227,7 +238,7 @@ def test_search_issues_rate_limit(self, mock_record):
227238
def test_search_project_rate_limit(self, mock_record):
228239
responses.add(
229240
responses.GET,
230-
self.base_url + "/search/repositories?q=org:test%20ex",
241+
self._build_repo_query_path(query="ex"),
231242
status=403,
232243
json={
233244
"message": "API rate limit exceeded",

tests/sentry/integrations/github_enterprise/test_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def test_get_repositories_search_param(self, mock_jwtm, _):
247247
with self.tasks():
248248
self.assert_setup_flow()
249249

250-
querystring = urlencode({"q": "org:Test Organization ex"})
250+
querystring = urlencode({"q": "fork:true org:Test Organization ex"})
251251
responses.add(
252252
responses.GET,
253253
f"{self.base_url}/search/repositories?{querystring}",

0 commit comments

Comments
 (0)