Skip to content

Commit

Permalink
Adding client to pull requests for catalog (#184)
Browse files Browse the repository at this point in the history
* Adding client to pull requests for catalog

* Added changelog

* adding client to pdfile
  • Loading branch information
cjlapao authored Aug 20, 2024
1 parent da7dc76 commit 63bf084
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

## [0.8.7] - 2024-08-20

### Added

- Added client to the pull request from the catalog

## [0.8.6] - 2024-07-11

### Fixed
Expand Down
10 changes: 10 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ while [[ $# -gt 0 ]]; do
INSTALL_SERVICE="false"
shift # past argument
;;
-v)
VERSION=$2
shift # past argument
shift # past argument
;;
--version)
VERSION=$2
shift # past argument
shift # past argument
;;
*)
echo "Invalid option $1" >&2
exit 1
Expand Down
1 change: 1 addition & 0 deletions src/catalog/models/pull_catalog_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type PullCatalogManifestRequest struct {
ProviderMetadata map[string]string `json:"provider_metadata,omitempty"`
StartAfterPull bool `json:"start_after_pull,omitempty"`
LocalMachineFolder string `json:"-"`
AmplitudeEvent string `json:"client,omitempty"`
}

func (r *PullCatalogManifestRequest) Validate() error {
Expand Down
40 changes: 40 additions & 0 deletions src/controllers/catalog.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/Parallels/prl-devops-service/models"
"github.com/Parallels/prl-devops-service/restapi"
"github.com/Parallels/prl-devops-service/serviceprovider"
"github.com/Parallels/prl-devops-service/telemetry"

"github.com/cjlapao/common-go/helper/http_helper"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -1529,10 +1531,43 @@ func PullCatalogManifestHandler() restapi.ControllerHandler {
})
return
}
sendTelemetry := false
var amplitudeEvent models.AmplitudeEvent
telemetryItem := telemetry.TelemetryItem{}
if request.AmplitudeEvent != "" {
decodedBytes, err := base64.StdEncoding.DecodeString(request.AmplitudeEvent)
if err == nil {
err := json.Unmarshal(decodedBytes, &amplitudeEvent)
if err == nil {
telemetryItem.Type = amplitudeEvent.EventType
if telemetryItem.Type == "" {
telemetryItem.Type = "DEVOPS_PULL_MANIFEST"
}
telemetryItem.Properties = amplitudeEvent.EventProperties
telemetryItem.Options = amplitudeEvent.UserProperties
telemetryItem.UserID = amplitudeEvent.UserId
telemetryItem.DeviceId = amplitudeEvent.DeviceId
if amplitudeEvent.Origin != "" {
telemetryItem.Properties["origin"] = amplitudeEvent.Origin
}
sendTelemetry = true
} else {
ctx.LogErrorf("Error unmarshalling amplitude event", err)
}
} else {
ctx.LogErrorf("Error decoding amplitude event", err)
}
}

manifest := catalog.NewManifestService(ctx)
resultManifest := manifest.Pull(ctx, &request)

if resultManifest.HasErrors() {
if sendTelemetry && amplitudeEvent.EventProperties != nil {
telemetryItem.Properties["success"] = "false"
telemetry.TrackEvent(telemetryItem)
}

errorMessage := "Error pulling manifest: \n"
for _, err := range resultManifest.Errors {
errorMessage += "\n" + err.Error() + " "
Expand All @@ -1544,6 +1579,11 @@ func PullCatalogManifestHandler() restapi.ControllerHandler {
return
}

if sendTelemetry && amplitudeEvent.EventProperties != nil {
telemetryItem.Properties["success"] = "true"
telemetry.TrackEvent(telemetryItem)
}

resultData := mappers.BasePullCatalogManifestResponseToApi(*resultManifest)
resultData.ID = resultManifest.ID

Expand Down
10 changes: 10 additions & 0 deletions src/models/amplitude.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type AmplitudeEvent struct {
EventType string `json:"event_type"`
EventProperties map[string]interface{} `json:"event_properties"`
UserProperties map[string]interface{} `json:"user_properties"`
DeviceId string `json:"device_id"`
UserId string `json:"user_id"`
Origin string `json:"origin"`
}
1 change: 1 addition & 0 deletions src/pdfile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewPDFileService(ctx basecontext.ApiContext, pdFile *models.PDFile) *PDFile
&processors.TagCommandProcessor{},
&processors.VersionCommandProcessor{},
&processors.CloneCommandProcessor{},
&processors.ClientCommandProcessor{},
},

pdfile: pdFile,
Expand Down
1 change: 1 addition & 0 deletions src/pdfile/models/pdfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PDFile struct {
CloneTo string `json:"CLONE_TO,omitempty" yaml:"CLONE_TO,omitempty"`
CloneId string `json:"CLONE_ID,omitempty" yaml:"CLONE_ID,omitempty"`
Operation string `json:"RUN,omitempty" yaml:"RUN,omitempty"`
Client string `json:"CLIENT,omitempty" yaml:"CLIENT,omitempty"`
}

func NewPdFile() *PDFile {
Expand Down
29 changes: 29 additions & 0 deletions src/pdfile/processors/client_processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package processors

import (
"errors"

"github.com/Parallels/prl-devops-service/basecontext"
"github.com/Parallels/prl-devops-service/pdfile/diagnostics"
"github.com/Parallels/prl-devops-service/pdfile/models"
)

type ClientCommandProcessor struct{}

func (p ClientCommandProcessor) Process(ctx basecontext.ApiContext, line string, dest *models.PDFile) (bool, *diagnostics.PDFileDiagnostics) {
diag := diagnostics.NewPDFileDiagnostics()
command := getCommand(line)
if command == nil {
return false, diag
}
if command.Command != "CLIENT" {
return false, diag
}
if command.Argument == "" {
diag.AddError(errors.New("client command is missing argument"))
}

dest.Client = command.Argument
ctx.LogDebugf("Processed by ClientCommandProcessor, line %v", line)
return true, diag
}
1 change: 1 addition & 0 deletions src/pdfile/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (p *PDFileService) runPull(ctx basecontext.ApiContext) (interface{}, *diagn
Path: p.pdfile.Destination,
Connection: p.pdfile.GetHostConnection(),
StartAfterPull: p.pdfile.StartAfterPull,
AmplitudeEvent: p.pdfile.Client,
}

if p.pdfile.Clone {
Expand Down
2 changes: 2 additions & 0 deletions src/pdfile/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func (p *PDFileService) Validate() *diagnostics.PDFileDiagnostics {
continue
case "DESCRIPTION":
continue
case "CLIENT":
continue
case "EXECUTE":
continue
case "CLONE":
Expand Down

0 comments on commit 63bf084

Please sign in to comment.