From 2296edb5037e4c56bafd9e6c5d363cffd1c75b88 Mon Sep 17 00:00:00 2001 From: Matthew Casperson Date: Mon, 15 Jul 2024 08:10:11 +1000 Subject: [PATCH 1/4] When updating a sensitive variable, the value needs to be null. This changes allows for a nil string for the value. --- pkg/variables/script_module_service.go | 26 ++++++++++++++++++-------- pkg/variables/variable.go | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pkg/variables/script_module_service.go b/pkg/variables/script_module_service.go index a3151aa5..a879cf5b 100644 --- a/pkg/variables/script_module_service.go +++ b/pkg/variables/script_module_service.go @@ -54,11 +54,11 @@ func (s *ScriptModuleService) Add(scriptModule *ScriptModule) (*ScriptModule, er variableSet := variablesResponse.(*VariableSet) scriptBodyVariable := NewVariable(fmt.Sprintf("Octopus.Script.Module[%s]", scriptModule.Name)) - scriptBodyVariable.Value = scriptModule.ScriptBody + scriptBodyVariable.Value = &scriptModule.ScriptBody variableSet.Variables = append(variableSet.Variables, scriptBodyVariable) syntaxVariable := NewVariable(fmt.Sprintf("Octopus.Script.Module.Language[%s]", scriptModule.Name)) - syntaxVariable.Value = scriptModule.Syntax + syntaxVariable.Value = &scriptModule.Syntax variableSet.Variables = append(variableSet.Variables, syntaxVariable) _, err = services.ApiUpdate(s.GetClient(), variableSet, new(VariableSet), variablesPath) @@ -140,12 +140,17 @@ func (s *ScriptModuleService) GetByID(id string) (*ScriptModule, error) { variableSet := variablesResponse.(*VariableSet) for _, variable := range variableSet.Variables { + value := "" + if variable.Value != nil { + value = *variable.Value + } + if strings.HasPrefix(variable.Name, "Octopus.Script.Module[") { - scriptModuleResponse.ScriptBody = variable.Value + scriptModuleResponse.ScriptBody = value } if strings.HasPrefix(variable.Name, "Octopus.Script.Module.Language[") { - scriptModuleResponse.Syntax = variable.Value + scriptModuleResponse.Syntax = value } } @@ -197,11 +202,11 @@ func (s *ScriptModuleService) Update(scriptModule *ScriptModule) (*ScriptModule, variableSet := variablesResponse.(*VariableSet) for _, variable := range variableSet.Variables { if strings.HasPrefix(variable.Name, "Octopus.Script.Module[") { - variable.Value = scriptModule.ScriptBody + variable.Value = &scriptModule.ScriptBody } if strings.HasPrefix(variable.Name, "Octopus.Script.Module.Language[") { - variable.Value = scriptModule.Syntax + variable.Value = &scriptModule.Syntax } } @@ -212,12 +217,17 @@ func (s *ScriptModuleService) Update(scriptModule *ScriptModule) (*ScriptModule, updatedVriableSet := updatedVariablesResponse.(*VariableSet) for _, variable := range updatedVriableSet.Variables { + value := "" + if variable.Value != nil { + value = *variable.Value + } + if strings.HasPrefix(variable.Name, "Octopus.Script.Module[") { - scriptModuleResponse.ScriptBody = variable.Value + scriptModuleResponse.ScriptBody = value } if strings.HasPrefix(variable.Name, "Octopus.Script.Module.Language[") { - scriptModuleResponse.Syntax = variable.Value + scriptModuleResponse.Syntax = value } } diff --git a/pkg/variables/variable.go b/pkg/variables/variable.go index 4a8c028b..6517fdad 100644 --- a/pkg/variables/variable.go +++ b/pkg/variables/variable.go @@ -10,7 +10,7 @@ type Variable struct { Prompt *VariablePromptOptions `json:"Prompt,omitempty"` Scope VariableScope `json:"Scope"` Type string `json:"Type"` - Value string `json:"Value"` + Value *string `json:"Value"` SpaceID string `json:"SpaceId,omitempty"` resources.Resource From 812a34ddd4af789fb51d5d7d32affb964c483eeb Mon Sep 17 00:00:00 2001 From: Matthew Casperson Date: Mon, 6 Jan 2025 09:59:39 +1000 Subject: [PATCH 2/4] Add support for persistent settings id --- pkg/projects/persistence_settings.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/projects/persistence_settings.go b/pkg/projects/persistence_settings.go index 0ed91513..9ae3277e 100644 --- a/pkg/projects/persistence_settings.go +++ b/pkg/projects/persistence_settings.go @@ -8,4 +8,5 @@ type PersistenceSettings interface { // persistenceSettings represents persistence settings associated with a project. type persistenceSettings struct { SettingsType PersistenceSettingsType `json:"Type"` + SettingsId *string `json:"Id"` } From cf8b041d6c5c94b3a82a968e2e38b703e4caf89a Mon Sep 17 00:00:00 2001 From: Matthew Casperson Date: Mon, 6 Jan 2025 10:08:54 +1000 Subject: [PATCH 3/4] Added support for github credentials --- pkg/credentials/github.go | 25 ++++++++++++++++++++++++ pkg/credentials/types.go | 1 + pkg/projects/git_persistence_settings.go | 7 +++++++ pkg/projects/persistence_settings.go | 1 - 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 pkg/credentials/github.go diff --git a/pkg/credentials/github.go b/pkg/credentials/github.go new file mode 100644 index 00000000..27711b13 --- /dev/null +++ b/pkg/credentials/github.go @@ -0,0 +1,25 @@ +package credentials + +// GitHub defines a reference GitHub connection. +type GitHub struct { + ID string `json:"Id"` + + gitCredential +} + +// NewReference creates and initializes a reference Git credential. +func NewGitHub(id string) *Reference { + return &Reference{ + ID: id, + gitCredential: gitCredential{ + CredentialType: GitCredentialTypeGitHub, + }, + } +} + +// Type returns the type for this Git credential. +func (u *GitHub) Type() Type { + return u.CredentialType +} + +var _ GitCredential = &GitHub{} diff --git a/pkg/credentials/types.go b/pkg/credentials/types.go index 22b959dc..1b4f7c18 100644 --- a/pkg/credentials/types.go +++ b/pkg/credentials/types.go @@ -6,4 +6,5 @@ const ( GitCredentialTypeAnonymous = Type("Anonymous") GitCredentialTypeReference = Type("Reference") GitCredentialTypeUsernamePassword = Type("UsernamePassword") + GitCredentialTypeGitHub = Type("GitHub") ) diff --git a/pkg/projects/git_persistence_settings.go b/pkg/projects/git_persistence_settings.go index 8e149b83..d98f7fdc 100644 --- a/pkg/projects/git_persistence_settings.go +++ b/pkg/projects/git_persistence_settings.go @@ -256,6 +256,13 @@ func (p *gitPersistenceSettings) UnmarshalJSON(b []byte) error { return err } p.credential = usernamePasswordGitCredential + case credentials.GitCredentialTypeGitHub: + var githubGitCredential *credentials.GitHub + err := json.Unmarshal(*gitCredentials, &githubGitCredential) + if err != nil { + return err + } + p.credential = githubGitCredential } var conversionState *json.RawMessage diff --git a/pkg/projects/persistence_settings.go b/pkg/projects/persistence_settings.go index 9ae3277e..0ed91513 100644 --- a/pkg/projects/persistence_settings.go +++ b/pkg/projects/persistence_settings.go @@ -8,5 +8,4 @@ type PersistenceSettings interface { // persistenceSettings represents persistence settings associated with a project. type persistenceSettings struct { SettingsType PersistenceSettingsType `json:"Type"` - SettingsId *string `json:"Id"` } From 69a675bfb138993c6a6ff6e5f4b32d3466559826 Mon Sep 17 00:00:00 2001 From: Matthew Casperson Date: Mon, 6 Jan 2025 10:12:08 +1000 Subject: [PATCH 4/4] Added test --- pkg/credentials/github_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 pkg/credentials/github_test.go diff --git a/pkg/credentials/github_test.go b/pkg/credentials/github_test.go new file mode 100644 index 00000000..2b2e54b7 --- /dev/null +++ b/pkg/credentials/github_test.go @@ -0,0 +1,28 @@ +package credentials_test + +import ( + "encoding/json" + "fmt" + "testing" + + "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/credentials" + "github.com/kinbiko/jsonassert" + "github.com/stretchr/testify/require" +) + +func TestGitHubMarshalJSON(t *testing.T) { + id := internal.GetRandomName() + reference := credentials.NewGitHub(id) + + referenceAsJSON, err := json.Marshal(reference) + require.NoError(t, err) + require.NotNil(t, referenceAsJSON) + + expectedJSON := fmt.Sprintf(`{ + "Id": "%s", + "Type": "%s" + }`, id, credentials.GitCredentialTypeGitHub) + + jsonassert.New(t).Assertf(expectedJSON, string(referenceAsJSON)) +}