Skip to content

Commit

Permalink
feat: add validate::slack_configured
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Sep 28, 2024
1 parent e65566e commit ac60b86
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function main::action() {
local develop=${3:-1:-main}
local force_release=${4:-false}

validate::slack_configured "$force_release"

# Ensure 'origin/' prefix
if [[ $target != origin/* ]]; then
target="origin/$target"
Expand Down
31 changes: 18 additions & 13 deletions src/slack.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#!/bin/bash

# shellcheck disable=SC2155
function release::notify_via_slack() {
function slack::notify() {
if [[ -z "${SLACK_CHANNEL_ID:-}" || -z "${SLACK_OAUTH_TOKEN:-}" ]]; then
echo -e "${COLOR_CYAN}Slack configuration missing." \
"Check your .env for SLACK_CHANNEL_ID & SLACK_OAUTH_TOKEN${COLOR_RESET}"
return
fi

local repo_info=$1
local release_name=$2
local changelog_url=$3
Expand All @@ -27,17 +33,16 @@ function release::notify_via_slack() {
}
EOF
)

if [[ -n $SLACK_CHANNEL_ID && -n $SLACK_OAUTH_TOKEN ]]; then
if [[ "$DRY_RUN" == false ]]; then
curl -X POST https://slack.com/api/chat.postMessage \
-H "Content-Type: application/json; charset=utf-8" \
-H "Authorization: Bearer $SLACK_OAUTH_TOKEN" \
--data "$slack_message" \
-s -o /tmp/slack-error.log
echo -e "${COLOR_GREEN}Notification sent via slack${COLOR_RESET}"
else
echo -e "${COLOR_CYAN}--dry-run enabled. Skipping notify slack${COLOR_RESET}"
fi
if [[ "$DRY_RUN" == true ]]; then
echo -e "${COLOR_CYAN}--dry-run enabled. Skipping notify slack${COLOR_RESET}"
return
fi

curl -X POST https://slack.com/api/chat.postMessage \
-H "Content-Type: application/json; charset=utf-8" \
-H "Authorization: Bearer $SLACK_OAUTH_TOKEN" \
--data "$slack_message" \
-s -o /tmp/slack-error.log

echo -e "${COLOR_GREEN}Notification sent via slack${COLOR_RESET}"
}
27 changes: 27 additions & 0 deletions src/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,30 @@ your${COLOR_RESET} ${COLOR_ORANGE}$source${COLOR_RESET}."
io::confirm_or_exit "$question"
fi
}
# shellcheck disable=SC2155
function validate::slack_configured() {
local force_release=$1

if [[ $force_release == true ]]; then
return
fi

# Check if SLACK_CHANNEL_ID and SLACK_OAUTH_TOKEN are set, and exit if they are missing
if [[ -z "${SLACK_CHANNEL_ID:-}" || -z "${SLACK_OAUTH_TOKEN:-}" ]]; then
echo -e "${COLOR_RED}Slack configuration missing.${COLOR_RESET}" \
"Check your .env for SLACK_CHANNEL_ID & SLACK_OAUTH_TOKEN"
exit 1
fi

# Make the curl request and capture the response
local response=$(curl -s -d "token=$SLACK_OAUTH_TOKEN" https://slack.com/api/auth.test)

# Manually extract the "ok" field from the JSON response
local ok=$(echo "$response" | grep -o '"ok":true')

# Check if the "ok" field is found and equals true
if [[ -z $ok ]]; then
echo -e "${COLOR_RED}Slack auth test failed. Please check your SLACK_OAUTH_TOKEN.${COLOR_RESET}"
exit 1
fi
}

0 comments on commit ac60b86

Please sign in to comment.