Skip to content

Commit

Permalink
star logic : don't panic if star api returns error
Browse files Browse the repository at this point in the history
fixes #376

commit-id:63a8a141
  • Loading branch information
ejoffe committed Jan 11, 2024
1 parent fb6d741 commit 85730d2
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions github/githubclient/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,22 @@ const (

func (c *client) MaybeStar(ctx context.Context, cfg *config.Config) {
if !cfg.State.Stargazer && cfg.State.RunCount%promptCycle == 0 {
if c.isStar(ctx) {
starred, err := c.isStar(ctx)
if err != nil {
fmt.Println("enjoying git spr? [Y/n]")
fmt.Print(" please add a star at https://github.com/ejoffe/spr")
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')
line = strings.TrimSpace(line)
if line != "n" {
cfg.State.Stargazer = true
rake.LoadSources(cfg.State,
rake.YamlFileWriter(config_parser.InternalConfigFilePath()))
fmt.Println("Thank You! Happy Coding!")
}
}

if starred {
log.Debug().Bool("stargazer", true).Msg("MaybeStar")
cfg.State.Stargazer = true
rake.LoadSources(cfg.State,
Expand All @@ -39,30 +54,32 @@ func (c *client) MaybeStar(ctx context.Context, cfg *config.Config) {
cfg.State.Stargazer = true
rake.LoadSources(cfg.State,
rake.YamlFileWriter(config_parser.InternalConfigFilePath()))
fmt.Println("Thank you! Happy Coding!")
fmt.Println("Thank You! Happy Coding!")
}
}
}
}

func (c *client) isStar(ctx context.Context) bool {
func (c *client) isStar(ctx context.Context) (bool, error) {
iteration := 0
cursor := ""
for {
resp, err := c.api.StarCheck(ctx, &cursor)
check(err)
if err != nil {
return false, err
}

edgeCount := len(*resp.Viewer.StarredRepositories.Edges)
if edgeCount == 0 {
log.Debug().Bool("stargazer", false).Msg("MaybeStar::isStar")
return false
return false, nil
}

sprRepo := fmt.Sprintf("%s/%s", sprRepoOwner, sprRepoName)
for _, node := range *resp.Viewer.StarredRepositories.Nodes {
if node.NameWithOwner == sprRepo {
log.Debug().Bool("stargazer", true).Msg("MaybeStar::isStar")
return true
return true, nil
}
}

Expand All @@ -73,7 +90,7 @@ func (c *client) isStar(ctx context.Context) bool {
if iteration > 10 {
// too many stars in the sky
log.Debug().Bool("stargazer", false).Msg("MaybeStar::isStar (too many stars)")
return false
return false, nil
}
}
}
Expand Down

0 comments on commit 85730d2

Please sign in to comment.