app_event.go 2.2 KB

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