From 95a533607df7888cb10939c513d522bd4e5013f5 Mon Sep 17 00:00:00 2001 From: libmartinito Date: Thu, 20 Jun 2024 21:36:21 +0800 Subject: [PATCH 1/7] feat: Add support for the previous flag for the test command --- cmd/codecrafters/main.go | 6 +++++- internal/commands/test.go | 4 ++-- internal/utils/codecrafters_client.go | 19 +++++++++++++------ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd/codecrafters/main.go b/cmd/codecrafters/main.go index ffc77b8..dd93c09 100644 --- a/cmd/codecrafters/main.go +++ b/cmd/codecrafters/main.go @@ -76,7 +76,11 @@ func run() error { switch cmd { case "test": - return commands.TestCommand(ctx) + testCmd := flag.NewFlagSet("test", flag.ExitOnError) + previous := testCmd.Bool("previous", false, "run tests for the current stage and all previous stages in ascending order") + testCmd.Parse(flag.Args()[1:]) // parse the args after the test command + + return commands.TestCommand(ctx, *previous) case "submit": return commands.SubmitCommand(ctx) case "help", diff --git a/internal/commands/test.go b/internal/commands/test.go index 507b0ef..b015ea8 100644 --- a/internal/commands/test.go +++ b/internal/commands/test.go @@ -17,7 +17,7 @@ import ( "github.com/rs/zerolog" ) -func TestCommand(ctx context.Context) (err error) { +func TestCommand(ctx context.Context, previous bool) (err error) { logger := zerolog.Ctx(ctx) logger.Debug().Msg("test command starts") @@ -105,7 +105,7 @@ func TestCommand(ctx context.Context) (err error) { logger.Debug().Msgf("creating submission for %s", tempCommitSha) - createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), tempCommitSha) + createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), tempCommitSha, previous) if err != nil { return fmt.Errorf("create submission: %w", err) } diff --git a/internal/utils/codecrafters_client.go b/internal/utils/codecrafters_client.go index 0005fd6..33f172d 100644 --- a/internal/utils/codecrafters_client.go +++ b/internal/utils/codecrafters_client.go @@ -88,13 +88,20 @@ func (c CodecraftersClient) headers() map[string]string { } } -func (c CodecraftersClient) CreateSubmission(repositoryId string, commitSha string) (CreateSubmissionResponse, error) { +func (c CodecraftersClient) CreateSubmission(repositoryId string, commitSha string, previous bool) (CreateSubmissionResponse, error) { + requestBody := map[string]interface{}{ + "repository_id": repositoryId, + "commit_sha": commitSha, + "should_auto_advance": false, + "stage_selection_strategy": "current_and_previous_descending", + } + + if previous { + requestBody["stage_selection_strategy"] = "current_and_previous_ascending" + } + response, err := grequests.Post(c.ServerUrl+"/services/cli/create_submission", &grequests.RequestOptions{ - JSON: map[string]interface{}{ - "repository_id": repositoryId, - "commit_sha": commitSha, - "should_auto_advance": false, - }, + JSON: requestBody, Headers: c.headers(), }) From f57deedaa8164401b27f86e9dae2fd0749013fd5 Mon Sep 17 00:00:00 2001 From: libmartinito Date: Thu, 20 Jun 2024 21:42:03 +0800 Subject: [PATCH 2/7] feat: Improve flag.Usage to reflect added --previous flag --- cmd/codecrafters/main.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd/codecrafters/main.go b/cmd/codecrafters/main.go index dd93c09..16a1b80 100644 --- a/cmd/codecrafters/main.go +++ b/cmd/codecrafters/main.go @@ -23,11 +23,16 @@ USAGE $ codecrafters [command] EXAMPLES - $ codecrafters test # Run tests without committing changes - $ codecrafters submit # Commit changes & submit to move to next step + $ codecrafters test # Run tests without committing changes + $ codecrafters test --previous # Run tests for the current stage and all previous stages in ascending order + $ codecrafters submit # Commit changes & submit to move to next step COMMANDS test: Run tests without committing changes + + Flags for test: + --previous Run tests for the current stage and all previous stages in ascending order + submit: Commit changes & submit to move to next step help: Show usage instructions From 73314e7e94ff098e31ae179057180409a511cdfa Mon Sep 17 00:00:00 2001 From: libmartinito Date: Fri, 21 Jun 2024 18:06:12 +0800 Subject: [PATCH 3/7] refactor: Update cli usage logs --- cmd/codecrafters/main.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/cmd/codecrafters/main.go b/cmd/codecrafters/main.go index 16a1b80..c4e6f7d 100644 --- a/cmd/codecrafters/main.go +++ b/cmd/codecrafters/main.go @@ -24,15 +24,11 @@ USAGE EXAMPLES $ codecrafters test # Run tests without committing changes - $ codecrafters test --previous # Run tests for the current stage and all previous stages in ascending order + $ codecrafters test --previous # Run tests for the current stage and all previous stages without comitting changes $ codecrafters submit # Commit changes & submit to move to next step COMMANDS test: Run tests without committing changes - - Flags for test: - --previous Run tests for the current stage and all previous stages in ascending order - submit: Commit changes & submit to move to next step help: Show usage instructions From 9f58359c046bc6f42258b891cf39e5b6ad49c691 Mon Sep 17 00:00:00 2001 From: libmartinito Date: Fri, 21 Jun 2024 18:09:02 +0800 Subject: [PATCH 4/7] refactor: Change variable for storing --previous flag value from previous to shouldTestPrevious --- cmd/codecrafters/main.go | 4 ++-- internal/commands/test.go | 4 ++-- internal/utils/codecrafters_client.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/codecrafters/main.go b/cmd/codecrafters/main.go index c4e6f7d..4001339 100644 --- a/cmd/codecrafters/main.go +++ b/cmd/codecrafters/main.go @@ -78,10 +78,10 @@ func run() error { switch cmd { case "test": testCmd := flag.NewFlagSet("test", flag.ExitOnError) - previous := testCmd.Bool("previous", false, "run tests for the current stage and all previous stages in ascending order") + shouldTestPrevious := testCmd.Bool("previous", false, "run tests for the current stage and all previous stages in ascending order") testCmd.Parse(flag.Args()[1:]) // parse the args after the test command - return commands.TestCommand(ctx, *previous) + return commands.TestCommand(ctx, *shouldTestPrevious) case "submit": return commands.SubmitCommand(ctx) case "help", diff --git a/internal/commands/test.go b/internal/commands/test.go index b015ea8..d9bcd62 100644 --- a/internal/commands/test.go +++ b/internal/commands/test.go @@ -17,7 +17,7 @@ import ( "github.com/rs/zerolog" ) -func TestCommand(ctx context.Context, previous bool) (err error) { +func TestCommand(ctx context.Context, shouldTestPrevious bool) (err error) { logger := zerolog.Ctx(ctx) logger.Debug().Msg("test command starts") @@ -105,7 +105,7 @@ func TestCommand(ctx context.Context, previous bool) (err error) { logger.Debug().Msgf("creating submission for %s", tempCommitSha) - createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), tempCommitSha, previous) + createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), tempCommitSha, shouldTestPrevious) if err != nil { return fmt.Errorf("create submission: %w", err) } diff --git a/internal/utils/codecrafters_client.go b/internal/utils/codecrafters_client.go index 33f172d..ad3030a 100644 --- a/internal/utils/codecrafters_client.go +++ b/internal/utils/codecrafters_client.go @@ -88,7 +88,7 @@ func (c CodecraftersClient) headers() map[string]string { } } -func (c CodecraftersClient) CreateSubmission(repositoryId string, commitSha string, previous bool) (CreateSubmissionResponse, error) { +func (c CodecraftersClient) CreateSubmission(repositoryId string, commitSha string, shouldTestPrevious bool) (CreateSubmissionResponse, error) { requestBody := map[string]interface{}{ "repository_id": repositoryId, "commit_sha": commitSha, @@ -96,7 +96,7 @@ func (c CodecraftersClient) CreateSubmission(repositoryId string, commitSha stri "stage_selection_strategy": "current_and_previous_descending", } - if previous { + if shouldTestPrevious { requestBody["stage_selection_strategy"] = "current_and_previous_ascending" } From 682590788cdd27cb65745e3db206ce7ee25e8e73 Mon Sep 17 00:00:00 2001 From: libmartinito Date: Fri, 21 Jun 2024 18:16:00 +0800 Subject: [PATCH 5/7] fix: Pass in false for shouldTestPrevious when CreateSubmission is called in the submit command --- internal/commands/submit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/commands/submit.go b/internal/commands/submit.go index 02d4c19..84b64c0 100644 --- a/internal/commands/submit.go +++ b/internal/commands/submit.go @@ -91,7 +91,7 @@ func SubmitCommand(ctx context.Context) (err error) { logger.Debug().Msgf("creating submission for %s", commitSha) - createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), commitSha) + createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), commitSha, false) if err != nil { return fmt.Errorf("create submission: %w", err) } From 15cc226dae41decabc5d724b66932b18794ebfb5 Mon Sep 17 00:00:00 2001 From: libmartinito Date: Fri, 21 Jun 2024 18:23:36 +0800 Subject: [PATCH 6/7] refactor: Moved decision on what stage selection strategy to use one layer up in caller --- internal/commands/submit.go | 2 +- internal/commands/test.go | 8 +++++++- internal/utils/codecrafters_client.go | 20 +++++++------------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/internal/commands/submit.go b/internal/commands/submit.go index 84b64c0..7bbcefd 100644 --- a/internal/commands/submit.go +++ b/internal/commands/submit.go @@ -91,7 +91,7 @@ func SubmitCommand(ctx context.Context) (err error) { logger.Debug().Msgf("creating submission for %s", commitSha) - createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), commitSha, false) + createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), commitSha, "current_and_previous_descending") if err != nil { return fmt.Errorf("create submission: %w", err) } diff --git a/internal/commands/test.go b/internal/commands/test.go index d9bcd62..eb341f3 100644 --- a/internal/commands/test.go +++ b/internal/commands/test.go @@ -105,7 +105,13 @@ func TestCommand(ctx context.Context, shouldTestPrevious bool) (err error) { logger.Debug().Msgf("creating submission for %s", tempCommitSha) - createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), tempCommitSha, shouldTestPrevious) + stageSelectionStrategy := "current_and_previous_descending" + + if shouldTestPrevious { + stageSelectionStrategy = "current_and_previous_ascending" + } + + createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), tempCommitSha, stageSelectionStrategy) if err != nil { return fmt.Errorf("create submission: %w", err) } diff --git a/internal/utils/codecrafters_client.go b/internal/utils/codecrafters_client.go index ad3030a..217b25e 100644 --- a/internal/utils/codecrafters_client.go +++ b/internal/utils/codecrafters_client.go @@ -88,20 +88,14 @@ func (c CodecraftersClient) headers() map[string]string { } } -func (c CodecraftersClient) CreateSubmission(repositoryId string, commitSha string, shouldTestPrevious bool) (CreateSubmissionResponse, error) { - requestBody := map[string]interface{}{ - "repository_id": repositoryId, - "commit_sha": commitSha, - "should_auto_advance": false, - "stage_selection_strategy": "current_and_previous_descending", - } - - if shouldTestPrevious { - requestBody["stage_selection_strategy"] = "current_and_previous_ascending" - } - +func (c CodecraftersClient) CreateSubmission(repositoryId string, commitSha string, stageSelectionStrategy string) (CreateSubmissionResponse, error) { response, err := grequests.Post(c.ServerUrl+"/services/cli/create_submission", &grequests.RequestOptions{ - JSON: requestBody, + JSON: map[string]interface{}{ + "repository_id": repositoryId, + "commit_sha": commitSha, + "should_auto_advance": false, + "stage_selection_strategy": stageSelectionStrategy, + }, Headers: c.headers(), }) From 6ae81b945c5ff98d85ea779c5e08775ddbfc6ae3 Mon Sep 17 00:00:00 2001 From: libmartinito Date: Sat, 29 Jun 2024 04:28:26 +0800 Subject: [PATCH 7/7] fix: Fix typo --- cmd/codecrafters/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/codecrafters/main.go b/cmd/codecrafters/main.go index 4001339..fffa3f0 100644 --- a/cmd/codecrafters/main.go +++ b/cmd/codecrafters/main.go @@ -24,7 +24,7 @@ USAGE EXAMPLES $ codecrafters test # Run tests without committing changes - $ codecrafters test --previous # Run tests for the current stage and all previous stages without comitting changes + $ codecrafters test --previous # Run tests for the current stage and all previous stages without committing changes $ codecrafters submit # Commit changes & submit to move to next step COMMANDS