Skip to content

Commit 4deed67

Browse files
committed
Refactor repository filter extraction in search utilities
- Renamed `extractRepoFilter` to `hasRepoFilter` to simplify the function's purpose. - Updated test cases in `search_utils_test.go` to reflect the new function name and logic. - Adjusted tests to focus on the presence of the `repo:` filter rather than extracting owner and repo details.
1 parent a0b5e3a commit 4deed67

File tree

2 files changed

+40
-77
lines changed

2 files changed

+40
-77
lines changed

pkg/github/search_utils.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,8 @@ func hasSpecificFilter(query, filterType, filterValue string) bool {
2424
return matched
2525
}
2626

27-
func extractRepoFilter(query string) (owner, repo string, found bool) {
28-
pattern := `(?:^|\s)repo:([^/\s]+)/([^\s]+)`
29-
re := regexp.MustCompile(pattern)
30-
matches := re.FindStringSubmatch(query)
31-
32-
if len(matches) >= 3 {
33-
return matches[1], matches[2], true
34-
}
35-
return "", "", false
27+
func hasRepoFilter(query string) bool {
28+
return hasFilter(query, "repo")
3629
}
3730

3831
func searchHandler(
@@ -61,14 +54,8 @@ func searchHandler(
6154
return mcp.NewToolResultError(err.Error()), nil
6255
}
6356

64-
if owner != "" && repo != "" {
65-
_, _, hasRepoFilter := extractRepoFilter(query)
66-
67-
// TODO: Existing owner and existing repo?
68-
if !hasRepoFilter {
69-
query = fmt.Sprintf("repo:%s/%s %s", owner, repo, query)
70-
}
71-
57+
if owner != "" && repo != "" && !hasRepoFilter(query) {
58+
query = fmt.Sprintf("repo:%s/%s %s", owner, repo, query)
7259
}
7360

7461
sort, err := OptionalParam[string](request, "sort")

pkg/github/search_utils_test.go

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -101,92 +101,68 @@ func Test_hasFilter(t *testing.T) {
101101
}
102102
}
103103

104-
func Test_extractRepoFilter(t *testing.T) {
104+
func Test_hasRepoFilter(t *testing.T) {
105105
tests := []struct {
106-
name string
107-
query string
108-
expectedOwner string
109-
expectedRepo string
110-
expectedFound bool
106+
name string
107+
query string
108+
expected bool
111109
}{
112110
{
113-
name: "query with repo: filter at beginning",
114-
query: "repo:github/github-mcp-server is:issue",
115-
expectedOwner: "github",
116-
expectedRepo: "github-mcp-server",
117-
expectedFound: true,
111+
name: "query with repo: filter at beginning",
112+
query: "repo:github/github-mcp-server is:issue",
113+
expected: true,
118114
},
119115
{
120-
name: "query with repo: filter in middle",
121-
query: "is:issue repo:octocat/Hello-World bug",
122-
expectedOwner: "octocat",
123-
expectedRepo: "Hello-World",
124-
expectedFound: true,
116+
name: "query with repo: filter in middle",
117+
query: "is:issue repo:octocat/Hello-World bug",
118+
expected: true,
125119
},
126120
{
127-
name: "query with repo: filter at end",
128-
query: "is:issue critical repo:owner/repo-name",
129-
expectedOwner: "owner",
130-
expectedRepo: "repo-name",
131-
expectedFound: true,
121+
name: "query with repo: filter at end",
122+
query: "is:issue critical repo:owner/repo-name",
123+
expected: true,
132124
},
133125
{
134-
name: "query with complex repo name",
135-
query: "repo:microsoft/vscode-extension-samples bug",
136-
expectedOwner: "microsoft",
137-
expectedRepo: "vscode-extension-samples",
138-
expectedFound: true,
126+
name: "query with complex repo name",
127+
query: "repo:microsoft/vscode-extension-samples bug",
128+
expected: true,
139129
},
140130
{
141-
name: "query without repo: filter",
142-
query: "is:issue bug critical",
143-
expectedOwner: "",
144-
expectedRepo: "",
145-
expectedFound: false,
131+
name: "query without repo: filter",
132+
query: "is:issue bug critical",
133+
expected: false,
146134
},
147135
{
148-
name: "query with malformed repo: filter (no slash)",
149-
query: "repo:github bug",
150-
expectedOwner: "",
151-
expectedRepo: "",
152-
expectedFound: false,
136+
name: "query with malformed repo: filter (no slash)",
137+
query: "repo:github bug",
138+
expected: true, // hasRepoFilter only checks for repo: prefix, not format
153139
},
154140
{
155-
name: "empty query",
156-
query: "",
157-
expectedOwner: "",
158-
expectedRepo: "",
159-
expectedFound: false,
141+
name: "empty query",
142+
query: "",
143+
expected: false,
160144
},
161145
{
162-
name: "query with multiple repo: filters (should match first)",
163-
query: "repo:github/first repo:octocat/second",
164-
expectedOwner: "github",
165-
expectedRepo: "first",
166-
expectedFound: true,
146+
name: "query with multiple repo: filters",
147+
query: "repo:github/first repo:octocat/second",
148+
expected: true,
167149
},
168150
{
169-
name: "query with repo: in text but not as filter",
170-
query: "this repo: is important",
171-
expectedOwner: "",
172-
expectedRepo: "",
173-
expectedFound: false,
151+
name: "query with repo: in text but not as filter",
152+
query: "this repo: is important",
153+
expected: false,
174154
},
175155
{
176-
name: "query with complex OR expression",
177-
query: "repo:github/github-mcp-server is:issue (label:critical OR label:urgent)",
178-
expectedOwner: "github",
179-
expectedRepo: "github-mcp-server",
180-
expectedFound: true,
156+
name: "query with complex OR expression",
157+
query: "repo:github/github-mcp-server is:issue (label:critical OR label:urgent)",
158+
expected: true,
181159
},
182160
}
183161

184162
for _, tt := range tests {
185163
t.Run(tt.name, func(t *testing.T) {
186-
owner, repo, found := extractRepoFilter(tt.query)
187-
assert.Equal(t, tt.expectedOwner, owner, "extractRepoFilter(%q) owner = %q, expected %q", tt.query, owner, tt.expectedOwner)
188-
assert.Equal(t, tt.expectedRepo, repo, "extractRepoFilter(%q) repo = %q, expected %q", tt.query, repo, tt.expectedRepo)
189-
assert.Equal(t, tt.expectedFound, found, "extractRepoFilter(%q) found = %v, expected %v", tt.query, found, tt.expectedFound)
164+
result := hasRepoFilter(tt.query)
165+
assert.Equal(t, tt.expected, result, "hasRepoFilter(%q) = %v, expected %v", tt.query, result, tt.expected)
190166
})
191167
}
192168
}

0 commit comments

Comments
 (0)