diff --git a/modules/github-bots/sdk/github.go b/modules/github-bots/sdk/github.go index 3f43dc2b..ae0559a2 100644 --- a/modules/github-bots/sdk/github.go +++ b/modules/github-bots/sdk/github.go @@ -579,6 +579,27 @@ func (c GitHubClient) GetFileContent(ctx context.Context, owner, repo, path, ref return content, 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 { + opt = &github.ListOptions{} + } + query := fmt.Sprintf("filename:%s repo:%s/%s", 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 filename %s in repository", path)); err != nil { + return &github.CodeSearchResult{}, err + } + + return result, nil +} + // ListFiles lists the files in a directory at a given ref func (c GitHubClient) ListFiles(ctx context.Context, owner, repo, path, ref string) ([]*github.RepositoryContent, error) { opts := &github.RepositoryContentGetOptions{Ref: ref} diff --git a/modules/github-bots/sdk/github_integration_test.go b/modules/github-bots/sdk/github_integration_test.go new file mode 100644 index 00000000..ed79c85d --- /dev/null +++ b/modules/github-bots/sdk/github_integration_test.go @@ -0,0 +1,36 @@ +//go:build integration +// +build integration + +package sdk + +import ( + "context" + "os" + "testing" + + "github.com/google/go-github/v61/github" +) + +// 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_SearchFilenameInRepository(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.SearchFilenameInRepository(ctx, repoOrg, repoName, "pyproject.toml", &github.ListOptions{}) + if err != nil { + t.Fatalf("SearchFilenameInRepository err: %v\n", err) + } + + if *result.Total == 0 { + t.Fatalf("SearchFilenameInRepository result is zero\n") + } +}