Skip to content
Draft
25 changes: 25 additions & 0 deletions pkg/credentials/github.go
Original file line number Diff line number Diff line change
@@ -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{}
28 changes: 28 additions & 0 deletions pkg/credentials/github_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
1 change: 1 addition & 0 deletions pkg/credentials/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ const (
GitCredentialTypeAnonymous = Type("Anonymous")
GitCredentialTypeReference = Type("Reference")
GitCredentialTypeUsernamePassword = Type("UsernamePassword")
GitCredentialTypeGitHub = Type("GitHub")
)
7 changes: 7 additions & 0 deletions pkg/projects/git_persistence_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 18 additions & 8 deletions pkg/variables/script_module_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand All @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/variables/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading