|
| 1 | +package mock |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/checkmarx/ast-cli/internal/wrappers/bitbucketserver" |
| 8 | + "github.com/pkg/errors" |
| 9 | +) |
| 10 | + |
| 11 | +type WrapperBitbucketServer struct { |
| 12 | + CorruptedRepos []string |
| 13 | +} |
| 14 | + |
| 15 | +type RepositoryView struct { |
| 16 | + Name string `json:"name"` |
| 17 | + UniqueContributors uint64 `json:"unique_contributors"` |
| 18 | +} |
| 19 | + |
| 20 | +type UserView struct { |
| 21 | + Name string `json:"name"` |
| 22 | + UniqueContributorsUsername string `json:"unique_contributors_username"` |
| 23 | +} |
| 24 | + |
| 25 | +func (m WrapperBitbucketServer) GetCommits(bitBucketURL, projectKey, repoSlug, bitBucketPassword string) ([]bitbucketserver.Commit, error) { |
| 26 | + for _, corruptedRepo := range m.CorruptedRepos { |
| 27 | + if repoSlug == corruptedRepo { |
| 28 | + return nil, errors.New(fmt.Sprintf("repository %s is corrupted", repoSlug)) |
| 29 | + } |
| 30 | + } |
| 31 | + return []bitbucketserver.Commit{ |
| 32 | + { |
| 33 | + Author: bitbucketserver. Author{ Name: "Mock Author", Email: "[email protected]"}, |
| 34 | + AuthorTimestamp: 1625078400000, |
| 35 | + }, |
| 36 | + }, nil |
| 37 | +} |
| 38 | +func (m WrapperBitbucketServer) GetRepositories(bitBucketURL, projectKey, bitBucketPassword string) ([]bitbucketserver.Repo, error) { |
| 39 | + return []bitbucketserver.Repo{ |
| 40 | + {Slug: "repo-1", Name: "Repository 1"}, |
| 41 | + {Slug: "repo-2", Name: "Repository 2"}, |
| 42 | + {Slug: "repo-3", Name: "Repository 3"}, |
| 43 | + }, nil |
| 44 | +} |
| 45 | +func (m WrapperBitbucketServer) GetProjects(bitBucketURL, bitBucketPassword string) ([]string, error) { |
| 46 | + // Return mock projects |
| 47 | + return []string{"project-1", "project-2"}, nil |
| 48 | +} |
| 49 | +func (m WrapperBitbucketServer) SearchRepos( |
| 50 | + project string, |
| 51 | + repos []string, |
| 52 | + bitBucketToken string, |
| 53 | +) ([]RepositoryView, []UserView, error) { |
| 54 | + var views []RepositoryView |
| 55 | + var viewsUsers []UserView |
| 56 | + for _, repo := range repos { |
| 57 | + _, err := m.GetCommits("mock-url", project, repo, bitBucketToken) |
| 58 | + if err != nil { |
| 59 | + log.Printf("Skipping repository %s/%s: Repository is corrupted (error: %v)", project, repo, err) |
| 60 | + continue |
| 61 | + } |
| 62 | + log.Printf("Processed repository %s/%s", project, repo) |
| 63 | + |
| 64 | + uniqueContributors := map[string]string{ |
| 65 | + "[email protected]": "Mock Author", |
| 66 | + } |
| 67 | + views = append( |
| 68 | + views, |
| 69 | + RepositoryView{ |
| 70 | + Name: fmt.Sprintf("%s/%s", project, repo), |
| 71 | + UniqueContributors: uint64(len(uniqueContributors)), |
| 72 | + }, |
| 73 | + ) |
| 74 | + for email, name := range uniqueContributors { |
| 75 | + viewsUsers = append( |
| 76 | + viewsUsers, |
| 77 | + UserView{ |
| 78 | + Name: fmt.Sprintf("%s/%s", project, repo), |
| 79 | + UniqueContributorsUsername: fmt.Sprintf("%s - %s", name, email), |
| 80 | + }, |
| 81 | + ) |
| 82 | + } |
| 83 | + } |
| 84 | + return views, viewsUsers, nil |
| 85 | +} |
0 commit comments