Skip to content

Commit 56ae66b

Browse files
committed
Initial release
0 parents  commit 56ae66b

File tree

8 files changed

+418
-0
lines changed

8 files changed

+418
-0
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
## [0.0.1] - 2020-04-17
11+
12+
### Added
13+
14+
- Initial plugin release
15+
- Limited string escaping for supplied values
16+
- Configuration for most event parameters
17+
18+
[Unreleased]: https://github.com/envato/create-datadog-event-buildkite-plugin/compare/v0.0.1...HEAD
19+
[0.0.1]: https://github.com/envato/create-datadog-event-buildkite-plugin/releases/tag/v0.0.1

CODE_OF_CONDUCT.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
We as members, contributors, and leaders pledge to make participation in our
6+
community a harassment-free experience for everyone, regardless of age, body
7+
size, visible or invisible disability, ethnicity, sex characteristics, gender
8+
identity and expression, level of experience, education, socio-economic status,
9+
nationality, personal appearance, race, religion, or sexual identity
10+
and orientation.
11+
12+
We pledge to act and interact in ways that contribute to an open, welcoming,
13+
diverse, inclusive, and healthy community.
14+
15+
## Our Standards
16+
17+
Examples of behavior that contributes to a positive environment for our
18+
community include:
19+
20+
* Demonstrating empathy and kindness toward other people
21+
* Being respectful of differing opinions, viewpoints, and experiences
22+
* Giving and gracefully accepting constructive feedback
23+
* Accepting responsibility and apologizing to those affected by our mistakes,
24+
and learning from the experience
25+
* Focusing on what is best not just for us as individuals, but for the
26+
overall community
27+
28+
Examples of unacceptable behavior include:
29+
30+
* The use of sexualized language or imagery, and sexual attention or
31+
advances of any kind
32+
* Trolling, insulting or derogatory comments, and personal or political attacks
33+
* Public or private harassment
34+
* Publishing others' private information, such as a physical or email
35+
address, without their explicit permission
36+
* Other conduct which could reasonably be considered inappropriate in a
37+
professional setting
38+
39+
## Enforcement Responsibilities
40+
41+
Community leaders are responsible for clarifying and enforcing our standards of
42+
acceptable behavior and will take appropriate and fair corrective action in
43+
response to any behavior that they deem inappropriate, threatening, offensive,
44+
or harmful.
45+
46+
Community leaders have the right and responsibility to remove, edit, or reject
47+
comments, commits, code, wiki edits, issues, and other contributions that are
48+
not aligned to this Code of Conduct, and will communicate reasons for moderation
49+
decisions when appropriate.
50+
51+
## Scope
52+
53+
This Code of Conduct applies within all community spaces, and also applies when
54+
an individual is officially representing the community in public spaces.
55+
Examples of representing our community include using an official e-mail address,
56+
posting via an official social media account, or acting as an appointed
57+
representative at an online or offline event.
58+
59+
## Enforcement
60+
61+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
62+
reported to the community leaders responsible for enforcement at [email protected].
63+
All complaints will be reviewed and investigated promptly and fairly.
64+
65+
All community leaders are obligated to respect the privacy and security of the
66+
reporter of any incident.
67+
68+
## Enforcement Guidelines
69+
70+
Community leaders will follow these Community Impact Guidelines in determining
71+
the consequences for any action they deem in violation of this Code of Conduct:
72+
73+
### 1. Correction
74+
75+
**Community Impact**: Use of inappropriate language or other behavior deemed
76+
unprofessional or unwelcome in the community.
77+
78+
**Consequence**: A private, written warning from community leaders, providing
79+
clarity around the nature of the violation and an explanation of why the
80+
behavior was inappropriate. A public apology may be requested.
81+
82+
### 2. Warning
83+
84+
**Community Impact**: A violation through a single incident or series
85+
of actions.
86+
87+
**Consequence**: A warning with consequences for continued behavior. No
88+
interaction with the people involved, including unsolicited interaction with
89+
those enforcing the Code of Conduct, for a specified period of time. This
90+
includes avoiding interactions in community spaces as well as external channels
91+
like social media. Violating these terms may lead to a temporary or
92+
permanent ban.
93+
94+
### 3. Temporary Ban
95+
96+
**Community Impact**: A serious violation of community standards, including
97+
sustained inappropriate behavior.
98+
99+
**Consequence**: A temporary ban from any sort of interaction or public
100+
communication with the community for a specified period of time. No public or
101+
private interaction with the people involved, including unsolicited interaction
102+
with those enforcing the Code of Conduct, is allowed during this period.
103+
Violating these terms may lead to a permanent ban.
104+
105+
### 4. Permanent Ban
106+
107+
**Community Impact**: Demonstrating a pattern of violation of community
108+
standards, including sustained inappropriate behavior, harassment of an
109+
individual, or aggression toward or disparagement of classes of individuals.
110+
111+
**Consequence**: A permanent ban from any sort of public interaction within
112+
the community.
113+
114+
## Attribution
115+
116+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
117+
version 2.0, available at
118+
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
119+
120+
Community Impact Guidelines were inspired by [Mozilla's code of conduct
121+
enforcement ladder](https://github.com/mozilla/diversity).
122+
123+
[homepage]: https://www.contributor-covenant.org
124+
125+
For answers to common questions about this code of conduct, see the FAQ at
126+
https://www.contributor-covenant.org/faq. Translations are available at
127+
https://www.contributor-covenant.org/translations.
128+

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Envato
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Create Datadog Event Buildkite Plugin
2+
3+
[Changelog] | [License (MIT)] | [Code of Conduct]
4+
5+
An experimental [Buildkite plugin](https://buildkite.com/docs/agent/v3/plugins) which creates Datadog events.
6+
7+
It contains a [command hook](hooks/command). No tests are currently included, but contributions are welcome.
8+
9+
Inspired by a desire to show releases on metrics graphs, and to include information such as the URL to the build.
10+
11+
## Features
12+
13+
- Creates a Datadog event
14+
- Basic JSON escaping, in case variables contain backslashes, quotes, tabs, or newlines
15+
- Supports most properties available in the Datadog event API
16+
17+
## Example
18+
19+
```yml
20+
steps:
21+
- plugins:
22+
envato/create-datadog-event#v0.0.1:
23+
api_key: $DATADOG_API_KEY
24+
aggregation_key: $BUILDKITE_BUILD_ID
25+
title: Deploying $BUILDKITE_PIPELINE_SLUG
26+
text: >
27+
Deploying $BUILDKITE_BRANCH/$BUILDKITE_COMMIT on behalf of
28+
$BUILDKITE_BUILD_CREATOR. More details: $BUILDKITE_BUILD_URL
29+
tags:
30+
- event-type:deployment
31+
- state:started
32+
- branch:$BUILDKITE_BRANCH
33+
# don't fail the deploy if the event failed
34+
soft_fail:
35+
- exit_status: '*'
36+
37+
# deploy step here...
38+
```
39+
40+
Supplying the aggregation key helps if you'd like to emit events for deploy started and deploy succeeded.
41+
42+
## Configuration
43+
44+
Most values correspond to the arguments in the [Post an event API].
45+
46+
| Required | Name | Description |
47+
| :------: | :-------- | :---------- |
48+
|Y| `api_key` | Datadog API key for authentication |
49+
|Y| `title` | Event title, 100 characters max |
50+
|Y| `text` | Event body as markdown, 4000 characters max |
51+
| | `priority` | Defaults to `normal`, can change to `low` |
52+
| | `host` | Host to associate with the event |
53+
| | `tags` | Array of event tags as strings |
54+
| | `alert_type` | If an alert event, indicate an `error`, `warning`, `info` or `success` event |
55+
| | `aggregation_key` | Key used to group related events in the Datadog event stream, 100 characters max |
56+
| | `source_type_name` | Type of event being posted |
57+
| | `related_event_id` | ID of the parent event, integer without quotes |
58+
| | `datadog_host` | Datadog API URL, e.g. for the European endpoint |
59+
60+
## License
61+
62+
MIT (see [LICENSE](LICENSE))
63+
64+
## Code of Conduct
65+
66+
Contributor Covenant 2.0 (see [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md))
67+
68+
## Maintainers
69+
70+
- [Liam Dawson](https://github.com/liamdawson/)
71+
72+
## About
73+
74+
This project is maintained by the [Envato engineering team][webuild] and funded by [Envato][envato].
75+
76+
[![Envato logo](https://opensource.envato.com/images/envato-oss-readme-logo.png)][envato]
77+
78+
Encouraging the use and creation of open source software is one of the ways we serve our community. See [our other projects][oss] or [come work with us][careers] where you'll find an incredibly diverse, intelligent and capable group of people who help make our company succeed and make our workplace fun, friendly and happy.
79+
80+
[Post an event API]: https://docs.datadoghq.com/api/?lang=bash#post-an-event
81+
[Changelog]: CHANGELOG.md
82+
[License (MIT)]: LICENSE
83+
[Code of Conduct]: CODE_OF_CONDUCT.md
84+
[webuild]: http://webuild.envato.com?utm_source=github
85+
[envato]: https://envato.com?utm_source=github
86+
[oss]: http://opensource.envato.com//?utm_source=github
87+
[careers]: http://careers.envato.com/?utm_source=github

docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '2'
2+
services:
3+
tests:
4+
image: buildkite/plugin-tester
5+
volumes:
6+
- ".:/plugin:ro"
7+
lint:
8+
image: buildkite/plugin-linter
9+
command: ['--id', 'envato/create-datadog-event']
10+
volumes:
11+
- ".:/plugin:ro"

hooks/command

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
main() {
5+
local datadog_host="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_DATADOG_HOST:-https://api.datadoghq.com}"
6+
local api_key="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_API_KEY}"
7+
8+
curl -X POST \
9+
-H "Content-type: application/json" \
10+
-H "DD-API-KEY: ${api_key}" \
11+
-d "$(make_body)" \
12+
"${datadog_host}/api/v1/events"
13+
}
14+
15+
make_body() {
16+
local msg_title="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_TITLE}"
17+
local msg_text="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_TEXT}"
18+
local priority="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_PRIORITY:-normal}"
19+
local host="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_HOST:-}"
20+
local alert_type="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_ALERT_TYPE:-info}"
21+
local aggregation_key="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_AGGREGATION_KEY:-}"
22+
local source_type_name="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_SOURCE_TYPE_NAME:-}"
23+
local related_event_id="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_RELATED_EVENT_ID:-}"
24+
25+
local tags=""
26+
27+
while IFS=$'\n' read -r tag ; do
28+
tags="$tags, \"$(escape_json_string "$tag")\""
29+
done < <(plugin_read_list TAGS)
30+
31+
tags="${tags:2}"
32+
33+
echo '{'
34+
echo " $(escaped_pair title "$msg_title")"
35+
echo " ,$(escaped_pair text "$msg_text")"
36+
echo " ,$(escaped_pair priority "$priority")"
37+
echo " ,$(escaped_pair alert_type "$alert_type")"
38+
39+
if [ -n "$host" ]; then
40+
echo " ,$(escaped_pair host "$host")"
41+
fi
42+
43+
if [ -n "$aggregation_key" ]; then
44+
echo " ,$(escaped_pair aggregation_key "$aggregation_key")"
45+
fi
46+
47+
if [ -n "$related_event_id" ]; then
48+
echo " ,$(escaped_pair related_event_id "$related_event_id")"
49+
fi
50+
51+
if [ -n "$source_type_name" ]; then
52+
echo " ,$(escaped_pair source_type_name "$source_type_name")"
53+
fi
54+
55+
if [ -n "$tags" ]; then
56+
echo ' ,"tags": ['"$tags"']'
57+
fi
58+
59+
echo '}'
60+
}
61+
62+
# probably buggy, PRs welcome
63+
escape_json_string() {
64+
local sed_cmd
65+
sed_cmd="$(command -v sed)"
66+
67+
# use gsed if it's available
68+
if command -v gsed >/dev/null 2>&1; then
69+
sed_cmd="$(command -v gsed)"
70+
fi
71+
72+
# escape slashes, tabs, newlines, then double-quotes
73+
echo "$1" | $sed_cmd -Ee 's/\\/\\\\/g' | $sed_cmd -Ee 's/\t/\\t/g' | $sed_cmd -Ee ':a;N;$!ba;s/\r{0,1}\n/\\n/g' | $sed_cmd -Ee 's/"/\\"/g'
74+
}
75+
76+
# https://github.com/buildkite-plugins/shellcheck-buildkite-plugin/blob/08694ed7f660dfd21e7639e8e861f23ba605d351/hooks/command#L5-L20
77+
plugin_read_list() {
78+
local prefix="BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_$1"
79+
local parameter="${prefix}_0"
80+
81+
if [[ -n "${!parameter:-}" ]]; then
82+
local i=0
83+
local parameter="${prefix}_${i}"
84+
while [[ -n "${!parameter:-}" ]]; do
85+
echo "${!parameter}"
86+
i=$((i+1))
87+
parameter="${prefix}_${i}"
88+
done
89+
elif [[ -n "${!prefix:-}" ]]; then
90+
echo "${!prefix}"
91+
fi
92+
}
93+
94+
escaped_pair() {
95+
echo "\"$1\": \"$(escape_json_string "$2")\""
96+
}
97+
98+
main

0 commit comments

Comments
 (0)