Skip to content

Commit

Permalink
fix: error handling + typo
Browse files Browse the repository at this point in the history
Signed-off-by: ismael FALL <[email protected]>
  • Loading branch information
Doozers committed Sep 2, 2022
1 parent 269e14a commit e5821b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
4 changes: 2 additions & 2 deletions go/cmd/yolo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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() })
}

Expand Down
58 changes: 37 additions & 21 deletions go/pkg/yolosvc/driver_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,21 +34,38 @@ 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()

worker := githubWorker{
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).
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit e5821b3

Please sign in to comment.