8000 feat(DEV-2781): Notify Not Running in a Git Repo by milldr · Pull Request #990 · cloudposse/atmos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(DEV-2781): Notify Not Running in a Git Repo #990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
363f993
Add check for git repository and required env vars
milldr Jan 30, 2025
7247d20
Merge branch 'main' into DEV-2781/warning-when-not-git-repo
milldr Jan 30, 2025
11e0b71
Add context and error handling to git repository check function
milldr Jan 30, 2025
14493bb
Update git repository check using go-git package
milldr Jan 30, 2025
f6379fa
check for the root of the git project
milldr Jan 30, 2025
abfbd2d
Apply suggestions from code review
milldr Jan 31, 2025
8881b46
Merge branch 'main' into DEV-2781/warning-when-not-git-repo
milldr Jan 31, 2025
09c2ef7
[autofix.ci] apply automated fixes
autofix-ci[bot] Jan 31, 2025
51fb464
corrected function name, return values
milldr Jan 31, 2025
9df2042
revert unintended changes
milldr Jan 31, 2025
43380e4
[autofix.ci] apply automated fixes
autofix-ci[bot] Jan 31, 2025
0553c0c
Merge branch 'main' into DEV-2781/warning-when-not-git-repo
milldr Jan 31, 2025
19d495e
revert git check for root
milldr Jan 31, 2025
10afc6f
[autofix.ci] apply automated fixes
autofix-ci[bot] Jan 31, 2025
bd1af2a
Merge branch 'main' into DEV-2781/warning-when-not-git-repo
osterman Feb 4, 2025
371c67e
merged main
milldr Feb 14, 2025
22f3229
resolved merged conflicts
milldr Feb 14, 2025
6668f6d
Merge branch 'main' into DEV-2781/warning-when-not-git-repo
osterman Feb 20, 2025
d0cc39b
Merge branch 'main' into DEV-2781/warning-when-not-git-repo
osterman Feb 20, 2025
2da0274
Merge branch 'main' into DEV-2781/warning-when-not-git-repo
osterman Feb 28, 2025
d06fa96
stash
milldr Mar 13, 2025
bf9db8a
Merge branch 'main' into DEV-2781/warning-when-not-git-repo
milldr Apr 18, 2025
baa7a58
PR feedback
milldr Apr 18, 2025
977fb92
Merge branch 'DEV-2781/warning-when-not-git-repo' of github.com:cloud…
milldr Apr 18, 2025
696a930
ai nitpick
milldr Apr 18, 2025
79ed161
use viper.BindEnv instead of os.Getenv
milldr Apr 18, 2025
f4ec298
Merge branch 'main' into DEV-2781/warning-when-not-git-repo
milldr Apr 19, 2025
9cf79b7
generate snapshots, remove checks for env
milldr Apr 20, 2025
62bd214
use base dir to test
milldr Apr 20, 2025
7fa944e
Added unit tests
milldr Apr 20, 2025
ee4b747
more acceptance tests
milldr Apr 21, 2025
8ff5503
more acceptance tests
milldr Apr 21, 2025
baf5e46
fixed grammar and typo in comments
milldr Apr 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"strings"
"time"

log "github.com/charmbracelet/log"
"github.com/samber/lo"
"github.com/spf13/cobra"

Expand All @@ -19,8 +20,12 @@
"github.com/cloudposse/atmos/pkg/ui/theme"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/cloudposse/atmos/pkg/version"
"github.com/go-git/go-git/v5"
)

// Define a constant for the dot string that appears multiple times.
const currentDirPath = "."

// ValidateConfig holds configuration options for Atmos validation.
// CheckStack determines whether stack configuration validation should be performed.
type ValidateConfig struct {
Expand Down Expand Up @@ -180,7 +185,7 @@
}

commandToRun := fmt.Sprintf("%s %s %s", os.Args[0], aliasCmd, strings.Join(args, " "))
err = e.ExecuteShell(atmosConfig, commandToRun, commandToRun, ".", nil, false)
err = e.ExecuteShell(atmosConfig, commandToRun, commandToRun, currentDirPath, nil, false)

Check warning on line 188 in cmd/cmd_utils.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd_utils.go#L188

Added line #L188 was not covered by tests
if err != nil {
u.LogErrorAndExit(err)
}
Expand Down Expand Up @@ -400,7 +405,7 @@
// If the command to get the value for the ENV var is provided, execute it
if valCommand != "" {
valCommandName := fmt.Sprintf("env-var-%s-valcommand", key)
res, err := e.ExecuteShellAndReturnOutput(atmosConfig, valCommand, valCommandName, ".", nil, false)
res, err := e.ExecuteShellAndReturnOutput(atmosConfig, valCommand, valCommandName, currentDirPath, nil, false)

Check warning on line 408 in cmd/cmd_utils.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd_utils.go#L408

Added line #L408 was not covered by tests
if err != nil {
u.LogErrorAndExit(err)
}
Expand Down Expand Up @@ -436,7 +441,7 @@

// Execute the command step
commandName := fmt.Sprintf("%s-step-%d", commandConfig.Name, i)
err = e.ExecuteShell(atmosConfig, commandToRun, commandName, ".", envVarsList, false)
err = e.ExecuteShell(atmosConfig, commandToRun, commandName, currentDirPath, envVarsList, false)

Check warning on line 444 in cmd/cmd_utils.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd_utils.go#L444

Added line #L444 was not covered by tests
if err != nil {
u.LogErrorAndExit(err)
}
Expand Down Expand Up @@ -523,6 +528,9 @@
u.LogErrorAndExit(err)
}

