Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
feat: support fetch with url (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimabru authored Oct 6, 2022
1 parent e682684 commit 1c480ff
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 56 deletions.
11 changes: 10 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@
"showLog": true
},
{
"name": "Fetch",
"name": "Fetch Github",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/main.go",
"env": {},
"args": ["fetch", "github", "allero-io"],
},
{
"name": "Fetch",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/main.go",
"env": {},
"args": ["fetch", "https://github.com/allero-io/allero", "https://gitlab.com/allero"],
}
]
}
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ clear-token:
create-bin:
goreleaser --snapshot --skip-publish --rm-dist

fetch:
go run main.go fetch https://github.com/allero-io/allero https://gitlab.com/allero

github:
# go run main.go fetch github supran2811/familyApp
go run main.go fetch github curbengh/hexo-yam
go run main.go fetch github supran2811/familyApp

gitlab:
# go run main.go fetch gitlab GitLab-examples/clojure-web-application
go run main.go fetch gitlab allero/demo
go run main.go fetch gitlab allero
25 changes: 15 additions & 10 deletions cmd/fetch/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package fetch

import (
"github.com/allero-io/allero/pkg/clients"
"github.com/allero-io/allero/pkg/connectors"
githubConnector "github.com/allero-io/allero/pkg/connectors/github"
"github.com/google/go-github/github"
"github.com/spf13/cobra"
)

type FetchGithubDependencies struct {
GithubClient *github.Client
GithubClient *github.Client
OwnersWithRepos []*connectors.OwnerWithRepo
}

var GITHUB_TOKEN string

