Skip to content

Commit

Permalink
add cmd to exclude repos
Browse files Browse the repository at this point in the history
  • Loading branch information
ysebyy committed Aug 27, 2024
1 parent bb4fa2f commit e368952
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
purgeCacheFlag = "purge-cache"
softExitFlag = "soft-exit"
orgFlag = "organization"
excludeReposFlag = "exclude-repos"
)

// ENV keys.
Expand Down Expand Up @@ -130,6 +131,7 @@ func init() {
tagsUsage = "tags to use when SBOMs are uploaded to Dependency Track (optional)"
purgeCacheUsage = "whether to purge gradle and go caches after a successful run (default: false)"
orgFlagUsage = "used when using organization github app"
excludeReposFlagUsage = "used to exclude repos from gathering on org mode"
softExitUsage = "used on cleanup to exit soft without crashing"
)

Expand All @@ -149,6 +151,7 @@ func init() {
rootCmd.PersistentFlags().BoolP(softExitFlag, "s", false, softExitUsage)

rootCmd.PersistentFlags().StringP(orgFlag, "g", "", orgFlagUsage)
rootCmd.PersistentFlags().StringSliceP(excludeReposFlag, "x", nil, excludeReposFlagUsage)
}

func initConfig() {
Expand Down
7 changes: 7 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ func createAppFromCLI(cmd *cobra.Command, verbose bool) (*app.App, error) {

options = append(options, app.WithTags(tags))

excludeRepos, err := cmd.Flags().GetStringSlice(excludeReposFlag)
if err != nil {
return nil, fmt.Errorf(errTemplate, excludeReposFlag)
}

options = append(options, app.WithExcludedRepos(excludeRepos))

orgName, err := cmd.Flags().GetString(orgFlag)
if err != nil {
log.Warn("github app org won't be used as no org set")
Expand Down
26 changes: 23 additions & 3 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

type App struct {
outputFile string
tags []string
tags, excludedRepos []string
githubUsername, githubAPIToken, organization string // TODO Move later on to a separate GitHub client
dependencyTrackClient *dtrack.DependencyTrackClient
purgeCache, softExit bool
Expand All @@ -40,7 +40,7 @@ type SBOMsFromFilesystemConfig struct {
}

type options struct {
tags []string
tags, excludedRepos []string
githubUsername, githubAPIToken, organization string // TODO Move later on to a separate GitHub client
dependencyTrackClient *dtrack.DependencyTrackClient
purgeCache, softExit bool
Expand Down Expand Up @@ -112,6 +112,14 @@ func WithTags(tags []string) Option {
}
}

func WithExcludedRepos(excludedRepos []string) Option {
return func(options *options) error {
options.excludedRepos = excludedRepos

return nil
}
}

func WithOrganization(orgName string) Option {
return func(options *options) error {
options.organization = orgName
Expand All @@ -134,7 +142,7 @@ func New(outputFile string, opts ...Option) (*App, error) {

app.githubUsername = options.githubUsername
app.githubAPIToken = options.githubAPIToken

app.excludedRepos = options.excludedRepos
app.tags = options.tags

app.purgeCache = options.purgeCache
Expand Down Expand Up @@ -193,10 +201,22 @@ func (a App) SBOMsFromOrganization(organizationURL string, delayAmount uint16) {
*/

collectSBOMsFromRepositories := func(repositoryURLs []string, apiToken string) {
repoMap := make(map[string]bool)
for _, repo := range a.excludedRepos {
repoMap[repo] = true
}

if apiToken != a.githubAPIToken && apiToken != "" {
a.githubAPIToken = apiToken
}
for idx, repositoryURL := range repositoryURLs {
parts := strings.Split(strings.TrimRight(repositoryURL, "/"), "/")
repoName := parts[len(parts)-1]

if repoMap[repoName] {
log.Infof("%s is in the exclude list, skipping", repoName)
continue
}
if idx == 0 {
a.sbomsFromRepositoryInternal(ctx, repositoryURL)
continue
Expand Down

0 comments on commit e368952

Please sign in to comment.