Skip to content

Refactor 2 #15

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
coverage:
precision: 2
round: nearest
range: "70...100"
status:
project:
default:
target: auto
threshold: 15%
Comment on lines +5 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Quote project-level threshold to avoid YAML parsing errors
Unquoted 15% may not be parsed correctly by YAML. Wrap it in quotes to ensure it’s interpreted as a string.
Apply this diff:

--- a/.github/codecov.yml
+++ b/.github/codecov.yml
@@ -8,7 +8,7 @@ status:
       default:
         target: auto
-        threshold: 15%
+        threshold: "15%"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
status:
project:
default:
target: auto
threshold: 15%
status:
project:
default:
target: auto
threshold: "15%"
🤖 Prompt for AI Agents
In .github/codecov.yml around lines 5 to 9, the project-level threshold value
15% is unquoted, which can cause YAML parsing errors. Fix this by wrapping the
threshold value in quotes, changing it from 15% to "15%".

patch:
default:
target: 30%
threshold: 30%
Comment on lines +10 to +13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Quote patch-level threshold to avoid YAML parsing errors
Similarly, the unquoted 30% can break the YAML loader. Please wrap it in quotes.

--- a/.github/codecov.yml
+++ b/.github/codecov.yml
@@ -12,7 +12,7 @@ status:
       default:
         target: 30%
-        threshold: 30%
+        threshold: "30%"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
patch:
default:
target: 30%
threshold: 30%
patch:
default:
target: 30%
threshold: "30%"
🤖 Prompt for AI Agents
In .github/codecov.yml around lines 10 to 13, the patch-level threshold value
30% is unquoted, which can cause YAML parsing errors. To fix this, wrap the
threshold value 30% in double quotes to ensure it is parsed correctly as a
string.

2 changes: 0 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ on:
tags:
- v*
pull_request:
pull_request_target:
types: [opened, edited]

