Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/github/repository_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,14 @@ func RepositoryResourceContentsHandler(getClient GetClientFn, getRawClient raw.G
}

resp, err := rawClient.GetRawContent(ctx, owner, repo, path, rawOpts)
if err != nil {
return nil, fmt.Errorf("failed to get raw content: %w", err)
}
defer func() {
_ = resp.Body.Close()
}()
// If the raw content is not found, we will fall back to the GitHub API (in case it is a directory)
switch {
case err != nil:
return nil, fmt.Errorf("failed to get raw content: %w", err)
case resp.StatusCode == http.StatusOK:
ext := filepath.Ext(path)
mimeType := resp.Header.Get("Content-Type")
Expand Down
66 changes: 66 additions & 0 deletions pkg/github/repository_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"errors"
"net/http"
"net/url"
"testing"
Expand All @@ -14,6 +15,16 @@
"github.com/stretchr/testify/require"
)


// errorTransport is a http.RoundTripper that always returns an error.
type errorTransport struct {
err error
}

func (t *errorTransport) RoundTrip(*http.Request) (*http.Response, error) {
return nil, t.err
}

type resourceResponseType int

const (
Expand Down Expand Up @@ -312,3 +323,58 @@
})
}
}

// Test_repositoryResourceContentsHandler_NetworkError tests that a network error
// during raw content fetch does not cause a panic (nil response body dereference).
func Test_repositoryResourceContentsHandler_NetworkError(t *testing.T) {
base, _ := url.Parse("https://raw.example.com/")
networkErr := errors.New("network error: connection refused")

httpClient := &http.Client{Transport: &errorTransport{err: networkErr}}
client := github.NewClient(httpClient)
mockRawClient := raw.NewClient(client, base)
handler := RepositoryResourceContentsHandler(stubGetClientFn(client), stubGetRawClientFn(mockRawClient))

Check failure on line 336 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

not enough arguments in call to RepositoryResourceContentsHandler

Check failure on line 336 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

not enough arguments in call to RepositoryResourceContentsHandler

Check failure on line 336 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

not enough arguments in call to RepositoryResourceContentsHandler

Check failure on line 336 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

not enough arguments in call to RepositoryResourceContentsHandler

Check failure on line 336 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

not enough arguments in call to RepositoryResourceContentsHandler

request := mcp.ReadResourceRequest{
Params: struct {

Check failure on line 339 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

cannot use struct{URI string `json:"uri"`; Arguments map[string]any `json:"arguments,omitempty"`}{…} (value of type struct{URI string "json:\"uri\""; Arguments map[string]any "json:\"arguments,omitempty\""}) as *mcp.ReadResourceParams value in struct literal

Check failure on line 339 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

cannot use struct{URI string `json:"uri"`; Arguments map[string]any `json:"arguments,omitempty"`}{…} (value of type struct{URI string "json:\"uri\""; Arguments map[string]any "json:\"arguments,omitempty\""}) as *mcp.ReadResourceParams value in struct literal

Check failure on line 339 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use struct{URI string `json:"uri"`; Arguments map[string]any `json:"arguments,omitempty"`}{…} (value of type struct{URI string "json:\"uri\""; Arguments map[string]any "json:\"arguments,omitempty\""}) as *mcp.ReadResourceParams value in struct literal

Check failure on line 339 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

cannot use struct{URI string `json:"uri"`; Arguments map[string]any `json:"arguments,omitempty"`}{…} (value of type struct{URI string "json:\"uri\""; Arguments map[string]any "json:\"arguments,omitempty\""}) as *mcp.ReadResourceParams value in struct literal

Check failure on line 339 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

cannot use struct{URI string `json:"uri"`; Arguments map[string]any `json:"arguments,omitempty"`}{…} (value of type struct{URI string "json:\"uri\""; Arguments map[string]any "json:\"arguments,omitempty\""}) as *mcp.ReadResourceParams value in struct literal
URI string `json:"uri"`
Arguments map[string]any `json:"arguments,omitempty"`
}{
Arguments: map[string]any{
"owner": []string{"owner"},
"repo": []string{"repo"},
"path": []string{"README.md"},
},
},
}

// This should not panic, even though the HTTP client returns an error
resp, err := handler(context.TODO(), request)

Check failure on line 352 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

cannot use request (variable of struct type mcp.ReadResourceRequest) as *mcp.ReadResourceRequest value in argument to handler

Check failure on line 352 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

cannot use request (variable of struct type mcp.ReadResourceRequest) as *mcp.ReadResourceRequest value in argument to handler

Check failure on line 352 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use request (variable of struct type mcp.ReadResourceRequest) as *mcp.ReadResourceRequest value in argument to handler

Check failure on line 352 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

cannot use request (variable of struct type mcp.ReadResourceRequest) as *mcp.ReadResourceRequest value in argument to handler

Check failure on line 352 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

cannot use request (variable of struct type mcp.ReadResourceRequest) as *mcp.ReadResourceRequest value in argument to handler
require.Error(t, err)
require.Nil(t, resp)
require.ErrorContains(t, err, "failed to get raw content")
}

func Test_GetRepositoryResourceContent(t *testing.T) {
mockRawClient := raw.NewClient(github.NewClient(nil), &url.URL{})
tmpl, _ := GetRepositoryResourceContent(nil, stubGetRawClientFn(mockRawClient), translations.NullTranslationHelper)
require.Equal(t, "repo://{owner}/{repo}/contents{/path*}", tmpl.URITemplate.Raw())

Check failure on line 361 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 361 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 361 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 361 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 361 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)
}

func Test_GetRepositoryResourceBranchContent(t *testing.T) {
mockRawClient := raw.NewClient(github.NewClient(nil), &url.URL{})
tmpl, _ := GetRepositoryResourceBranchContent(nil, stubGetRawClientFn(mockRawClient), translations.NullTranslationHelper)
require.Equal(t, "repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}", tmpl.URITemplate.Raw())

Check failure on line 367 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 367 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 367 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 367 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 367 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)
}
func Test_GetRepositoryResourceCommitContent(t *testing.T) {
mockRawClient := raw.NewClient(github.NewClient(nil), &url.URL{})
tmpl, _ := GetRepositoryResourceCommitContent(nil, stubGetRawClientFn(mockRawClient), translations.NullTranslationHelper)
require.Equal(t, "repo://{owner}/{repo}/sha/{sha}/contents{/path*}", tmpl.URITemplate.Raw())

Check failure on line 372 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 372 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 372 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 372 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 372 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)
}

func Test_GetRepositoryResourceTagContent(t *testing.T) {
mockRawClient := raw.NewClient(github.NewClient(nil), &url.URL{})
tmpl, _ := GetRepositoryResourceTagContent(nil, stubGetRawClientFn(mockRawClient), translations.NullTranslationHelper)
require.Equal(t, "repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}", tmpl.URITemplate.Raw())

Check failure on line 378 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 378 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 378 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

tmpl.URITemplate.Raw undefined (type string has no field or method Raw) (typecheck)

Check failure on line 378 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)

Check failure on line 378 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

tmpl.URITemplate.Raw undefined (type string has no field or method Raw)
}

Loading