jjb: Add `to_groovy` filter to convert variables to groovy syntax
authorKienan Stewart <kstewart@efficios.com>
Fri, 11 Oct 2024 12:57:06 +0000 (08:57 -0400)
committerKienan Stewart <kstewart@efficios.com>
Fri, 11 Oct 2024 13:03:19 +0000 (09:03 -0400)
To update jobs that use this filter, use recent jjb versions and
execute it as follows:

```
PYTHONPATH=. jenkins-job update ./jobs/linux.yml
```

Change-Id: I09b4b5e3e57f4bfeb76eac00399f3bb885bb1b2b
Signed-off-by: Kienan Stewart <kstewart@efficios.com>
jjb_filters/__init__.py [new file with mode: 0644]

diff --git a/jjb_filters/__init__.py b/jjb_filters/__init__.py
new file mode 100644 (file)
index 0000000..93e9f19
--- /dev/null
@@ -0,0 +1,24 @@
+#!/usr/bin/env python3
+"""
+"""
+import jinja2
+@jinja2.pass_environment
+def do_groovy(env, data, skip_list_wrap=False):
+    list_format="[{}]"
+    if skip_list_wrap:
+        list_format="{}"
+    if isinstance(data, str):
+        return '"{}"'.format(data.replace('"', '\"'))
+    elif isinstance(data, list) or isinstance(data, tuple):
+        _data = [do_groovy(env, d) for d in data]
+        return list_format.format(", ".join(_data))
+    elif isinstance(data, dict):
+        _data = ["{}: {}".format(key, do_groovy(env, value)) for key, value in data.items()]
+        return "[{}]".format(", ".join(_data))
+    elif isinstance(data, bool):
+        return 'true' if data else 'false'
+    else:
+        raise Exception("Unknown data type: '{}'".format(type(data)))
+FILTERS = {
+    "to_groovy": do_groovy,
+}
This page took 0.023449 seconds and 4 git commands to generate.