From 86e2f9471924af99913b5905d1cc04b90d0ce157 Mon Sep 17 00:00:00 2001 From: Micah Martin Date: Wed, 16 Mar 2022 22:21:11 -0400 Subject: [PATCH] Gitsync setup --- go.mod | 1 + go.sum | 1 + harness/nextgen/model_connector_response.go | 2 +- harness/nextgen/model_entity_git_details.go | 16 +++-- .../model_entity_git_details_helper.go | 60 +++++++++++++++++++ .../model_entity_git_details_options.go | 15 +++++ .../nextgen/model_entity_git_details_test.go | 27 +++++++++ harness/nextgen/model_pipeline.go | 9 +++ harness/nextgen/model_pipeline_yaml.go | 5 ++ .../nextgen/model_pms_pipeline_response.go | 1 + .../model_pms_pipeline_response_serializer.go | 36 +++++++++++ harness/nextgen/model_service_response.go | 6 +- 12 files changed, 171 insertions(+), 8 deletions(-) create mode 100644 harness/nextgen/model_entity_git_details_helper.go create mode 100644 harness/nextgen/model_entity_git_details_options.go create mode 100644 harness/nextgen/model_entity_git_details_test.go create mode 100644 harness/nextgen/model_pipeline.go create mode 100644 harness/nextgen/model_pipeline_yaml.go create mode 100644 harness/nextgen/model_pms_pipeline_response_serializer.go diff --git a/go.mod b/go.mod index 83ef4525..fc129c0f 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/stretchr/testify v1.7.0 github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a + gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/go.sum b/go.sum index 2501b1a2..581c83ec 100644 --- a/go.sum +++ b/go.sum @@ -1368,6 +1368,7 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/harness/nextgen/model_connector_response.go b/harness/nextgen/model_connector_response.go index 1cffbb15..ff1b31b7 100644 --- a/harness/nextgen/model_connector_response.go +++ b/harness/nextgen/model_connector_response.go @@ -18,7 +18,7 @@ type ConnectorResponse struct { LastModifiedAt int64 `json:"lastModifiedAt,omitempty"` Status *ConnectorConnectivityDetails `json:"status,omitempty"` ActivityDetails *ConnectorActivityDetails `json:"activityDetails,omitempty"` - // This indicates if this Connector is managed by Harness or not. If True, Harness can manage and modify this Connector. + // This indicates if this Connector is managed by Harness or not. If True, Harness can manage and modify this Connector. HarnessManaged bool `json:"harnessManaged,omitempty"` GitDetails *EntityGitDetails `json:"gitDetails,omitempty"` EntityValidityDetails *EntityGitDetails `json:"entityValidityDetails,omitempty"` diff --git a/harness/nextgen/model_entity_git_details.go b/harness/nextgen/model_entity_git_details.go index 32f0d302..3dda7cec 100644 --- a/harness/nextgen/model_entity_git_details.go +++ b/harness/nextgen/model_entity_git_details.go @@ -11,8 +11,16 @@ package nextgen // This contains Validity Details of the Entity type EntityGitDetails struct { - // Indicates if the Entity is valid - Valid bool `json:"valid,omitempty"` - // This has the Git File content if the entity is invalid - InvalidYaml string `json:"invalidYaml,omitempty"` + // Object Id of the Entity + ObjectId string `json:"objectId,omitempty"` + // Branch Name + Branch string `json:"branch,omitempty"` + // Git Sync Config Id + RepoIdentifier string `json:"repoIdentifier,omitempty"` + // Root Folder Path of the Entity + RootFolder string `json:"rootFolder,omitempty"` + // File Path of the Entity + FilePath string `json:"filePath,omitempty"` + // Name of the repo + RepoName string `json:"repoName,omitempty"` } diff --git a/harness/nextgen/model_entity_git_details_helper.go b/harness/nextgen/model_entity_git_details_helper.go new file mode 100644 index 00000000..b1edade1 --- /dev/null +++ b/harness/nextgen/model_entity_git_details_helper.go @@ -0,0 +1,60 @@ +package nextgen + +import ( + "github.com/jinzhu/copier" +) + +func (d *EntityGitDetailsOptions) ToPipelinesApiPostPipelineOpts() *PipelinesApiPostPipelineOpts { + opts := &PipelinesApiPostPipelineOpts{} + err := copier.Copy(opts, d) + if err != nil { + return nil + } + return opts +} + +func (d *EntityGitDetailsOptions) ToPipelinesApiUpdatePipelineV2Opts() *PipelinesApiUpdatePipelineV2Opts { + opts := &PipelinesApiUpdatePipelineV2Opts{} + err := copier.Copy(opts, d) + if err != nil { + return nil + } + return opts +} + +func (d *EntityGitDetailsOptions) ToPipelinesApiGetPipelineOpts() *PipelinesApiGetPipelineOpts { + opts := &PipelinesApiGetPipelineOpts{} + err := copier.Copy(opts, d) + if err != nil { + return nil + } + return opts +} + +func (d *EntityGitDetailsOptions) ToPipelinesApiDeletePipelineOpts() *PipelinesApiDeletePipelineOpts { + opts := &PipelinesApiDeletePipelineOpts{} + err := copier.Copy(opts, d) + opts.LastObjectId = d.ObjectId + if err != nil { + return nil + } + return opts +} + +func (d *PipelineEntityGitDetails) ToEntityGitDetails() *EntityGitDetails { + gitEntity := &EntityGitDetails{} + err := copier.Copy(gitEntity, d) + if err != nil { + return nil + } + return gitEntity +} + +func (d *EntityGitDetails) IsEmpty() bool { + return d.ObjectId == "" && + d.Branch == "" && + d.RepoIdentifier == "" && + d.RootFolder == "" && + d.FilePath == "" && + d.RepoName == "" +} diff --git a/harness/nextgen/model_entity_git_details_options.go b/harness/nextgen/model_entity_git_details_options.go new file mode 100644 index 00000000..a3f2b1fe --- /dev/null +++ b/harness/nextgen/model_entity_git_details_options.go @@ -0,0 +1,15 @@ +package nextgen + +import "github.com/antihax/optional" + +// EntityGitDetailsOptions used for translating between the different API input options +type EntityGitDetailsOptions struct { + Branch optional.String + RepoIdentifier optional.String + RootFolder optional.String + FilePath optional.String + CommitMsg optional.String + IsNewBranch optional.Bool + BaseBranch optional.String + ObjectId optional.String +} diff --git a/harness/nextgen/model_entity_git_details_test.go b/harness/nextgen/model_entity_git_details_test.go new file mode 100644 index 00000000..4daab99a --- /dev/null +++ b/harness/nextgen/model_entity_git_details_test.go @@ -0,0 +1,27 @@ +package nextgen_test + +import ( + "testing" + + "github.com/harness/harness-go-sdk/harness/nextgen" + "github.com/jinzhu/copier" + "github.com/stretchr/testify/require" +) + +func TestSerialization(t *testing.T) { + + gitEntity := &nextgen.EntityGitDetails{ + ObjectId: "object_id", + Branch: "branch", + RepoIdentifier: "repo_id", + RootFolder: "root_folder", + FilePath: "file_path", + RepoName: "repo_name", + } + + opts := &nextgen.PipelinesApiUpdatePipelineOpts{} + + err := copier.Copy(opts, gitEntity) + require.NoError(t, err) + +} diff --git a/harness/nextgen/model_pipeline.go b/harness/nextgen/model_pipeline.go new file mode 100644 index 00000000..f91fb1f1 --- /dev/null +++ b/harness/nextgen/model_pipeline.go @@ -0,0 +1,9 @@ +package nextgen + +type Pipeline struct { + Name string `yaml:"name"` + Identifier string `yaml:"identifier"` + ProjectIdentifier string `yaml:"projectIdentifier"` + OrgIdentifier string `yaml:"orgIdentifier"` + Yaml string `yaml:"-"` +} diff --git a/harness/nextgen/model_pipeline_yaml.go b/harness/nextgen/model_pipeline_yaml.go new file mode 100644 index 00000000..fd4bc343 --- /dev/null +++ b/harness/nextgen/model_pipeline_yaml.go @@ -0,0 +1,5 @@ +package nextgen + +type PipelineData struct { + Pipeline Pipeline `yaml:"pipeline"` +} diff --git a/harness/nextgen/model_pms_pipeline_response.go b/harness/nextgen/model_pms_pipeline_response.go index 139cb906..4f2513a9 100644 --- a/harness/nextgen/model_pms_pipeline_response.go +++ b/harness/nextgen/model_pms_pipeline_response.go @@ -16,4 +16,5 @@ type PmsPipelineResponse struct { GitDetails *PipelineEntityGitDetails `json:"gitDetails,omitempty"` EntityValidityDetails *PipelineEntityGitDetails `json:"entityValidityDetails,omitempty"` Modules []string `json:"modules,omitempty"` + PipelineData *PipelineData `json:"-"` } diff --git a/harness/nextgen/model_pms_pipeline_response_serializer.go b/harness/nextgen/model_pms_pipeline_response_serializer.go new file mode 100644 index 00000000..24477ab1 --- /dev/null +++ b/harness/nextgen/model_pms_pipeline_response_serializer.go @@ -0,0 +1,36 @@ +package nextgen + +import ( + "encoding/json" + + "gopkg.in/yaml.v3" +) + +func (a *PmsPipelineResponse) UnmarshalJSON(data []byte) error { + + type Alias PmsPipelineResponse + + aux := &struct { + *Alias + }{ + Alias: (*Alias)(a), + } + + err := json.Unmarshal(data, &aux) + if err != nil { + return err + } + + err = yaml.Unmarshal([]byte(aux.YamlPipeline), &a.PipelineData) + // a.PipelineData.Pipeline.Yaml = string(data) + + return err +} + +// func (a *PmsPipelineResponse) MarshalJSON() ([]byte, error) { +// type Alias PmsPipelineResponse + +// a.YamlPipeline = a.PipelineData.Pipeline.Yaml + +// return json.Marshal((*Alias)(a)) +// } diff --git a/harness/nextgen/model_service_response.go b/harness/nextgen/model_service_response.go index 75310791..6a68bb76 100644 --- a/harness/nextgen/model_service_response.go +++ b/harness/nextgen/model_service_response.go @@ -10,7 +10,7 @@ package nextgen type ServiceResponse struct { - Service *ServiceResponse `json:"service,omitempty"` - CreatedAt int64 `json:"createdAt,omitempty"` - LastModifiedAt int64 `json:"lastModifiedAt,omitempty"` + Service *ServiceRequest `json:"service,omitempty"` + CreatedAt int64 `json:"createdAt,omitempty"` + LastModifiedAt int64 `json:"lastModifiedAt,omitempty"` }