generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 185
feat: implement recursive CRD discovery and robust GitHub URL parsing #887
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
Open
muhammadolammi
wants to merge
17
commits into
meshery:master
Choose a base branch
from
muhammadolammi:feat/auto-discovery-of-crd-gh
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8196b38
fix ExtractZip function, so all folders inside zip are created in cur…
muhammadolammi 067a69f
check for files to skip before creating the dir and add __MACOSX to s…
muhammadolammi 494fe1e
fix Zip Slip attack and add test
muhammadolammi f177806
add test that fail for old function
muhammadolammi 9002212
add test that fail for old function
muhammadolammi d3cd80e
add test that fail for old function
muhammadolammi fb4ff3d
make err a mesherror
muhammadolammi b8ab325
copy from dev
muhammadolammi 6c4c078
Added [Feature] Auto-discover CRDs from repository root URL
muhammadolammi d843222
intended tests
muhammadolammi 5715b0f
route https protocol to git on certain conditions
muhammadolammi 477ce39
add more tests
muhammadolammi 01e3717
add robust test and use temp dir for tests
muhammadolammi 0225ce3
solve potential panic error
muhammadolammi fec7814
Update generators/github/git_repo.go
aabidsofi19 c01a7c0
Merge branch 'master' into feat/auto-discovery-of-crd-gh
simihablo 9c2f3c5
Merge branch 'master' into feat/auto-discovery-of-crd-gh
leecalcote File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| ) | ||
|
|
||
| func GetDefaultBranchFromGitHub(baseUrl, owner, repo string, client *http.Client) (string, error) { | ||
| if client == nil { | ||
| client = http.DefaultClient | ||
| } | ||
| url := fmt.Sprintf("%s/repos/%s/%s", baseUrl, owner, repo) | ||
| resp, err := client.Get(url) | ||
| if err != nil { | ||
| return "", ErrGetDefaultBranch(err, owner, repo) | ||
| } | ||
| defer resp.Body.Close() | ||
| if resp.StatusCode != 200 { | ||
| return "", ErrGetDefaultBranch(fmt.Errorf("github api: %d", resp.StatusCode), owner, repo) | ||
| } | ||
| var out struct { | ||
| DefaultBranch string `json:"default_branch"` | ||
| } | ||
| if err := json.NewDecoder(resp.Body).Decode(&out); err != nil { | ||
| return "", ErrGetDefaultBranch(err, owner, repo) | ||
| } | ||
| return out.DefaultBranch, nil | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGetGithubRepoBranch(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| owner string | ||
| repo string | ||
| mockStatus int | ||
| mockBody any // Using any allows us to pass structs or raw strings | ||
| wantBranch string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "valid repo", | ||
| owner: "octocat", | ||
| repo: "Hello-World", | ||
| mockStatus: http.StatusOK, | ||
| mockBody: map[string]string{"default_branch": "main"}, | ||
| wantBranch: "main", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "valid repo with master branch", | ||
| owner: "octocat", | ||
| repo: "Hello-Mesh", | ||
| mockStatus: http.StatusOK, | ||
| mockBody: map[string]string{"default_branch": "master"}, | ||
| wantBranch: "master", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "repo not found", | ||
| owner: "octocat", | ||
| repo: "NonExistentRepo", | ||
| mockStatus: http.StatusNotFound, | ||
| mockBody: map[string]string{"message": "Not Found"}, | ||
| wantBranch: "", | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| expectedPath := fmt.Sprintf("/repos/%s/%s", tt.owner, tt.repo) | ||
| if r.URL.Path != expectedPath { | ||
| t.Errorf("expected path %s, got %s", expectedPath, r.URL.Path) | ||
| } | ||
|
|
||
| w.WriteHeader(tt.mockStatus) | ||
| json.NewEncoder(w).Encode(tt.mockBody) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| branch, err := GetDefaultBranchFromGitHub(server.URL, tt.owner, tt.repo, server.Client()) | ||
|
|
||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("GetDefaultBranchFromGitHub() unexpected error state: %v", err) | ||
| } | ||
| if branch != tt.wantBranch { | ||
| t.Errorf("got branch %q, want %q", branch, tt.wantBranch) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "net/url" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestExtractRepoDetailsFromSourceURL(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| wantOwner string | ||
| wantRepo string | ||
| wantBranch string | ||
| wantRoot string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "owner and repo only", | ||
| input: "git://github.com/meshery/meshkit", | ||
| wantOwner: "meshery", | ||
| wantRepo: "meshkit", | ||
| wantBranch: "master", | ||
| wantRoot: "/**", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "owner repo and branch", | ||
| input: "git://github.com/meshery/meshkit/master", | ||
| wantOwner: "meshery", | ||
| wantRepo: "meshkit", | ||
| wantBranch: "master", | ||
| wantRoot: "/**", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "owner repo branch and path", | ||
| input: "git://github.com/meshery/meshkit/master/install/kubernetes", | ||
| wantOwner: "meshery", | ||
| wantRepo: "meshkit", | ||
| wantBranch: "master", | ||
| wantRoot: "install/kubernetes", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "invalid single component", | ||
| input: "git://github.com/meshery", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "trailing slash on repo", | ||
| input: "git://github.com/meshery/meshkit/", | ||
| wantOwner: "meshery", | ||
| wantRepo: "meshkit", | ||
| wantBranch: "master", | ||
| wantRoot: "/**", | ||
| wantErr: false, | ||
| }, | ||
| // AFter gemini review these should gail | ||
| { | ||
| name: "trailing slash triggers default branch", | ||
| input: "git://github.com/meshery/meshkit/", // Trailing slash | ||
| wantOwner: "meshery", | ||
| wantRepo: "meshkit", | ||
| wantBranch: "master", // The old code would return "" here | ||
| wantRoot: "/**", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "nested path depth greater than 4", | ||
| input: "git://github.com/meshery/meshkit/master/install/kubernetes/helm/meshery", | ||
| wantOwner: "meshery", | ||
| wantRepo: "meshkit", | ||
| wantBranch: "master", | ||
| wantRoot: "install/kubernetes/helm/meshery", // The old code would cut this off | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "multiple slashes in path", | ||
| input: "git://github.com/meshery/meshkit///master/", | ||
| wantOwner: "meshery", | ||
| wantRepo: "meshkit", | ||
| wantBranch: "master", | ||
| wantRoot: "/**", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "GitHub Browser URL - Tree", | ||
| input: "https://github.com/meshery/meshkit/tree/master/install", | ||
| wantOwner: "meshery", | ||
| wantRepo: "meshkit", | ||
| wantBranch: "master", | ||
| wantRoot: "install", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "GitHub Browser URL - Blob", | ||
| input: "https://github.com/meshery/meshkit/blob/master/models/meshmodel.yaml", | ||
| wantOwner: "meshery", | ||
| wantRepo: "meshkit", | ||
| wantBranch: "master", | ||
| wantRoot: "models/meshmodel.yaml", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "GitHub Release URL", | ||
| input: "https://github.com/meshery/meshkit/releases/tag/v0.6.0", | ||
| wantOwner: "meshery", | ||
| wantRepo: "meshkit", | ||
| wantBranch: "v0.6.0", | ||
| wantRoot: "/**", | ||
| wantErr: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| u, err := url.Parse(tt.input) | ||
| if err != nil { | ||
| t.Fatalf("failed to parse url %s: %v", tt.input, err) | ||
| } | ||
| gr := GitRepo{URL: u} | ||
| owner, repo, branch, root, err := gr.ExtractRepoDetailsFromSourceURL() | ||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("unexpected error state: got err=%v, wantErr=%v", err, tt.wantErr) | ||
| } | ||
| if err != nil { | ||
| // on error we are done | ||
| return | ||
| } | ||
| if owner != tt.wantOwner { | ||
| t.Fatalf("owner: got %q want %q", owner, tt.wantOwner) | ||
| } | ||
| if repo != tt.wantRepo { | ||
| t.Fatalf("repo: got %q want %q", repo, tt.wantRepo) | ||
| } | ||
| if branch != tt.wantBranch { | ||
| t.Fatalf("branch: got %q want %q", branch, tt.wantBranch) | ||
| } | ||
| if root != tt.wantRoot { | ||
| t.Fatalf("root: got %q want %q", root, tt.wantRoot) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test makes real network calls to the GitHub API when a URL with only an owner and repo is provided (e.g.,
git://github.com/meshery/meshkit). This makes the test non-hermetic, slow, and potentially flaky, depending on network conditions and the state of the remote repository (e.g., if the default branch changes).Unit tests should be self-contained and not rely on external services. The call to
GetDefaultBranchFromGitHubwithinextractRepoDetailsFromSourceURLshould be mocked.One way to achieve this is to introduce a function variable in
git_repo.gothat can be replaced during tests.In
generators/github/git_repo.go:Then, in
generators/github/git_repo_test.go, you can mock it:This change will make your tests fast, reliable, and independent of external services.