Skip to content
Merged
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
84 changes: 84 additions & 0 deletions pkg/platformhubversioncontrolsettings/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package platformhubversioncontrolsettings

import (
"encoding/json"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/credentials"
)

// Resource represents Platform Hub version control settings
type Resource struct {
URL string `json:"Url"`
Credentials credentials.GitCredential `json:"Credentials"`
DefaultBranch string `json:"DefaultBranch"`
BasePath string `json:"BasePath"`
}

// NewResource creates a new Platform Hub version control settings resource
func NewResource(url string, creds credentials.GitCredential, defaultBranch string, basePath string) *Resource {
return &Resource{
URL: url,
Credentials: creds,
DefaultBranch: defaultBranch,
BasePath: basePath,
}
}

// UnmarshalJSON sets the resource to its representation in JSON
func (r *Resource) UnmarshalJSON(b []byte) error {
var fields struct {
URL string `json:"Url"`
DefaultBranch string `json:"DefaultBranch"`
BasePath string `json:"BasePath"`
}
if err := json.Unmarshal(b, &fields); err != nil {
return err
}

r.URL = fields.URL
r.DefaultBranch = fields.DefaultBranch
r.BasePath = fields.BasePath

var rawResource map[string]*json.RawMessage
if err := json.Unmarshal(b, &rawResource); err != nil {
return err
}

var credentialsRaw *json.RawMessage
var credentialsProperties map[string]*json.RawMessage
var credentialType credentials.Type

if rawResource["Credentials"] != nil {
credentialsValue := rawResource["Credentials"]

if err := json.Unmarshal(*credentialsValue, &credentialsRaw); err != nil {
return err
}

if err := json.Unmarshal(*credentialsRaw, &credentialsProperties); err != nil {
return err
}

if credentialsProperties["Type"] != nil {
t := credentialsProperties["Type"]
json.Unmarshal(*t, &credentialType)
}
}

switch credentialType {
case credentials.GitCredentialTypeAnonymous:
var anonymousCredential *credentials.Anonymous
if err := json.Unmarshal(*credentialsRaw, &anonymousCredential); err != nil {
return err
}
r.Credentials = anonymousCredential
case credentials.GitCredentialTypeUsernamePassword:
var usernamePasswordCredential *credentials.UsernamePassword
if err := json.Unmarshal(*credentialsRaw, &usernamePasswordCredential); err != nil {
return err
}
r.Credentials = usernamePasswordCredential
}

return nil
}
19 changes: 19 additions & 0 deletions pkg/platformhubversioncontrolsettings/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package platformhubversioncontrolsettings

import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient"
)

const (
path = "/api/platformhub/versioncontrol"
)

// Get returns the Platform Hub version control settings.
func Get(client newclient.Client) (*Resource, error) {
return newclient.Get[Resource](client.HttpSession(), path)
}

// Update modifies the Platform Hub version control settings.
func Update(client newclient.Client, resource *Resource) (*Resource, error) {
return newclient.Put[Resource](client.HttpSession(), path, resource)
}
5 changes: 4 additions & 1 deletion test/e2e/account_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,11 @@ func CreateTestUsernamePasswordAccount(t *testing.T, client *client.Client) acco
require.NotNil(t, client)

name := internal.GetRandomName()
username := internal.GetRandomName()

account, err := accounts.NewUsernamePasswordAccount(name)
account, err := accounts.NewUsernamePasswordAccount(name, func(a *accounts.UsernamePasswordAccount) {
a.Username = username
})
require.NotNil(t, account)
require.NoError(t, err)
require.NoError(t, account.Validate())
Expand Down
Loading