Skip to content

Commit

Permalink
Merge pull request #25 from gkze/star-from-user
Browse files Browse the repository at this point in the history
Add support for starring from user
  • Loading branch information
gkze authored Jul 16, 2020
2 parents 5a3ffc9 + 4535662 commit 2699378
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cmd/stars/stars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
}

Expand Down Expand Up @@ -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
},
}
Expand All @@ -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
Expand Down
44 changes: 44 additions & 0 deletions starmanager/starmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2699378

Please sign in to comment.