diff --git a/modules/github-bots/sdk/github.go b/modules/github-bots/sdk/github.go index 584d6790..dbc6484a 100644 --- a/modules/github-bots/sdk/github.go +++ b/modules/github-bots/sdk/github.go @@ -585,6 +585,27 @@ func (c GitHubClient) GetFileContent(ctx context.Context, owner, repo, path, ref return content, nil } +// SearchContentInFilenameInRepository searches for a text in a filename in a specific repository +func (c GitHubClient) SearchContentInFilenameInRepository(ctx context.Context, owner, repo, path, content string, opt *github.ListOptions) (*github.CodeSearchResult, error) { + if opt == nil { + opt = &github.ListOptions{} + } + query := fmt.Sprintf("%s in:file filename:%s repo:%s/%s", content, path, owner, repo) + result, resp, err := c.inner.Search.Code( + ctx, + query, + &github.SearchOptions{ + ListOptions: *opt, + }, + ) + + if err := validateResponse(ctx, err, resp, fmt.Sprintf("search content %s in repository", content)); err != nil { + return &github.CodeSearchResult{}, err + } + + return result, nil +} + // SearchFilenameInRepository searches for a filename in a specific repository func (c GitHubClient) SearchFilenameInRepository(ctx context.Context, owner, repo, path string, opt *github.ListOptions) (*github.CodeSearchResult, error) { if opt == nil { diff --git a/modules/github-bots/sdk/github_integration_test.go b/modules/github-bots/sdk/github_integration_test.go index ed79c85d..5c681ed0 100644 --- a/modules/github-bots/sdk/github_integration_test.go +++ b/modules/github-bots/sdk/github_integration_test.go @@ -34,3 +34,27 @@ func Test_SearchFilenameInRepository(t *testing.T) { t.Fatalf("SearchFilenameInRepository result is zero\n") } } + +// NOTE: This is an integration test that requires 'GITHUB_TOKEN' env variable to be set! +// It is recommended to run this test in a local environment. +func Test_SearchContentInFilenameInRepository(t *testing.T) { + ctx := context.Background() + + if os.Getenv("GITHUB_TOKEN") == "" { + t.Fatalf("GITHUB_TOKEN env var not set\n") + } + + // create a GitHub client + repoOrg := "kserve" + repoName := "kserve" + // sdk allows an override of GIT_TOKEN env var so we can test from a local environment + cli := NewGitHubClient(ctx, repoOrg, repoName, "test") + result, err := cli.SearchContentInFilenameInRepository(ctx, repoOrg, repoName, "pyproject.toml", "py37", &github.ListOptions{}) + if err != nil { + t.Fatalf("SearchContentInFilenameInRepository err: %v\n", err) + } + + if *result.Total == 0 { + t.Fatalf("SearchContentInFilenameInRepository result is zero\n") + } +}