Skip to content

Commit

Permalink
Fix JSON marshalling and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geofflamrock committed Nov 20, 2024
1 parent 0ee01f1 commit 51cef96
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
27 changes: 15 additions & 12 deletions pkg/projects/git_persistence_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type GitPersistenceSettings interface {
Credential() credentials.GitCredential
SetCredential(credential credentials.GitCredential)

ConversionState() gitPersistenceSettingsConversionState
VariablesAreInGit() bool
RunbooksAreInGit() bool

// Deprecated: This is not settable against a real Octopus project it is only used for testing purposes.
Expand Down Expand Up @@ -114,8 +114,8 @@ func (g *gitPersistenceSettings) SetCredential(credential credentials.GitCredent
g.credential = credential
}

func (g *gitPersistenceSettings) ConversionState() gitPersistenceSettingsConversionState {
return g.conversionState
func (g *gitPersistenceSettings) VariablesAreInGit() bool {
return g.conversionState.VariablesAreInGit
}

func (g *gitPersistenceSettings) RunbooksAreInGit() bool {
Expand All @@ -138,14 +138,14 @@ func (p *gitPersistenceSettings) MarshalJSON() ([]byte, error) {
}

persistenceSettings := struct {
BasePath string `json:"BasePath,omitempty"`
Credentials credentials.GitCredential `json:"Credentials,omitempty"`
DefaultBranch string `json:"DefaultBranch,omitempty"`
IsDefaultBranchProtected bool `json:"ProtectedDefaultBranch"`
ProtectedBranchNamePatterns []string `json:"ProtectedBranchNamePatterns"`
URL string `json:"Url,omitempty"`
Type PersistenceSettingsType `json:"Type,omitempty"`
ConversionState gitPersistenceSettingsConversionState `json:"ConversionState,omitempty"`
BasePath string `json:"BasePath,omitempty"`
Credentials credentials.GitCredential `json:"Credentials,omitempty"`
DefaultBranch string `json:"DefaultBranch,omitempty"`
IsDefaultBranchProtected bool `json:"ProtectedDefaultBranch"`
ProtectedBranchNamePatterns []string `json:"ProtectedBranchNamePatterns"`
URL string `json:"Url,omitempty"`
Type PersistenceSettingsType `json:"Type,omitempty"`
ConversionState map[string]interface{} `json:"ConversionState,omitempty"`
}{
BasePath: p.BasePath(),
Credentials: p.Credential(),
Expand All @@ -154,7 +154,10 @@ func (p *gitPersistenceSettings) MarshalJSON() ([]byte, error) {
ProtectedBranchNamePatterns: protectedBranches,
URL: p.URL().String(),
Type: p.Type(),
ConversionState: p.ConversionState(),
ConversionState: map[string]interface{}{
"VariablesAreInGit": p.conversionState.VariablesAreInGit,
"RunbooksAreInGit": p.conversionState.RunbooksAreInGit,
},
}

return json.Marshal(persistenceSettings)
Expand Down
54 changes: 51 additions & 3 deletions pkg/projects/git_persistence_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ func TestGitPersistenceSettingsMarshalJSONWithProtectedDefaultBranch(t *testing.
"ProtectedBranchNamePatterns": [],
"ProtectedDefaultBranch": true,
"Type": "%s",
"Url": "%s"
"Url": "%s",
"ConversionState": {
"VariablesAreInGit": false,
"RunbooksAreInGit": false
}
}`, basePath, gitCredentialsAsJSON, defaultBranch, projects.PersistenceSettingsTypeVersionControlled, url.String())

gitPersistenceSettings := projects.NewGitPersistenceSettings(basePath, gitCredentials, defaultBranch, protectedBranchNamePatterns, url)
Expand Down Expand Up @@ -121,7 +125,11 @@ func TestGitPersistenceSettingsMarshalJSONWithProtectedDefaultBranchAsLastItem(t
"ProtectedBranchNamePatterns": ["foo"],
"ProtectedDefaultBranch": true,
"Type": "%s",
"Url": "%s"
"Url": "%s",
"ConversionState": {
"VariablesAreInGit": false,
"RunbooksAreInGit": false
}
}`, basePath, gitCredentialsAsJSON, defaultBranch, projects.PersistenceSettingsTypeVersionControlled, url.String())

gitPersistenceSettings := projects.NewGitPersistenceSettings(basePath, gitCredentials, defaultBranch, protectedBranchNamePatterns, url)
Expand Down Expand Up @@ -155,7 +163,11 @@ func TestGitPersistenceSettingsMarshalJSONWithoutProtectedDefaultBranch(t *testi
"ProtectedBranchNamePatterns": ["%s"],
"ProtectedDefaultBranch": false,
"Type": "%s",
"Url": "%s"
"Url": "%s",
"ConversionState": {
"VariablesAreInGit": false,
"RunbooksAreInGit": false
}
}`, basePath, gitCredentialsAsJSON, defaultBranch, protectedBranchName, projects.PersistenceSettingsTypeVersionControlled, url.String())

gitPersistenceSettings := projects.NewGitPersistenceSettings(basePath, gitCredentials, defaultBranch, protectedBranchNamePatterns, url)
Expand Down Expand Up @@ -341,3 +353,39 @@ func TestGitPersistenceSettingsUnmarshalJSONWithProtectedBranchIsDefault(t *test
require.Equal(t, url, gitPersistenceSettings.URL())
require.Equal(t, []string{defaultBranch}, gitPersistenceSettings.ProtectedBranchNamePatterns())
}

func TestGitPersistenceSettingsUnmarshalJSON_ConversionState(t *testing.T) {
password := core.NewSensitiveValue(internal.GetRandomName())
username := internal.GetRandomName()

basePath := internal.GetRandomName()
defaultBranch := internal.GetRandomName()
url, err := url.Parse("https://example.com/")
usernamePasswordGitCredential := credentials.NewUsernamePassword(username, password)
require.NoError(t, err)

gitCredentialsAsJSON, err := json.Marshal(usernamePasswordGitCredential)
require.NoError(t, err)
require.NotNil(t, gitCredentialsAsJSON)

inputJSON := fmt.Sprintf(`{
"BasePath": "%s",
"Credentials": %s,
"DefaultBranch": "%s",
"ProtectedBranchNamePatterns": [],
"ProtectedDefaultBranch": false,
"Type": "%s",
"Url": "%s",
"ConversionState": {
"VariablesAreInGit": true,
"RunbooksAreInGit": true
}
}`, basePath, gitCredentialsAsJSON, defaultBranch, projects.PersistenceSettingsTypeVersionControlled, url.String())

gitPersistenceSettings := projects.NewGitPersistenceSettings("", nil, "", []string{}, nil)
err = json.Unmarshal([]byte(inputJSON), &gitPersistenceSettings)
require.NoError(t, err)
require.NotNil(t, gitPersistenceSettings)
require.Equal(t, true, gitPersistenceSettings.VariablesAreInGit())
require.Equal(t, true, gitPersistenceSettings.RunbooksAreInGit())
}

0 comments on commit 51cef96

Please sign in to comment.