diff --git a/go/cmd/yolo/main.go b/go/cmd/yolo/main.go index 208c81cb..8a286c16 100644 --- a/go/cmd/yolo/main.go +++ b/go/cmd/yolo/main.go @@ -90,7 +90,7 @@ func yolo(args []string) error { serverFlagSet.StringVar(&bintrayToken, "bintray-token", "", "Bintray API Token") serverFlagSet.StringVar(&circleciToken, "circleci-token", "", "CircleCI API Token") serverFlagSet.StringVar(&githubToken, "github-token", "", "GitHub API Token") - serverFlagSet.StringVar(&githubRepos, "github-repos", "", "GitHub repositories to watch") + serverFlagSet.StringVar(&githubRepos, "github-repos", "berty/berty", "GitHub repositories to watch") serverFlagSet.StringVar(&dbStorePath, "db-path", ":memory:", "DB Store path") serverFlagSet.StringVar(&artifactsCachePath, "artifacts-cache-path", "", "Artifacts caching path") serverFlagSet.IntVar(&maxBuilds, "max-builds", 100, "maximum builds to fetch from external services (pagination)") @@ -211,7 +211,7 @@ func yolo(args []string) error { gr.Add(func() error { return svc.PkgmanWorker(ctx, opts) }, func(_ error) { cancel() }) } if githubToken != "" { - opts := yolosvc.GithubWorkerOpts{Logger: logger, MaxBuilds: maxBuilds, ClearCache: cc, Once: once, Repos: githubRepos} + opts := yolosvc.GithubWorkerOpts{Logger: logger, MaxBuilds: maxBuilds, ClearCache: cc, Once: once, ReposFilter: githubRepos} gr.Add(func() error { return svc.GitHubWorker(ctx, opts) }, func(_ error) { cancel() }) } diff --git a/go/pkg/yolosvc/driver_github.go b/go/pkg/yolosvc/driver_github.go index f68d6595..6922acaa 100644 --- a/go/pkg/yolosvc/driver_github.go +++ b/go/pkg/yolosvc/driver_github.go @@ -13,12 +13,18 @@ import ( ) type GithubWorkerOpts struct { - Logger *zap.Logger - MaxBuilds int - LoopAfter time.Duration - ClearCache *abool.AtomicBool - Once bool - Repos string + Logger *zap.Logger + MaxBuilds int + LoopAfter time.Duration + ClearCache *abool.AtomicBool + Once bool + ReposFilter string +} + +type githubRepoConfig struct { + owner string + repo string + // workflows map[int64]struct{} } type githubWorker struct { @@ -28,13 +34,21 @@ type githubWorker struct { repoConfigs []githubRepoConfig } -type githubRepoConfig struct { - owner string - repo string - // workflows map[int64]struct{} +func (worker githubWorker) ValidateConfig() (bool, error) { + if worker.opts.ReposFilter == "" { + return true, fmt.Errorf("no repos filter provided") + } + + for _, repo := range strings.Split(worker.opts.ReposFilter, ",") { + parts := strings.Split(repo, "/") + if len(parts) != 2 { + return false, fmt.Errorf("invalid repo config: %s", repo) + } + } + return true, nil } -// GithubWorker goals is to manage the github update routine, it should try to support as much errors as possible by itself +// GitHubWorker goals is to manage the github update routine, it should try to support as much errors as possible by itself func (svc *service) GitHubWorker(ctx context.Context, opts GithubWorkerOpts) error { opts.applyDefaults() @@ -42,7 +56,16 @@ func (svc *service) GitHubWorker(ctx context.Context, opts GithubWorkerOpts) err svc: svc, opts: opts, logger: opts.Logger.Named("ghub"), - repoConfigs: parseRepoConfigs(opts.Repos), + repoConfigs: parseRepoFilter(opts.ReposFilter), + } + + shouldRun, err := worker.ValidateConfig() + if err != nil { + return err + } + + if !shouldRun { + return nil } // fetch GitHub base objects (the ones that don't change very often). @@ -105,17 +128,10 @@ func (svc *service) GitHubWorker(ctx context.Context, opts GithubWorkerOpts) err } } -func parseRepoConfigs(repos string) []githubRepoConfig { - if repos == "" { - return []githubRepoConfig{} - } - +func parseRepoFilter(filter string) []githubRepoConfig { var repoConfigs []githubRepoConfig - for _, repo := range strings.Split(repos, ",") { + for _, repo := range strings.Split(filter, ",") { parts := strings.Split(repo, "/") - if len(parts) != 2 { - panic(fmt.Sprintf("invalid repo config: %s", repo)) - } // TODO: support filters (e.g. "yolo/*") repoConfigs = append(repoConfigs, githubRepoConfig{ owner: parts[0],