1
1
package com.monta.slack.notifier
2
2
3
- import com.monta.slack.notifier.model.GithubPushContext
3
+ import com.monta.slack.notifier.model.GithubEvent
4
4
import com.monta.slack.notifier.model.JobStatus
5
5
import com.monta.slack.notifier.model.JobType
6
+ import com.monta.slack.notifier.model.SlackBlock
6
7
import com.monta.slack.notifier.model.SlackMessage
7
8
import com.monta.slack.notifier.util.JsonUtil
9
+ import com.monta.slack.notifier.util.buildTitle
8
10
import com.monta.slack.notifier.util.client
9
11
import io.ktor.client.request.*
10
12
import io.ktor.client.statement.*
@@ -21,14 +23,14 @@ class SlackClient(
21
23
) {
22
24
23
25
suspend fun create (
24
- githubPushContext : GithubPushContext ,
26
+ githubEvent : GithubEvent ,
25
27
jobType : JobType ,
26
28
jobStatus : JobStatus ,
27
29
): String {
28
30
val response = makeSlackRequest(
29
31
url = " https://slack.com/api/chat.postMessage" ,
30
- message = generateMessage (
31
- githubPushContext = githubPushContext ,
32
+ message = generateMessageFromGithubEvent (
33
+ githubEvent = githubEvent ,
32
34
jobType = jobType,
33
35
jobStatus = jobStatus
34
36
)
@@ -39,16 +41,16 @@ class SlackClient(
39
41
40
42
suspend fun update (
41
43
messageId : String ,
42
- githubPushContext : GithubPushContext ,
44
+ githubEvent : GithubEvent ,
43
45
jobType : JobType ,
44
46
jobStatus : JobStatus ,
45
47
): String {
46
48
val previousMessage = getSlackMessageById(messageId)
47
49
48
50
val response = makeSlackRequest(
49
51
url = " https://slack.com/api/chat.update" ,
50
- message = generateMessage (
51
- githubPushContext = githubPushContext ,
52
+ message = generateMessageFromGithubEvent (
53
+ githubEvent = githubEvent ,
52
54
jobType = jobType,
53
55
jobStatus = jobStatus,
54
56
messageId = messageId,
@@ -59,8 +61,66 @@ class SlackClient(
59
61
return requireNotNull(response?.ts)
60
62
}
61
63
62
- private fun generateMessage (
63
- githubPushContext : GithubPushContext ,
64
+ private fun generateSlackMessageFromEvent (
65
+ githubEvent : GithubEvent ,
66
+ serviceName : String? ,
67
+ serviceEmoji : String? ,
68
+ slackChannelId : String ,
69
+ messageId : String? ,
70
+ attachments : List <SlackMessage .Attachment >? ,
71
+ ): SlackMessage {
72
+ val title = buildTitle(githubEvent.repository, githubEvent.workflow, serviceName, serviceEmoji)
73
+
74
+ return SlackMessage (
75
+ channel = slackChannelId,
76
+ ts = messageId,
77
+ text = title,
78
+ blocks = listOf (
79
+ SlackBlock (
80
+ type = " header" ,
81
+ text = SlackBlock .Text (
82
+ type = " plain_text" ,
83
+ text = title
84
+ )
85
+ ),
86
+ SlackBlock (
87
+ type = " divider"
88
+ ),
89
+ SlackBlock (
90
+ type = " section" ,
91
+ fields = listOf (
92
+ SlackBlock .Text (
93
+ type = " mrkdwn" ,
94
+ text = " \n *Branch:*\n ${githubEvent.refName} "
95
+ ),
96
+ SlackBlock .Text (
97
+ type = " mrkdwn" ,
98
+ text = " \n *Run:*\n <${githubEvent.getRunUrl()} |${githubEvent.runId} >"
99
+ ),
100
+ SlackBlock .Text (
101
+ type = " mrkdwn" ,
102
+ text = " \n *Comitter:*\n ${githubEvent.displayName} "
103
+ ),
104
+ SlackBlock .Text (
105
+ type = " mrkdwn" ,
106
+ text = " \n *Message:*\n <${githubEvent.getCommitUrl()} |${githubEvent.getCommitMessage()} >"
107
+ ),
108
+ SlackBlock .Text (
109
+ type = " mrkdwn" ,
110
+ text = " \n *SHA:*\n <${githubEvent.getCommitUrl()} |${githubEvent.commitSHA} >"
111
+ )
112
+ )
113
+ ),
114
+ SlackBlock (
115
+ type = " divider"
116
+ )
117
+ ),
118
+ attachments = attachments
119
+ )
120
+ }
121
+
122
+ private fun generateMessageFromGithubEvent (
123
+ githubEvent : GithubEvent ,
64
124
jobType : JobType ,
65
125
jobStatus : JobStatus ,
66
126
messageId : String? = null,
@@ -86,7 +146,8 @@ class SlackClient(
86
146
)
87
147
)
88
148
89
- return githubPushContext.toMessage(
149
+ return generateSlackMessageFromEvent(
150
+ githubEvent = githubEvent,
90
151
serviceName = serviceName,
91
152
serviceEmoji = serviceEmoji,
92
153
slackChannelId = slackChannelId,
0 commit comments