env:
GO_VERSION: "~1.20"
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Spin up your entire dev stack with one command.
- [Example Configurations](#example-configurations)
- [Integrations](#integrations)
- [Integration: dotenv-vault](#integration-dotenv-vault)
- [Integration: Infisical](#integration-infisical)
- [Integration: Telegram Notifications](#integration-telegram-notifications)
- [Integration: Slack Notifications](#integration-slack-notifications)
- [Integration: Desktop Notifications](#integration-desktop-notifications)
Expand Down Expand Up @@ -499,6 +500,23 @@ env:
- dotenv://vault # loads .env.vault, if it exists
```

### Integration: Infisical

`StackUp` includes an integration for loading secrets from an `Infisical` workspace (see the [Infisical website](https://infisical.com/)).

You must define an environment variable _(either in a `.env` file or manually)_ named `INFISICAL_API_TOKEN` that contains an Infisical project's service token.

The `Infisical` integration supports loading secrets from a workspace, environment, or both. To load secrets from a workspace (project), add an entry to the `env` section with the value `infisical://workspace-id:environment`. The `workspace-id` is the id of the workspace to load secrets from, and the `environment` is the name of the environment to load secrets from.

```yaml
env:
- MY_ENV_VAR_ONE=test1234
# load all secrets from the Infisical workspace with the id 123425fa2002e3a200d7a300 from the dev environment:
- infisical://123425fa2002e3a200d7a300:dev
```

Note that all secrets the service token has access to will be imported into the environment.

### Integration: Telegram Notifications

`StackUp` includes an integration for sending notifications via Telegram. To configure the integration, see the [Telegram Notifications](#configuration-settings-notifications-telegram) section of the [Configuration: Settings](#configuration-settings) documentation.
Expand Down
6 changes: 2 additions & 4 deletions Taskfile.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,9 @@ tasks:
- cp -f ./.env {{.BUILD_OUTPUT_DIR}}

update-checksums:
dir: templates/remote-includes
cmds:
- sha256sum *.yaml > checksums.sha256.txt
- git add checksums.sha256.txt
- git commit -m "update checksum files"
- sha256sum ./templates/remote-includes/*.yaml > ./templates/remote-includes/checksums.sha256.txt
- sed 's/\.\/templates\/remote-includes\///g' ./templates/remote-includes/checksums.sha256.txt --in-place

autobuild:
interactive: true
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
golang.org/x/crypto v0.12.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ github.com/slack-go/slack v0.12.3 h1:92/dfFU8Q5XP6Wp5rr5/T5JHLM5c5Smtn53fhToAP88
github.com/slack-go/slack v0.12.3/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
8 changes: 4 additions & 4 deletions lib/app/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type ScheduledTask struct {
TaskReferenceContract
}

func (task *Task) canRunOnCurrentPlatform() bool {
func (task *Task) CanRunOnCurrentPlatform() bool {
if task.Platforms == nil || len(task.Platforms) == 0 {
return true
}
Expand All @@ -67,7 +67,7 @@ func (task *Task) canRunOnCurrentPlatform() bool {
return false
}

func (task *Task) canRunConditionally() bool {
func (task *Task) CanRunConditionally() bool {
if len(strings.TrimSpace(task.If)) == 0 {
return true
}
Expand Down Expand Up @@ -163,12 +163,12 @@ func (task *Task) prepareRun() (bool, func()) {
task.Path = task.JsEngine.Evaluate(task.Path).(string)
}

if !task.canRunConditionally() {
if !task.CanRunConditionally() {
support.SkippedMessageWithSymbol(task.GetDisplayName())
return false, nil
}

if !task.canRunOnCurrentPlatform() {
if !task.CanRunOnCurrentPlatform() {
support.SkippedMessageWithSymbol("Task '" + task.GetDisplayName() + "' is not supported on this operating system.")
return false, nil
}
Expand Down
123 changes: 123 additions & 0 deletions lib/app/tasks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package app_test

import (
"runtime"
"testing"

"github.com/stackup-app/stackup/lib/app"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

type MockJsEngine struct {
MockEvaluate func(script string) interface{}
mock.Mock
}

func (m *MockJsEngine) Evaluate(script string) interface{} {
return m.MockEvaluate(script)
}
Comment on lines +12 to +19
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix MockJsEngine implementation inconsistency.

The MockJsEngine struct embeds mock.Mock but uses a custom MockEvaluate function field instead of leveraging the mock framework properly. This creates confusion and doesn't follow testify best practices.

Choose one of these approaches:

Option 1: Use testify mock framework properly:

 type MockJsEngine struct {
-	MockEvaluate func(script string) interface{}
 	mock.Mock
 }

 func (m *MockJsEngine) Evaluate(script string) interface{} {
-	return m.MockEvaluate(script)
+	args := m.Called(script)
+	return args.Get(0)
 }

Option 2: Use simple function field approach:

 type MockJsEngine struct {
 	MockEvaluate func(script string) interface{}
-	mock.Mock
 }
🤖 Prompt for AI Agents
In lib/app/tasks_test.go around lines 12 to 19, the MockJsEngine struct
inconsistently mixes testify's mock.Mock embedding with a custom MockEvaluate
function field, which is confusing and not aligned with testify best practices.
To fix this, either remove the MockEvaluate field and implement Evaluate by
calling m.Called(script).Get(0) to use testify's mocking properly, or remove the
mock.Mock embedding and keep only the function field for a simpler approach.
Choose one consistent mocking strategy and update the Evaluate method
accordingly.


func TestGetDisplayName(t *testing.T) {
assert := assert.New(t)

// Test case: When Include field has a value with "https://"
task1 := app.Task{
Include: "https://example.com",
Name: "TestName",
Id: "TestId",
Uuid: "TestUuid",
}
assert.Equal("example.com", task1.GetDisplayName())

// Test case: When Include field has a value without "https://"
task2 := app.Task{
Include: "example.com",
Name: "TestName",
Id: "TestId",
Uuid: "TestUuid",
}
assert.Equal("example.com", task2.GetDisplayName())

// Test case: When Name field has a value
task3 := app.Task{
Name: "TestName",
Id: "TestId",
Uuid: "TestUuid",
}
assert.Equal("TestName", task3.GetDisplayName())

// Test case: When Id field has a value
task4 := app.Task{
Id: "TestId",
Uuid: "TestUuid",
}
assert.Equal("TestId", task4.GetDisplayName())

// Test case: When only Uuid field has a value
task5 := app.Task{
Uuid: "TestUuid",
}
assert.Equal("TestUuid", task5.GetDisplayName())

// Test case: When no fields have values
task6 := app.Task{}
assert.Equal("", task6.GetDisplayName())
}

func TestCanRunOnCurrentPlatform(t *testing.T) {
assert := assert.New(t)

// Test case: When Platforms is nil
task1 := &app.Task{}
assert.True(task1.CanRunOnCurrentPlatform())

// Test case: When Platforms is empty
task2 := &app.Task{Platforms: []string{}}
assert.True(task2.CanRunOnCurrentPlatform())

// Test case: When Platforms contains the current platform (case insensitive)
task3 := &app.Task{Platforms: []string{runtime.GOOS}}
assert.True(task3.CanRunOnCurrentPlatform())

// Test case: When Platforms does not contain the current platform
task4 := &app.Task{Platforms: []string{"someotherplatform"}}
assert.False(task4.CanRunOnCurrentPlatform())
}

// func TestCanRunConditionally(t *testing.T) {
// assert := assert.New(t)

// // Test case: When If field is empty
// task1 := &app.Task{If: ""}
// assert.True(task1.CanRunConditionally())

// // Test case: When JsEngine.Evaluate returns true
// getJsEngine := func(mockEval func(script string) interface{}) interface{} {
// engine := &MockJsEngine{
// MockEvaluate: mockEval,
// }

// return engine
// }

// jsengine2 := getJsEngine(func(script string) interface{} {
// return true
// })

// task2 := &app.Task{
// If: "{{ some condition }}",
// JsEngine: jsengine2.(*scripting.JavaScriptEngine),
// }
// assert.True(task2.CanRunConditionally())

// // Test case: When JsEngine.Evaluate returns false
// jsengine3 := getJsEngine(func(script string) interface{} {
// return false
// })
// task3 := &app.Task{
// If: "some condition",
// JsEngine: jsengine3.(*scripting.JavaScriptEngine),
// }
// assert.False(task3.CanRunConditionally())
// }
Comment on lines +88 to +123
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix type casting issue in commented test.

The commented test has a type casting issue on line 110 that would prevent compilation. The getJsEngine function returns interface{} but tries to cast to *scripting.JavaScriptEngine.

If you plan to uncomment this test, fix the type issue:

-// 	task2 := &app.Task{
-// 		If:       "{{ some condition }}",
-// 		JsEngine: jsengine2.(*scripting.JavaScriptEngine),
-// 	}
+// 	task2 := &app.Task{
+// 		If:       "{{ some condition }}",
+// 		JsEngine: jsengine2.(*MockJsEngine),
+// 	}

However, note that this would still not work since JsEngine expects *scripting.JavaScriptEngine, not *MockJsEngine. Consider creating a proper interface or adjusting the test design.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In lib/app/tasks_test.go between lines 88 and 123, the commented test has a type
casting issue where getJsEngine returns interface{} but the code casts it to
*scripting.JavaScriptEngine, which is incorrect since the returned object is
actually *MockJsEngine. To fix this, either change getJsEngine to return
*scripting.JavaScriptEngine or adjust the test to use an interface that both
*MockJsEngine and *scripting.JavaScriptEngine implement, then assign JsEngine to
that interface type. This will resolve the type mismatch and allow the test to
compile and run correctly.

30 changes: 28 additions & 2 deletions lib/app/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"errors"
"fmt"
"os"
"strings"
"sync"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/stackup-app/stackup/lib/consts"
"github.com/stackup-app/stackup/lib/debug"
"github.com/stackup-app/stackup/lib/gateway"
"github.com/stackup-app/stackup/lib/integrations"
"github.com/stackup-app/stackup/lib/messages"
"github.com/stackup-app/stackup/lib/scripting"
"github.com/stackup-app/stackup/lib/settings"
Expand Down Expand Up @@ -40,22 +42,32 @@ type StackupWorkflow struct {
Cache *cache.Cache
JsEngine *scripting.JavaScriptEngine
Gateway *gateway.Gateway
Integrations map[string]integrations.Integration
ProcessMap *sync.Map
CommandStartCb types.CommandCallback
ExitAppFunc func()
types.AppWorkflowContract
}

func CreateWorkflow(gw *gateway.Gateway, processMap *sync.Map) *StackupWorkflow {
return &StackupWorkflow{
result := &StackupWorkflow{
Settings: &settings.Settings{},
Preconditions: []*WorkflowPrecondition{},
Tasks: []*Task{},
State: WorkflowState{},
Includes: []WorkflowInclude{},
Gateway: gw,
ProcessMap: processMap,
Integrations: map[string]integrations.Integration{},
}

result.Integrations = integrations.List(result.AsContract)

return result
}

func (workflow *StackupWorkflow) AsContract() types.AppWorkflowContract {
return workflow
}

func (workflow *StackupWorkflow) FindTaskById(id string) (any, bool) {
Expand All @@ -82,6 +94,10 @@ func (workflow *StackupWorkflow) FindTaskByUuid(uuid string) *Task {
return nil
}

func (workflow *StackupWorkflow) GetEnvSection() []string {
return workflow.Env
}

func (workflow *StackupWorkflow) TryLoadDotEnvVaultFile() {
if !utils.ArrayContains(workflow.Env, "dotenv://vault") {
return
Expand Down Expand Up @@ -109,7 +125,17 @@ func (workflow *StackupWorkflow) Initialize(engine *scripting.JavaScriptEngine,
workflow.JsEngine = engine

utils.ImportEnvDefsIntoEnvironment(workflow.Env)
workflow.TryLoadDotEnvVaultFile()

for _, integration := range workflow.Integrations {
if integration.IsEnabled() {
err := integration.Run()

if err != nil {
fmt.Printf(" integration error [%s]: %v\n", integration.Name(), err)
}
}
}

workflow.InitializeSections()
workflow.processIncludes()
}
Expand Down
50 changes: 50 additions & 0 deletions lib/app/workflow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package app_test

import (
"sync"
"testing"

"github.com/stackup-app/stackup/lib/app"
"github.com/stackup-app/stackup/lib/gateway"
"github.com/stretchr/testify/assert"
)

func TestCreateWorkflow(t *testing.T) {
gw := &gateway.Gateway{} // Mock or initialize as needed
processMap := &sync.Map{}
workflow := app.CreateWorkflow(gw, processMap)

assert.NotNil(t, workflow)
assert.NotNil(t, workflow.Settings)
assert.NotNil(t, workflow.Preconditions)
assert.NotNil(t, workflow.Tasks)
assert.NotNil(t, workflow.Includes)
assert.NotNil(t, workflow.Gateway)
assert.NotNil(t, workflow.ProcessMap)
assert.NotNil(t, workflow.Integrations)
}

func TestAsContract(t *testing.T) {
workflow := &app.StackupWorkflow{}
contract := workflow.AsContract()

assert.NotNil(t, contract)
}

func TestFindTaskById(t *testing.T) {
workflow := &app.StackupWorkflow{
Tasks: []*app.Task{
{Id: "test1"},
{Id: "test2"},
},
}

taskAny, found := workflow.FindTaskById("test1")
var task *app.Task = taskAny.(*app.Task)

assert.True(t, found)
assert.Equal(t, "test1", task.Id)

_, notFound := workflow.FindTaskById("test3")
assert.False(t, notFound)
}
Loading
Loading