Skip to content

Commit b0e8dcb

Browse files
authored
Pull current git commit from git/clone output (#316)
1 parent 0e53d8c commit b0e8dcb

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

internal/cli/service_sandbox.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -671,18 +671,21 @@ func (s Service) syncChangesToSandbox(jsonMode bool) error {
671671
return nil
672672
}
673673

674-
// Get COMMIT_SHA from sandbox environment to reset to the correct base commit
675-
exitCode, commitSHA, err := s.SSHClient.ExecuteCommandWithOutput("echo $COMMIT_SHA")
674+
// Get RWX_GIT_COMMIT_SHA from sandbox environment to reset to the correct base commit
675+
exitCode, commitSHA, err := s.SSHClient.ExecuteCommandWithOutput("echo $RWX_GIT_COMMIT_SHA")
676676
if err != nil {
677-
return errors.Wrap(err, "failed to get COMMIT_SHA from sandbox")
677+
return errors.Wrap(err, "failed to get RWX_GIT_COMMIT_SHA from sandbox")
678678
}
679679
if exitCode != 0 {
680-
return fmt.Errorf("failed to get COMMIT_SHA from sandbox (exit code %d)", exitCode)
680+
return fmt.Errorf("failed to get RWX_GIT_COMMIT_SHA from sandbox (exit code %d)", exitCode)
681681
}
682-
683682
commitSHA = strings.TrimSpace(commitSHA)
683+
if idx := strings.Index(commitSHA, "+"); idx != -1 {
684+
commitSHA = commitSHA[:idx]
685+
}
686+
684687
if commitSHA == "" {
685-
return fmt.Errorf("COMMIT_SHA environment variable is not set in the sandbox. Add `env: COMMIT_SHA: ${{ init.commit }}` to your sandbox task definition")
688+
return fmt.Errorf("RWX_GIT_COMMIT_SHA environment variable is not set in the sandbox. The sandbox task should be dependent on a task that uses git/clone")
686689
}
687690

688691
// Reset working directory to the base commit (clears any previous patches)

internal/cli/service_sandbox_test.go

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ func TestService_ExecSandbox_Sync(t *testing.T) {
553553
require.Equal(t, "echo hello", commandOrder[2])
554554
})
555555

556-
t.Run("returns error when COMMIT_SHA is not set", func(t *testing.T) {
556+
t.Run("returns error when RWX_GIT_COMMIT_SHA is not set", func(t *testing.T) {
557557
setup := setupTest(t)
558558

559559
runID := "run-no-commit-sha-123"
@@ -577,7 +577,7 @@ func TestService_ExecSandbox_Sync(t *testing.T) {
577577
}
578578

579579
setup.mockSSH.MockExecuteCommandWithOutput = func(command string) (int, string, error) {
580-
return 0, "", nil // COMMIT_SHA not set
580+
return 0, "", nil // Env var not set
581581
}
582582

583583
_, err := setup.service.ExecSandbox(cli.ExecSandboxConfig{
@@ -589,7 +589,63 @@ func TestService_ExecSandbox_Sync(t *testing.T) {
589589
})
590590

591591
require.Error(t, err)
592-
require.Contains(t, err.Error(), "COMMIT_SHA environment variable is not set")
592+
require.Contains(t, err.Error(), "RWX_GIT_COMMIT_SHA environment variable is not set")
593+
require.Contains(t, err.Error(), "sandbox task should be dependent on a task that uses git/clone")
594+
})
595+
596+
t.Run("strips + suffix from RWX_GIT_COMMIT_SHA", func(t *testing.T) {
597+
setup := setupTest(t)
598+
599+
runID := "run-strip-suffix-123"
600+
address := "192.168.1.1:22"
601+
commitSHAWithSuffix := "abc123def456+extra"
602+
expectedCommitSHA := "abc123def456"
603+
var resetCommand string
604+
605+
setup.mockAPI.MockGetSandboxConnectionInfo = func(id string) (api.SandboxConnectionInfo, error) {
606+
return api.SandboxConnectionInfo{
607+
Sandboxable: true,
608+
Address: address,
609+
PrivateUserKey: sandboxPrivateTestKey,
610+
PublicHostKey: sandboxPublicTestKey,
611+
}, nil
612+
}
613+
614+
setup.mockSSH.MockConnect = func(addr string, _ ssh.ClientConfig) error {
615+
return nil
616+
}
617+
618+
setup.mockGit.MockGeneratePatch = func(pathspec []string) ([]byte, *git.LFSChangedFilesMetadata, error) {
619+
return []byte("patch"), nil, nil
620+
}
621+
622+
setup.mockSSH.MockExecuteCommandWithOutput = func(command string) (int, string, error) {
623+
return 0, commitSHAWithSuffix, nil
624+
}
625+
626+
setup.mockSSH.MockExecuteCommand = func(cmd string) (int, error) {
627+
if strings.Contains(cmd, "git reset") {
628+
resetCommand = cmd
629+
}
630+
return 0, nil
631+
}
632+
633+
setup.mockSSH.MockExecuteCommandWithStdin = func(command string, stdin io.Reader) (int, error) {
634+
return 0, nil
635+
}
636+
637+
result, err := setup.service.ExecSandbox(cli.ExecSandboxConfig{
638+
ConfigFile: ".rwx/sandbox.yml",
639+
Command: []string{"echo", "hello"},
640+
RunID: runID,
641+
Json: true,
642+
Sync: true,
643+
})
644+
645+
require.NoError(t, err)
646+
require.Equal(t, 0, result.ExitCode)
647+
require.Contains(t, resetCommand, expectedCommitSHA)
648+
require.NotContains(t, resetCommand, "+")
593649
})
594650
}
595651

0 commit comments

Comments
 (0)