From 82ee3166c2ce0d080f43fd70c20757cf9d8202fc Mon Sep 17 00:00:00 2001 From: hectorj2f Date: Wed, 27 Nov 2024 19:52:40 +0100 Subject: [PATCH 1/3] github-bots: add a function to search for a filename in a repo Signed-off-by: hectorj2f --- modules/github-bots/sdk/github.go | 16 ++++++++++++++++ modules/github-bots/test/main.go | 1 + 2 files changed, 17 insertions(+) create mode 100644 modules/github-bots/test/main.go diff --git a/modules/github-bots/sdk/github.go b/modules/github-bots/sdk/github.go index 3f43dc2b..e7d0adad 100644 --- a/modules/github-bots/sdk/github.go +++ b/modules/github-bots/sdk/github.go @@ -579,6 +579,22 @@ 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) (*github.CodeSearchResult, error) { + query := fmt.Sprintf("filename:%s repo:%s/%s", path, owner, repo) + result, resp, err := c.inner.Search.Code( + ctx, + query, + &github.SearchOptions{}, + ) + + 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/test/main.go b/modules/github-bots/test/main.go new file mode 100644 index 00000000..56e54040 --- /dev/null +++ b/modules/github-bots/test/main.go @@ -0,0 +1 @@ +package test From 88590ada94815a3b0aff0ade8c71602f34fa27e3 Mon Sep 17 00:00:00 2001 From: hectorj2f Date: Wed, 27 Nov 2024 21:00:49 +0100 Subject: [PATCH 2/3] add ListOptions to the list of parameters Signed-off-by: hectorj2f --- modules/github-bots/sdk/github.go | 9 +++++++-- modules/github-bots/test/main.go | 1 - 2 files changed, 7 insertions(+), 3 deletions(-) delete mode 100644 modules/github-bots/test/main.go diff --git a/modules/github-bots/sdk/github.go b/modules/github-bots/sdk/github.go index e7d0adad..ae0559a2 100644 --- a/modules/github-bots/sdk/github.go +++ b/modules/github-bots/sdk/github.go @@ -580,12 +580,17 @@ func (c GitHubClient) GetFileContent(ctx context.Context, owner, repo, path, ref } // SearchFilenameInRepository searches for a filename in a specific repository -func (c GitHubClient) SearchFilenameInRepository(ctx context.Context, owner, repo, path string) (*github.CodeSearchResult, error) { +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{}, + &github.SearchOptions{ + ListOptions: *opt, + }, ) if err := validateResponse(ctx, err, resp, fmt.Sprintf("search filename %s in repository", path)); err != nil { diff --git a/modules/github-bots/test/main.go b/modules/github-bots/test/main.go deleted file mode 100644 index 56e54040..00000000 --- a/modules/github-bots/test/main.go +++ /dev/null @@ -1 +0,0 @@ -package test From 8ce35e57ab55943cc6e0ca2475e9994d47aaf596 Mon Sep 17 00:00:00 2001 From: hectorj2f Date: Wed, 27 Nov 2024 21:14:54 +0100 Subject: [PATCH 3/3] test: add integration test for github Signed-off-by: hectorj2f --- .../sdk/github_integration_test.go | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 modules/github-bots/sdk/github_integration_test.go 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") + } +}