/
main.php
146 lines (131 loc) · 3.85 KB
/
main.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
<?php
namespace BugsnagExport;
use GuzzleHttp\Psr7\Uri;
require_once __DIR__ . '/vendor/autoload.php';
/**
* @param string[] $row
* @return string
*/
function format_csv_row(array $row) {
$parts = [];
foreach ($row as $cell) {
if (
strpos($cell, "\n") !== false ||
strpos($cell, ',') !== false ||
strpos($cell, '"') !== false ||
($cell === '' && count($row) == 1)
) {
$cell = '"' . str_replace('"', '""', $cell) . '"';
}
$parts[] = $cell;
}
return join(',', $parts);
}
/**
* @param mixed[] $array
* @return string[]
*/
function flatten_array(array $array) {
$res = [];
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach (flatten_array($v) as $k2 => $v2) {
$res["$k.$k2"] = "$v2";
}
} else {
$res[$k] = "$v";
}
}
return $res;
}
/**
* @param array $data
* @return string
*/
function to_csv(array $data) {
$rows = [];
$merged = [];
foreach ($data as $row) {
$row = flatten_array($row);
$rows[] = $row;
$merged = $merged ? array_intersect_key($merged, $row) : $row;
}
$columns = array_keys($merged);
$result = format_csv_row($columns) . "\n";
foreach ($rows as $d) {
$row = [];
foreach ($columns as $col)
$row[] = array_key_exists($col, $d) ? $d[$col] : '';
$result .= format_csv_row($row) . "\n";
}
return $result;
}
class BugsnagAPI {
private $client;
/**
* @param string $token
* @param string $username
* @param string $password
*/
function __construct($token, $username, $password) {
$uri = new Uri('https://api.bugsnag.com/');
$config = [];
if ($token) {
$config['headers']['Authorization'] = "token $token";
} else if ($username && $password) {
$uri = $uri->withUserInfo(rawurlencode($username), rawurlencode($password));
} else {
die('Please specify either --token or --username and --password');
}
$config['base_uri'] = $uri;
$this->client = new \GuzzleHttp\Client($config);
}
/**
* @param string $path
* @param int|null $limit
* @return array
*/
function get($path, $limit) {
$limit = $limit === null ? PHP_INT_MAX : $limit;
$result = [];
$uri = new Uri($path);
while ($uri && count($result) < $limit) {
$response = $this->client->get($uri);
$result = array_merge($result, \PureJSON\JSON::decode($response->getBody()->getContents()));
$uri = null;
foreach ($response->getHeader('link') as $link_) {
foreach (explode(',', $link_) as $link) {
list($l, $r) = explode('; ', $link, 2);
if ($r === 'rel="next"') {
$uri = new Uri(substr($l, 1, -1));
$uri = $uri->withScheme(null)->withHost(null);
break;
}
}
}
}
$result = array_slice($result, 0, $limit);
return $result;
}
}
function main() {
$args = \Docopt::handle(<<<'s'
Usage:
bugsnag-csv-export PATH [options]
Options:
--token=AUTH_TOKEN Authorization token
--username=AUTH_USER Authorization username
--password=AUTH_PASS Authorization password
--save=PATH Save output to the specified file instead of stdout [default: php://output]
--limit=LIMIT Maximum number of rows [default: 1000]
s
);
$client = new BugsnagAPI(
$args['--token'],
$args['--username'],
$args['--password']
);
file_put_contents($args['--save'], to_csv($client->get($args['PATH'], $args['--limit'])));
}
date_default_timezone_set(getenv('TZ') ?: 'UTC');
main();