-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathvalidate.py
More file actions
286 lines (249 loc) · 11.1 KB
/
Copy pathvalidate.py
File metadata and controls
286 lines (249 loc) · 11.1 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
#!/usr/bin/env python3
"""Validate the marketplace: .devin-plugin/plugin.json and the plugins/ we author.
python3 scripts/validate.py # structural checks
python3 scripts/validate.py --fix # also rewrite plugin.json in canonical form
python3 scripts/validate.py --fetch # also confirm every pinned sha exists upstream
"""
from __future__ import annotations
import argparse
import json
import re
import subprocess
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
MANIFEST = ROOT / ".devin-plugin" / "plugin.json"
PLUGINS = ROOT / "plugins"
SHA_RE = re.compile(r"^[0-9a-f]{40}$")
NAME_RE = re.compile(r"^[a-z0-9]+([.-][a-z0-9]+)*$")
URL_RE = re.compile(r"^https://[^\s/]+/[^\s]+\.git$")
LOCAL_RE = re.compile(r"^\./plugins/([a-z0-9]+([.-][a-z0-9]+)*)$")
PLACEHOLDER_RE = re.compile(r"\$\{([^}]*)\}")
CREDENTIAL_NAME_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
RUNTIME_PLACEHOLDERS = {"CLAUDE_PLUGIN_ROOT", "PLUGIN_ROOT"}
LOGO_RE = re.compile(r"^[A-Za-z0-9_-]+(/[A-Za-z0-9_-]+)*\.(svg|png|jpg|jpeg|webp)$")
TOP_LEVEL_KEYS = {"name", "description", "homepage", "repository", "skills", "optionalPlugins"}
UPSTREAM_KEYS = {"source", "url", "path", "sha"}
PLUGIN_KEYS = {
"name",
"displayName",
"description",
"homepage",
"repository",
"logo",
"keywords",
"mcpServers",
}
STDIO_KEYS = {"command", "args", "env"}
HTTP_KEYS = {"url", "headers", "transport", "oauthClientId", "oauthScopes"}
def identity(plugin: object) -> tuple[str, str]:
if isinstance(plugin, str):
return (plugin, "")
return (plugin["url"], plugin.get("path", ""))
def check_upstream(where: str, plugin: dict, errors: list[str]) -> None:
extra = set(plugin) - UPSTREAM_KEYS
if extra:
errors.append(f"{where}: unexpected keys {sorted(extra)}")
kind = plugin.get("source")
if kind not in ("url", "git-subdir"):
errors.append(f"{where}: source must be 'url' or 'git-subdir'")
url = plugin.get("url")
if not isinstance(url, str) or not URL_RE.match(url):
errors.append(f"{where}: url must be an https git URL ending in .git")
sha = plugin.get("sha")
if not isinstance(sha, str) or not SHA_RE.match(sha):
errors.append(f"{where}: sha must be a full 40-character lowercase commit sha")
path = plugin.get("path")
if kind == "git-subdir":
if not isinstance(path, str) or not path or path.startswith("/") or ".." in path.split("/"):
errors.append(f"{where}: git-subdir needs a relative 'path' inside the repository")
elif path is not None:
errors.append(f"{where}: 'path' is only valid for git-subdir sources")
def check_entry(index: int, plugin: object, errors: list[str]) -> None:
where = f"optionalPlugins[{index}]"
if isinstance(plugin, str):
match = LOCAL_RE.match(plugin)
if not match:
errors.append(f"{where}: a local reference must look like './plugins/<slug>'")
elif not (PLUGINS / match.group(1) / ".devin-plugin" / "plugin.json").is_file():
errors.append(f"{where}: {plugin} has no .devin-plugin/plugin.json")
return
if isinstance(plugin, dict):
check_upstream(where, plugin, errors)
return
errors.append(f"{where}: must be a './plugins/<slug>' string or a pinned upstream object")
def placeholder_name(reference: str) -> str:
# `${NAME:-default}` falls back to `default` when NAME has no saved value.
return reference.split(":-", 1)[0]
def check_placeholders(where: str, value: str, errors: list[str]) -> None:
for reference in PLACEHOLDER_RE.findall(value):
name = placeholder_name(reference)
if name in RUNTIME_PLACEHOLDERS:
continue
if not CREDENTIAL_NAME_RE.match(name):
errors.append(f"{where}: placeholder ${{{name}}} is not a credential name the runtime can resolve")
elif name.startswith("MCP_"):
errors.append(f"{where}: placeholder ${{{name}}} uses the retired MCP_ prefix; name the credential ${{{name[4:]}}}")
def check_env_placeholders(where: str, env: dict[str, object], errors: list[str]) -> None:
for key, value in env.items():
if not isinstance(value, str):
continue
for reference in PLACEHOLDER_RE.findall(value):
name = placeholder_name(reference)
if name not in RUNTIME_PLACEHOLDERS and name != key:
errors.append(f"{where}: env {key} references ${{{name}}}; a saved credential is matched by the env key, so it must be ${{{key}}}")
def check_server(where: str, slug: str, config: object, errors: list[str]) -> None:
if not isinstance(config, dict):
errors.append(f"{where}: server '{slug}' must be an object")
return
stdio = "command" in config
allowed = STDIO_KEYS if stdio else HTTP_KEYS
extra = set(config) - allowed - {"description"}
if extra:
errors.append(f"{where}: server '{slug}' has unexpected keys {sorted(extra)}")
if stdio:
args = config.get("args", [])
env = config.get("env", {})
if not isinstance(args, list):
errors.append(f"{where}: server '{slug}' args must be a list")
return
if not isinstance(env, dict):
errors.append(f"{where}: server '{slug}' env must be an object")
return
strings = [config["command"], *args, *env.values()]
check_env_placeholders(f"{where} ({slug})", env, errors)
else:
url = config.get("url")
headers = config.get("headers", {})
if not isinstance(url, str) or not url.startswith("https://"):
errors.append(f"{where}: server '{slug}' needs an https url or a command")
return
if not isinstance(headers, dict):
errors.append(f"{where}: server '{slug}' headers must be an object")
return
strings = [url, *headers.values()]
for value in strings:
if not isinstance(value, str):
errors.append(f"{where}: server '{slug}' has a non-string value")
continue
check_placeholders(f"{where} ({slug})", value, errors)
def check_logo(where: str, slug: str, logo: object, errors: list[str]) -> None:
if not isinstance(logo, str) or not logo:
errors.append(f"{where}: logo must be a string")
return
if logo.startswith("https://"):
return
if not LOGO_RE.match(logo):
errors.append(f"{where}: logo must be an https url or a plain repo-relative file path")
return
if not (PLUGINS / slug / logo).is_file():
errors.append(f"{where}: logo '{logo}' does not exist in the plugin directory")
def check_local_plugin(slug: str, errors: list[str]) -> None:
where = f"plugins/{slug}"
path = PLUGINS / slug / ".devin-plugin" / "plugin.json"
if not path.is_file():
errors.append(f"{where}: missing .devin-plugin/plugin.json")
return
data = json.loads(path.read_text())
extra = set(data) - PLUGIN_KEYS
if extra:
errors.append(f"{where}: unexpected keys {sorted(extra)}")
if data.get("name") != slug:
errors.append(f"{where}: name must match the directory ({slug})")
display_name = data.get("displayName")
if display_name is not None and (
not isinstance(display_name, str)
or display_name != display_name.strip()
or not display_name
or any(ch.isspace() and ch != " " for ch in display_name)
):
errors.append(f"{where}: displayName must be one non-empty line without surrounding whitespace")
logo = data.get("logo")
if logo is not None:
check_logo(where, slug, logo, errors)
servers = data.get("mcpServers")
if not isinstance(servers, dict) or len(servers) != 1 or slug not in servers:
errors.append(f"{where}: mcpServers must declare exactly one server named '{slug}'")
return
check_server(where, slug, servers[slug], errors)
def canonical(data: dict) -> str:
ordered: list[object] = []
for plugin in sorted(data["optionalPlugins"], key=identity):
if isinstance(plugin, str):
ordered.append(plugin)
continue
entry = {"source": plugin["source"], "url": plugin["url"]}
if "path" in plugin:
entry["path"] = plugin["path"]
entry["sha"] = plugin["sha"]
ordered.append(entry)
out = {key: data[key] for key in data if key != "optionalPlugins"}
out["optionalPlugins"] = ordered
return json.dumps(out, indent=2) + "\n"
def fetch_ok(plugin: dict) -> bool:
with tempfile.TemporaryDirectory() as tmp:
cmds = (
["git", "init", "-q"],
["git", "fetch", "-q", "--depth", "1", plugin["url"], plugin["sha"]],
)
return all(
subprocess.run(cmd, cwd=tmp, capture_output=True).returncode == 0 for cmd in cmds
)
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--fix", action="store_true")
parser.add_argument("--fetch", action="store_true")
args = parser.parse_args()
text = MANIFEST.read_text()
data = json.loads(text)
errors: list[str] = []
if not isinstance(data, dict):
print("plugin.json: top level must be an object")
return 1
extra = set(data) - TOP_LEVEL_KEYS
if extra:
errors.append(f"unexpected top-level keys {sorted(extra)}")
if "requiredPlugins" in data:
errors.append("marketplace plugins are optional; use optionalPlugins")
if not isinstance(data.get("name"), str) or not NAME_RE.match(data["name"]):
errors.append("name must be lowercase alphanumeric with '-' or '.' separators")
plugins = data.get("optionalPlugins")
if not isinstance(plugins, list):
errors.append("optionalPlugins must be a list")
plugins = []
for i, plugin in enumerate(plugins):
check_entry(i, plugin, errors)
if errors:
print("\n".join(errors))
return 1
seen: dict[tuple[str, str], int] = {}
for i, plugin in enumerate(plugins):
key = identity(plugin)
if key in seen:
errors.append(f"optionalPlugins[{i}] duplicates optionalPlugins[{seen[key]}]: {key}")
seen[key] = i
listed = {identity(p)[0] for p in plugins if isinstance(p, str)}
authored = sorted(d.name for d in PLUGINS.iterdir() if d.is_dir()) if PLUGINS.is_dir() else []
for slug in authored:
if f"./plugins/{slug}" not in listed:
errors.append(f"plugins/{slug}: not listed in optionalPlugins")
check_local_plugin(slug, errors)
want = canonical(data)
if text != want:
if args.fix:
MANIFEST.write_text(want)
print("rewrote .devin-plugin/plugin.json")
else:
errors.append("plugin.json is not canonical; run scripts/validate.py --fix")
if args.fetch:
for plugin in plugins:
if isinstance(plugin, dict) and not fetch_ok(plugin):
errors.append(f"{plugin['url']}: sha {plugin['sha']} not found upstream")
if errors:
print("\n".join(errors))
return 1
print(f"ok: {len(plugins)} entries ({len(authored)} authored, {len(plugins) - len(authored)} upstream)")
return 0
if __name__ == "__main__":
sys.exit(main())