Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows extra message #39

Merged
merged 7 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ The token of your Slack application which is allows chatting on your Slack organ

If you which to use a Docker registry proxy, specify the path prefix here

### `EXTRA_SLACK_MESSAGE` (string)

Contains an extra message to add on the bottom of the generated message. You can include a variable using the syntax `{var}`.

```yaml
EXTRA_SLACK_MESSAGE: "This project has a libyear of {LIBYEAR_DRIFT}"
```
with `LIBYEAR_DRIFT` environment variable set to `0.5` will add the message _This project has a libyear of 0.5_

## Create a bot for your workspace.
See Slack documentation [Create a bot for your workspace](https://slack.com/intl/en-fr/help/articles/115005265703-Create-a-bot-for-your-workspace) for instruction to create a Slack application.

Expand Down
21 changes: 12 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ services:
slack-notification:
image: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_DOCKER_CACHE}node:21
environment:
SLACK_TOKEN: ${TOKEN_VALUE}
SLACK_CHANNEL: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_SLACK_CHANNEL}
ARTIFACTS: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_ARTIFACTS}
BUILDKITE_BRANCH: ${BUILDKITE_BRANCH}
BUILDKITE_BUILD_AUTHOR: ${BUILDKITE_BUILD_AUTHOR}
BUILDKITE_BUILD_NUMBER: ${BUILDKITE_BUILD_NUMBER}
BUILDKITE_BUILD_URL: ${BUILDKITE_BUILD_URL}
BUILDKITE_PIPELINE_NAME: ${BUILDKITE_PIPELINE_NAME}
BUILDKITE_BRANCH: ${BUILDKITE_BRANCH}
BUILDKITE_MESSAGE: ${BUILDKITE_MESSAGE}
BUILDKITE_COMMIT: ${BUILDKITE_COMMIT}
BUILDKITE_BUILD_AUTHOR: ${BUILDKITE_BUILD_AUTHOR}
BUILDKITE_MESSAGE: ${BUILDKITE_MESSAGE}
BUILDKITE_PIPELINE_NAME: ${BUILDKITE_PIPELINE_NAME}
DEBUG: ${DEBUG}
EXTRA_SLACK_MESSAGE: ${EXTRA_SLACK_MESSAGE}
NPM_REGISTRY: ${NPM_REGISTRY}
NPM_TOKEN: ${NPM_TOKEN}
ARTIFACTS: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_ARTIFACTS}
TEST_SUITES_0_ARTIFACTS: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_TEST_SUITES_0_ARTIFACTS}
TEST_SUITES_0_NAME: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_TEST_SUITES_0_NAME}
SLACK_CHANNEL: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_SLACK_CHANNEL}
SLACK_TOKEN: ${TOKEN_VALUE}
TEST_SUITES_1_ARTIFACTS: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_TEST_SUITES_1_ARTIFACTS}
TEST_SUITES_1_NAME: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_TEST_SUITES_1_NAME}
TEST_SUITES_2_ARTIFACTS: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_TEST_SUITES_2_ARTIFACTS}
Expand All @@ -27,6 +27,9 @@ services:
TEST_SUITES_4_NAME: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_TEST_SUITES_4_NAME}
TEST_SUITES_5_ARTIFACTS: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_TEST_SUITES_5_ARTIFACTS}
TEST_SUITES_5_NAME: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_TEST_SUITES_5_NAME}
TEST_SUITES_0_ARTIFACTS: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_TEST_SUITES_0_ARTIFACTS}
TEST_SUITES_0_NAME: ${BUILDKITE_PLUGIN_JUNIT_SLACK_NOTIFICATION_TEST_SUITES_0_NAME}

command: /app/scripts/run.sh
volumes:
- $ARTIFACTS_DIR:/app/reports
Expand Down
24 changes: 24 additions & 0 deletions hooks/command
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ fi
# shellcheck source=lib/shared.bash
. "$DIR/../lib/shared.bash"

if [ "$DEBUG" = "true" ]; then
# display all environment variables except tokens
env | grep -v TOKEN
fi

TOKEN_ENV_NAME="$(plugin_read_config SLACK_TOKEN_ENV_NAME "SLACK_TOKEN")"

TOKEN_VALUE=$(eval "echo \${$TOKEN_ENV_NAME:-}")
Expand All @@ -24,6 +29,25 @@ if [[ -z "${TOKEN_VALUE}" ]]; then
exit 1
fi

if [ -z "${EXTRA_SLACK_MESSAGE}" ]; then
EXTRA_SLACK_MESSAGE="$(plugin_read_config EXTRA_SLACK_MESSAGE "")"
fi

if [ -z "${EXTRA_SLACK_MESSAGE}" ]; then
echo "EXTRA_SLACK_MESSAGE is empty."
else
# Loop through all environment variables
for var in $(compgen -v); do
# Check if the variable is in the string
if [[ $EXTRA_SLACK_MESSAGE == *"{${var}}"* ]]; then
# Substitute the variable in the string
EXTRA_SLACK_MESSAGE=${EXTRA_SLACK_MESSAGE/\{${var}\}/${!var}}
fi
done
echo "EXTRA_SLACK_MESSAGE is: ${EXTRA_SLACK_MESSAGE}"
fi
export EXTRA_SLACK_MESSAGE;

PLUGIN_DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)/.."

ARTIFACTS_DIR="$(pwd)/$(mktemp -d "junits-slack-notification-plugin-artifacts-tmp.XXXXXXXXXX")"
Expand Down
2 changes: 2 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ configuration:
type: string
SLACK_TOKEN_ENV_NAME:
type: string
EXTRA_SLACK_MESSAGE:
type: string
required:
- SLACK_CHANNEL
additionalProperties: false
7 changes: 4 additions & 3 deletions src/interfaces/junitResult.interface.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {JunitSuiteResult} from "./junitSuiteResult.interface";

export interface JunitResult {
build_id: number,
build_url: string;
buildkite_pipeline: string,
extra_message?: string;
git_branch_name: string,
build_id: number,
git_comment: string,
git_username: string,
git_log: string,
build_url: string;
git_username: string,
suite: JunitSuiteResult[];
}
15 changes: 14 additions & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@ import {sendResultToSlack} from "./slackNotification";
import {parseFiles} from "./xmlParser";
import {getStats} from "./testcaseStats";

function logEnvironmentVariableWhenDebugging() {
if (process.env.DEBUG === "true") {
// display all environment variables except tokens
Object.keys(process.env)
.filter(key => !key.includes("TOKEN"))
.forEach(key => {
console.log(`${key}: ${process.env[key]}`);
});
}
}

export const run = async (): Promise<void> => {
logEnvironmentVariableWhenDebugging();
const commit: JunitResult = {
build_id: parseInt(process.env.BUILDKITE_BUILD_NUMBER, 10),
build_url: process.env.BUILDKITE_BUILD_URL,
buildkite_pipeline: process.env.BUILDKITE_PIPELINE_NAME,
extra_message: process.env.EXTRA_SLACK_MESSAGE,
git_branch_name: process.env.BUILDKITE_BRANCH,
git_comment: process.env.BUILDKITE_MESSAGE.split("\n")[0],
git_log: process.env.BUILDKITE_COMMIT.substring(0, 7),
git_username: process.env.BUILDKITE_BUILD_AUTHOR,
suite: []
suite: [],
};

const SLACK_TOKEN = process.env.SLACK_TOKEN;
Expand Down
13 changes: 12 additions & 1 deletion src/slackNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ export const getSlackMessageAttachments = (results: JunitResult): unknown => {
};
});

const extra = results.extra_message?.length > 0? [
{
"type": "section",
"text": {
"text": results.extra_message,
"type": "mrkdwn"
}
}
]: [];

return [
{
"color": getColor(results),
Expand All @@ -84,7 +94,8 @@ export const getSlackMessageAttachments = (results: JunitResult): unknown => {
"url": results.build_url
}
},
...details
...details,
...extra
]
}
];
Expand Down
Loading