app_event.go 2.0 KB

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