func NewGithubCommand(deps *FetchCommandDependencies) *cobra.Command {
githubCmd := &cobra.Command{
Use: "github org/repo...",
Expand All @@ -30,17 +30,19 @@ func NewGithubCommand(deps *FetchCommandDependencies) *cobra.Command {
deps.PosthogClient.PublishEventWithArgs("data fetched", args)
},
RunE: func(cmd *cobra.Command, args []string) error {
GITHUB_TOKEN = deps.ConfigurationManager.GetGithubToken()
githubClient := clients.CreateGithubClient(GITHUB_TOKEN)
ownersWithRepos := connectors.SplitParentRepo(args)
githubClient := clients.CreateGithubClient(*deps.ConfigurationManager)

fetchGithubDeps := &FetchGithubDependencies{
GithubClient: githubClient,
GithubClient: githubClient,
OwnersWithRepos: ownersWithRepos,
}

return executeGithub(fetchGithubDeps, args)
return executeGithub(fetchGithubDeps)
},
PostRun: func(cmd *cobra.Command, args []string) {
tokenWasProvided := GITHUB_TOKEN != ""
githubToken := deps.ConfigurationManager.GetGithubToken()
tokenWasProvided := githubToken != ""

analyticsArgs := make(map[string]any)
analyticsArgs["Total Fetched Repos"] = reposFetchCounter
Expand All @@ -52,10 +54,13 @@ func NewGithubCommand(deps *FetchCommandDependencies) *cobra.Command {
return githubCmd
}

func executeGithub(deps *FetchGithubDependencies, args []string) error {
func executeGithub(deps *FetchGithubDependencies) error {
githubConnectorDeps := &githubConnector.GithubConnectorDependencies{Client: deps.GithubClient}
githubConnector := githubConnector.New(githubConnectorDeps)
reposFetchCounter, err = githubConnector.Get(args)

var err error
reposFetchCounter, err = githubConnector.Get(deps.OwnersWithRepos)

if err != nil {
return err
}
Expand Down
23 changes: 13 additions & 10 deletions cmd/fetch/gitlab.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package fetch

import (
"github.com/allero-io/allero/pkg/clients"
"github.com/allero-io/allero/pkg/connectors"
gitlabConnector "github.com/allero-io/allero/pkg/connectors/gitlab"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
)

type FetchGitlabDependencies struct {
GitlabClient *gitlab.Client
GitlabClient *gitlab.Client
OwnersWithRepos []*connectors.OwnerWithRepo
}

var GITLAB_TOKEN string

func NewGitlabCommand(deps *FetchCommandDependencies) *cobra.Command {
githubCmd := &cobra.Command{
Use: "gitlab org/repo...",
Expand All @@ -29,19 +30,20 @@ func NewGitlabCommand(deps *FetchCommandDependencies) *cobra.Command {
deps.PosthogClient.PublishEventWithArgs("data fetched", args)
},
RunE: func(cmd *cobra.Command, args []string) error {
GITLAB_TOKEN = deps.ConfigurationManager.GetGitlabToken()
gitlabClient, err := gitlab.NewClient(GITLAB_TOKEN)
ownersWithRepos := connectors.SplitParentRepo(args)
gitlabClient, err := clients.CreateGitlabClient(*deps.ConfigurationManager)
if err != nil {
return err
}

fetchGitlabDeps := &FetchGitlabDependencies{
GitlabClient: gitlabClient,
GitlabClient: gitlabClient,
OwnersWithRepos: ownersWithRepos,
}
return executeGitlab(fetchGitlabDeps, args)
return executeGitlab(fetchGitlabDeps)
},
PostRun: func(cmd *cobra.Command, args []string) {
tokenWasProvided := GITHUB_TOKEN != ""
tokenWasProvided := deps.ConfigurationManager.GetGitlabToken()

analyticsArgs := make(map[string]any)
analyticsArgs["Total Fetched Repos"] = reposFetchCounter
Expand All @@ -53,10 +55,11 @@ func NewGitlabCommand(deps *FetchCommandDependencies) *cobra.Command {
return githubCmd
}

func executeGitlab(deps *FetchGitlabDependencies, args []string) error {
func executeGitlab(deps *FetchGitlabDependencies) error {
gitlabConnectorDeps := &gitlabConnector.GitlabConnectorDependencies{Client: deps.GitlabClient}
gitlabConnector := gitlabConnector.New(gitlabConnectorDeps)

reposFetchCounter, err = gitlabConnector.Get(args)
var err error
reposFetchCounter, err = gitlabConnector.Get(deps.OwnersWithRepos)
return err
}
96 changes: 88 additions & 8 deletions cmd/fetch/main.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,111 @@
package fetch

import (
"strings"

"github.com/allero-io/allero/pkg/clients"
"github.com/allero-io/allero/pkg/configurationManager"
"github.com/allero-io/allero/pkg/connectors"
githubConnector "github.com/allero-io/allero/pkg/connectors/github"
gitlabConnector "github.com/allero-io/allero/pkg/connectors/gitlab"
"github.com/allero-io/allero/pkg/posthog"
"github.com/spf13/cobra"
)

var (
reposFetchCounter int
err error
)

var scmPrefixes = map[string]string{
"github": "https://github.com/",
"gitlab": "https://gitlab.com/",
}

type FetchCommandDependencies struct {
ConfigurationManager *configurationManager.ConfigurationManager
PosthogClient *posthog.PosthogClient
}

var fetchCmd = &cobra.Command{
Use: "fetch",
Short: "Fetch data of repositories",
Long: "Fetch data of repositories and entire organizations from a specified SCM platform",
Example: `allero fetch github allero-io dapr/dapr`,
}

func New(deps *FetchCommandDependencies) *cobra.Command {
fetchCmd := &cobra.Command{
Use: "fetch",
Short: "Fetch data of repositories",
Long: "Fetch data of repositories and entire organizations from several SCM platforms",
Example: `allero fetch https://github.com/allero-io/allero`,
RunE: func(cmd *cobra.Command, args []string) error {
return executeFetch(deps, args)
},
}

fetchCmd.AddCommand(NewGithubCommand(deps))
fetchCmd.AddCommand(NewGitlabCommand(deps))

return fetchCmd
}

func executeFetch(deps *FetchCommandDependencies, args []string) error {
scmMapping := getAllOwnersWithRepo(args)
var err error

for scm, ownersWithRepos := range scmMapping {
switch scm {

case "github":
githubConnectorDeps := &githubConnector.GithubConnectorDependencies{Client: clients.CreateGithubClient(*deps.ConfigurationManager)}
githubConnector := githubConnector.New(githubConnectorDeps)

reposFetchCounter, err = githubConnector.Get(ownersWithRepos)
if err != nil {
return err
}

case "gitlab":
gitlabClient, err := clients.CreateGitlabClient(*deps.ConfigurationManager)
if err != nil {
return err
}

gitlabConnectorDeps := &gitlabConnector.GitlabConnectorDependencies{Client: gitlabClient}
gitlabConnector := gitlabConnector.New(gitlabConnectorDeps)

reposFetchCounter, err = gitlabConnector.Get(ownersWithRepos)
if err != nil {
return err
}
}
}

return nil
}

func getAllOwnersWithRepo(args []string) map[string][]*connectors.OwnerWithRepo {
scmToArgs := getArgsPerScm(args)
scmMapping := getReposPerScm(scmToArgs)

return scmMapping
}

func getArgsPerScm(args []string) map[string][]string {
scmToArgs := make(map[string][]string)
for _, arg := range args {
for scm, prefix := range scmPrefixes {
if strings.HasPrefix(arg, prefix) {
trimmedArg := strings.TrimPrefix(arg, prefix)
scmToArgs[scm] = append(scmToArgs[scm], trimmedArg)
}
}
}

return scmToArgs
}

func getReposPerScm(scmToArgs map[string][]string) map[string][]*connectors.OwnerWithRepo {
scmMapping := make(map[string][]*connectors.OwnerWithRepo)

for scm, _ := range scmToArgs {
ownersWithRepos := connectors.SplitParentRepo(scmToArgs[scm])
scmMapping[scm] = ownersWithRepos
}

return scmMapping
}
4 changes: 3 additions & 1 deletion pkg/clients/githubClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"net/http"

"github.com/allero-io/allero/pkg/configurationManager"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)

func CreateGithubClient(githubToken string) *github.Client {
func CreateGithubClient(configurationManager configurationManager.ConfigurationManager) *github.Client {
githubToken := configurationManager.GetGithubToken()
httpClient := &http.Client{}

if githubToken != "" {
Expand Down
16 changes: 16 additions & 0 deletions pkg/clients/gitlabClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package clients

import (
"github.com/allero-io/allero/pkg/configurationManager"
"github.com/xanzy/go-gitlab"
)

func CreateGitlabClient(configurationManager configurationManager.ConfigurationManager) (*gitlab.Client, error) {
GITLAB_TOKEN := configurationManager.GetGitlabToken()
gitlabClient, err := gitlab.NewClient(GITLAB_TOKEN)
if err != nil {
return nil, err
}

return gitlabClient, nil
}
20 changes: 10 additions & 10 deletions pkg/connectors/github/githubConnector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func New(deps *GithubConnectorDependencies) *GithubConnector {
}
}

func (gc *GithubConnector) Get(args []string) (int, error) {
repositoriesChan := gc.getAllRepositories(args)
func (gc *GithubConnector) Get(ownersWithRepos []*connectors.OwnerWithRepo) (int, error) {
repositoriesChan := gc.getAllRepositories(ownersWithRepos)

githubJsonObject := make(map[string]*GithubOwner)
reposFetchCounter := 0
Expand Down Expand Up @@ -200,32 +200,32 @@ func (gc *GithubConnector) matchedFiles(tree *github.Tree, regex string) []strin
return matchedFiles
}

func (gc *GithubConnector) getAllRepositories(args []string) chan *GithubRepositoryApiResponse {
func (gc *GithubConnector) getAllRepositories(ownersWithRepos []*connectors.OwnerWithRepo) chan *GithubRepositoryApiResponse {
repositoriesChan := make(chan *GithubRepositoryApiResponse)

go func() {
defer close(repositoriesChan)

for _, arg := range args {
ownerWithRepo := connectors.SplitParentRepo(arg)
for _, ownerWithRepo := range ownersWithRepos {
fullName := ownerWithRepo.Owner + "/" + ownerWithRepo.Repo

if ownerWithRepo.Repo != "" {
fmt.Printf("Start fetching repository %s/%s\n", ownerWithRepo.Owner, ownerWithRepo.Repo)
fmt.Printf("Start fetching github repository %s/%s\n", ownerWithRepo.Owner, ownerWithRepo.Repo)
repoMetadata, _, err := gc.client.Repositories.Get(context.Background(), ownerWithRepo.Owner, ownerWithRepo.Repo)
if err != nil {
err = fmt.Errorf("unable to get repository %s", arg)
err = fmt.Errorf("unable to get github repository %s", fullName)
}

repositoriesChan <- &GithubRepositoryApiResponse{
Repository: repoMetadata,
Error: err,
}
fmt.Printf("Finished fetching repository %s/%s\n", ownerWithRepo.Owner, ownerWithRepo.Repo)
fmt.Printf("Finished fetching github repository %s/%s\n", ownerWithRepo.Owner, ownerWithRepo.Repo)

} else {
ownerType, err := gc.getGithubOwnerType(ownerWithRepo.Owner)
if err != nil {
err = fmt.Errorf("unable to get data on owner %s", ownerWithRepo.Owner)
err = fmt.Errorf("unable to get data on github owner %s", ownerWithRepo.Owner)

repositoriesChan <- &GithubRepositoryApiResponse{
Repository: nil,
Expand All @@ -234,7 +234,7 @@ func (gc *GithubConnector) getAllRepositories(args []string) chan *GithubReposit
} else {
ownerRepos, err := ListByOwnerWithPagination(gc.client, ownerWithRepo.Owner, ownerType)
if err != nil {
err = fmt.Errorf("unable to get repositories from owner %s", ownerWithRepo.Owner)
err = fmt.Errorf("unable to get repositories from github owner %s", ownerWithRepo.Owner)
}

for repo := range ownerRepos {
Expand Down
Loading

0 comments on commit 1c480ff

Please sign in to comment.