Skip to content

Commit

Permalink
github-bots: add a function to search for a filename in a repo (#643)
Browse files Browse the repository at this point in the history
On the ai remediation bot, we need to search for specific filenames on a
repository such as go.mod, pyproject.toml, Cargo.toml... If these files
are found then we get the content specifying the path using another
existing function GetFileContent which would be used to generate a
precise patch.

---------

Signed-off-by: hectorj2f <[email protected]>
  • Loading branch information
hectorj2f authored Nov 27, 2024
1 parent f5d0763 commit 8333060
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
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
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")
}
}

0 comments on commit 8333060

Please sign in to comment.