helpers.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package slack
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/porter-dev/porter/internal/notifier"
  6. )
  7. func getSlackBlocks(opts *notifier.NotifyOpts) ([]*SlackBlock, []*SlackBlock) {
  8. res := []*SlackBlock{}
  9. if opts.Status == notifier.StatusHelmDeployed || opts.Status == notifier.StatusHelmFailed {
  10. res = append(res, getHelmMessageBlock(opts))
  11. } else if opts.Status == notifier.StatusPodCrashed {
  12. res = append(res, getPodCrashedMessageBlock(opts))
  13. }
  14. res = append(
  15. res,
  16. getDividerBlock(),
  17. getMarkdownBlock(fmt.Sprintf("*Name:* %s", "`"+opts.Name+"`")),
  18. getMarkdownBlock(fmt.Sprintf("*Namespace:* %s", "`"+opts.Namespace+"`")),
  19. )
  20. if opts.Timestamp != nil {
  21. res = append(res, getMarkdownBlock(fmt.Sprintf(
  22. "*Timestamp:* <!date^%d^Alerted at {date_num} {time_secs}|Alerted at %s>",
  23. opts.Timestamp.Unix(),
  24. opts.Timestamp.Format("2006-01-02 15:04:05 UTC"),
  25. )),
  26. )
  27. }
  28. if opts.Status == notifier.StatusHelmDeployed || opts.Status == notifier.StatusHelmFailed {
  29. res = append(res, getMarkdownBlock(fmt.Sprintf("*Version:* %d", opts.Version)))
  30. }
  31. basicRes := res
  32. infoBlock := getInfoBlock(opts)
  33. if infoBlock != nil {
  34. res = append(res, infoBlock)
  35. }
  36. return res, basicRes
  37. }
  38. func getDividerBlock() *SlackBlock {
  39. return &SlackBlock{
  40. Type: "divider",
  41. }
  42. }
  43. func getMarkdownBlock(md string) *SlackBlock {
  44. return &SlackBlock{
  45. Type: "section",
  46. Text: &SlackText{
  47. Type: "mrkdwn",
  48. Text: md,
  49. },
  50. }
  51. }
  52. func getHelmMessageBlock(opts *notifier.NotifyOpts) *SlackBlock {
  53. var md string
  54. switch opts.Status {
  55. case notifier.StatusHelmDeployed:
  56. md = getHelmSuccessMessage(opts)
  57. case notifier.StatusHelmFailed:
  58. md = getHelmFailedMessage(opts)
  59. }
  60. return getMarkdownBlock(md)
  61. }
  62. func getPodCrashedMessageBlock(opts *notifier.NotifyOpts) *SlackBlock {
  63. md := fmt.Sprintf(
  64. ":x: Your application %s crashed on Porter. <%s|View the application.>",
  65. "`"+opts.Name+"`",
  66. opts.URL,
  67. )
  68. return getMarkdownBlock(md)
  69. }
  70. func getInfoBlock(opts *notifier.NotifyOpts) *SlackBlock {
  71. var md string
  72. switch opts.Status {
  73. case notifier.StatusHelmFailed:
  74. md = getFailedInfoMessage(opts)
  75. case notifier.StatusPodCrashed:
  76. md = getFailedInfoMessage(opts)
  77. default:
  78. return nil
  79. }
  80. return getMarkdownBlock(md)
  81. }
  82. func getHelmSuccessMessage(opts *notifier.NotifyOpts) string {
  83. return fmt.Sprintf(
  84. ":rocket: Your application %s was successfully updated on Porter! <%s|View the new release.>",
  85. "`"+opts.Name+"`",
  86. opts.URL,
  87. )
  88. }
  89. func getHelmFailedMessage(opts *notifier.NotifyOpts) string {
  90. return fmt.Sprintf(
  91. ":x: Your application %s failed to deploy on Porter. <%s|View the status here.>",
  92. "`"+opts.Name+"`",
  93. opts.URL,
  94. )
  95. }
  96. func getFailedInfoMessage(opts *notifier.NotifyOpts) string {
  97. info := opts.Info
  98. // TODO: this casing is quite ugly and looks for particular types of API server
  99. // errors, otherwise it truncates the error message to 200 characters. This should
  100. // handle the errors more gracefully.
  101. if strings.Contains(info, "Invalid value:") {
  102. errArr := strings.Split(info, "Invalid value:")
  103. // look for "unmarshalerDecoder" error
  104. if strings.Contains(info, "unmarshalerDecoder") {
  105. udArr := strings.Split(info, "unmarshalerDecoder:")
  106. info = errArr[0] + udArr[1]
  107. } else {
  108. info = errArr[0] + "..."
  109. }
  110. } else if len(info) > 200 {
  111. info = info[0:200] + "..."
  112. }
  113. return fmt.Sprintf("```\n%s\n```", info)
  114. }