forked from mikkelson/clubhouse-csv-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.php
245 lines (218 loc) · 9.07 KB
/
app.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
238
239
240
241
242
243
244
245
<?php
/**
* @link https://github.com/mikkelson/clubhouse-csv-import
* @author James Mikkelson
*/
ini_set('error_reporting', 0);
if (!empty($_FILES['csv']['name']) && substr($_FILES['csv']['name'], -4) == '.csv') {
if (!$_FILES['csv']['error']) {
$skipped = 0;
$count = 0;
$failed = 0;
$members = array();
$workflow_states = array();
$csv_lines = unpackCsv($_FILES['csv']['tmp_name']);
//loop through csv lines and add to clubhouse as stories
$total = count($csv_lines);
$apiLimit = 200;
$apiCount = 0;
// If user columns, get member list to translate to UUID's
if (isset($csv_lines[0]) && array_key_exists('owners', $csv_lines[0])) {
$return = reqClubhouse('members', $_POST['token'], null);
foreach ($return as $member) {
$members[$member->profile->email_address] = $member->id;
}
}
// If workflow state column, get list of workflow states to translate to ID's
if (isset($csv_lines[0]) && array_key_exists('state', $csv_lines[0])) {
$return = reqClubhouse('workflows', $_POST['token'], null);
foreach ($return as $workflow) {
foreach ($workflow->project_ids as $project_id) {
foreach ($workflow->states as $state) {
$workflow_states[$project_id][$state->name] = $state->id;
}
}
}
}
foreach ($csv_lines as $line) {
$count++;
$apiCount++;
if ($apiCount == $apiLimit) {
//sleep for a 60 seconds, avoid Clubhouse API limit
sleep(60);
$apiCount = 0;
}
//project_id, name and story_type are required by the Clubhouse API
if (empty($line['project_id']) || empty($line['name']) || empty($line['story_type'])) {
$error_lines[] = "Line " . $count . " is missing required field <i>project_id</i>, <i>name</i> or <i>story_type</i>";
$skipped++;
$failed++;
continue;
}
// Required columns
$payload = array("project_id" => $line['project_id'],
"name" => $line['name'],
"story_type" => $line['story_type']
);
// Optional columns
// addIfNotEmpty('milestone_id', $line, $payload);
addIfNotEmpty('description', $line, $payload);
addIfNotEmpty('estimate', $line, $payload);
addIfNotEmpty('epic_id', $line, $payload);
addIfNotEmpty('external_id', $line, $payload);
addIfNotEmpty('requested_by_id', $line, $payload);
addIfNotEmptyAsArray('owner_ids', $line, ' |;|,|\n', $payload);
addIfNotEmptyAsHash('labels', $line, ';|,|\n', 'name', $payload);
addIfNotEmptyAsArray('external_links', $line, ' |;|,|\n', $payload);
addIfNotEmpty('external_id', $line, $payload);
addIfNotEmpty('workflow_state_id', $line, $payload);
addIfNotEmptyAsTasks('tasks', $line, $payload);
addIfNotEmptyAsMemberArray('owners', 'owner_ids', $line, $members, $payload);
addIfNotEmptyAsMember('requester', 'requested_by_id', $line, $members, $payload);
addIfNotEmptyAsWorkflowState('state', 'workflow_state_id', $line, $workflow_states, $payload);
if (isset($payload['owner_ids']) && isset($payload['tasks'])) {
foreach ($payload['tasks'] as &$task)
$task['owner_ids'] = $payload['owner_ids'];
}
$data = json_encode($payload);
//make Clubhouse POST request
$result = reqClubhouse('stories', $_POST['token'], $data);
if (!empty($result->created_at)) {
@$counts[$line['story_type']]++;
} elseif (!empty($result->message)) {
$error_lines[] = "Line " . $count . ": <em>" . $line['name'] . "</em> failed: " . $result->message . "";
$failed++;
} else {
$error_lines[] = "Line " . $count . ": <em>" . $line['name'] . "</em> failed: Unexpected error";
$failed++;
}
}
} else {
echo 'CSV upload failed: ' . $_FILES['csv']['error'];
}
}
/**
*
* If $src['key'] value is not empty, add a $key with the value to $dest
*
*/
function addIfNotEmpty($key, $src, &$dest) {
if (isNotEmptyString($src[$key])) $dest[$key] = $src[$key];
}
/**
*
* If $src['key'] value is not empty, explode to array (w/ $delim as delimeter),
* and add a $key with the array as its value to $dest.
*
*/
function addIfNotEmptyAsArray($key, $src, $delim, &$dest)
{
if (isNotEmptyString($src[$key]))
$dest[$key] = preg_split('/ *(' . $delim . ') */', $src[$key]);
}
function addIfNotEmptyAsMemberArray($key_from, $key_to, $src, $members, &$dest)
{
if (isNotEmptyString($src[$key_from]) && !isNotEmptyString($src[$key_to])) {
$member_split = preg_split('/[,; ] */', $src[$key_from]);
for ($i = 0; $i < count($member_split); $i++)
$member_split[$i] = $members[$member_split[$i]];
$dest[$key_to] = array_filter($member_split);
}
}
function addIfNotEmptyAsMember($key_from, $key_to, $src, $members, &$dest)
{
if (isNotEmptyString($src[$key_from]) && !isNotEmptyString($src[$key_to]))
$dest[$key_to] = $members[$src[$key_from]];
}
/**
*
* If $src['key'] value is not empty, explode to array (w/ $delim as delimeter),
* and add a $key with a hash (associative array) of the values as its value to
* $dest, using $secondkey as the internal key
*
*/
function addIfNotEmptyAsHash($key, $src, $delim, $secondkey, &$dest)
{
if (isNotEmptyString($src[$key])) {
$hash = array();
$values = preg_split('/ *(' . $delim . ') */', $src[$key]);
foreach ($values as $item) {
$hash[] = array($secondkey => $item);
}
$dest[$key] = $hash;
}
}
function addIfNotEmptyAsTasks($key, $src, &$dest)
{
if (isNotEmptyString($src[$key])) {
// Export starts with "[ ] " or "[X] ", delimited by ";[ ] " or ";[X] "
// Other options:
// Start/delmited with "*" or "-" maybe surrounded by spaces/line breaks
// Delimited by numbers, maybe surrounded by spaces/line breaks
// Delimited by ";" maybe surrounded by spaces/line breaks
if (preg_match('/^\[[ X]\]/', $src[$key])) {
$task_list_string = preg_replace('/^\[[ X]\] */', '', $src[$key]);
$task_list = preg_split('/;\[[ X]\] */', $task_list_string);
} else if (preg_match('/^ *[*-]]/', $src[$key])) {
$task_list_string = preg_replace('/^ *[*-] */', '', $src[$key]);
$task_list = preg_split('/[,;\n ]*[*-] */', $task_list_string);
} else if (preg_match('/^\d+[\. :-]]/', $src[$key])) {
$task_list_string = preg_replace('/^\d+[\.:-] */', '', $src[$key]);
$task_list = preg_split('/[,;\n ]*\d+[\.:-] */', $task_list_string);
} else {
$task_list = preg_split('/;[\n ]*/', $src[$key]);
}
if (count($task_list) > 0) $dest[$key] = array_map(fn($task) => array('description' => $task), $task_list);
}
}
function addIfNotEmptyAsWorkflowState($key_from, $key_to, $src, $workflow_states, &$dest)
{
if (isNotEmptyString($src[$key_from]) && !isNotEmptyString($src[$key_to]))
$dest[$key_to] = $workflow_states[$src['project_id']][$src[$key_from]];
}
function isNotEmptyString($str){
return (isset($str) && (strlen(trim($str)) > 0));
}
function unpackCsv($csv) {
$row = 1;
$csv_lines = array();
if (($handle = fopen($csv, "r")) !== false) {
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$num = count($data);
for ($c = 0; $c < $num; $c++) {
if ($row == 1) {
//first row, map columns to Clubhouse API fields
$api_field_mapping[] = $data[$c];
} else {
@$csv_lines[$row - 2][$api_field_mapping[$c]] = $data[$c];
}
}
$row++;
}
fclose($handle);
}
return $csv_lines;
}
function reqClubhouse($service, $token, $data)
{
$endpoint_url = 'https://api.clubhouse.io/api/v3/' . $service;
$ch = curl_init($endpoint_url);
if ($data) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Clubhouse-Token: ' . $token,
'Content-Length: ' . strlen($data))
);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}