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

Include Slack webhook example script #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,11 @@ $ detect-secrets-server scan yelp/detect-secrets \
--output-hook pysensu \
--output-config examples/pysensu.config.yaml
```

#### Slack

For Python 3 users, you can utilize the example Slack webhook script under
`examples/slack_webhook.py`. You'll need to add a `config.ini` with your Slack webhook
URL. This pretty formats the JSON and also builds a direct URL to the repos to help quickly
review. You'll need to add the [slack_webhook dependency](https://pypi.org/project/slack-webhook/).
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can provide an example like the pysensu invocation above.

68 changes: 68 additions & 0 deletions examples/slack_webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3.6 '
"""
Sample config.ini
[Slack]
webhook = https://hooks.slack.com/services/TXXXXX/BXXXXX
"""
import configparser
import json
import sys

from slack_webhook import Slack


def main():
# leaving this so cron picks up this output as doublecheck
print('repo:', sys.argv[1])
print(json.dumps(json.loads(sys.argv[2]), indent=2, sort_keys=True))

# pull Slack webhook out of config file
# Use ConfigParser to build necessary secrets for integrations
config = configparser.ConfigParser()
config.read('config.ini')
slack = Slack(url=config['Slack']['webhook'])

# build variables for this wacky json
# there'll only ever be one repo
repo = sys.argv[1]
payload = json.loads(sys.argv[2])

filepaths = list(payload.keys())
for filepath in filepaths:
instances = payload[filepath]
for instance in instances:
sp3nx0r marked this conversation as resolved.
Show resolved Hide resolved
author = instance['author']
commit = instance['commit']
detected_type = instance['type']
line_number = instance['line_number']

# build the message using Slack's "legacy" attachment
slack.post(text="<insert funny gerblin gibberish>",
sp3nx0r marked this conversation as resolved.
Show resolved Hide resolved
attachments=[{
"fallback": "Required plain-text summary of the attachment.",
"color": "#36a64f",
"title": "repo: {}".format(repo),
"title_link": "https://www.github.com/{}/blob/{}/{}#L{}".format(repo, commit, filepath, line_number),
"text": "File: {}".format(filepath),
"fields": [
{
"title": "Type",
"value": detected_type,
"short": "true"
},
{
"title": "Author",
"value": author,
"short": "true"
}, {
"title": "Commit",
"value": commit,
"short": "false"
}
]
}]
)


if __name__ == '__main__':
main()