Skip to content

Commit 3e848b1

Browse files
committed
integrations: Add ClickUp integration script.
Add a python script to help integrate Zulip with Clickup. Urlopen is used instead of the usual requests library inorder to make the script standalone. Fixes zulip#26529
1 parent e9d8ef3 commit 3e848b1

File tree

4 files changed

+506
-0
lines changed

4 files changed

+506
-0
lines changed

zulip/integrations/clickup/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# A script that automates setting up a webhook with ClickUp
2+
3+
Usage :
4+
5+
1. Make sure you have all of the relevant ClickUp credentials before
6+
executing the script:
7+
- The ClickUp Team ID
8+
- The ClickUp Client ID
9+
- The ClickUp Client Secret
10+
11+
2. Execute the script :
12+
13+
$ python zulip_clickup.py --clickup-team-id <clickup_team_id> \
14+
--clickup-client-id <clickup_board_name> \
15+
--clickup-client-secret <clickup_board_id> \
16+
17+
For more information, please see Zulip's documentation on how to set up
18+
a ClickUp integration [here](https://zulip.com/integrations/doc/clickup).

zulip/integrations/clickup/__init__.py

Whitespace-only changes.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import io
2+
from functools import wraps
3+
from typing import Any, Callable, Dict, List, Optional, Union
4+
from unittest import TestCase
5+
from unittest.mock import DEFAULT, patch
6+
7+
from integrations.clickup import zulip_clickup
8+
9+
MOCK_WEBHOOK_URL = (
10+
"https://YourZulipApp.com/api/v1/external/clickup?api_key=TJ9DnIiNqt51bpfyPll5n2uT4iYxMBW9"
11+
)
12+
13+
MOCK_AUTH_CODE = "332KKA3321NNAK3MADS"
14+
MOCK_AUTH_CODE_URL = f"https://YourZulipApp.com/?code={MOCK_AUTH_CODE}"
15+
MOCK_API_KEY = "X" * 32
16+
17+
SCRIPT_PATH = "integrations.clickup.zulip_clickup"
18+
19+
MOCK_CREATED_WEBHOOK_ID = "13-13-13-13-1313-13"
20+
MOCK_DELETE_WEBHOOK_ID = "12-12-12-12-12"
21+
MOCK_GET_WEBHOOK_IDS = {"endpoint": MOCK_WEBHOOK_URL, "id": MOCK_DELETE_WEBHOOK_ID}
22+
23+
CLICKUP_TEAM_ID = "teamid123"
24+
CLICKUP_CLIENT_ID = "clientid321"
25+
CLICKUP_CLIENT_SECRET = "clientsecret322" # noqa: S105
26+
27+
28+
def make_clickup_request_side_effect(
29+
path: str, query: Dict[str, Union[str, List[str]]], method: str
30+
) -> Optional[Dict[str, Any]]:
31+
api_data_mapper: Dict[str, Dict[str, Dict[str, Any]]] = { # path -> method -> response
32+
"oauth/token": {
33+
"POST": {"access_token": MOCK_API_KEY},
34+
}, # used for get_access_token()
35+
f"team/{CLICKUP_TEAM_ID}/webhook": {
36+
"POST": {"id": MOCK_CREATED_WEBHOOK_ID},
37+
"GET": {"webhooks": [MOCK_GET_WEBHOOK_IDS]},
38+
}, # used for create_webhook(), get_webhooks()
39+
f"webhook/{MOCK_DELETE_WEBHOOK_ID}": {"DELETE": {}}, # used for delete_webhook()
40+
}
41+
return api_data_mapper.get(path, {}).get(method, DEFAULT)
42+
43+
44+
def mock_script_args() -> Callable[[Any], Callable[..., Any]]:
45+
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
46+
@wraps(func)
47+
def wrapper(*args: Any, **kwargs: Any) -> Any:
48+
mock_user_inputs = [
49+
MOCK_WEBHOOK_URL, # input for 1st step
50+
MOCK_AUTH_CODE_URL, # input for 3rd step
51+
"1,2,3,4,5", # third input for 4th step
52+
]
53+
with patch(
54+
"sys.argv",
55+
[
56+
"zulip_clickup.py",
57+
"--clickup-team-id",
58+
CLICKUP_TEAM_ID,
59+
"--clickup-client-id",
60+
CLICKUP_CLIENT_ID,
61+
"--clickup-client-secret",
62+
CLICKUP_CLIENT_SECRET,
63+
"--zulip-webhook-url",
64+
MOCK_WEBHOOK_URL,
65+
],
66+
), patch("os.system"), patch("time.sleep"), patch("sys.exit"), patch(
67+
"builtins.input", side_effect=mock_user_inputs
68+
), patch(
69+
SCRIPT_PATH + ".ClickUpAPIHandler.make_clickup_request",
70+
side_effect=make_clickup_request_side_effect,
71+
):
72+
result = func(*args, **kwargs)
73+
74+
return result
75+
76+
return wrapper
77+
78+
return decorator
79+
80+
81+
class ZulipClickUpScriptTest(TestCase):
82+
@mock_script_args()
83+
def test_valid_arguments(self) -> None:
84+
with patch(SCRIPT_PATH + ".run") as mock_run, patch(
85+
"sys.stdout", new=io.StringIO()
86+
) as mock_stdout:
87+
zulip_clickup.main()
88+
self.assertRegex(mock_stdout.getvalue(), r"Running Zulip Clickup Integration...")
89+
mock_run.assert_called_once_with(
90+
"clientid321", "clientsecret322", "teamid123", MOCK_WEBHOOK_URL
91+
)
92+
93+
def test_missing_arguments(self) -> None:
94+
with self.assertRaises(SystemExit) as cm:
95+
with patch("sys.stderr", new=io.StringIO()) as mock_stderr:
96+
zulip_clickup.main()
97+
self.assertEqual(cm.exception.code, 2)
98+
self.assertRegex(
99+
mock_stderr.getvalue(),
100+
r"""the following arguments are required: --clickup-team-id, --clickup-client-id, --clickup-client-secret, --zulip-webhook-url\n""",
101+
)
102+
103+
@mock_script_args()
104+
def test_step_two(self) -> None:
105+
with patch("webbrowser.open") as mock_open, patch(
106+
"sys.stdout", new=io.StringIO()
107+
) as mock_stdout:
108+
zulip_clickup.main()
109+
redirect_uri = "https://YourZulipApp.com"
110+
mock_open.assert_called_once_with(
111+
f"https://app.clickup.com/api?client_id=clientid321&redirect_uri={redirect_uri}"
112+
)
113+
expected_output = r"STEP 1[\s\S]*ClickUp authorization page will open in your browser\.[\s\S]*Please authorize your workspace\(s\)\.[\s\S]*Click 'Connect Workspace' on the page to proceed\.\.\."
114+
self.assertRegex(
115+
mock_stdout.getvalue(),
116+
expected_output,
117+
)
118+
119+
@mock_script_args()
120+
def test_step_three(self) -> None:
121+
with patch("webbrowser.open"), patch("sys.stdout", new=io.StringIO()) as mock_stdout:
122+
zulip_clickup.main()
123+
self.assertRegex(
124+
mock_stdout.getvalue(),
125+
(
126+
r"STEP 2[\s\S]*After you've authorized your workspace,\s*you should be redirected to your home URL.\s*Please copy your home URL and paste it below.\s*It should contain a code, and look similar to this:\s*e.g. https://YourZulipDomain\.com/\?code=332KKA3321NNAK3MADS"
127+
),
128+
)
129+
130+
@mock_script_args()
131+
def test_step_four(self) -> None:
132+
with patch("webbrowser.open"), patch("sys.stdout", new=io.StringIO()) as mock_stdout:
133+
zulip_clickup.main()
134+
self.assertRegex(
135+
mock_stdout.getvalue(),
136+
(
137+
r"STEP 3[\s\S]*Please select which ClickUp event notification\(s\) you'd[\s\S]*like to receive in your Zulip app\.[\s\S]*EVENT CODES:[\s\S]*1 = task[\s\S]*2 = list[\s\S]*3 = folder[\s\S]*4 = space[\s\S]*5 = goals[\s\S]*Here's an example input if you intend to only receive notifications[\s\S]*related to task, list and folder: 1,2,3"
138+
),
139+
)
140+
141+
@mock_script_args()
142+
def test_final_step(self) -> None:
143+
with patch("webbrowser.open"), patch("sys.stdout", new=io.StringIO()) as mock_stdout:
144+
zulip_clickup.main()
145+
self.assertRegex(
146+
mock_stdout.getvalue(),
147+
(
148+
r"SUCCESS: Completed integrating your Zulip app with ClickUp!\s*webhook_id: \d+-\d+-\d+-\d+-\d+-\d+\s*You may delete this script or run it again to reconfigure\s*your integration\."
149+
),
150+
)

0 commit comments

Comments
 (0)