|
1 | 1 | package fetch |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/allero-io/allero/pkg/clients" |
4 | 7 | "github.com/allero-io/allero/pkg/configurationManager" |
| 8 | + "github.com/allero-io/allero/pkg/connectors" |
| 9 | + githubConnector "github.com/allero-io/allero/pkg/connectors/github" |
| 10 | + gitlabConnector "github.com/allero-io/allero/pkg/connectors/gitlab" |
5 | 11 | "github.com/allero-io/allero/pkg/posthog" |
6 | 12 | "github.com/spf13/cobra" |
7 | 13 | ) |
8 | 14 |
|
9 | 15 | var ( |
10 | 16 | reposFetchCounter int |
11 | | - err error |
12 | 17 | ) |
13 | 18 |
|
| 19 | +var scmPrefixes = map[string]string{ |
| 20 | + "github": "https://github.com/", |
| 21 | + "gitlab": "https://gitlab.com/", |
| 22 | +} |
| 23 | + |
14 | 24 | type FetchCommandDependencies struct { |
15 | 25 | ConfigurationManager *configurationManager.ConfigurationManager |
16 | 26 | PosthogClient *posthog.PosthogClient |
17 | 27 | } |
18 | 28 |
|
19 | | -var fetchCmd = &cobra.Command{ |
20 | | - Use: "fetch", |
21 | | - Short: "Fetch data of repositories", |
22 | | - Long: "Fetch data of repositories and entire organizations from a specified SCM platform", |
23 | | - Example: `allero fetch github allero-io dapr/dapr`, |
24 | | -} |
25 | | - |
26 | 29 | func New(deps *FetchCommandDependencies) *cobra.Command { |
| 30 | + fetchCmd := &cobra.Command{ |
| 31 | + Use: "fetch", |
| 32 | + Short: "Fetch data of repositories", |
| 33 | + Long: "Fetch data of repositories and entire organizations from several SCM platforms", |
| 34 | + Example: `allero fetch https://github.com/allero-io/allero`, |
| 35 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 36 | + return executeFetch(deps, args) |
| 37 | + }, |
| 38 | + } |
| 39 | + |
27 | 40 | fetchCmd.AddCommand(NewGithubCommand(deps)) |
28 | 41 | fetchCmd.AddCommand(NewGitlabCommand(deps)) |
29 | 42 |
|
30 | 43 | return fetchCmd |
31 | 44 | } |
| 45 | + |
| 46 | +func executeFetch(deps *FetchCommandDependencies, args []string) error { |
| 47 | + scmMapping := getAllOwnersWithRepo(args) |
| 48 | + var err error |
| 49 | + |
| 50 | + for scm, ownersWithRepos := range scmMapping { |
| 51 | + switch scm { |
| 52 | + |
| 53 | + case "github": |
| 54 | + githubConnectorDeps := &githubConnector.GithubConnectorDependencies{Client: clients.CreateGithubClient(*deps.ConfigurationManager)} |
| 55 | + githubConnector := githubConnector.New(githubConnectorDeps) |
| 56 | + |
| 57 | + reposFetchCounter, err = githubConnector.Get(ownersWithRepos) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + case "gitlab": |
| 63 | + gitlabClient, err := clients.CreateGitlabClient(*deps.ConfigurationManager) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + gitlabConnectorDeps := &gitlabConnector.GitlabConnectorDependencies{Client: gitlabClient} |
| 69 | + gitlabConnector := gitlabConnector.New(gitlabConnectorDeps) |
| 70 | + |
| 71 | + reposFetchCounter, err = gitlabConnector.Get(ownersWithRepos) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func getAllOwnersWithRepo(args []string) map[string][]*connectors.OwnerWithRepo { |
| 82 | + scmToArgs := getArgsPerScm(args) |
| 83 | + scmMapping := getReposPerScm(scmToArgs) |
| 84 | + |
| 85 | + return scmMapping |
| 86 | +} |
| 87 | + |
| 88 | +func getArgsPerScm(args []string) map[string][]string { |
| 89 | + scmToArgs := make(map[string][]string) |
| 90 | + for _, arg := range args { |
| 91 | + for scm, prefix := range scmPrefixes { |
| 92 | + if strings.HasPrefix(arg, prefix) { |
| 93 | + trimmedArg := strings.TrimPrefix(arg, prefix) |
| 94 | + scmToArgs[scm] = append(scmToArgs[scm], trimmedArg) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return scmToArgs |
| 100 | +} |
| 101 | + |
| 102 | +func getReposPerScm(scmToArgs map[string][]string) map[string][]*connectors.OwnerWithRepo { |
| 103 | + scmMapping := make(map[string][]*connectors.OwnerWithRepo) |
| 104 | + |
| 105 | + for scm, _ := range scmToArgs { |
| 106 | + ownersWithRepos := connectors.SplitParentRepo(scmToArgs[scm]) |
| 107 | + scmMapping[scm] = ownersWithRepos |
| 108 | + } |
| 109 | + |
| 110 | + return scmMapping |
| 111 | +} |
0 commit comments