Skip to content

Instantly share code, notes, and snippets.

@paul-schwendenman
Created March 22, 2017 05:47
Show Gist options
  • Save paul-schwendenman/a4a42f6146c3f44c007fb8dd62cbdc03 to your computer and use it in GitHub Desktop.
Save paul-schwendenman/a4a42f6146c3f44c007fb8dd62cbdc03 to your computer and use it in GitHub Desktop.
Manage Trello webhooks
'''
Script to manage Trello hooks
This script is used to manage Trello hooks. It has sub-commands to create, list
and delete webhooks. It uses two environment variables to authenticate with
Trello: TRELLO_TOKEN and TRELLO_KEY.
Tests:
To run the tests::
nosetests test_setup_trello_hooks.py
Examples:
Creating a new webhook::
$ python setup_trello_hooks.py create 57e97628d89b38b11b165167 \
"https://example.com"
{
"active": true,
"idModel": "57e97628d89b38b11b165167",
"callbackURL": "https://example.com",
"id": "58d1c4ba2ddf6661ccaf59bd",
"description": "PNDA PLZ Review Board Webhook"
}
List current webhooks::
$ python setup_trello_hooks.py list
[
{
"active": true,
"idModel": "57e97628d89b38b11b165167",
"callbackURL": "http://example.com",
"id": "58d1c35f4e2c3dc25b9726ab",
"description": "PNDA PLZ Review Board Webhook"
}
]
Delete webhook::
$ python setup_trello_hooks.py delete 58d1c35f4e2c3dc25b9726ab
{
"_value": null
}
'''
from __future__ import print_function
import argparse
import json
import os
import requests
URL = "https://api.trello.com/1/tokens/{trello_token}/webhooks/"
def list_webhooks(url, querystring):
'''
List existing webhooks
Args:
url (str): url for the Trello API
querystring (dict): used to pass Trello API key
Returns:
requests.models.Response: response object which contains a
server's response to an HTTP request.
'''
return requests.request("GET", url, params=querystring)
def create_webhook(url, querystring, trello_backlog_list_id, callback_url):
'''
Delete an existing webhook
Args:
url (str): url for the Trello API
querystring (dict): used to pass Trello API key
trello_backlog_list_id (str): ID for the webhook to be deleted
callback_url (str): URL for the AWS lambda
Returns:
requests.models.Response: response object which contains a
server's response to an HTTP request.
'''
headers = {
'content-type': "application/json",
}
payload = {
"description": "PNDA PLZ Review Board Webhook",
"callbackURL": callback_url,
"idModel": trello_backlog_list_id,
}
return requests.request(
"POST",
url,
data=json.dumps(payload),
headers=headers,
params=querystring)
def delete_webhook(url, querystring, webhook_id):
'''
Delete an existing webhook
Args:
url (str): url for the Trello API
querystring (dict): used to pass Trello API key
webhook_id (str): ID for the webhook to be deleted
Returns:
requests.models.Response: response object which contains a
server's response to an HTTP request.
'''
return requests.request(
"DELETE", url + webhook_id, params=querystring)
def main(argv=None):
'''
Main function to parse arguements and load environment variables
'''
parser = argparse.ArgumentParser(
description='tool for managing Trello webhooks')
parser.add_argument(
'--trello-key',
default=os.environ.get('TRELLO_KEY', None),
help='Trello API Key (Can also be specified as environment variable)')
parser.add_argument(
'--trello-token',
default=os.environ.get('TRELLO_TOKEN', None),
help='Trello API Token (Can also be specified as environment variable)')
subparsers = parser.add_subparsers(
help='available sub-commands', dest='subcommand')
subparsers.add_parser('list', help='list webhooks')
create_parser = subparsers.add_parser('create', help='create new webhook')
create_parser.add_argument('list_id', help='ID for Backlog list in Trello')
create_parser.add_argument(
'callback_url', help='Callback URL for AWS Lambda function')
delete_parser = subparsers.add_parser('delete', help='delete webhook')
delete_parser.add_argument('webhook_id', help='ID of Trello Webhook')
args = parser.parse_args(argv)
if not args.trello_key or not args.trello_token:
exit(parser.print_usage())
querystring = {"key": args.trello_key}
if args.subcommand == 'list':
response = list_webhooks(
URL.format(trello_token=args.trello_token), querystring)
elif args.subcommand == 'create':
response = create_webhook(
URL.format(trello_token=args.trello_token),
querystring,
args.list_id,
args.callback_url)
elif args.subcommand == 'delete':
response = delete_webhook(
URL.format(trello_token=args.trello_token),
querystring,
args.webhook_id)
print(json.dumps(response.json(), indent=4))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment