Skip to content

Commit

Permalink
fix: populate script syntax and body when retrieving all script modul…
Browse files Browse the repository at this point in the history
…es (#303)
  • Loading branch information
hnrkndrssn authored Feb 13, 2025
1 parent cc9533b commit a8aaea7
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 20 deletions.
60 changes: 40 additions & 20 deletions pkg/scriptmodules/script_module_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package scriptmodules

import (
"fmt"
"strings"

"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/variables"
"github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates"
"strings"
)

const contentType = "ScriptModule"
Expand Down Expand Up @@ -85,12 +86,19 @@ func Get(client newclient.Client, spaceID string, libraryVariablesQuery variable
return nil, err
}

resp, err := newclient.Get[resources.Resources[*variables.ScriptModule]](client.HttpSession(), expandedUri)
scriptModulesResponse, err := newclient.Get[resources.Resources[*variables.ScriptModule]](client.HttpSession(), expandedUri)
if err != nil {
return &resources.Resources[*variables.ScriptModule]{}, err
}

return resp, nil
for _, scriptModule := range scriptModulesResponse.Items {
err := populateScriptModuleSyntaxAndBody(client, spaceID, scriptModule)
if err != nil {
return nil, err
}
}

return scriptModulesResponse, nil
}

// GetByID returns the script module that matches the space ID and input ID. If one
Expand All @@ -112,27 +120,13 @@ func GetByID(client newclient.Client, spaceID string, id string) (*variables.Scr
})

scriptModuleResponse, err := newclient.Get[variables.ScriptModule](client.HttpSession(), expandedUri)

// get associated variable set
variablesPath, err := client.URITemplateCache().Expand(uritemplates.Variables, map[string]any{
"spaceId": spaceID,
"id": scriptModuleResponse.VariableSetID,
})

variablesResponse, err := newclient.Get[variables.VariableSet](client.HttpSession(), variablesPath)
if err != nil {
return nil, err
}

variableSet := *variablesResponse
for _, variable := range variableSet.Variables {
if strings.HasPrefix(variable.Name, "Octopus.Script.Module[") {
scriptModuleResponse.ScriptBody = variable.Value
}

if strings.HasPrefix(variable.Name, "Octopus.Script.Module.Language[") {
scriptModuleResponse.Syntax = variable.Value
}
err = populateScriptModuleSyntaxAndBody(client, spaceID, scriptModuleResponse)
if err != nil {
return nil, err
}

return scriptModuleResponse, nil
Expand Down Expand Up @@ -218,3 +212,29 @@ func DeleteByID(client newclient.Client, spaceID string, id string) error {

return newclient.Delete(client.HttpSession(), expandedUri)
}

func populateScriptModuleSyntaxAndBody(client newclient.Client, spaceID string, scriptModuleResponse *variables.ScriptModule) error {
// get associated variable set
variablesPath, err := client.URITemplateCache().Expand(uritemplates.Variables, map[string]any{
"spaceId": spaceID,
"id": scriptModuleResponse.VariableSetID,
})

variablesResponse, err := newclient.Get[variables.VariableSet](client.HttpSession(), variablesPath)
if err != nil {
return err
}

variableSet := *variablesResponse
for _, variable := range variableSet.Variables {
if strings.HasPrefix(variable.Name, "Octopus.Script.Module[") {
scriptModuleResponse.ScriptBody = variable.Value
}

if strings.HasPrefix(variable.Name, "Octopus.Script.Module.Language[") {
scriptModuleResponse.Syntax = variable.Value
}
}

return nil
}
84 changes: 84 additions & 0 deletions test/e2e/script_module_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package e2e

import (
"testing"

"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/scriptmodules"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/variables"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestScriptModuleAddGetAndDelete_NewClient(t *testing.T) {
client := getOctopusClient()
require.NotNil(t, client)

space := GetDefaultSpace(t, client)
require.NotNil(t, space)

name := internal.GetRandomName()
description := internal.GetRandomName()
scriptBody := "function Say-Hello()\r\n{\r\n Write-Output \"Hello, Octopus!\"\r\n}\r\n"
syntax := "PowerShell"

scriptModule := variables.NewScriptModule(name)
scriptModule.Description = description
scriptModule.ScriptBody = scriptBody
scriptModule.Syntax = syntax
require.NoError(t, scriptModule.Validate())

createdScriptModule, err := scriptmodules.Add(client, scriptModule)
require.NoError(t, err)
require.NotNil(t, createdScriptModule)
require.NotEmpty(t, createdScriptModule.GetID())
require.Equal(t, description, createdScriptModule.Description)
require.Equal(t, name, createdScriptModule.Name)
require.Equal(t, scriptBody, createdScriptModule.ScriptBody)
require.Equal(t, syntax, createdScriptModule.Syntax)

allScriptModules, err := scriptmodules.Get(client, space.GetID(), variables.LibraryVariablesQuery{
ContentType: "ScriptModules",
})

require.NoError(t, err)
require.NotNil(t, allScriptModules)

for _, resource := range allScriptModules.Items {
resourceToCompare, err := scriptmodules.GetByID(client, space.GetID(), resource.GetID())
require.NoError(t, err)
IsEqualScriptModules(t, resource, resourceToCompare)
}

DeleteTestScriptModule(t, client, space.GetID(), createdScriptModule)
}

func IsEqualScriptModules(t *testing.T, expected *variables.ScriptModule, actual *variables.ScriptModule) {
// equality cannot be determined through a direct comparison (below)
// because APIs like GetByPartialName do not include the fields,
// LastModifiedBy and LastModifiedOn
//
// assert.EqualValues(expected, actual)
//
// this statement (above) is expected to succeed, but it fails due to these
// missing fields

// IResource
assert.Equal(t, expected.GetID(), actual.GetID())
assert.True(t, internal.IsLinksEqual(expected.GetLinks(), actual.GetLinks()))

// script module
assert.Equal(t, expected.Description, actual.Description)
assert.Equal(t, expected.Name, actual.Name)
assert.Equal(t, expected.ScriptBody, actual.ScriptBody)
assert.Equal(t, expected.SpaceID, actual.SpaceID)
assert.Equal(t, expected.Syntax, actual.Syntax)
assert.Equal(t, expected.VariableSetID, actual.VariableSetID)
}

func DeleteTestScriptModule(t *testing.T, client *client.Client, spaceID string, scriptModule *variables.ScriptModule) error {
require.NotNil(t, scriptModule)

return scriptmodules.DeleteByID(client, spaceID, scriptModule.GetID())
}

0 comments on commit a8aaea7

Please sign in to comment.