2018-03-24 13:58:54 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# An easy Trello integration for Zulip.
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import requests
|
|
|
|
|
|
|
|
def get_model_id(options: argparse.Namespace) -> str:
|
|
|
|
"""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
|
|
|
|
)
|
|
|
|
|
|
|
|
if trello_response.status_code is not 200:
|
|
|
|
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']
|
|
|
|
|
|
|
|
|
|
|
|
def get_webhook_id(options: argparse.Namespace, id_model: str) -> str:
|
|
|
|
"""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
|
|
|
|
)
|
|
|
|
|
|
|
|
if trello_response.status_code is not 200:
|
|
|
|
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-04-03 20:22:10 -04:00
|
|
|
def log_webhook_info(options: argparse.Namespace, id_webhook: str) -> None:
|
2018-03-24 13:58:54 -04:00
|
|
|
"""log_webhook_info
|
|
|
|
|
|
|
|
Log webhook info in csv file for possible future use
|
|
|
|
|
|
|
|
:options: argparse.Namespace arguments
|
|
|
|
:id_webhook: str Trello webhook id
|
|
|
|
"""
|
|
|
|
|
|
|
|
with open('zulip_trello_webhooks.csv', 'a') as webhooks_file:
|
|
|
|
webhooks_file.write(
|
2018-04-03 20:22:10 -04:00
|
|
|
'{},{}\n'.format(
|
2018-03-24 13:58:54 -04:00
|
|
|
options.trello_board_name,
|
|
|
|
id_webhook
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2018-04-03 20:22:10 -04:00
|
|
|
def create_webhook(options: argparse.Namespace) -> None:
|
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:
|
|
|
|
print('Success! The webhook id is', id_webhook)
|
|
|
|
|
|
|
|
# The webhook was successfully created,
|
|
|
|
# Log informations for possible future needs
|
|
|
|
print('Logging webhook information')
|
|
|
|
|
2018-04-03 20:22:10 -04:00
|
|
|
log_webhook_info(options, id_webhook)
|
|
|
|
print('Success! The webhook for the {} Trello board was successfully created.'.format(
|
|
|
|
options.trello_board_name))
|
|
|
|
print('\nYou can find the webhooks information in the zulip_trello_webhooks.csv file.')
|
2018-03-24 13:58:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
2018-04-03 20:22:10 -04:00
|
|
|
parser = argparse.ArgumentParser()
|
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()
|