-
Notifications
You must be signed in to change notification settings - Fork 1
/
Clickup.php
237 lines (199 loc) · 6.33 KB
/
Clickup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/*
* @author Kostadin Bashev | Webcode Ltd.
* @copyright Copyright (c) 2023 Webcode Ltd. (https://webcode.bg/)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
class Clickup
{
const CLICKUP_API_URL = 'https://api.clickup.com/api/v2/';
private object $user;
private array $teams = [];
private array $spaces = [];
private array $tasks = [];
private int $spaceId;
public function __construct()
{
if (isset($_SERVER['HTTP_PRIVATE_TOKEN']) && $response = $this->apiCall('user')) {
if (!empty($response) && isset($response->user->id)) {
$this->user = $response->user;
}
}
if (empty($this->user)) {
header('HTTP/1.0 403 Forbidden');
exit();
}
}
private function apiCall(string $url, array $params = [])
{
$curl = curl_init();
$options = [
CURLOPT_URL => self::CLICKUP_API_URL . $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => !empty($params) ? 'POST' : 'GET',
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: ' . $_SERVER['HTTP_PRIVATE_TOKEN']
],
];
if (!empty($params)) {
$options[CURLOPT_POSTFIELDS] = json_encode($params);
}
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200) {
curl_close($curl);
return json_decode($response);
}
curl_close($curl);
return json_decode($response);
}
/**
* Get All Teams (Companies) on which user is added.
*
* @return array
*/
private function getTeams(): array
{
if (empty($this->teams)) {
$userTeams = [];
$teams = $this->apiCall('team');
if (isset($teams->teams)) {
foreach ($teams->teams as $team) {
if (isset($team->id) && isset($team->members)) {
foreach ($team->members as $member) {
if ($member->user->email == $this->user->email) {
$userTeams[] = $team->id;
}
}
}
}
}
$this->teams = $userTeams;
}
return $this->teams;
}
/**
* Get All Spaces to logged user.
*
* @return array
*/
public function getSpaces(): array
{
if (empty($this->spaces)) {
$userSpaces = [];
foreach ($this->getTeams() as $userTeam) {
$spaces = $this->apiCall(sprintf('team/%s/space?archived=false', $userTeam));
if (isset($spaces->spaces)) {
foreach ($spaces->spaces as $space) {
$space->team_id = $userTeam;
$userSpaces[convertToInt($space->id)] = $space;
}
}
}
$this->spaces = $userSpaces;
}
return $this->spaces;
}
public function setSpaceId(int $spaceId): void
{
$this->spaceId = $spaceId;
}
/**
* @return int
*/
public function getSpaceId(): int
{
return $this->spaceId;
}
public function getCurrentSpace(): ?object
{
if (empty($this->spaces)) {
$this->getSpaces();
}
return $this->spaces[$this->getSpaceId()] ?? null;
}
private function getFolders(): array
{
$spaceFolders = [];
$folders = $this->apiCall(sprintf('space/%s/folder', $this->getSpaceId()));
if (isset($folders->folders)) {
foreach ($folders->folders as $folder) {
$spaceFolders[] = $folder;
}
}
return $spaceFolders;
}
/**
* Get Lists related to user spaces.
*
* @return array
*/
private function getLists(): array
{
$userLists = [];
if ($folders = $this->getFolders()) {
foreach ($folders as $folder) {
foreach ($folder->lists as $list) {
$userLists[] = $list->id;
}
}
}
$lists = $this->apiCall(sprintf('space/%s/list', $this->getSpaceId()));
if (isset($lists->lists)) {
foreach ($lists->lists as $list) {
$userLists[] = $list->id;
}
}
return $userLists;
}
/**
* Get All tasks for user.
*
* @return array
*/
public function getTasks(): array
{
if (empty($this->tasks)) {
$userTasks = [];
$queryParams = http_build_query([
'include_closed' => !isset($_GET['state']) || $_GET['state'] !== 'opened',
'archived' => false,
'subtasks' => true,
'assignees' => [$this->user->id]
]);
foreach ($this->getLists() as $userList) {
$tasks = $this->apiCall(sprintf('list/%s/task?%s', $userList, $queryParams));
if (isset($tasks->tasks)) {
$userTasks = array_merge($userTasks, $tasks->tasks);
}
}
$this->tasks = $userTasks;
}
return $this->tasks;
}
public function addTracking($teamId, $taskId, $duration)
{
$tracking = [
'description' => 'Automatically tracked by PHPStorm',
'tags' => [
[
'name' => 'Development',
'tag_bg' => '#BF55EC',
'tag_fg' => '#BF55EC'
]
],
'start' => round(microtime(true)*1000) - $duration,
'billable' => true,
'duration' => $duration,
'assignee' => $this->user->id,
'tid' => $taskId
];
return $this->apiCall(sprintf('team/%s/time_entries', $teamId), $tracking);
}
}