Skip to content

Commit

Permalink
Add support for deleting archived stars
Browse files Browse the repository at this point in the history
  • Loading branch information
gkze committed Nov 14, 2018
1 parent 7ec6a7a commit ccd32e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,14 @@ func main() {
Value: 2,
Usage: "Number of months to delete projects older than",
},
cli.BoolFlag{
Name: "include-archived, a",
Usage: "Include archived stars",
},
},
Action: func(c *cli.Context) error {
sm.SaveIfEmpty()
if err := sm.RemoveOlderThan(c.Int("months")); err != nil {
if err := sm.Cleanup(c.Int("months"), c.Bool("include-archived")); err != nil {
return err
}

Expand Down
17 changes: 12 additions & 5 deletions starmanager/starmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Star struct {
URL string `storm:"id,index,unique"`
Language string `storm:"index"`
Stargazers int
Archived bool `storm:"index"`
Description string `storm:"index"`
Topics []string `storm:"index"`
}
Expand Down Expand Up @@ -137,6 +138,7 @@ func (s *StarManager) SaveStarredRepository(repo *github.Repository, wg *sync.Wa
Stargazers: *repo.StargazersCount,
Description: desc,
Topics: repo.Topics,
Archived: *repo.Archived,
})
if err != nil {
return err
Expand Down Expand Up @@ -308,21 +310,26 @@ func (s *StarManager) RemoveStar(star *Star, wg *sync.WaitGroup) (bool, error) {
return true, nil
}

// RemoveOlderThan removes stars older than a specified time
func (s *StarManager) RemoveOlderThan(months int) error {
// Cleanup removes stars older than a specified time in months and optionally archived stars.
func (s *StarManager) Cleanup(age int, archived bool) error {
allStars := []*Star{}
toDelete := make(chan *Star)
wg := sync.WaitGroup{}
then := time.Now().AddDate(0, -months, 0)
then := time.Now().AddDate(0, -age, 0)

if err := s.DB.All(&allStars); err != nil {
return err
}

log.Printf("Filtering stars to delete (from %d)...", len(allStars))
for _, star := range allStars {
if star.PushedAt.Before(then) {
log.Printf("Queueing %s for deletion (last pushed at %+v)", star.URL, star.PushedAt)
if star.PushedAt.Before(then) || star.Archived == archived {
log.Printf(
"Queueing %s for deletion (last pushed at %+v, archive status: %t)",
star.URL,
star.PushedAt,
star.Archived,
)

go func(ch chan *Star, s *Star, wg *sync.WaitGroup) {
wg.Add(1)
Expand Down

0 comments on commit ccd32e6

Please sign in to comment.