Skip to content

Commit df21811

Browse files
authored
Merge pull request #164 from L0RD-ZER0/master
Add support for Application Tokens + Resolve Remaining Issues
2 parents 68c1f35 + 0d712e4 commit df21811

5 files changed

Lines changed: 57 additions & 10 deletions

File tree

Dockerfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ COPY main.go ${GOPATH}/src/github.com/rtcamp/action-slack-notify
1212
ENV CGO_ENABLED 0
1313
ENV GOOS linux
1414

15-
RUN go get -v ./...
16-
RUN go build -a -installsuffix cgo -ldflags '-w -extldflags "-static"' -o /go/bin/slack-notify .
15+
RUN go build -a -installsuffix cgo -ldflags '-w -extldflags "-static"' -o /go/bin/slack-notify main.go
1716

1817
# alpine:latest as of 2024-03-11
1918
FROM alpine@sha256:6457d53fb065d6f250e1504b9bc42d5b6c65941d57532c072d929dd0628977d0
@@ -31,7 +30,7 @@ RUN apk update \
3130
python3 \
3231
py3-pip \
3332
rsync \
34-
&& pip3 install shyaml \
33+
&& python3 -m pip install --break-system-packages shyaml \
3534
&& rm -rf /var/cache/apk/*
3635

3736
# Setup Vault

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ 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`.
6263
SLACK_CUSTOM_PAYLOAD | - | If you want to send a custom payload to slack, you can pass it as a string to this variable. This will override all other variables and send the custom payload to slack. Example: `SLACK_CUSTOM_PAYLOAD: '{"text": "Hello, World!"}'`, Note: This payload should be in JSON format, and is not validated by the action.
64+
ENABLE_ESCAPES | - | If set to `true`, will enable backslash escape sequences such as `\n`, `\t`, etc. in the message. Note: This only works for custom messages and not for the default message generated by the action.
6365

6466

6567
You can see the action block with all variables as below:

entrypoint.sh

Lines changed: 10 additions & 1 deletion
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

Lines changed: 34 additions & 5 deletions
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

Lines changed: 9 additions & 1 deletion
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")
@@ -57,10 +61,14 @@ if [[ -n "$SITE_NAME" ]]; then
5761
fi
5862

5963

60-
if [[ -z "$SLACK_MESSAGE" ]]; then
64+
if [[ -z "$SLACK_MESSAGE" && "null" != "$COMMIT_MESSAGE" ]]; then
6165
SLACK_MESSAGE="$COMMIT_MESSAGE"
6266
fi
6367

68+
if [[ -z "$SLACK_MESSAGE" ]]; then
69+
SLACK_MESSAGE="Notification from action run \`$GITHUB_RUN_NUMBER\`, which ran against commit \`${GITHUB_SHA}\` from branch \`${GITHUB_BRANCH}\` of \`${GITHUB_REPOSITORY}\` repository."
70+
fi
71+
6472
if [[ "true" == "$ENABLE_ESCAPES" ]]; then
6573
SLACK_MESSAGE="$(echo -e "$SLACK_MESSAGE")"
6674
fi

0 commit comments

Comments
 (0)