diff --git a/cmd/stars/stars.go b/cmd/stars/stars.go index e887440..4d98d01 100644 --- a/cmd/stars/stars.go +++ b/cmd/stars/stars.go @@ -81,7 +81,7 @@ func mkAddStarsCmd() *cobra.Command { (fromURL != "" && fromOrg != "") { return errors.New( - "Can only pass one of (-u,--from-urls | -o,--from-org | -g,--from-user)", + "Can only pass one of: -u/--from-urls, -o/--from-org, -g/--from-user", ) } @@ -113,6 +113,11 @@ func mkAddStarsCmd() *cobra.Command { return sm.StarRepositoriesFromOrg(fromOrg, addMonths, concurrency) } + if fromUser != "" { + log.Infof("Attempting to star repositories from %s\n", fromUser) + return sm.StarRepositoriesFromUser(fromUser, addMonths, concurrency) + } + return nil }, } @@ -122,7 +127,10 @@ func mkAddStarsCmd() *cobra.Command { &fromURL, "from-url", "u", "", "URL to crawl to add new stars from", ) addStarsCmd.PersistentFlags().StringVarP( - &fromOrg, "from-org", "r", "", "URL to crawl to add new stars from", + &fromOrg, "from-org", "r", "", "Organization to add new stars from", + ) + addStarsCmd.PersistentFlags().StringVarP( + &fromUser, "from-user", "s", "", "User to add new stars from", ) return addStarsCmd diff --git a/starmanager/starmanager.go b/starmanager/starmanager.go index e1e25a7..9f58118 100644 --- a/starmanager/starmanager.go +++ b/starmanager/starmanager.go @@ -427,6 +427,50 @@ func (s *StarManager) StarRepositoriesFromOrg( return starErr } +// StarRepositoriesFromUser stars all of a given user's repositories. +func (s *StarManager) StarRepositoriesFromUser( + username string, notOlderThanMonths, maxConcurrency int, +) error { + repoURLs := []*url.URL{} + + repos, _, err := s.client.Repositories.List( + s.context, username, &github.RepositoryListOptions{}, + ) + if err != nil { + return fmt.Errorf( + "error listing repositories for user %s: %w", username, err, + ) + } + + log.Infof("Parsing %d repos into URLs\n", len(repos)) + for _, repo := range repos { + repoURL, err := url.Parse(repo.GetHTMLURL()) + if err != nil { + log.Errorf( + "encountered error parsing repo url for %s/%s: %+v", + username, repo.GetName(), err, + ) + } + + log.Debugf("Parsed %+v\n", repoURL) + + repoURLs = append(repoURLs, repoURL) + } + + count, err := s.StarRepositoriesFromURLs( + repoURLs, notOlderThanMonths, maxConcurrency, + ) + if err != nil { + return fmt.Errorf( + "encountered error starring repositories for user %s: %w", + username, err, + ) + } + + log.Infof("Successfully starred %d repos\n", count) + return nil +} + // SaveStarredRepository saves a single starred repository to the local cache. func (s *StarManager) SaveStarredRepository( star *github.StarredRepository, wg *sync.WaitGroup,