Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github-bots: add a function to search for a filename in a repo #643

Merged
merged 3 commits into from
Nov 27, 2024
Merged
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
21 changes: 21 additions & 0 deletions modules/github-bots/sdk/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
hectorj2f marked this conversation as resolved.
Show resolved Hide resolved
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}
Expand Down
36 changes: 36 additions & 0 deletions modules/github-bots/sdk/github_integration_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}