Skip to content

Commit 98f9b97

Browse files
committed
refactor: modernize code with modernize and intrange
1 parent 266bd93 commit 98f9b97

38 files changed

+635
-647
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ jobs:
2020
- name: golangci-lint
2121
uses: golangci/golangci-lint-action@v9
2222
with:
23-
version: v2.5
23+
version: v2.8

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ linters:
99
- gosec
1010
- makezero
1111
- misspell
12+
- modernize
1213
- nakedret
1314
- revive
1415
- errcheck
1516
- staticcheck
1617
- govet
1718
- ineffassign
19+
- intrange
1820
- unused
1921
exclusions:
2022
generated: lax
@@ -27,6 +29,11 @@ linters:
2729
- third_party$
2830
- builtin$
2931
- examples$
32+
- internal/githubv4mock
33+
rules:
34+
- linters:
35+
- revive
36+
text: "var-naming: avoid package names that conflict with Go standard library package names"
3037
settings:
3138
staticcheck:
3239
checks:

cmd/github-mcp-server/generate_docs.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/url"
77
"os"
8+
"slices"
89
"sort"
910
"strings"
1011

@@ -234,7 +235,7 @@ func writeToolDoc(buf *strings.Builder, tool inventory.ServerTool) {
234235

235236
for i, propName := range paramNames {
236237
prop := schema.Properties[propName]
237-
required := contains(schema.Required, propName)
238+
required := slices.Contains(schema.Required, propName)
238239
requiredStr := "optional"
239240
if required {
240241
requiredStr = "required"
@@ -289,15 +290,6 @@ func scopesEqual(a, b []string) bool {
289290
return true
290291
}
291292

292-
func contains(slice []string, item string) bool {
293-
for _, s := range slice {
294-
if s == item {
295-
return true
296-
}
297-
}
298-
return false
299-
}
300-
301293
// indentMultilineDescription adds the specified indent to all lines after the first line.
302294
// This ensures that multi-line descriptions maintain proper markdown list formatting.
303295
func indentMultilineDescription(description, indent string) string {
@@ -319,14 +311,14 @@ func replaceSection(content, startMarker, endMarker, newContent string) (string,
319311
start := fmt.Sprintf("<!-- %s -->", startMarker)
320312
end := fmt.Sprintf("<!-- %s -->", endMarker)
321313

322-
startIdx := strings.Index(content, start)
314+
before, _, ok := strings.Cut(content, start)
323315
endIdx := strings.Index(content, end)
324-
if startIdx == -1 || endIdx == -1 {
316+
if !ok || endIdx == -1 {
325317
return "", fmt.Errorf("markers not found: %s / %s", start, end)
326318
}
327319

328320
var buf strings.Builder
329-
buf.WriteString(content[:startIdx])
321+
buf.WriteString(before)
330322
buf.WriteString(start)
331323
buf.WriteString("\n")
332324
buf.WriteString(newContent)
@@ -426,6 +418,7 @@ func generateRemoteOnlyToolsetsDoc() string {
426418

427419
return strings.TrimSuffix(buf.String(), "\n")
428420
}
421+
429422
func generateDeprecatedAliasesDocs(docsPath string) error {
430423
// Read the current file
431424
content, err := os.ReadFile(docsPath) //#nosec G304

cmd/mcpcurl/main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ type (
7373

7474
// RequestParams contains the tool name and arguments
7575
RequestParams struct {
76-
Name string `json:"name"`
77-
Arguments map[string]interface{} `json:"arguments"`
76+
Name string `json:"name"`
77+
Arguments map[string]any `json:"arguments"`
7878
}
7979

8080
// Content matches the response format of a text content response
@@ -308,8 +308,8 @@ func addCommandFromTool(toolsCmd *cobra.Command, tool *Tool, prettyPrint bool) {
308308
}
309309

310310
// buildArgumentsMap extracts flag values into a map of arguments
311-
func buildArgumentsMap(cmd *cobra.Command, tool *Tool) (map[string]interface{}, error) {
312-
arguments := make(map[string]interface{})
311+
func buildArgumentsMap(cmd *cobra.Command, tool *Tool) (map[string]any, error) {
312+
arguments := make(map[string]any)
313313

314314
for name, prop := range tool.InputSchema.Properties {
315315
switch prop.Type {
@@ -340,7 +340,7 @@ func buildArgumentsMap(cmd *cobra.Command, tool *Tool) (map[string]interface{},
340340
}
341341
case "object":
342342
if jsonStr, _ := cmd.Flags().GetString(name + "-json"); jsonStr != "" {
343-
var jsonArray []interface{}
343+
var jsonArray []any
344344
if err := json.Unmarshal([]byte(jsonStr), &jsonArray); err != nil {
345345
return nil, fmt.Errorf("error parsing JSON for %s: %w", name, err)
346346
}
@@ -355,7 +355,7 @@ func buildArgumentsMap(cmd *cobra.Command, tool *Tool) (map[string]interface{},
355355
}
356356

357357
// buildJSONRPCRequest creates a JSON-RPC request with the given tool name and arguments
358-
func buildJSONRPCRequest(method, toolName string, arguments map[string]interface{}) (string, error) {
358+
func buildJSONRPCRequest(method, toolName string, arguments map[string]any) (string, error) {
359359
id, err := rand.Int(rand.Reader, big.NewInt(10000))
360360
if err != nil {
361361
return "", fmt.Errorf("failed to generate random ID: %w", err)
@@ -432,7 +432,7 @@ func printResponse(response string, prettyPrint bool) error {
432432
// Extract text from content items of type "text"
433433
for _, content := range resp.Result.Content {
434434
if content.Type == "text" {
435-
var textContentObj map[string]interface{}
435+
var textContentObj map[string]any
436436
err := json.Unmarshal([]byte(content.Text), &textContentObj)
437437

438438
if err == nil {
@@ -445,7 +445,7 @@ func printResponse(response string, prettyPrint bool) error {
445445
}
446446

447447
// Fallback parsing as JSONL
448-
var textContentList []map[string]interface{}
448+
var textContentList []map[string]any
449449
if err := json.Unmarshal([]byte(content.Text), &textContentList); err != nil {
450450
return fmt.Errorf("failed to parse text content as a list: %w", err)
451451
}

internal/toolsnaps/toolsnaps_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,23 @@ func TestToolSnapKeysSorted(t *testing.T) {
195195

196196
// Given a tool with fields that could be in any order
197197
type complexTool struct {
198-
Name string `json:"name"`
199-
Description string `json:"description"`
200-
Properties map[string]interface{} `json:"properties"`
201-
Annotations map[string]interface{} `json:"annotations"`
198+
Name string `json:"name"`
199+
Description string `json:"description"`
200+
Properties map[string]any `json:"properties"`
201+
Annotations map[string]any `json:"annotations"`
202202
}
203203

204204
tool := complexTool{
205205
Name: "test_tool",
206206
Description: "A test tool",
207-
Properties: map[string]interface{}{
207+
Properties: map[string]any{
208208
"zzz": "last",
209209
"aaa": "first",
210210
"mmm": "middle",
211-
"owner": map[string]interface{}{"type": "string", "description": "Owner"},
212-
"repo": map[string]interface{}{"type": "string", "description": "Repo"},
211+
"owner": map[string]any{"type": "string", "description": "Owner"},
212+
"repo": map[string]any{"type": "string", "description": "Repo"},
213213
},
214-
Annotations: map[string]interface{}{
214+
Annotations: map[string]any{
215215
"readOnly": true,
216216
"title": "Test",
217217
},
@@ -227,7 +227,7 @@ func TestToolSnapKeysSorted(t *testing.T) {
227227
require.NoError(t, err)
228228

229229
// Verify that the JSON is properly sorted by checking key order
230-
var parsed map[string]interface{}
230+
var parsed map[string]any
231231
err = json.Unmarshal(snapJSON, &parsed)
232232
require.NoError(t, err)
233233

@@ -285,7 +285,7 @@ func TestStructFieldOrderingSortedAlphabetically(t *testing.T) {
285285
aFieldIndex := -1
286286
mFieldIndex := -1
287287
zFieldIndex := -1
288-
for i := 0; i < len(snapStr)-7; i++ {
288+
for i := range len(snapStr) - 7 {
289289
switch snapStr[i : i+6] {
290290
case "aField":
291291
aFieldIndex = i

pkg/buffer/buffer.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,14 @@ func ProcessResponseAsRingBufferToEnd(httpResp *http.Response, maxJobLogLines in
112112
}
113113

114114
var result []string
115-
linesInBuffer := totalLines
116-
if linesInBuffer > maxJobLogLines {
117-
linesInBuffer = maxJobLogLines
118-
}
115+
linesInBuffer := min(totalLines, maxJobLogLines)
119116

120117
startIndex := 0
121118
if totalLines > maxJobLogLines {
122119
startIndex = writeIndex
123120
}
124121

125-
for i := 0; i < linesInBuffer; i++ {
122+
for i := range linesInBuffer {
126123
idx := (startIndex + i) % maxJobLogLines
127124
if validLines[idx] {
128125
result = append(result, lines[idx])

pkg/github/actions.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,7 @@ func downloadLogContent(ctx context.Context, logURL string, tailLines int, maxLi
178178
return "", 0, httpResp, fmt.Errorf("failed to download logs: HTTP %d", httpResp.StatusCode)
179179
}
180180

181-
bufferSize := tailLines
182-
if bufferSize > maxLines {
183-
bufferSize = maxLines
184-
}
181+
bufferSize := min(tailLines, maxLines)
185182

186183
processedInput, totalLines, httpResp, err := buffer.ProcessResponseAsRingBufferToEnd(httpResp, bufferSize)
187184
if err != nil {
@@ -577,9 +574,9 @@ func ActionsRunTrigger(t translations.TranslationHelperFunc) inventory.ServerToo
577574
runID, _ := OptionalIntParam(args, "run_id")
578575

579576
// Get optional inputs parameter
580-
var inputs map[string]interface{}
577+
var inputs map[string]any
581578
if requestInputs, ok := args["inputs"]; ok {
582-
if inputsMap, ok := requestInputs.(map[string]interface{}); ok {
579+
if inputsMap, ok := requestInputs.(map[string]any); ok {
583580
inputs = inputsMap
584581
}
585582
}
@@ -982,7 +979,7 @@ func getWorkflowRunUsage(ctx context.Context, client *github.Client, owner, repo
982979
return utils.NewToolResultText(string(r)), nil, nil
983980
}
984981

985-
func runWorkflow(ctx context.Context, client *github.Client, owner, repo, workflowID, ref string, inputs map[string]interface{}) (*mcp.CallToolResult, any, error) {
982+
func runWorkflow(ctx context.Context, client *github.Client, owner, repo, workflowID, ref string, inputs map[string]any) (*mcp.CallToolResult, any, error) {
986983
event := github.CreateWorkflowDispatchEventRequest{
987984
Ref: ref,
988985
Inputs: inputs,

pkg/github/code_scanning_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Test_GetCodeScanningAlert(t *testing.T) {
4141
tests := []struct {
4242
name string
4343
mockedClient *http.Client
44-
requestArgs map[string]interface{}
44+
requestArgs map[string]any
4545
expectError bool
4646
expectedAlert *github.Alert
4747
expectedErrMsg string
@@ -51,7 +51,7 @@ func Test_GetCodeScanningAlert(t *testing.T) {
5151
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
5252
GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber: mockResponse(t, http.StatusOK, mockAlert),
5353
}),
54-
requestArgs: map[string]interface{}{
54+
requestArgs: map[string]any{
5555
"owner": "owner",
5656
"repo": "repo",
5757
"alertNumber": float64(42),
@@ -67,7 +67,7 @@ func Test_GetCodeScanningAlert(t *testing.T) {
6767
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
6868
}),
6969
}),
70-
requestArgs: map[string]interface{}{
70+
requestArgs: map[string]any{
7171
"owner": "owner",
7272
"repo": "repo",
7373
"alertNumber": float64(9999),
@@ -158,7 +158,7 @@ func Test_ListCodeScanningAlerts(t *testing.T) {
158158
tests := []struct {
159159
name string
160160
mockedClient *http.Client
161-
requestArgs map[string]interface{}
161+
requestArgs map[string]any
162162
expectError bool
163163
expectedAlerts []*github.Alert
164164
expectedErrMsg string
@@ -175,7 +175,7 @@ func Test_ListCodeScanningAlerts(t *testing.T) {
175175
mockResponse(t, http.StatusOK, mockAlerts),
176176
),
177177
}),
178-
requestArgs: map[string]interface{}{
178+
requestArgs: map[string]any{
179179
"owner": "owner",
180180
"repo": "repo",
181181
"ref": "main",
@@ -194,7 +194,7 @@ func Test_ListCodeScanningAlerts(t *testing.T) {
194194
_, _ = w.Write([]byte(`{"message": "Unauthorized access"}`))
195195
}),
196196
}),
197-
requestArgs: map[string]interface{}{
197+
requestArgs: map[string]any{
198198
"owner": "owner",
199199
"repo": "repo",
200200
},

pkg/github/context_tools.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func GetTeams(t translations.TranslationHelperFunc) inventory.ServerTool {
179179
} `graphql:"organizations(first: 100)"`
180180
} `graphql:"user(login: $login)"`
181181
}
182-
vars := map[string]interface{}{
182+
vars := map[string]any{
183183
"login": githubv4.String(username),
184184
}
185185
if err := gqlClient.Query(ctx, &q, vars); err != nil {
@@ -262,7 +262,7 @@ func GetTeamMembers(t translations.TranslationHelperFunc) inventory.ServerTool {
262262
} `graphql:"team(slug: $teamSlug)"`
263263
} `graphql:"organization(login: $org)"`
264264
}
265-
vars := map[string]interface{}{
265+
vars := map[string]any{
266266
"org": githubv4.String(org),
267267
"teamSlug": githubv4.String(teamSlug),
268268
}

pkg/github/context_tools_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func Test_GetTeams(t *testing.T) {
215215
// to ensure each test gets a fresh client
216216
gqlClientForTestuser := func() *githubv4.Client {
217217
queryStr := "query($login:String!){user(login: $login){organizations(first: 100){nodes{login,teams(first: 100, userLogins: [$login]){nodes{name,slug,description}}}}}}"
218-
vars := map[string]interface{}{
218+
vars := map[string]any{
219219
"login": "testuser",
220220
}
221221
matcher := githubv4mock.NewQueryMatcher(queryStr, vars, mockTeamsResponse)
@@ -225,7 +225,7 @@ func Test_GetTeams(t *testing.T) {
225225

226226
gqlClientForSpecificuser := func() *githubv4.Client {
227227
queryStr := "query($login:String!){user(login: $login){organizations(first: 100){nodes{login,teams(first: 100, userLogins: [$login]){nodes{name,slug,description}}}}}}"
228-
vars := map[string]interface{}{
228+
vars := map[string]any{
229229
"login": "specificuser",
230230
}
231231
matcher := githubv4mock.NewQueryMatcher(queryStr, vars, mockTeamsResponse)
@@ -235,7 +235,7 @@ func Test_GetTeams(t *testing.T) {
235235

236236
gqlClientNoTeams := func() *githubv4.Client {
237237
queryStr := "query($login:String!){user(login: $login){organizations(first: 100){nodes{login,teams(first: 100, userLogins: [$login]){nodes{name,slug,description}}}}}}"
238-
vars := map[string]interface{}{
238+
vars := map[string]any{
239239
"login": "testuser",
240240
}
241241
matcher := githubv4mock.NewQueryMatcher(queryStr, vars, mockNoTeamsResponse)
@@ -419,7 +419,7 @@ func Test_GetTeamMembers(t *testing.T) {
419419
// Create GQL clients for different test scenarios
420420
gqlClientWithMembers := func() *githubv4.Client {
421421
queryStr := "query($org:String!$teamSlug:String!){organization(login: $org){team(slug: $teamSlug){members(first: 100){nodes{login}}}}}"
422-
vars := map[string]interface{}{
422+
vars := map[string]any{
423423
"org": "testorg",
424424
"teamSlug": "testteam",
425425
}
@@ -430,7 +430,7 @@ func Test_GetTeamMembers(t *testing.T) {
430430

431431
gqlClientNoMembers := func() *githubv4.Client {
432432
queryStr := "query($org:String!$teamSlug:String!){organization(login: $org){team(slug: $teamSlug){members(first: 100){nodes{login}}}}}"
433-
vars := map[string]interface{}{
433+
vars := map[string]any{
434434
"org": "testorg",
435435
"teamSlug": "emptyteam",
436436
}

0 commit comments

Comments
 (0)