Feroze Mohideen преди 2 години
родител
ревизия
3a948063fc

+ 3 - 3
api/server/handlers/porter_app/create.go

@@ -182,7 +182,7 @@ func (c *CreatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 		// create the pre-deploy job chart if it does not exist (only done by front-end currently, where we set overrideRelease=true)
 		if request.OverrideRelease && preDeployJobValues != nil {
 			telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "installing-pre-deploy-job", Value: true})
-			conf, err := createReleaseJobChart(
+			conf, err := createPreDeployJobChart(
 				ctx,
 				stackName,
 				preDeployJobValues,
@@ -291,7 +291,7 @@ func (c *CreatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 				helmRelease, err := helmAgent.GetRelease(ctx, releaseJobName, 0, false)
 				if err != nil {
 					telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "creating-pre-deploy-job", Value: true})
-					conf, err := createReleaseJobChart(
+					conf, err := createPreDeployJobChart(
 						ctx,
 						stackName,
 						preDeployJobValues,
@@ -484,7 +484,7 @@ func createPorterAppDeployEvent(
 	return &event, nil
 }
 
-func createReleaseJobChart(
+func createPreDeployJobChart(
 	ctx context.Context,
 	stackName string,
 	values map[string]interface{},

+ 46 - 55
api/server/handlers/porter_app/create_and_update_events.go

@@ -249,75 +249,66 @@ func (p *CreateUpdatePorterAppEventHandler) maybeUpdateDeployEvent(ctx context.C
 	}
 	telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "new-status", Value: newStatusStr})
 
-	existingEvents, _, err := p.Repo().PorterAppEvent().ListEventsByPorterAppIDAndType(ctx, appID, string(types.PorterAppEventType_Deploy))
+	matchEvent, err := p.Repo().PorterAppEvent().ReadDeployEventByRevision(ctx, appID, revisionFloat64)
 	if err != nil {
-		_ = telemetry.Error(ctx, span, err, "error listing porter app events for deploy event type")
+		_ = telemetry.Error(ctx, span, err, "error finding matching deploy event")
 		return types.PorterAppEvent{}
 	}
 
 	telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "updating-deployment-event", Value: false})
 
-	var matchEvent *models.PorterAppEvent
-	for _, existingEvent := range existingEvents {
-		if existingEvent.Status != "PROGRESSING" {
-			continue
-		}
-		if _, ok := existingEvent.Metadata["revision"]; !ok {
-			continue
-		}
-		if existingEvent.Metadata["revision"] == revisionFloat64 {
-			matchEvent = existingEvent
-			break
-		}
+	// first check to see if the event is empty, meaning there was no match found
+	if matchEvent.Type != string(types.PorterAppEventType_Deploy) || matchEvent.Status != "PROGRESSING" {
+		return types.PorterAppEvent{}
 	}
 
-	if matchEvent != nil {
-		serviceStatus, ok := matchEvent.Metadata["service_status"]
-		if !ok {
-			_ = telemetry.Error(ctx, span, nil, "service status not found in deploy event metadata")
-			return types.PorterAppEvent{}
-		}
-		serviceStatusMap, ok := serviceStatus.(map[string]interface{})
-		if !ok {
-			_ = telemetry.Error(ctx, span, nil, "service status not a map[string]interface")
-			return types.PorterAppEvent{}
-		}
-		if _, ok := serviceStatusMap[serviceName]; !ok {
-			_ = telemetry.Error(ctx, span, nil, fmt.Sprintf("service status not found for service %s", serviceName))
-			return types.PorterAppEvent{}
-		}
+	fmt.Printf("found matching deploy event: %v\n", matchEvent)
+
+	serviceStatus, ok := matchEvent.Metadata["service_status"]
+	if !ok {
+		_ = telemetry.Error(ctx, span, nil, "service status not found in deploy event metadata")
+		return types.PorterAppEvent{}
+	}
+	serviceStatusMap, ok := serviceStatus.(map[string]interface{})
+	if !ok {
+		_ = telemetry.Error(ctx, span, nil, "service status not a map[string]interface")
+		return types.PorterAppEvent{}
+	}
+	if _, ok := serviceStatusMap[serviceName]; !ok {
+		_ = telemetry.Error(ctx, span, nil, fmt.Sprintf("service status not found for service %s", serviceName))
+		return types.PorterAppEvent{}
+	}
 
-		// only update service status if it has not been updated yet
-		if serviceStatusMap[serviceName] == "PROGRESSING" {
-			serviceStatusMap[serviceName] = newStatusStr
+	// only update service status if it has not been updated yet
+	if serviceStatusMap[serviceName] == "PROGRESSING" {
+		serviceStatusMap[serviceName] = newStatusStr
 
-			allServicesDone := true
-			anyServicesFailed := false
-			for _, status := range serviceStatusMap {
-				if status == "PROGRESSING" {
-					allServicesDone = false
-					break
-				}
-				if status == "FAILED" {
-					anyServicesFailed = true
-				}
+		allServicesDone := true
+		anyServicesFailed := false
+		for _, status := range serviceStatusMap {
+			if status == "PROGRESSING" {
+				allServicesDone = false
+				break
 			}
-			if allServicesDone {
-				if anyServicesFailed {
-					matchEvent.Status = "FAILED"
-				} else {
-					matchEvent.Status = "SUCCESS"
-				}
+			if status == "FAILED" {
+				anyServicesFailed = true
 			}
-
-			matchEvent.Metadata["service_status"] = serviceStatusMap
-			err = p.Repo().PorterAppEvent().UpdateEvent(ctx, matchEvent)
-			if err != nil {
-				_ = telemetry.Error(ctx, span, err, "error updating deploy event")
-				return matchEvent.ToPorterAppEvent()
+		}
+		if allServicesDone {
+			if anyServicesFailed {
+				matchEvent.Status = "FAILED"
+			} else {
+				matchEvent.Status = "SUCCESS"
 			}
-			telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "updating-deployment-event", Value: true})
 		}
+
+		matchEvent.Metadata["service_status"] = serviceStatusMap
+		err = p.Repo().PorterAppEvent().UpdateEvent(ctx, &matchEvent)
+		if err != nil {
+			_ = telemetry.Error(ctx, span, err, "error updating deploy event")
+			return matchEvent.ToPorterAppEvent()
+		}
+		telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "updating-deployment-event", Value: true})
 	}
 
 	return types.PorterAppEvent{}

+ 15 - 30
dashboard/package-lock.json

@@ -16426,8 +16426,7 @@
     "@icons/material": {
       "version": "0.2.4",
       "resolved": "https://registry.npmjs.org/@icons/material/-/material-0.2.4.tgz",
-      "integrity": "sha512-QPcGmICAPbGLGb6F/yNf/KzKqvFx8z5qx3D1yFqVAjoFmXK35EgyW+cJ57Te3CNsmzblwtzakLGFqHPqrfb4Tw==",
-      "requires": {}
+      "integrity": "sha512-QPcGmICAPbGLGb6F/yNf/KzKqvFx8z5qx3D1yFqVAjoFmXK35EgyW+cJ57Te3CNsmzblwtzakLGFqHPqrfb4Tw=="
     },
     "@ironplans/api": {
       "version": "0.4.1",
@@ -16607,8 +16606,7 @@
     "@material-ui/types": {
       "version": "5.1.0",
       "resolved": "https://registry.npmjs.org/@material-ui/types/-/types-5.1.0.tgz",
-      "integrity": "sha512-7cqRjrY50b8QzRSYyhSpx4WRw2YuO0KKIGQEVk5J8uoz2BanawykgZGoWEqKm7pVIbzFDN0SpPcVV4IhOFkl8A==",
-      "requires": {}
+      "integrity": "sha512-7cqRjrY50b8QzRSYyhSpx4WRw2YuO0KKIGQEVk5J8uoz2BanawykgZGoWEqKm7pVIbzFDN0SpPcVV4IhOFkl8A=="
     },
     "@material-ui/utils": {
       "version": "4.11.3",
@@ -16923,8 +16921,7 @@
       "version": "7.2.1",
       "resolved": "https://registry.npmjs.org/@testing-library/user-event/-/user-event-7.2.1.tgz",
       "integrity": "sha512-oZ0Ib5I4Z2pUEcoo95cT1cr6slco9WY7yiPpG+RGNkj8YcYgJnM7pXmYmorNOReh8MIGcKSqXyeGjxnr8YiZbA==",
-      "dev": true,
-      "requires": {}
+      "dev": true
     },
     "@types/body-parser": {
       "version": "1.19.2",
@@ -18141,15 +18138,13 @@
       "version": "1.0.1",
       "resolved": "https://registry.npmjs.org/ajv-errors/-/ajv-errors-1.0.1.tgz",
       "integrity": "sha512-DCRfO/4nQ+89p/RK43i8Ezd41EqdGIU4ld7nGF8OQ14oc/we5rEntLCUa7+jrn3nn83BosfwZA0wb4pon2o8iQ==",
-      "dev": true,
-      "requires": {}
+      "dev": true
     },
     "ajv-keywords": {
       "version": "3.5.2",
       "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-3.5.2.tgz",
       "integrity": "sha512-5p6WTN0DdTGVQk6VjcEju19IgaHudalcfabD7yhDGeA6bcQnmL+CpveLJq/3hvfwd1aof6L386Ougkx6RfyMIQ==",
-      "dev": true,
-      "requires": {}
+      "dev": true
     },
     "anser": {
       "version": "2.1.1",
@@ -21086,8 +21081,7 @@
     "goober": {
       "version": "2.1.12",
       "resolved": "https://registry.npmjs.org/goober/-/goober-2.1.12.tgz",
-      "integrity": "sha512-yXHAvO08FU1JgTXX6Zn6sYCUFfB/OJSX8HHjDSgerZHZmFKAb08cykp5LBw5QnmyMcZyPRMqkdyHUSSzge788Q==",
-      "requires": {}
+      "integrity": "sha512-yXHAvO08FU1JgTXX6Zn6sYCUFfB/OJSX8HHjDSgerZHZmFKAb08cykp5LBw5QnmyMcZyPRMqkdyHUSSzge788Q=="
     },
     "good-listener": {
       "version": "1.2.2",
@@ -21473,8 +21467,7 @@
       "version": "5.1.0",
       "resolved": "https://registry.npmjs.org/icss-utils/-/icss-utils-5.1.0.tgz",
       "integrity": "sha512-soFhflCVWLfRNOPU3iv5Z9VUdT44xFRbzjLsEzSr5AQmgqPMTHdU3PMT1Cf1ssx8fLNJDA1juftYl+PUcv3MqA==",
-      "dev": true,
-      "requires": {}
+      "dev": true
     },
     "ieee754": {
       "version": "1.2.1",
@@ -22351,8 +22344,7 @@
     "markdown-to-jsx": {
       "version": "7.2.0",
       "resolved": "https://registry.npmjs.org/markdown-to-jsx/-/markdown-to-jsx-7.2.0.tgz",
-      "integrity": "sha512-3l4/Bigjm4bEqjCR6Xr+d4DtM1X6vvtGsMGSjJYyep8RjjIvcWtrXBS8Wbfe1/P+atKNMccpsraESIaWVplzVg==",
-      "requires": {}
+      "integrity": "sha512-3l4/Bigjm4bEqjCR6Xr+d4DtM1X6vvtGsMGSjJYyep8RjjIvcWtrXBS8Wbfe1/P+atKNMccpsraESIaWVplzVg=="
     },
     "material-colors": {
       "version": "1.2.6",
@@ -23270,8 +23262,7 @@
       "version": "3.0.0",
       "resolved": "https://registry.npmjs.org/postcss-modules-extract-imports/-/postcss-modules-extract-imports-3.0.0.tgz",
       "integrity": "sha512-bdHleFnP3kZ4NYDhuGlVK+CMrQ/pqUm8bx/oGL93K6gVwiclvX5x0n76fYMKuIGKzlABOy13zsvqjb0f92TEXw==",
-      "dev": true,
-      "requires": {}
+      "dev": true
     },
     "postcss-modules-local-by-default": {
       "version": "4.0.0",
@@ -23568,8 +23559,7 @@
     "react-animate-height": {
       "version": "3.1.1",
       "resolved": "https://registry.npmjs.org/react-animate-height/-/react-animate-height-3.1.1.tgz",
-      "integrity": "sha512-UkC6+V3ZlCneBRaSM7aUctDJ+PRP6ztcGtxvU7MTeoMMWPhz8BQNaX7QWaZrkzp1ih1G8uZZ+DI9nfLvtD6OdQ==",
-      "requires": {}
+      "integrity": "sha512-UkC6+V3ZlCneBRaSM7aUctDJ+PRP6ztcGtxvU7MTeoMMWPhz8BQNaX7QWaZrkzp1ih1G8uZZ+DI9nfLvtD6OdQ=="
     },
     "react-beautiful-dnd": {
       "version": "13.1.1",
@@ -23688,8 +23678,7 @@
     "react-onclickoutside": {
       "version": "6.12.2",
       "resolved": "https://registry.npmjs.org/react-onclickoutside/-/react-onclickoutside-6.12.2.tgz",
-      "integrity": "sha512-NMXGa223OnsrGVp5dJHkuKxQ4czdLmXSp5jSV9OqiCky9LOpPATn3vLldc+q5fK3gKbEHvr7J1u0yhBh/xYkpA==",
-      "requires": {}
+      "integrity": "sha512-NMXGa223OnsrGVp5dJHkuKxQ4czdLmXSp5jSV9OqiCky9LOpPATn3vLldc+q5fK3gKbEHvr7J1u0yhBh/xYkpA=="
     },
     "react-popper": {
       "version": "2.3.0",
@@ -23761,8 +23750,7 @@
     "react-table": {
       "version": "7.8.0",
       "resolved": "https://registry.npmjs.org/react-table/-/react-table-7.8.0.tgz",
-      "integrity": "sha512-hNaz4ygkZO4bESeFfnfOft73iBUj8K5oKi1EcSHPAibEydfsX2MyU6Z8KCr3mv3C9Kqqh71U+DhZkFvibbnPbA==",
-      "requires": {}
+      "integrity": "sha512-hNaz4ygkZO4bESeFfnfOft73iBUj8K5oKi1EcSHPAibEydfsX2MyU6Z8KCr3mv3C9Kqqh71U+DhZkFvibbnPbA=="
     },
     "react-transition-group": {
       "version": "4.4.5",
@@ -25544,14 +25532,12 @@
       "version": "1.1.3",
       "resolved": "https://registry.npmjs.org/use-memo-one/-/use-memo-one-1.1.3.tgz",
       "integrity": "sha512-g66/K7ZQGYrI6dy8GLpVcMsBp4s17xNkYJVSMvTEevGy3nDxHOfE6z8BVE22+5G5x7t3+bhzrlTDB7ObrEE0cQ==",
-      "dev": true,
-      "requires": {}
+      "dev": true
     },
     "use-sync-external-store": {
       "version": "1.2.0",
       "resolved": "https://registry.npmjs.org/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz",
-      "integrity": "sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA==",
-      "requires": {}
+      "integrity": "sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA=="
     },
     "util": {
       "version": "0.11.1",
@@ -26845,8 +26831,7 @@
       "version": "7.5.9",
       "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.9.tgz",
       "integrity": "sha512-F+P9Jil7UiSKSkppIiD94dN07AwvFixvLIj1Og1Rl9GGMuNipJnV9JzjD6XuqmAeiswGvUmNLjr5cFuXwNS77Q==",
-      "dev": true,
-      "requires": {}
+      "dev": true
     },
     "xtend": {
       "version": "4.0.2",

+ 15 - 27
internal/repository/gorm/porter_app_event.go

@@ -2,6 +2,7 @@ package gorm
 
 import (
 	"context"
+	"encoding/json"
 	"errors"
 	"strconv"
 	"time"
@@ -46,28 +47,6 @@ func (repo *PorterAppEventRepository) ListEventsByPorterAppID(ctx context.Contex
 	return apps, paginatedResult, nil
 }
 
-func (repo *PorterAppEventRepository) ListEventsByPorterAppIDAndType(ctx context.Context, porterAppID uint, eventType string, opts ...helpers.QueryOption) ([]*models.PorterAppEvent, helpers.PaginatedResult, error) {
-	apps := []*models.PorterAppEvent{}
-	paginatedResult := helpers.PaginatedResult{}
-
-	id := strconv.Itoa(int(porterAppID))
-	if id == "" {
-		return nil, paginatedResult, errors.New("invalid porter app id supplied")
-	}
-
-	db := repo.db.Model(&models.PorterAppEvent{})
-	resultDB := db.Where("porter_app_id = ? AND type = ?", id, eventType).Order("created_at DESC")
-	resultDB = resultDB.Scopes(helpers.Paginate(db, &paginatedResult, opts...))
-
-	if err := resultDB.Find(&apps).Error; err != nil {
-		if !errors.Is(err, gorm.ErrRecordNotFound) {
-			return nil, paginatedResult, err
-		}
-	}
-
-	return apps, paginatedResult, nil
-}
-
 func (repo *PorterAppEventRepository) CreateEvent(ctx context.Context, appEvent *models.PorterAppEvent) error {
 	if appEvent.ID == uuid.Nil {
 		appEvent.ID = uuid.New()
@@ -124,15 +103,24 @@ func (repo *PorterAppEventRepository) ReadEvent(ctx context.Context, id uuid.UUI
 	return appEvent, nil
 }
 
-func (repo *PorterAppEventRepository) FindDeployEventByRevision(ctx context.Context, porterAppID uint, revision float64) (models.PorterAppEvent, error) {
+func (repo *PorterAppEventRepository) ReadDeployEventByRevision(ctx context.Context, porterAppID uint, revision float64) (models.PorterAppEvent, error) {
 	appEvent := models.PorterAppEvent{}
 
-	id := strconv.Itoa(int(porterAppID))
-	if id == "" {
-		return appEvent, errors.New("invalid porter app id supplied")
+	if porterAppID == 0 {
+		return appEvent, errors.New("invalid porter app ID supplied")
+	}
+
+	// Convert porterAppID to string
+	strAppID := strconv.Itoa(int(porterAppID))
+
+	// Convert revision to JSON number string
+	revJSON, err := json.Marshal(revision)
+	if err != nil {
+		return appEvent, errors.New("unable to marshal revision")
 	}
+	strRevision := string(revJSON)
 
-	if err := repo.db.Where("porter_app_id = ? AND type = 'DEPLOY' AND metadata ->> 'revision' = ?", id, revision).First(&appEvent).Error; err != nil {
+	if err := repo.db.Where("porter_app_id = ? AND metadata->>'revision' = ?", strAppID, strRevision).First(&appEvent).Error; err != nil {
 		return appEvent, err
 	}
 

+ 1 - 1
internal/repository/porter_app_event.go

@@ -11,8 +11,8 @@ import (
 // PorterAppEventRepository represents the set of queries on the PorterAppEvent model
 type PorterAppEventRepository interface {
 	ListEventsByPorterAppID(ctx context.Context, porterAppID uint, opts ...helpers.QueryOption) ([]*models.PorterAppEvent, helpers.PaginatedResult, error)
-	ListEventsByPorterAppIDAndType(ctx context.Context, porterAppID uint, eventType string, opts ...helpers.QueryOption) ([]*models.PorterAppEvent, helpers.PaginatedResult, error)
 	CreateEvent(ctx context.Context, appEvent *models.PorterAppEvent) error
 	UpdateEvent(ctx context.Context, appEvent *models.PorterAppEvent) error
 	ReadEvent(ctx context.Context, id uuid.UUID) (models.PorterAppEvent, error)
+	ReadDeployEventByRevision(ctx context.Context, porterAppID uint, revision float64) (models.PorterAppEvent, error)
 }