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 argparse
|
2021-05-28 05:00:04 -04:00
|
|
|
import sys
|
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
|
|
|
|
2021-05-28 05:03:46 -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
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
trello_api_url = "https://api.trello.com/1/board/{}".format(options.trello_board_id)
|
2018-03-24 13:58:54 -04:00
|
|
|
|
|
|
|
params = {
|
2021-05-28 05:05:11 -04:00
|
|
|
"key": options.trello_api_key,
|
|
|
|
"token": options.trello_token,
|
2018-03-24 13:58:54 -04:00
|
|
|
}
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
trello_response = requests.get(trello_api_url, params=params)
|
2018-03-24 13:58:54 -04:00
|
|
|
|
2020-04-09 20:14:01 -04:00
|
|
|
if trello_response.status_code != 200:
|
2021-05-28 05:05:11 -04:00
|
|
|
print("Error: Can't get the idModel. Please check the configuration")
|
2018-03-24 13:58:54 -04:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
board_info_json = trello_response.json()
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
return board_info_json["id"]
|
2018-03-24 13:58:54 -04:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
trello_api_url = "https://api.trello.com/1/webhooks/"
|
2018-03-24 13:58:54 -04:00
|
|
|
|
|
|
|
data = {
|
2021-05-28 05:05:11 -04:00
|
|
|
"key": options.trello_api_key,
|
|
|
|
"token": options.trello_token,
|
|
|
|
"description": "Webhook for Zulip integration (From Trello {} to Zulip)".format(
|
2018-03-24 13:58:54 -04:00
|
|
|
options.trello_board_name,
|
|
|
|
),
|
2021-05-28 05:05:11 -04:00
|
|
|
"callbackURL": options.zulip_webhook_url,
|
|
|
|
"idModel": id_model,
|
2018-03-24 13:58:54 -04:00
|
|
|
}
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
trello_response = requests.post(trello_api_url, data=data)
|
2018-03-24 13:58:54 -04:00
|
|
|
|
2020-04-09 20:14:01 -04:00
|
|
|
if trello_response.status_code != 200:
|
2021-05-28 05:05:11 -04:00
|
|
|
print("Error: Can't create the Webhook:", trello_response.text)
|
2018-03-24 13:58:54 -04:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
webhook_info_json = trello_response.json()
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
return webhook_info_json["id"]
|
2018-03-24 13:58:54 -04:00
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
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
|
2021-05-28 05:05:11 -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:
|
2021-05-28 05:05:11 -04:00
|
|
|
print("Success! The idModel is", id_model)
|
2018-03-24 13:58:54 -04:00
|
|
|
|
|
|
|
id_webhook = get_webhook_id(options, id_model)
|
|
|
|
|
|
|
|
if id_webhook:
|
2021-05-28 05:05:11 -04:00
|
|
|
print("Success! The webhook ID is", id_webhook)
|
2018-03-24 13:58:54 -04:00
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
print(
|
2021-05-28 05:05:11 -04:00
|
|
|
"Success! The webhook for the {} Trello board was successfully created.".format(
|
2021-05-28 05:03:46 -04:00
|
|
|
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)
|
2021-05-28 05:05:11 -04:00
|
|
|
parser.add_argument("--trello-board-name", required=True, help="The Trello board name.")
|
2021-05-28 05:03:46 -04:00
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--trello-board-id",
|
2021-05-28 05:03:46 -04:00
|
|
|
required=True,
|
2021-05-28 05:05:11 -04:00
|
|
|
help=("The Trello board short ID. Can usually be found " "in the URL of the Trello board."),
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--trello-api-key",
|
2021-05-28 05:03:46 -04:00
|
|
|
required=True,
|
|
|
|
help=(
|
2021-05-28 05:05:11 -04:00
|
|
|
"Visit https://trello.com/1/appkey/generate to generate "
|
|
|
|
"an APPLICATION_KEY (need to be logged into Trello)."
|
2021-05-28 05:03:46 -04:00
|
|
|
),
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--trello-token",
|
2021-05-28 05:03:46 -04:00
|
|
|
required=True,
|
|
|
|
help=(
|
2021-05-28 05:05:11 -04:00
|
|
|
"Visit https://trello.com/1/appkey/generate and under "
|
|
|
|
"`Developer API Keys`, click on `Token` and generate "
|
|
|
|
"a Trello access token."
|
2021-05-28 05:03:46 -04:00
|
|
|
),
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--zulip-webhook-url", required=True, help="The webhook URL that Trello will query."
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
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
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
if __name__ == "__main__":
|
2018-03-24 13:58:54 -04:00
|
|
|
main()
|