Skip to content

Commit

Permalink
feat(task): Add capability to the slack task to dump file content and…
Browse files Browse the repository at this point in the history
… submodule git log
  • Loading branch information
pastequo committed Jul 30, 2024
1 parent 8a2d1f0 commit a7dbebe
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 3 deletions.
3 changes: 2 additions & 1 deletion task/slack-webhook-notification/0.1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ Sends message to slack using incoming webhook
|message|Message to be sent||true|
|secret-name|Secret with least one key where value is webhook URL for slack. eg. oc create secret generic my-secret --from-literal team1=https://hooks.slack.com/services/XXX/XXXXXX --from-literal team2=https://hooks.slack.com/services/YYY/YYYYYY |slack-webhook-notification-secret|false|
|key-name|Key in the key in secret which contains webhook URL for slack.||true|

|files|List of file to dump. The content will be added to the message.|[]|false|
|submodules|List of submodules name to dump. Git log since previous submodule commit will be added to the message.|[]|false|
123 changes: 121 additions & 2 deletions task/slack-webhook-notification/0.1/slack-webhook-notification.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ spec:
default: slack-webhook-notification-secret
- name: key-name
description: Key in the key in secret which contains webhook URL for slack.
- name: submodules
type: array
description: List of submodules name to dump. Git log since previous submodule commit will be added to the message.
default: []
- name: files
type: array
description: List of file to dump. The content will be added to the message.
default: []
volumes:
- name: webhook-secret
secret:
secretName: $(params.secret-name)
optional: true
steps:
- name: send-message
image: registry.access.redhat.com/ubi9/ubi-minimal:9.4-1194@sha256:73f7dcacb460dad137a58f24668470a5a2e47378838a0190eef0ab532c6e8998
image: registry.access.redhat.com/ubi9/toolbox@sha256:131d7386c8f3520cab8d6359884f8f45bf2d0acab0f2e1a9a531d37cf0a3f324
# per https://kubernetes.io/docs/concepts/containers/images/#imagepullpolicy-defaulting
# the cluster will set imagePullPolicy to IfNotPresent
volumeMounts:
Expand All @@ -43,10 +51,121 @@ spec:
value: $(params.message)
script: |
#!/usr/bin/env bash
# ##########
# HELPERS
# ##########
function escapeDoubleQuote { echo "${1//\"/\\\"}"; }
# ##########
# DUMPERS
# ##########
function dumpSeparator {
echo "-----------------------------------------"
}
function dumpFile {
filePath=$1
read -r -d '' ret << EOM
*${filePath}:*
\`\`\`
$(cat "${filePath}")
\`\`\`
EOM
echo "${ret}"
}
function dumpSubmodule {
name=$1
path=$(git config -f .gitmodules --get submodule."${name}".path)
url=$(git config -f .gitmodules --get submodule."${name}".url)
current_commit=$(git -C "${path}" rev-parse HEAD)
previous_commit=$(git diff HEAD~1 "${path}" | grep commit | head -n 1 | awk '{print $3;}')
if [ "${previous_commit}" = "" ]; then
previous_commit=${current_commit}
fi
commits=None
if [ "${previous_commit}" != "${current_commit}" ]; then
commits=$(git -C "${path}" log --pretty=format:'%h %<(27,trunc)%an %s' --abbrev-commit "${previous_commit}".."${current_commit}")
fi
read -r -d '' ret << EOM
*Submodule ${name} (<${url}/commits/${current_commit}|commits>):*
\`\`\`
${commits}
\`\`\`
EOM
echo "${ret}"
}
# ###############
# ARGS PARSING
# ###############
FILES=""
SUBMODULES=""
while [[ $# -gt 0 ]]; do
case $1 in
--files)
shift
while [[ $# -gt 0 ]] && ! [[ "$1" =~ --.* ]]; do
FILES+=" $1"
shift
done
;;
--submodules)
shift
while [[ $# -gt 0 ]] && ! [[ "$1" =~ --.* ]]; do
SUBMODULES+=" $1"
shift
done
;;
esac
done
# ####################
# PARAMS VALIDATION
# ####################
if [ -f "/etc/secrets/$KEY_NAME" ]; then
WEBHOOK_URL=$(cat "/etc/secrets/$KEY_NAME")
else
echo "Secret not defined properly"
exit 1
fi
curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$MESSAGE\"}" $WEBHOOK_URL
# #######
# DUMP
# #######
slack_message=${MESSAGE}
if [ "$FILES" != "" ]; then
slack_message+="\n$(dumpSeparator)"
fi
for file in ${FILES}; do
content=$(dumpFile "${file}")
slack_message+="\n${content}"
done
if [ "$SUBMODULES" != "" ]; then
slack_message+="\n$(dumpSeparator)"
fi
for submodule in ${SUBMODULES}; do
content=$(dumpSubmodule "${submodule}")
slack_message+="\n${content}"
done
escaped_slack_message=$(escapeDoubleQuote "${slack_message}")
curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"${escaped_slack_message}\"}" "$WEBHOOK_URL"
workingDir: $(workspaces.source.path)/source
workspaces:
- name: source
description: Workspace containing the source code to build.

0 comments on commit a7dbebe

Please sign in to comment.