Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Merge requests
!53
Slack utils
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Slack utils
slack-utils
into
main
Overview
1
Commits
3
Pipelines
0
Changes
3
1 unresolved thread
Hide all comments
Merged
Jano Hendriks
requested to merge
slack-utils
into
main
6 months ago
Overview
1
Commits
3
Pipelines
0
Changes
3
1 unresolved thread
Hide all comments
Expand
0
0
Merge request reports
Compare
main
version 2
fac51e5f
6 months ago
version 1
12fe6157
6 months ago
main (base)
and
version 1
latest version
1f097f9f
3 commits,
6 months ago
version 2
fac51e5f
2 commits,
6 months ago
version 1
12fe6157
1 commit,
6 months ago
3 files
+
125
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
slack_utils/slack_utils.go
0 → 100644
+
117
−
0
Options
package
slack_utils
import
(
"fmt"
"github.com/samber/lo"
"github.com/slack-go/slack"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
"reflect"
"regexp"
)
type
SlackField
struct
{
Label
string
Value
any
}
type
SlackClient
struct
{
Client
*
slack
.
Client
}
func
GetSlackClient
(
apiKey
string
)
*
SlackClient
{
return
&
SlackClient
{
Client
:
slack
.
New
(
apiKey
),
}
}
func
(
s
*
SlackClient
)
SendAlert
(
message
string
,
channel
string
,
parentMessageTimestamp
...
string
)
string
{
if
s
==
nil
||
s
.
Client
==
nil
{
// If the slack client isn't set, log the message to Raygun instead
logs
.
ErrorMsg
(
message
)
return
""
}
slackMessageOptions
:=
[]
slack
.
MsgOption
{
slack
.
MsgOptionText
(
message
,
false
),
}
if
len
(
parentMessageTimestamp
)
>
0
{
slackMessageOptions
=
append
(
slackMessageOptions
,
slack
.
MsgOptionTS
(
parentMessageTimestamp
[
0
]))
}
return
s
.
postMessage
(
channel
,
slackMessageOptions
)
}
func
(
s
*
SlackClient
)
SendAlertWithFields
(
message
string
,
channel
string
,
slackFields
[]
SlackField
,
parentMessageTimestamp
...
string
)
string
{
if
s
==
nil
||
s
.
Client
==
nil
{
// If the slack client isn't set, log the message to Raygun instead
slackFieldMap
:=
slackFieldsToMap
(
slackFields
)
logs
.
ErrorWithFields
(
slackFieldMap
,
errors
.
Error
(
message
))
return
""
}
blocks
:=
[]
slack
.
Block
{
slack
.
NewSectionBlock
(
slack
.
NewTextBlockObject
(
"plain_text"
,
message
,
hasEmoji
(
message
),
false
),
nil
,
nil
,
),
}
slackFieldsChunked
:=
lo
.
Chunk
(
slackFields
,
2
)
for
_
,
slackFieldsChunk
:=
range
slackFieldsChunked
{
fieldBlocksForChunk
:=
[]
*
slack
.
TextBlockObject
{}
for
_
,
slackField
:=
range
slackFieldsChunk
{
slackFieldValue
:=
slackField
.
Value
// if slackField.Value is a pointer, dereference it
if
reflect
.
ValueOf
(
slackField
.
Value
)
.
Kind
()
==
reflect
.
Ptr
{
slackFieldValue
=
reflect
.
ValueOf
(
slackField
.
Value
)
.
Elem
()
.
Interface
()
}
text
:=
fmt
.
Sprintf
(
"*%s*
\n
%v"
,
slackField
.
Label
,
slackFieldValue
)
fieldBlocksForChunk
=
append
(
fieldBlocksForChunk
,
slack
.
NewTextBlockObject
(
"mrkdwn"
,
text
,
false
,
false
,
))
}
blocks
=
append
(
blocks
,
slack
.
NewSectionBlock
(
nil
,
fieldBlocksForChunk
,
nil
))
}
slackMessageOptions
:=
[]
slack
.
MsgOption
{
slack
.
MsgOptionBlocks
(
blocks
...
),
}
if
len
(
parentMessageTimestamp
)
>
0
{
slackMessageOptions
=
append
(
slackMessageOptions
,
slack
.
MsgOptionTS
(
parentMessageTimestamp
[
0
]))
}
return
s
.
postMessage
(
channel
,
slackMessageOptions
)
}
func
(
s
*
SlackClient
)
postMessage
(
channel
string
,
messageOptions
[]
slack
.
MsgOption
)
string
{
_
,
messageTimestamp
,
err
:=
s
.
Client
.
PostMessage
(
channel
,
messageOptions
...
)
if
err
!=
nil
{
logs
.
ErrorWithMsg
(
"Error sending slack: %v+
\n
"
,
err
)
}
return
messageTimestamp
}
func
hasEmoji
(
message
string
)
bool
{
emojiRegex
:=
regexp
.
MustCompile
(
`:[a-zA-Z0-9_]+:`
)
return
emojiRegex
.
MatchString
(
message
)
}
func
slackFieldsToMap
(
fields
[]
SlackField
)
map
[
string
]
any
{
slackFieldsMap
:=
make
(
map
[
string
]
any
)
for
_
,
field
:=
range
fields
{
slackFieldsMap
[
field
.
Label
]
=
field
.
Value
}
return
slackFieldsMap
}
Loading