-
Notifications
You must be signed in to change notification settings - Fork 28
Fix handle corrupted repos In BitBucket utils contributor-count (AST-76750) #980
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
Merged
cx-itay-paz
merged 11 commits into
main
from
Bug/Fix-contributor-cout-bitbucket-corrupted-repos
Dec 25, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a2a87f6
Fix handle corrupted repos
cx-itay-paz 814c68f
Add test to bitBucket searchRepos with Corrupted Repo
cx-itay-paz 9184939
Fix lint
cx-itay-paz 5856184
Merge branch 'main' into Bug/Fix-contributor-cout-bitbucket-corrupted…
cx-itay-paz b1aa6f5
Change sructs names in mock file
cx-itay-paz 2326c47
Fix test to use bitbucket server wrapper and change names of structs …
cx-itay-paz e90978b
Fix lint errors
cx-itay-paz 623d58e
Fix linter
cx-itay-paz ac1e441
Fix space in linter
cx-itay-paz f6df434
fix linter
cx-itay-paz 7d551bb
Merge branch 'main' into Bug/Fix-contributor-cout-bitbucket-corrupted…
cx-itay-paz 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
48 changes: 48 additions & 0 deletions
48
internal/commands/util/usercount/bitbucketserver/bitbucket-server_test.go
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,48 @@ | ||
| package bitbucketserver | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "log" | ||
| "testing" | ||
|
|
||
| "github.com/checkmarx/ast-cli/internal/wrappers/mock" | ||
| "gotest.tools/assert" | ||
| ) | ||
|
|
||
| func TestSearchReposWithCorruptedRepositories(t *testing.T) { | ||
| mockWrapper := mock.WrapperBitbucketServer{ | ||
| CorruptedRepos: []string{"repo-2"}, | ||
| } | ||
|
|
||
| var logOutput bytes.Buffer | ||
| log.SetOutput(&logOutput) | ||
| defer log.SetOutput(nil) | ||
|
|
||
| project := "mock-project" | ||
| repos := []string{"repo-1", "repo-2", "repo-3"} | ||
| token := "mock-token" | ||
|
|
||
| views, viewsUsers, err := mockWrapper.SearchRepos(project, repos, token) | ||
|
|
||
| assert.NilError(t, err, "SearchRepos should not return an error") | ||
|
|
||
| assert.Equal(t, len(views), 2, "Only valid repositories should be processed") | ||
| assert.Equal(t, views[0].Name, "mock-project/repo-1", "First repository name should match") | ||
| assert.Equal(t, views[1].Name, "mock-project/repo-3", "Second repository name should match") | ||
|
|
||
| assert.Equal(t, len(viewsUsers), 2, "Each repository should have 1 contributor") | ||
| assert.Equal(t, viewsUsers[0].Name, "mock-project/repo-1", "Contributor should match first repository") | ||
| assert.Equal(t, viewsUsers[1].Name, "mock-project/repo-3", "Contributor should match second repository") | ||
|
|
||
| logStr := logOutput.String() | ||
| assert.Assert(t, containsLog(logStr, "Skipping repository mock-project/repo-2: Repository is corrupted"), "Log should contain corrupted repository message") | ||
| assert.Assert(t, containsLog(logStr, "Processed repository mock-project/repo-1"), "Log should confirm successful processing of repo-1") | ||
| assert.Assert(t, containsLog(logStr, "Processed repository mock-project/repo-3"), "Log should confirm successful processing of repo-3") | ||
|
|
||
| t.Log("Captured Logs:") | ||
| t.Log(logStr) | ||
| } | ||
|
|
||
| func containsLog(logStr, expected string) bool { | ||
| return bytes.Contains([]byte(logStr), []byte(expected)) | ||
| } |
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,85 @@ | ||
| package mock | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
|
|
||
| "github.com/checkmarx/ast-cli/internal/wrappers/bitbucketserver" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| type WrapperBitbucketServer struct { | ||
| CorruptedRepos []string | ||
| } | ||
|
|
||
| type RepositoryView struct { | ||
| Name string `json:"name"` | ||
| UniqueContributors uint64 `json:"unique_contributors"` | ||
| } | ||
|
|
||
| type UserView struct { | ||
| Name string `json:"name"` | ||
| UniqueContributorsUsername string `json:"unique_contributors_username"` | ||
| } | ||
|
|
||
| func (m WrapperBitbucketServer) GetCommits(bitBucketURL, projectKey, repoSlug, bitBucketPassword string) ([]bitbucketserver.Commit, error) { | ||
| for _, corruptedRepo := range m.CorruptedRepos { | ||
| if repoSlug == corruptedRepo { | ||
| return nil, errors.New(fmt.Sprintf("repository %s is corrupted", repoSlug)) | ||
| } | ||
| } | ||
| return []bitbucketserver.Commit{ | ||
| { | ||
| Author: bitbucketserver.Author{Name: "Mock Author", Email: "[email protected]"}, | ||
| AuthorTimestamp: 1625078400000, | ||
| }, | ||
| }, nil | ||
| } | ||
| func (m WrapperBitbucketServer) GetRepositories(bitBucketURL, projectKey, bitBucketPassword string) ([]bitbucketserver.Repo, error) { | ||
| return []bitbucketserver.Repo{ | ||
| {Slug: "repo-1", Name: "Repository 1"}, | ||
| {Slug: "repo-2", Name: "Repository 2"}, | ||
| {Slug: "repo-3", Name: "Repository 3"}, | ||
| }, nil | ||
| } | ||
| func (m WrapperBitbucketServer) GetProjects(bitBucketURL, bitBucketPassword string) ([]string, error) { | ||
| // Return mock projects | ||
| return []string{"project-1", "project-2"}, nil | ||
| } | ||
| func (m WrapperBitbucketServer) SearchRepos( | ||
| project string, | ||
| repos []string, | ||
| bitBucketToken string, | ||
| ) ([]RepositoryView, []UserView, error) { | ||
| var views []RepositoryView | ||
| var viewsUsers []UserView | ||
| for _, repo := range repos { | ||
| _, err := m.GetCommits("mock-url", project, repo, bitBucketToken) | ||
| if err != nil { | ||
| log.Printf("Skipping repository %s/%s: Repository is corrupted (error: %v)", project, repo, err) | ||
| continue | ||
| } | ||
| log.Printf("Processed repository %s/%s", project, repo) | ||
|
|
||
| uniqueContributors := map[string]string{ | ||
| "[email protected]": "Mock Author", | ||
| } | ||
| views = append( | ||
| views, | ||
| RepositoryView{ | ||
| Name: fmt.Sprintf("%s/%s", project, repo), | ||
| UniqueContributors: uint64(len(uniqueContributors)), | ||
| }, | ||
| ) | ||
| for email, name := range uniqueContributors { | ||
| viewsUsers = append( | ||
| viewsUsers, | ||
| UserView{ | ||
| Name: fmt.Sprintf("%s/%s", project, repo), | ||
| UniqueContributorsUsername: fmt.Sprintf("%s - %s", name, email), | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
| return views, viewsUsers, nil | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.