From 85730d21da5177dc270789b6abd5a2f1af857cf1 Mon Sep 17 00:00:00 2001 From: Eitan Joffe Date: Thu, 11 Jan 2024 13:20:37 -0800 Subject: [PATCH 1/3] star logic : don't panic if star api returns error fixes #376 commit-id:63a8a141 --- github/githubclient/star.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/github/githubclient/star.go b/github/githubclient/star.go index ce19bf4..b7a0585 100644 --- a/github/githubclient/star.go +++ b/github/githubclient/star.go @@ -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, @@ -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 } } @@ -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 } } } From 098eb7f6af04ccf50e056f818c0e69bd5e5395ee Mon Sep 17 00:00:00 2001 From: Eitan Joffe Date: Thu, 11 Jan 2024 13:53:06 -0800 Subject: [PATCH 2/3] init : write repo and user config if they don't exist fixes #372 commit-id:66c8d4c5 --- config/config_parser/config_parser.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/config/config_parser/config_parser.go b/config/config_parser/config_parser.go index 59268ae..ce09978 100644 --- a/config/config_parser/config_parser.go +++ b/config/config_parser/config_parser.go @@ -1,6 +1,7 @@ package config_parser import ( + "errors" "fmt" "os" "path" @@ -49,6 +50,16 @@ func ParseConfig(gitcmd git.GitInterface) *config.Config { rake.LoadSources(cfg.State, rake.YamlFileWriter(InternalConfigFilePath())) + // init case : if yaml config files not found : create them + if _, err := os.Stat(RepoConfigFilePath(gitcmd)); errors.Is(err, os.ErrNotExist) { + rake.LoadSources(cfg.Repo, + rake.YamlFileWriter(RepoConfigFilePath(gitcmd))) + } + + if _, err := os.Stat(UserConfigFilePath()); errors.Is(err, os.ErrNotExist) { + rake.LoadSources(cfg.User, + rake.YamlFileWriter(UserConfigFilePath())) + } return cfg } From f922aa58311c2ba613117f5a9c5f4c59f9d69337 Mon Sep 17 00:00:00 2001 From: Eitan Joffe Date: Thu, 11 Jan 2024 16:16:56 -0800 Subject: [PATCH 3/3] Check for backslashes in remote branch name Fixes #332 commit-id:a7c1a3c3 --- cmd/spr/main.go | 6 ++++++ config/config_parser/config_parser.go | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/cmd/spr/main.go b/cmd/spr/main.go index f857d94..13781d1 100644 --- a/cmd/spr/main.go +++ b/cmd/spr/main.go @@ -40,6 +40,12 @@ func main() { } cfg := config_parser.ParseConfig(gitcmd) + + err = config_parser.CheckConfig(cfg) + if err != nil { + fmt.Println(err) + os.Exit(2) + } gitcmd = realgit.NewGitCmd(cfg) ctx := context.Background() diff --git a/config/config_parser/config_parser.go b/config/config_parser/config_parser.go index ce09978..37150f0 100644 --- a/config/config_parser/config_parser.go +++ b/config/config_parser/config_parser.go @@ -6,6 +6,7 @@ import ( "os" "path" "path/filepath" + "strings" "github.com/ejoffe/rake" "github.com/ejoffe/spr/config" @@ -63,6 +64,13 @@ func ParseConfig(gitcmd git.GitInterface) *config.Config { return cfg } +func CheckConfig(cfg *config.Config) error { + if strings.Contains(cfg.Repo.GitHubBranch, "/") { + return errors.New("Remote branch name must not contain backslashes '/'") + } + return nil +} + func RepoConfigFilePath(gitcmd git.GitInterface) string { rootdir := gitcmd.RootDir() filepath := filepath.Clean(path.Join(rootdir, ".spr.yml"))