diff --git a/.woodpecker.yml b/.woodpecker.yml new file mode 100644 index 0000000..ce6fcaf --- /dev/null +++ b/.woodpecker.yml @@ -0,0 +1,5 @@ +steps: + lint: + image: koalaman/shellcheck + commands: + - shellcheck entrypoint.sh diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b841600 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2023 Alexander "PapaTutuWawa" + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..d8497b4 --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# woodpecker-xmpp + +A simple plugin for [Woodpecker CI](https://woodpecker-ci.org/) that allows sending notifications +about the pipeline status using XMPP (direct messages or MUC messages). + +The plugin is just a small shell script wrapping [go-sendxmpp](https://salsa.debian.org/mdosch/go-sendxmpp). + +## Config + +In order for the plugin to work, the following secrets must be set: + +| Secret | Description | +| --- | --- | +| `xmpp_jid` | The JID to authenticate as | +| `xmpp_password` | The password to authenticate with | + +If your server has special connection parameters (hostname:port), then you can also +specify the `xmpp_server` secret to something like `example.org:443`. + +Plaintext configuration options for the plugin are: + +| Setting | Description ] +| --- | --- | +| `xmpp_tls` | If set, enables direct TLS | +| `xmpp_is_muc` | If set, tells go-sendxmpp that the message is to be sent to a groupchat | +| `xmpp_recipient` | The recipient (XMPP account or MUC) of the message | +| `xmpp_alias` | The nickname to use when joining the MUC. Has no effect if `xmpp_is_muc` is not set | + +## Testing + +Build the image using docker or podman as `papatutuwawa/woodpecker-xmpp` and run + +``` +docker run --rm \ + -e CI_BUILD_STATUS=success \ + -e CI_COMMIT_HASH=aaaaaaaaa \ + -e CI_REPO=example-user/repo \ + -e XMPP_JID=user@example.org \ + -e XMPP_PASSWORD=s3cr3t-p4ssw0rd \ + -e PLUGIN_XMPP_RECIPIENT=other-jid@example.org \ + papatutuwawa/woodpecker-xmpp +``` + +## License + +See `./LICENSE`. diff --git a/entrypoint.sh b/entrypoint.sh index b30ee89..df252e6 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,26 +1,37 @@ #!/bin/bash +# Allow debugging +[[ -n "${PLUGIN_DEBUG}" ]] && set -x + +# Set the correct message if [[ "${CI_BUILD_STATUS}" = "success" ]]; then - MESSAGE="✔️ Build for ${CI_COMMIT_SHA} on ${CI_REPO} succeeded." + MESSAGE="✔️ Pipeline for ${CI_COMMIT_SHA} on ${CI_REPO} succeeded." else - MESSAGE="❌ Build for ${CI_COMMIT_SHA} on ${CI_REPO} failed on ${CI_MACHINE} (${CI_SYSTEM_ARCH}).\nSee ${CI_BUILD_LINK}" + MESSAGE="❌ Pipeline for ${CI_COMMIT_SHA} on ${CI_REPO} failed on ${CI_MACHINE} (${CI_SYSTEM_ARCH}).\nSee ${CI_BUILD_LINK}" fi +# Tell go-sendxmpp about the server to connect to, if specified SERVER_DETAILS="" -if [[ ! -z "${XMPP_SERVER}" ]]; then +if [[ -n "${XMPP_SERVER}" ]]; then SERVER_DETAILS=(-j "${XMPP_SERVER}") fi +# Force TLS, if configured SERVER_TLS="" -if [[ ! -z "${PLUGIN_XMPP_TLS}" ]]; then +if [[ -n "${PLUGIN_XMPP_TLS}" ]]; then SERVER_TLS=--tls fi +# Enable groupchat settings, if configured. GROUPCHAT="" -if [[ ! -z "${PLUGIN_XMPP_IS_MUC}" ]]; then +if [[ -n "${PLUGIN_XMPP_IS_MUC}" ]]; then GROUPCHAT="-c" # Make the bot nickname configurable - BOT_ALIAS=(-a ${PLUGIN_XMPP_ALIAS:-"Woodpecker CI"}) + BOT_ALIAS=(-a "${PLUGIN_XMPP_ALIAS:-"Woodpecker CI"}") fi -echo -e "$MESSAGE" | go-sendxmpp -u ${XMPP_JID} -p ${XMPP_PASSWORD} ${SERVER_DETAILS[@]} ${SERVER_TLS} ${BOT_ALIAS} ${GROUPCHAT} ${PLUGIN_XMPP_MUC} +# "Post" the message +# Disable SC2086 (double quote to prevent word splitting) and SC2068 (double quote array expansion to prevent word splitting) +# because we DO want word splitting here. +# shellcheck disable=SC2086,SC2068 +echo -e "$MESSAGE" | go-sendxmpp -u ${XMPP_JID} -p ${XMPP_PASSWORD} ${SERVER_DETAILS[@]} ${SERVER_TLS} ${BOT_ALIAS[@]} ${GROUPCHAT} ${PLUGIN_XMPP_RECIPIENT}