-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdocs_to_postman.py
More file actions
332 lines (309 loc) · 11.8 KB
/
Copy pathdocs_to_postman.py
File metadata and controls
332 lines (309 loc) · 11.8 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python
# python docs_to_postman.py | newman run /dev/stdin
from __future__ import print_function
import re
import os
import sys
import json
import copy
import email.message
import urllib.parse as urlparse
PARSER_RE = re.compile(r'\n```\s*([a-z]*)(.*?)\n```|\n(#+)\s*([^\n]+)|\n([a-z]+)[ \t]*:[ \t]*([^\n]+)', re.DOTALL)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# The migrated docs site: the Astro content is the source of truth. Its pages carry
# the E2E requests (```rest / ```http / ```json) and assertions (in <!-- e2e:begin -->
# comments), and its sidebar encodes the reading order.
CONTENT_DIR = os.path.join(BASE_DIR, 'docs', 'src', 'content', 'docs')
SIDEBAR_PATH = os.path.join(BASE_DIR, 'docs', 'src', 'lib', 'sidebar.mjs')
ASSETS_DIR = os.path.join(BASE_DIR, 'docs', 'public', 'assets')
def content_order():
"""Absolute md paths in sidebar reading order (depth-first).
The doc-driven E2E examples build on each other across pages (index a document,
then get it, update it, delete it; the /bank/ queries assume a dataset state), so
they must run in the order the docs are meant to be read. That order is the
committed Starlight sidebar (src/lib/sidebar.mjs), which replaced the old Jekyll
docs.yaml when the site migrated to Astro. Paths are lower-cased for matching so
the case-insensitively-slugged test pages (tests/dataTypes/...) line up on
case-sensitive filesystems too. Returns [] if the sidebar is absent (caller then
falls back to a stable alphabetical order)."""
try:
txt = open(SIDEBAR_PATH).read()
except OSError:
return []
m = re.search(r'export\s+const\s+sidebar\s*=\s*(\[[\s\S]*\])\s*;', txt)
if not m:
return []
try:
data = json.loads(m.group(1))
except ValueError:
return []
order = []
def walk(items):
for it in items:
slug = it.get('slug')
link = it.get('link')
if slug is not None:
order.append(slug)
elif link:
order.append(link.lstrip('/'))
if it.get('items'):
walk(it['items'])
walk(data)
return [os.path.abspath(os.path.join(CONTENT_DIR, s + '.md')).lower() for s in order]
def parse_filename(filename, index, all_tests):
filename_path, _ = os.path.splitext(filename)
data = open(filename).read()
fnp = filename_path
file_context = {
'filename': filename,
'titles': [],
}
context = {}
context.update(file_context)
while fnp and fnp != BASE_DIR and fnp != '/':
if fnp in index:
file_context['titles'].insert(0, (0, index.get(fnp)))
fnp = os.path.dirname(fnp)
# print(filename_path, base_titles)
def process(m):
groups = m.groups()
# print(groups)
if groups[0] in ('json', 'rest', 'http'):
# Flush:
if context and 'request' in context:
all_tests.append(copy.deepcopy(context))
context.clear()
context.update(file_context)
context['request'] = groups[1].strip()
elif groups[0] == 'js':
context.setdefault('tests', []).append(groups[1].strip())
elif groups[2]:
# Flush:
if context and 'request' in context:
all_tests.append(copy.deepcopy(context))
context.clear()
context.update(file_context)
# Add title:
level = len(groups[2])
file_context['titles'] = [title for title in file_context.get('titles', []) if title[0] < level]
file_context['titles'].append((level, groups[3]))
# Clear description:
file_context.pop('description', None)
context.pop('description', None)
elif groups[5]:
name = groups[4]
if name == 'description':
# Persist description:
file_context[name] = groups[5]
context[name] = groups[5]
elif name == 'title':
# Flush:
if context and 'request' in context:
all_tests.append(copy.deepcopy(context))
context.clear()
context.update(file_context)
# Add title:
index[filename_path] = groups[5]
file_context['titles'].append((1, groups[5]))
else:
context[name] = groups[5]
PARSER_RE.sub(process, data)
# Flush:
if context and 'request' in context:
all_tests.append(copy.deepcopy(context))
def parse_directory(directory, index, all_tests):
# Collect every .md under the tree, then process in a DETERMINISTIC order: the
# sidebar reading order first (so dependent examples run in the intended
# sequence), then anything not in the sidebar, alphabetically. This replaces the
# platform-dependent os.walk order (see content_order()).
found = []
for path, dirs, files in os.walk(directory):
for f in files:
if f.endswith('.md'):
found.append(os.path.abspath(os.path.join(path, f)))
rank = {p: i for i, p in enumerate(content_order())}
big = len(rank)
# Match case-insensitively: the sidebar slugs are lower-cased, the test pages sit
# at camelCase paths on disk (tests/dataTypes/...).
found.sort(key=lambda p: (rank.get(p.lower(), big), p))
for filename in found:
parse_filename(filename, index, all_tests)
def main():
index = {}
all_tests = []
if len(sys.argv) > 1:
for arg in sys.argv:
if os.path.abspath(arg) != os.path.abspath(__file__):
if os.path.isdir(arg):
parse_directory(arg, index, all_tests)
else:
parse_filename(arg, index, all_tests)
else:
parse_directory(CONTENT_DIR, index, all_tests)
# print(json.dumps(all_tests, indent=4))
collection = {
"info": {
"name": "Xapiand",
"description": "Xapiand is A Modern Highly Available Distributed RESTful Search and Storage Engine built for the Cloud and with Data Locality in mind.",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"variable": [
{
"key": "domain",
"value": "localhost:8880",
"type": "string"
}
],
"item": [],
}
for test in all_tests:
items = collection["item"]
title = []
description = test.get('description')
titles = test['titles']
if not description:
if titles:
description = titles[-1][1]
titles = titles[:-1]
for _, name in titles:
for item in items:
if item.get("name") == name and 'item' in item:
break
else:
item = {
"name": name,
"item": []
}
items.append(item)
title.append(item["name"])
items = item["item"]
url = ""
method = None
body = []
headers = email.message.Message()
for i, line in enumerate(test['request'].split('\n')):
line = line.strip()
if i == 0:
method, _, url = line.partition(' ')
if not re.match(r'[A-Z]+', method):
method = None
break
elif not body:
if not line:
body.append(line)
else:
k, _, v = line.partition(':')
if not _:
raise ValueError("Malformed header in {} ({}): {}".format(test['filename'], ' / '.join(title), repr(line)))
headers.add_header(k.strip(), v.strip())
else:
body.append(line)
if not method:
continue
for k, v in {
"Content-Type": "application/json"
}.items():
if k not in headers:
headers[k] = v
header = []
for k, v in headers.items():
header.append({
"type": "text",
"key": k,
"value": v,
})
parsed = urlparse.urlparse(url)
path = parsed.path[1:] if parsed.path.startswith('/') else parsed.path
path = path.split('/')
qs = urlparse.parse_qs(parsed.query + '&' + test.get('params', ''))
for k, v in {
"commit": None,
"volatile": None,
"echo": None,
}.items():
if k not in qs:
qs[k] = v
query = []
for k, v in qs.items():
if v is None:
# A valueless flag (commit / volatile / echo): emit as a bare param.
query.append({
"key": k,
"value": None,
})
else:
# urlparse.parse_qs returns each value as a list; Postman expects a
# string per query entry, so emit one entry per value. (Previously the
# raw list was passed through, which newman serialised as a bare
# valueless param -- e.g. `sort=foo` became `?sort`, so every
# sort/params-bearing request was sent malformed and 400'd.)
for item in (v if isinstance(v, list) else [v]):
query.append({
"key": k,
"value": item,
})
url = {
"host": [
"{{domain}}"
],
"path": path,
}
if query:
url["query"] = query
body = '\n'.join(body).strip()
request = {
"method": method,
"url": url,
}
if header:
request["header"] = header
if body:
if body[0] == '@':
request["body"] = {
"mode": "file",
"file": {
"src": os.path.join(ASSETS_DIR, body[1:])
}
}
else:
request["body"] = {
"mode": "raw",
"raw": body,
}
item = {
"request": request,
}
if description:
item["name"] = description
items.append(item)
tests = test.get('tests')
if not tests:
# No explicit assertions in this doc block: synthesize a default so
# every request is verified at least at the status level (this is what
# makes the assertion-only e2e gate a complete regression net without a
# response-body baseline). A `status: NNN` marker -- harness metadata
# inside a {% comment %} block, invisible in the rendered docs -- pins a
# specific expected status for the deliberate error/edge demos
# (unimplemented CLOSE/OPEN -> 501, placeholder paths -> 404,
# invalid-input examples -> 400); everything else asserts 2xx success.
status = test.get('status')
if status:
tests = ['pm.test("Status is %d", function () {\n pm.response.to.have.status(%d);\n});' % (int(status), int(status))]
else:
tests = ['pm.test("Response is success", function () {\n pm.response.to.be.success;\n});']
if tests:
scripts = []
item["event"] = [{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": scripts,
}
}]
for script in tests:
scripts.append(script)
# print(test)
json.dump(collection, sys.stdout, indent=2)
return 0
if __name__ == '__main__':
sys.exit(main())