From a7dbebef76d3b9e2593d6f2868605517609585f4 Mon Sep 17 00:00:00 2001 From: Matthieu Bernardin Date: Mon, 29 Jul 2024 17:15:32 +0200 Subject: [PATCH] feat(task): Add capability to the slack task to dump file content and submodule git log --- task/slack-webhook-notification/0.1/README.md | 3 +- .../0.1/slack-webhook-notification.yaml | 123 +++++++++++++++++- 2 files changed, 123 insertions(+), 3 deletions(-) diff --git a/task/slack-webhook-notification/0.1/README.md b/task/slack-webhook-notification/0.1/README.md index 0aefc20ce9..779ee2a03a 100644 --- a/task/slack-webhook-notification/0.1/README.md +++ b/task/slack-webhook-notification/0.1/README.md @@ -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| diff --git a/task/slack-webhook-notification/0.1/slack-webhook-notification.yaml b/task/slack-webhook-notification/0.1/slack-webhook-notification.yaml index 7abe41bc55..34dded50ef 100644 --- a/task/slack-webhook-notification/0.1/slack-webhook-notification.yaml +++ b/task/slack-webhook-notification/0.1/slack-webhook-notification.yaml @@ -20,6 +20,14 @@ 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: @@ -27,7 +35,7 @@ spec: 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: @@ -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.