Skip to content

Commit 18b891d

Browse files
committed
Add support for slack application tokens
1 parent 68c1f35 commit 18b891d

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ SLACK_FOOTER | Powered By rtCamp's GitHub Actions Library | S
5656
MSG_MINIMAL | - | If set to `true`, removes: `Ref`, `Event`, `Actions URL` and `Commit` from the message. You can optionally whitelist any of these 4 removed values by passing it comma separated to the variable instead of `true`. (ex: `MSG_MINIMAL: event` or `MSG_MINIMAL: ref,actions url`, etc.)
5757
SLACKIFY_MARKDOWN | - | If set to `true`, it will convert markdown to slack format. (ex: `*bold*` to `bold`) Note: This only works for custom messages and not for the default message generated by the action. Credits: [slackify-markdown-action](https://github.com/marketplace/actions/slack-markdown-converter)
5858
SLACK_THREAD_TS | - | If you want to send message in a thread, you can pass the timestamp of the parent message to this variable. You can get the timestamp of the parent message from the message URL in Slack. (ex: `SLACK_THREAD_TS: 1586130833.000100`)
59+
SLACK_TOKEN | - | If you want to send message to a channel using a slack token. You will need to pass a channel in order to send messages using token, requiring a value for ``SLACK_CHANNEL``. Note that in case both webhook url and token are provided, webhook url will be prioritized.
5960
SLACK_ON_SUCCESS | - | If set, will send the provided message instead of the default message when the passed status (through ``SLACK_COLOR``) is `success`.
6061
SLACK_ON_FAILURE | - | If set, will send the provided message instead of the default message when the passed status (through ``SLACK_COLOR``) is `failure`.
6162
SLACK_ON_CANCEL | - | If set, will send the provided message instead of the default message when the passed status (through ``SLACK_COLOR``) is `cancelled`.

entrypoint.sh

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Check required env variables
44
flag=0
5+
mode="WEBHOOK"
56
if [[ -z "$SLACK_WEBHOOK" ]]; then
67
flag=1
78
missing_secret="SLACK_WEBHOOK"
@@ -13,11 +14,19 @@ if [[ -z "$SLACK_WEBHOOK" ]]; then
1314
fi
1415
fi
1516

17+
if [[ "$flag" -eq 1 ]] && [[ -n "$SLACK_TOKEN" || -n "$SLACK_CHANNEL" ]] ; then
18+
# Basically, if both SLACK_TOKEN and SLACK_CHANNEL are provided, then it's a token mode
19+
flag=0
20+
mode="TOKEN"
21+
fi
22+
1623
if [[ "$flag" -eq 1 ]]; then
17-
printf "[\e[0;31mERROR\e[0m] Secret \`$missing_secret\` is missing. Please add it to this action for proper execution.\nRefer https://github.com/rtCamp/action-slack-notify for more information.\n"
24+
echo -e "[\e[0;31mERROR\e[0m] Secret \`$missing_secret\` is missing. Alternatively, a pair of \`SLACK_TOKEN\` and \`SLACK_CHANNEL\` can be provided. Please add it to this action for proper execution.\nRefer https://github.com/rtCamp/action-slack-notify for more information.\n"
1825
exit 1
1926
fi
2027

28+
export MSG_MODE="$mode"
29+
2130
# custom path for files to override default files
2231
custom_path="$GITHUB_WORKSPACE/.github/slack"
2332
main_script="/main.sh"

main.go

+34-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
EnvMinimal = "MSG_MINIMAL"
3131
EnvSlackLinkNames = "SLACK_LINK_NAMES"
3232
EnvThreadTs = "SLACK_THREAD_TS"
33+
EnvMessageMode = "MSG_MODE"
3334
)
3435

3536
type Webhook struct {
@@ -64,16 +65,24 @@ type Field struct {
6465
func main() {
6566
endpoint := os.Getenv(EnvSlackWebhook)
6667
custom_payload := envOr(EnvSlackCustom, "")
68+
if endpoint == "" {
69+
if os.Getenv(EnvSlackChannel) == "" {
70+
fmt.Fprintln(os.Stderr, "Channel is required for sending message using a token")
71+
os.Exit(1)
72+
}
73+
if os.Getenv(EnvMessageMode) == "TOKEN" {
74+
endpoint = "https://slack.com/api/chat.postMessage"
75+
} else {
76+
fmt.Fprintln(os.Stderr, "URL is required")
77+
os.Exit(2)
78+
}
79+
}
6780
if custom_payload != "" {
6881
if err := send_raw(endpoint, []byte(custom_payload)); err != nil {
6982
fmt.Fprintf(os.Stderr, "Error sending message: %s\n", err)
7083
os.Exit(2)
7184
}
7285
} else {
73-
if endpoint == "" {
74-
fmt.Fprintln(os.Stderr, "URL is required")
75-
os.Exit(2)
76-
}
7786
text := os.Getenv(EnvSlackMessage)
7887
if text == "" {
7988
fmt.Fprintln(os.Stderr, "Message is required")
@@ -262,7 +271,27 @@ func send(endpoint string, msg Webhook) error {
262271

263272
func send_raw(endpoint string, payload []byte) error {
264273
b := bytes.NewBuffer(payload)
265-
res, err := http.Post(endpoint, "application/json", b)
274+
275+
var res *http.Response
276+
var err error
277+
278+
switch os.Getenv(EnvMessageMode) {
279+
case "WEBHOOK":
280+
res, err = http.Post(endpoint, "application/json", b)
281+
case "TOKEN":
282+
req, err := http.NewRequest("POST", endpoint, b)
283+
if err != nil {
284+
return fmt.Errorf("Error creating request: %s\n", err)
285+
}
286+
req.Header.Set("Content-Type", "application/json")
287+
req.Header.Set("Authorization", "Bearer "+os.Getenv("SLACK_TOKEN"))
288+
client := &http.Client{}
289+
res, err = client.Do(req)
290+
default:
291+
fmt.Fprintf(os.Stderr, "Invalid message mode: %s\n", os.Getenv(EnvMessageMode))
292+
os.Exit(6)
293+
}
294+
266295
if err != nil {
267296
return err
268297
}

main.sh

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ if [[ -z "$SLACK_WEBHOOK" ]]; then
3737
fi
3838
fi
3939

40+
if [[ -z "$SLACK_WEBHOOK" ]]; then
41+
printf "[\e[0;31mERROR\e[0m] Secret \`SLACK_WEBHOOK\` is missing. Falling back to using \`SLACK_TOKEN\` and \`SLACK_CHANNEL\`.\n"
42+
fi
43+
4044
if [[ -f "$hosts_file" ]]; then
4145
hostname=$(cat "$hosts_file" | shyaml get-value "$GITHUB_BRANCH.hostname")
4246
user=$(cat "$hosts_file" | shyaml get-value "$GITHUB_BRANCH.user")

0 commit comments

Comments
 (0)