BuildMessage constructs the Google Chat message payload.
(ctx webhook.Context)
| 91 | |
| 92 | // BuildMessage constructs the Google Chat message payload. |
| 93 | func BuildMessage(ctx webhook.Context) MessagePayload { |
| 94 | header := &CardHeader{ |
| 95 | Title: levelEmoji(ctx.Level) + escapeText(ctx.Title), |
| 96 | } |
| 97 | if ctx.Project != nil { |
| 98 | header.Subtitle = escapeText(ctx.Project.Title) |
| 99 | } |
| 100 | |
| 101 | widgets := []Widget{} |
| 102 | if ctx.Description != "" { |
| 103 | widgets = append(widgets, textWidget(ctx.Description)) |
| 104 | } |
| 105 | if ctx.Issue != nil { |
| 106 | if ctx.Issue.Name != "" { |
| 107 | widgets = append(widgets, textWidgetHTML(fmt.Sprintf("<b>%s</b>", escapeText(ctx.Issue.Name)))) |
| 108 | } |
| 109 | if ctx.Issue.Description != "" { |
| 110 | widgets = append(widgets, textWidgetHTML(fmt.Sprintf("<b>%s</b>", escapeText(common.TruncateStringWithDescription(ctx.Issue.Description))))) |
| 111 | } |
| 112 | } else if ctx.Rollout != nil && ctx.Rollout.Title != "" { |
| 113 | widgets = append(widgets, textWidgetHTML(fmt.Sprintf("<b>%s</b>", escapeText(ctx.Rollout.Title)))) |
| 114 | } |
| 115 | |
| 116 | if metadata := metadataText(ctx); metadata != "" { |
| 117 | widgets = append(widgets, textWidgetHTML(metadata)) |
| 118 | } |
| 119 | if ctx.Link != "" { |
| 120 | widgets = append(widgets, Widget{ |
| 121 | ButtonList: &ButtonList{ |
| 122 | Buttons: []Button{ |
| 123 | { |
| 124 | Text: "View in Bytebase", |
| 125 | OnClick: &OnClick{ |
| 126 | OpenLink: &OpenLink{URL: ctx.Link}, |
| 127 | }, |
| 128 | }, |
| 129 | }, |
| 130 | }, |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | card := Card{Header: header} |
| 135 | if len(widgets) > 0 { |
| 136 | card.Sections = []Section{{Widgets: widgets}} |
| 137 | } |
| 138 | |
| 139 | return MessagePayload{ |
| 140 | CardsV2: []CardV2{ |
| 141 | { |
| 142 | Card: card, |
| 143 | }, |
| 144 | }, |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func metadataText(ctx webhook.Context) string { |
| 149 | parts := []string{} |