2020-04-02 09:59:28 -04:00
|
|
|
#!/usr/bin/env python3
|
2018-03-24 13:58:54 -04:00
|
|
|
#
|
|
|
|
# An easy Trello integration for Zulip.
|
|
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import argparse
|
2018-09-22 15:25:39 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
import requests
|
|
|
|
except ImportError:
|
|
|
|
print("Error: Please install the python requests library before running this script:")
|
|
|
|
print("http://docs.python-requests.org/en/master/user/install/")
|
|
|
|
sys.exit(1)
|
2018-03-24 13:58:54 -04:00
|
|
|
|
2018-09-14 16:31:13 -04:00
|
|
|
def get_model_id(options):
|
2018-03-24 13:58:54 -04:00
|
|
|
"""get_model_id
|
|
|
|
|
|
|
|
Get Model Id from Trello API
|
|
|
|
|
|
|
|
:options: argparse.Namespace arguments
|
|
|
|
|
|
|
|
:returns: str id_model Trello board idModel
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2018-04-03 20:22:10 -04:00
|
|
|
trello_api_url = 'https://api.trello.com/1/board/{}'.format(
|
2018-03-24 13:58:54 -04:00
|
|
|
options.trello_board_id
|
|
|
|
)
|
|
|
|
|
|
|
|
params = {
|
2018-09-14 15:54:40 -04:00
|
|
|
'key': options.trello_api_key,
|
|
|
|
'token': options.trello_token,
|
2018-03-24 13:58:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
trello_response = requests.get(
|
|
|
|
trello_api_url,
|
|
|
|
params=params
|
|
|
|
)
|
|
|
|
|
2020-04-09 20:14:01 -04:00
|
|
|
if trello_response.status_code != 200:
|
2018-03-24 13:58:54 -04:00
|
|
|
print('Error: Can\'t get the idModel. Please check the configuration')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
board_info_json = trello_response.json()
|
|
|
|
|
|
|
|
return board_info_json['id']
|
|
|
|
|
|
|
|
|
2018-09-14 16:31:13 -04:00
|
|
|
def get_webhook_id(options, id_model):
|
2018-03-24 13:58:54 -04:00
|
|
|
"""get_webhook_id
|
|
|
|
|
|
|
|
Get webhook id from Trello API
|
|
|
|
|
|
|
|
:options: argparse.Namespace arguments
|
|
|
|
:id_model: str Trello board idModel
|
|
|
|
|
|
|
|
:returns: str id_webhook Trello webhook id
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
trello_api_url = 'https://api.trello.com/1/webhooks/'
|
|
|
|
|
|
|
|
data = {
|
2018-09-14 15:54:40 -04:00
|
|
|
'key': options.trello_api_key,
|
|
|
|
'token': options.trello_token,
|
2018-04-03 20:22:10 -04:00
|
|
|
'description': 'Webhook for Zulip integration (From Trello {} to Zulip)'.format(
|
2018-03-24 13:58:54 -04:00
|
|
|
options.trello_board_name,
|
|
|
|
),
|
2018-09-14 15:54:40 -04:00
|
|
|
'callbackURL': options.zulip_webhook_url,
|
2018-03-24 13:58:54 -04:00
|
|
|
'idModel': id_model
|
|
|
|
}
|
|
|
|
|
|
|
|
trello_response = requests.post(
|
|
|
|
trello_api_url,
|
|
|
|
data=data
|
|
|
|
)
|
|
|
|
|
2020-04-09 20:14:01 -04:00
|
|
|
if trello_response.status_code != 200:
|
2018-03-24 13:58:54 -04:00
|
|
|
print('Error: Can\'t create the Webhook:', trello_response.text)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
webhook_info_json = trello_response.json()
|
|
|
|
|
|
|
|
return webhook_info_json['id']
|
|
|
|
|
2018-09-14 16:31:13 -04:00
|
|
|
def create_webhook(options):
|
2018-03-24 13:58:54 -04:00
|
|
|
"""create_webhook
|
|
|
|
|
|
|
|
Create Trello webhook
|
|
|
|
|
|
|
|
:options: argparse.Namespace arguments
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# first, we need to get the idModel
|
2018-04-03 20:22:10 -04:00
|
|
|
print('Getting Trello idModel for the {} board...'.format(options.trello_board_name))
|
2018-03-24 13:58:54 -04:00
|
|
|
|
|
|
|
id_model = get_model_id(options)
|
|
|
|
|
|
|
|
if id_model:
|
|
|
|
print('Success! The idModel is', id_model)
|
|
|
|
|
|
|
|
id_webhook = get_webhook_id(options, id_model)
|
|
|
|
|
|
|
|
if id_webhook:
|
2018-09-14 16:18:54 -04:00
|
|
|
print('Success! The webhook ID is', id_webhook)
|
2018-03-24 13:58:54 -04:00
|
|
|
|
2018-04-03 20:22:10 -04:00
|
|
|
print('Success! The webhook for the {} Trello board was successfully created.'.format(
|
|
|
|
options.trello_board_name))
|
2018-03-24 13:58:54 -04:00
|
|
|
|
2018-09-14 16:31:13 -04:00
|
|
|
def main():
|
|
|
|
description = """
|
|
|
|
zulip_trello.py is a handy little script that allows Zulip users to
|
|
|
|
quickly set up a Trello webhook.
|
|
|
|
|
|
|
|
Note: The Trello webhook instructions available on your Zulip server
|
|
|
|
may be outdated. Please make sure you follow the updated instructions
|
2020-06-08 17:03:27 -04:00
|
|
|
at <https://zulip.com/integrations/doc/trello>.
|
2018-09-14 16:31:13 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description=description)
|
2018-09-14 15:54:40 -04:00
|
|
|
parser.add_argument('--trello-board-name',
|
|
|
|
required=True,
|
|
|
|
help='The Trello board name.')
|
|
|
|
parser.add_argument('--trello-board-id',
|
|
|
|
required=True,
|
|
|
|
help=('The Trello board short ID. Can usually be found '
|
|
|
|
'in the URL of the Trello board.'))
|
|
|
|
parser.add_argument('--trello-api-key',
|
|
|
|
required=True,
|
|
|
|
help=('Visit https://trello.com/1/appkey/generate to generate '
|
|
|
|
'an APPLICATION_KEY (need to be logged into Trello).'))
|
|
|
|
parser.add_argument('--trello-token',
|
|
|
|
required=True,
|
|
|
|
help=('Visit https://trello.com/1/appkey/generate and under '
|
|
|
|
'`Developer API Keys`, click on `Token` and generate '
|
|
|
|
'a Trello access token.'))
|
|
|
|
parser.add_argument('--zulip-webhook-url',
|
|
|
|
required=True,
|
|
|
|
help='The webhook URL that Trello will query.')
|
2018-03-24 13:58:54 -04:00
|
|
|
|
2018-04-03 20:22:10 -04:00
|
|
|
options = parser.parse_args()
|
|
|
|
create_webhook(options)
|
2018-03-24 13:58:54 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|