// Check if we're in a git repo. Warn if not.
verifyInsideGitRepo()

Check warning on line 533 in cmd/cmd_utils.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd_utils.go#L532-L533

Added lines #L532 - L533 were not covered by tests
if atmosConfig.Default {
// If Atmos did not find an `atmos.yaml` config file and is using the default config
u.PrintMessageInColor("atmos.yaml", c1)
Expand Down Expand Up @@ -665,6 +673,31 @@
return info
}

// isGitRepository checks if the current directory is within a git repository.
func isGitRepository() bool {
_, err := git.PlainOpenWithOptions(currentDirPath, &git.PlainOpenOptions{
DetectDotGit: true,
})
if err != nil {
if !errors.Is(err, git.ErrRepositoryNotExists) {
log.Debug("git check failed", "error", err)
}

Check warning on line 684 in cmd/cmd_utils.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd_utils.go#L683-L684

Added lines #L683 - L684 were not covered by tests
return false
}

return true
}

// verifyInsideGitRepo checks if we're in a git repo.
func verifyInsideGitRepo() bool {
// Check if we're in a git repo
if !isGitRepository() {
log.Warn("You're not inside a git repository. Atmos feels lonely outside - bring it home!\n")
return false
}
return true
}

func showErrorExampleFromMarkdown(cmd *cobra.Command, arg string) {
commandPath := cmd.CommandPath()
suggestions := []string{}
Expand Down
68 changes: 68 additions & 0 deletions cmd/cmd_utils_test.go
67E6
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestVerifyInsideGitRepo(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "git-repo-verify-test-*")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tmpDir)

// Save current working directory
currentDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}

// Test cases
tests := []struct {
name string
setup func() error
expected bool
}{
{
name: "outside git repository",
setup: func() error {
return os.Chdir(tmpDir)
},
expected: false,
},
{
name: "inside git repository",
setup: func() error {
if err := os.Chdir(currentDir); err != nil {
return err
}
return nil
},
expected: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup test environment
if err := tt.setup(); err != nil {
t.Fatalf("Failed to setup test: %v", err)
}

// Run test
result := verifyInsideGitRepo()

// Assert result
assert.Equal(t, tt.expected, result)
})
}

// Restore original working directory
if err := os.Chdir(currentDir); err != nil {
t.Fatalf("Failed to restore working directory: %v", err)
}
}
A3E2

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions tests/test-cases/empty-dir.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,81 @@ tests:
expect:
diff: []
exit_code: 0

- name: atmos warns if not in git repo with no atmos config
enabled: true
snapshot: true
description: "Test that atmos warns if not run inside of a git repo without an atmos config"
workdir: "/"
command: "atmos"
args:
- list
- stacks
env:
ATMOS_LOGS_LEVEL: Warning
# Atmos should fail and include the git repo warning
expect:
diff: []
stderr:
- ".*You're not inside a git repository. Atmos feels lonely outside - bring it home!.*"
exit_code: 1 # We expect a non-zero exit code because the dir doesnt have an atmos config

# We need to test from outside a git repo.
# We also need to provide a static path to the atmos config that can we used both in GHA and locally.
# To do so, we can change up 1 directory outside that atmos project and assume the project is named "atmos".
- name: atmos doesnt warn if not in git repo with atmos config
enabled: true
snapshot: true
description: "Test that atmos doesnt warn if not run inside of a git repo but has an atmos config"
workdir: "../../"
command: "atmos"
args:
- list
- stacks
env:
ATMOS_LOGS_LEVEL: Warning
ATMOS_BASE_PATH: "./atmos/tests/fixtures/scenarios/complete"
# Atmos should pass and not include the git repo warning
expect:
diff: []
stdout:
- !not ".*You're not inside a git repository\\. Atmos feels lonely outside - bring it home!.*"
stderr:
- "^$"
exit_code: 0

- name: atmos doesnt warn if in git repo with atmos config
enabled: true
snapshot: true
description: "Test that atmos doesnt warn if run inside of a git repo with an atmos config"
workdir: "fixtures/scenarios/complete"
command: "atmos"
args:
- list
- stacks
env:
ATMOS_LOGS_LEVEL: Warning
# Atmos should pass and not include the git repo warning
expect:
diff: []
stdout:
- !not ".*You're not inside a git repository\\. Atmos feels lonely outside - bring it home!.*"
stderr:
- "^$"
exit_code: 0

- name: atmos doesnt warn if in git repo with no atmos config
enabled: true
snapshot: true
description: "Test that atmos doesnt warn if run inside of a git repo with no atmos config"
workdir: "fixtures/scenarios/empty-dir"
command: "atmos"
args: []
env:
ATMOS_LOGS_LEVEL: Warning
# Atmos should fail but not include the git repo warning
expect:
diff: []
stderr:
- !not ".*You're not inside a git repository\\. Atmos feels lonely outside - bring it home!.*"
exit_code: 1
Loading
0