app_event.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package notifications
  2. import (
  3. "encoding/json"
  4. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  5. "github.com/porter-dev/porter/api/types"
  6. "github.com/porter-dev/porter/internal/models"
  7. )
  8. // AppEventMetadata is the metadata for an app event
  9. type AppEventMetadata struct {
  10. // AgentEventID is the ID of the porter agent event that triggered this app event
  11. AgentEventID int `json:"agent_event_id"`
  12. // Revision is the revision number of the app when this event was fired
  13. Revision int `json:"revision"`
  14. // AppRevisionID is the revision ID of the app when this event was fired
  15. AppRevisionID string `json:"app_revision_id"`
  16. // ServiceName refers to the name of the service this event refers to
  17. ServiceName string `json:"service_name"`
  18. // ServiceType refers to the type of the service this event refers to
  19. ServiceType string `json:"service_type"`
  20. // ShortSummary is the short summary of the app event
  21. ShortSummary string `json:"short_summary"`
  22. // Summary is the summary of the app event
  23. Summary string `json:"summary"`
  24. // AppName is the name of the app that this event refers to
  25. AppName string `json:"app_name"`
  26. // Detail is the detail of the app event
  27. Detail string `json:"detail"`
  28. // JobRunID is the ID of the job run that this event refers to, if applicable
  29. JobRunID string `json:"job_run_id"`
  30. // AppEventType is the type of the event this AppEvent is generated for
  31. AppEventType porterv1.AppEventType `json:"app_event_type"`
  32. // DeployStatus is the status of the deployment, if applicable
  33. DeployStatus types.PorterAppEventStatus `json:"deploy_status"`
  34. }
  35. // ParseAgentEventMetadata parses raw app event metadata to a AppEventMetadata struct
  36. func ParseAgentEventMetadata(metadata map[string]interface{}) (*AppEventMetadata, error) {
  37. appEventMetadata := &AppEventMetadata{}
  38. bytes, err := json.Marshal(metadata)
  39. if err != nil {
  40. return nil, err
  41. }
  42. err = json.Unmarshal(bytes, appEventMetadata)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return appEventMetadata, nil
  47. }
  48. // NotificationFromPorterAppEvent converts a PorterAppEvent to a Notification
  49. func NotificationFromPorterAppEvent(appEvent *models.PorterAppEvent) (*Notification, error) {
  50. notification := &Notification{}
  51. bytes, err := json.Marshal(appEvent.Metadata)
  52. if err != nil {
  53. return notification, err
  54. }
  55. err = json.Unmarshal(bytes, notification)
  56. if err != nil {
  57. return notification, err
  58. }
  59. return notification, nil
  60. }