@webship.co/workspace-plane 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,596 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # Plane for the Webship Workspace: build a site of Plane (the open-source project and work management software,
4
+ # Community Edition) from its all-in-one image, run Plane's own commands in its container, add its default accounts and
5
+ # their memberships of a sample workspace and project, add its sample content, and wire its step pack into the testing
6
+ # setup.
7
+ #
8
+ # Placed by `plugins.sh --install plane` in core/scripts/plugins/plane/functions/, where bootstrap sources it after the
9
+ # core's own functions. Everything runs in containers, never on the host:
10
+ # - DDEV (the default): Plane's all-in-one image (its proxy, web app, admin, space, API, live server and workers) is a
11
+ # service of a generic project, on the project's own PostgreSQL (the db service), with Valkey, RabbitMQ and an S3
12
+ # store (MinIO) as services beside it, at https://<name>.ddev.site; the project's web container keeps the mail
13
+ # catcher. The stack is heavy: the builder asks for 4 GB of free memory before it starts.
14
+ # Docker Compose, Podman, Kubernetes, Lando and Docksal are not proven yet: asked for one, the builder says so and
15
+ # changes nothing.
16
+ # The site's accounts and content go through Django's own shell in Plane's container (plane-site.py), its input never
17
+ # on a command line.
18
+ #
19
+ # The container tool has no project type for Plane, so a site of this plugin is recognised by a file on DDEV
20
+ # (.ddev/config.plane.yaml, written by the builder).
21
+ # Four names are the ones the core's tools look for:
22
+ # set_plane_users the default accounts (cmd-tools-add-users.sh <project> plane)
23
+ # add_plane_users make them, and their memberships (the same tool, and --add-users of the builder)
24
+ # cancel_plane_users keep them from logging in (cmd-tools-cancel-users.sh <project> plane)
25
+ # testing_configure_plane the step pack in the testing stack (cmd-tools-testing.sh <project>)
26
+
27
+ # The step pack a site is tested with, and the release line it is required at.
28
+ readonly PLANE_STEPS_PACKAGE='@webship-js/plane-steps';
29
+ readonly PLANE_STEPS_VERSION='^2.0.0';
30
+
31
+ # The release the builder runs when none is asked for (a tag of the all-in-one image).
32
+ readonly PLANE_DEFAULT_RELEASE='v1.5.1';
33
+
34
+ # The sample workspace (its slug) and project (its identifier and name) the default accounts are members of, and the
35
+ # folder of a site with the plugin's scripts and the site's own values (its container reads them at /opt/webship).
36
+ readonly PLANE_WORKSPACE='webship';
37
+ readonly PLANE_PROJECT='WEB';
38
+ readonly PLANE_PROJECT_NAME='Webship site';
39
+ readonly PLANE_FILES='plane';
40
+
41
+ # The free memory a build asks for, in MB: the all-in-one image runs a dozen processes, beside PostgreSQL, Valkey,
42
+ # RabbitMQ and the store.
43
+ readonly PLANE_MEMORY_MB=4096;
44
+
45
+ # How many logins Plane allows from one address on a site the builder makes (--login-limit, or site_plane_login_limit
46
+ # of the folder's settings): a dev and test setting. Plane's own default is 10/minute; a test suite logs in more often
47
+ # than a person. A Plane the dashboard is connected to with --connect keeps its own: nothing here touches it.
48
+ readonly PLANE_LOGIN_LIMIT='100/minute';
49
+
50
+ # The default accounts, the ones a scenario logs in as: an Admin of the sample workspace and project (owner), a Member
51
+ # (member), a Guest (reader), and an account with no membership (stranger), whom the project is refused. Plane logs in
52
+ # by e-mail address. Accounts are configuration, made once on the site; a test suite never creates or deletes one. The
53
+ # workspace account the builder makes with --install is the instance's administrator and is not in this list.
54
+ function set_plane_users() {
55
+ users=(owner member reader stranger);
56
+ local user ;
57
+ for user in "${users[@]}" ; do
58
+ printf -v "user_${user}_name" '%s' "${user}" ;
59
+ printf -v "user_${user}_mail" '%s' "${user}@example.com" ;
60
+ printf -v "user_${user}_password" '%s' 'dD.123123ddd' ;
61
+ printf -v "user_${user}_fullname" '%s' "${user^} Test" ;
62
+ done
63
+ user_owner_role='Admin' ;
64
+ user_member_role='Member' ;
65
+ user_reader_role='Guest' ;
66
+ user_stranger_role='' ;
67
+ }
68
+
69
+ # The runtime of the project in this folder, or of a new build (--runtime, else the workspace's setting, else DDEV).
70
+ # A core without the runtime layer has DDEV only.
71
+ function plane_runtime() {
72
+ if declare -f runtime_of > /dev/null ; then runtime_of "$@" ; else echo 'ddev' ; fi
73
+ }
74
+
75
+ # The address of the site in this folder (or of a new build named PROJECT_NAME, on DDEV).
76
+ function plane_url() {
77
+ echo "https://${1:-${PROJECT_NAME}}.ddev.site" ;
78
+ }
79
+
80
+ # Is this folder a Plane site of this plugin? On DDEV the builder leaves .ddev/config.plane.yaml in it.
81
+ function plane_is_project() {
82
+ local project_path="$1" ;
83
+ [ -f "${project_path}/.ddev/config.plane.yaml" ] && return 0 ;
84
+ if declare -f rt_is_platform > /dev/null ; then rt_is_platform "${project_path}" plane ; return $? ; fi
85
+ return 1 ;
86
+ }
87
+
88
+ # Go to the project a tool was asked about, or say why not.
89
+ function plane_enter_project() {
90
+ local project_path="${WORKSPACE_ROOT}/${doc_name}/${PROJECT_NAME}";
91
+ if [ ! -d "${project_path}" ] ; then
92
+ echo "No project ${PROJECT_NAME} in ${WORKSPACE_ROOT}/${doc_name}." ;
93
+ return 1 ;
94
+ fi
95
+ if ! plane_is_project "${project_path}" ; then
96
+ echo "${doc_name}/${PROJECT_NAME} is not a Plane site of this plugin." ;
97
+ return 1 ;
98
+ fi
99
+ plane_refresh_files "${project_path}" || return 1 ;
100
+ cd "${project_path}" || return 1 ;
101
+ }
102
+
103
+ # The site's copy of the plugin's scripts (plane/), brought up to the installed plugin's, so a plugin updated with
104
+ # --install reaches the sites built before it. The site's own values (plane/*.env) are left as they are.
105
+ function plane_refresh_files() {
106
+ local project_path="$1" from="${WORKSPACE_SCRIPTS}/plugins/plane" f ;
107
+ [ -d "${project_path}/${PLANE_FILES}" ] || return 0 ;
108
+ for f in plane-site.py plane-start.sh ; do
109
+ if [ -f "${from}/${f}" ] && ! cmp -s "${from}/${f}" "${project_path}/${PLANE_FILES}/${f}" ; then
110
+ cp "${from}/${f}" "${project_path}/${PLANE_FILES}/${f}" || return 1 ;
111
+ fi
112
+ done
113
+ }
114
+
115
+ # A value of the site's own values (plane/env), never printed by the tools: PLANE_RELEASE.
116
+ function plane_setting() {
117
+ sed -n "s/^$1=\\(.*\\)\$/\\1/p" "${2:-.}/${PLANE_FILES}/env" 2>/dev/null | head -n 1 ;
118
+ }
119
+
120
+ # A value made for this site only: letters and digits.
121
+ function plane_secret() {
122
+ head -c 200 /dev/urandom | base64 -w0 | tr -dc 'A-Za-z0-9' | head -c "${1:-64}" ;
123
+ }
124
+
125
+ # The free memory of this machine, in MB (what the kernel says is available).
126
+ function plane_free_memory() {
127
+ awk '/^MemAvailable:/ { print int($2 / 1024) }' /proc/meminfo 2>/dev/null ;
128
+ }
129
+
130
+ # Write the project's files: Plane's services (.ddev/docker-compose.plane.yaml), its image in .ddev/.env, the claim
131
+ # (.ddev/config.plane.yaml) and the plugin's scripts.
132
+ function plane_write_config() {
133
+ local project_path="$1" release="$2" ;
134
+ local from="${WORKSPACE_SCRIPTS}/plugins/plane" f ;
135
+ for f in docker-compose.plane.yaml plane-site.py plane-start.sh ; do
136
+ if [ ! -f "${from}/${f}" ] ; then
137
+ echo "${f} is not in ${from}: install the plugin (plugins.sh --install plane)." ;
138
+ return 1 ;
139
+ fi
140
+ done
141
+ mkdir -p "${project_path}/${PLANE_FILES}" || return 1 ;
142
+ cp "${from}/plane-site.py" "${from}/plane-start.sh" "${project_path}/${PLANE_FILES}/" || return 1 ;
143
+ cp "${from}/docker-compose.plane.yaml" "${project_path}/.ddev/docker-compose.plane.yaml" || return 1 ;
144
+ printf 'PLANE_IMAGE=%s\n' "docker.io/makeplane/plane-aio-community:${release}" > "${project_path}/.ddev/.env" || return 1 ;
145
+ cat > "${project_path}/.ddev/config.plane.yaml" <<YAMLEOF
146
+ # Plane in this project: its services (docker-compose.plane.yaml), its image in .env, the site's own values in
147
+ # ../plane/*.env. Written by the Plane builder of the workspace; build again rather than editing.
148
+ web_environment:
149
+ - PLANE_RELEASE=${release}
150
+ YAMLEOF
151
+ }
152
+
153
+ # The site's own values (plane/env, plane/rabbitmq.env, plane/minio.env, which only you can read): the address, the
154
+ # project's database, the queue, the cache and the store with keys of this site only, and its secret keys.
155
+ function plane_write_env() {
156
+ local project_path="$1" release="$2" base_url="$3" login_limit="${4:-${PLANE_LOGIN_LIMIT}}" ;
157
+ local host="${base_url#*://}" mq store_user store_key ;
158
+ host="${host%%/*}" ;
159
+ mq="$(plane_secret 32)" ; store_user="plane$(plane_secret 12)" ; store_key="$(plane_secret 40)" ;
160
+ ( umask 077 && cat > "${project_path}/${PLANE_FILES}/env" <<ENVEOF
161
+ # The values of this Plane site, made by the Plane builder of the workspace: its secret keys and the keys of its queue
162
+ # and its store are of this site only. Read by Plane's container and by the plugin's scripts; never printed.
163
+ PLANE_RELEASE=${release}
164
+ DOMAIN_NAME=${host}
165
+ APP_DOMAIN=${host}
166
+ APP_PROTOCOL=https
167
+ SITE_ADDRESS=:80
168
+ WEB_URL=${base_url}
169
+ CORS_ALLOWED_ORIGINS=${base_url}
170
+ DATABASE_URL=postgresql://db:db@db:5432/db
171
+ REDIS_URL=redis://valkey:6379/
172
+ AMQP_URL=amqp://plane:${mq}@rabbitmq:5672/plane
173
+ AWS_REGION=us-east-1
174
+ AWS_ACCESS_KEY_ID=${store_user}
175
+ AWS_SECRET_ACCESS_KEY=${store_key}
176
+ AWS_S3_BUCKET_NAME=uploads
177
+ AWS_S3_ENDPOINT_URL=http://minio:9000
178
+ SECRET_KEY=$(plane_secret 64)
179
+ LIVE_SERVER_SECRET_KEY=$(plane_secret 64)
180
+ GUNICORN_WORKERS=1
181
+ # The logins Plane allows from one address (--login-limit): a dev and test setting, as a test suite logs in more often
182
+ # than a person; Plane's own default is 10/minute. plane-start.sh puts it into the image's own settings file.
183
+ AUTHENTICATION_RATE_LIMIT=${login_limit}
184
+ ENVEOF
185
+ cat > "${project_path}/${PLANE_FILES}/rabbitmq.env" <<ENVEOF
186
+ RABBITMQ_DEFAULT_USER=plane
187
+ RABBITMQ_DEFAULT_PASS=${mq}
188
+ RABBITMQ_DEFAULT_VHOST=plane
189
+ ENVEOF
190
+ cat > "${project_path}/${PLANE_FILES}/minio.env" <<ENVEOF
191
+ MINIO_ROOT_USER=${store_user}
192
+ MINIO_ROOT_PASSWORD=${store_key}
193
+ ENVEOF
194
+ ) ;
195
+ }
196
+
197
+ # Run a command line in Plane's container, in its backend folder (/app/backend), with the site's values; what is piped
198
+ # in reaches it.
199
+ function plane_exec() {
200
+ ddev exec -s plane "sh -c $(printf '%q' "cd /app/backend && ${1}")" ;
201
+ }
202
+
203
+ # The answer of Plane's instance endpoint (200 once its API serves and reaches its database), asked inside the
204
+ # project's network, from the web container.
205
+ function plane_probe() {
206
+ ddev exec "curl -s -o /dev/null -m 10 -w '%{http_code}' http://plane:80/api/instances/" < /dev/null 2>/dev/null | tr -d '\r' | tail -1 ;
207
+ }
208
+
209
+ # Whether the site's instance answers that it is set up (a visitor gets the login form, not the setup's "Get started"),
210
+ # asked inside the project's network, from the web container.
211
+ function plane_setup_done() {
212
+ ddev exec "curl -s -m 10 http://plane:80/api/instances/" < /dev/null 2>/dev/null | tr -d '\r' | grep -q '"is_setup_done": *true' ;
213
+ }
214
+
215
+ # Wait until the site answers, in the project's folder: up to fifteen minutes, as the first start migrates the database.
216
+ function plane_wait() {
217
+ local attempt code="" ;
218
+ for attempt in $(seq 1 300) ; do
219
+ code="$(plane_probe)" ;
220
+ [ "${code}" == '200' ] && return 0 ;
221
+ sleep 3 ;
222
+ done
223
+ echo "Plane did not answer (its instance endpoint: ${code:-none}); the end of its log:" ;
224
+ ddev logs -s plane --tail 40 < /dev/null 2>/dev/null ;
225
+ return 1 ;
226
+ }
227
+
228
+ # Run verbs of plane-site.py (defaults, accounts, content, tidy, list, token, version) with Django's own shell in
229
+ # Plane's container. A payload (accounts, passwords) travels on the input, never on a command line.
230
+ function plane_site() {
231
+ plane_exec "PLANE_VERBS='$*' python manage.py shell -c \"exec(open('/opt/webship/plane-site.py').read())\"" ;
232
+ }
233
+
234
+ # The release the site runs, as Plane says it.
235
+ function plane_version() {
236
+ ( cd "${WORKSPACE_ROOT}/${doc_name}/${PROJECT_NAME}" 2>/dev/null && plane_site version < /dev/null 2>/dev/null ) | tr -d '\r' | sed -n 's/^VERSION //p' | tail -n 1 ;
237
+ }
238
+
239
+ # Rows of accounts for plane-site.py, as the base64 of its JSON list: "login|mail|password|fullname|admin|role|action"
240
+ # lines in PLANE_ROWS, which travel to Python in the environment, never on a command line.
241
+ function plane_accounts_payload() {
242
+ PLANE_ROWS="$1" python3 -c '
243
+ import base64, json, os
244
+ rows = []
245
+ for line in os.environ["PLANE_ROWS"].split("\x1e"):
246
+ if not line:
247
+ continue
248
+ login, mail, password, fullname, admin, role, action = line.split("\x1f")
249
+ rows.append({"login": login, "mail": mail, "password": password, "fullname": fullname, "admin": admin == "yes",
250
+ "role": role, "action": action})
251
+ print(base64.b64encode(json.dumps(rows).encode()).decode())' ;
252
+ }
253
+
254
+ # Build a Plane site from nothing: the project and Plane, its instance set up; with --install the workspace account as
255
+ # the instance's administrator; with --add-users the default accounts, members of the sample workspace and project; with
256
+ # --sample-content the project's work items, its cycle and a comment the step pack is tested on.
257
+ function plane_build() {
258
+ trim_project_name ;
259
+ local release="${PLANE_VERSION:-${site_plane_release:-${PLANE_DEFAULT_RELEASE}}}" ;
260
+ [[ "${release}" =~ ^[0-9] ]] && release="v${release}" ;
261
+ local login_limit="${LOGIN_LIMIT:-${site_plane_login_limit:-${PLANE_LOGIN_LIMIT}}}" ;
262
+ local project_path="${WORKSPACE_ROOT}/${doc_name}/${PROJECT_NAME}" ;
263
+ local runtime ; runtime="$(cd "${WORKSPACE_ROOT}/${doc_name}" && plane_runtime)" ;
264
+
265
+ if [ "${runtime}" != 'ddev' ] ; then
266
+ echo "Plane builds on DDEV for now (--runtime ddev), not on ${runtime}: that runtime is not proven for it yet. Nothing was changed." ;
267
+ exit 1 ;
268
+ fi
269
+ if ! [[ "${release}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] ; then
270
+ echo "--release takes a release of Plane (v1.5.1), not ${release}. Nothing was changed." ;
271
+ exit 1 ;
272
+ fi
273
+ if ! [[ "${login_limit}" =~ ^[1-9][0-9]{0,5}/(second|minute|hour|day)$ ]] ; then
274
+ echo "--login-limit takes a number of logins and a period (100/minute, 10/minute, 500/hour), not ${login_limit}. Nothing was changed." ;
275
+ exit 1 ;
276
+ fi
277
+ if [ "${ADD_USERS}" == 'yes' ] && [ "${INSTALL}" != 'yes' ] ; then
278
+ echo "--add-users needs --install: the default accounts go into the site --install sets up." ;
279
+ exit 1 ;
280
+ fi
281
+ if [ "${SAMPLE_CONTENT}" == 'yes' ] && [ "${ADD_USERS}" != 'yes' ] ; then
282
+ echo "--sample-content needs --add-users: the sample project's members are the default accounts." ;
283
+ exit 1 ;
284
+ fi
285
+ if [ ${#PROJECT_NAME} -gt 40 ] || ! [[ "${PROJECT_NAME}" =~ ^[a-z0-9][a-z0-9-]*$ ]] ; then
286
+ echo "The project name ${PROJECT_NAME} must be small letters, digits and dashes (its address and its containers take it). Nothing was changed." ;
287
+ exit 1 ;
288
+ fi
289
+ if [ "${INSTALL}" == 'yes' ] && [ ${#account_pass} -lt 8 ] ; then
290
+ echo "The workspace account's password (core/config/settings.yml) has fewer than eight characters, which Plane asks of a password: --install makes it an account of the site. Nothing was changed." ;
291
+ exit 1 ;
292
+ fi
293
+ local free ; free="$(plane_free_memory)" ;
294
+ if [ -n "${free}" ] && [ "${free}" -lt "${PLANE_MEMORY_MB}" ] ; then
295
+ echo "Plane needs about ${PLANE_MEMORY_MB} MB of free memory (its all-in-one image, PostgreSQL, Valkey, RabbitMQ and its store); this machine has ${free} MB available now. Stop a site you do not need, then build again. Nothing was changed." ;
296
+ exit 1 ;
297
+ fi
298
+
299
+ cd ${WORKSPACE_ROOT}/${doc_name} || exit 1 ;
300
+
301
+ # A project of the same name goes first, with its containers and its volumes (Plane's database, queue and store).
302
+ drop_database ;
303
+ rm -rf "${PROJECT_NAME:?}" ;
304
+ mkdir -p "${PROJECT_NAME}" ;
305
+ cd "${project_path}" || exit 1 ;
306
+
307
+ # A generic project on PostgreSQL 15, the release Plane's own stack runs.
308
+ ddev config --project-type=generic --project-name=${PROJECT_NAME} --database=postgres:15 \
309
+ --nodejs-version=$(ddev_nodejs_version) < /dev/null || exit 1 ;
310
+ plane_write_config "${project_path}" "${release}" || exit 1 ;
311
+ local base_url ; base_url="$(plane_url)" ;
312
+ plane_write_env "${project_path}" "${release}" "${base_url}" "${login_limit}" || exit 1 ;
313
+
314
+ echo "Starting the project and Plane ${release}: its images are about 800 MB the first time, and the first start migrates the database." ;
315
+ ddev_up start -y < /dev/null || exit 1 ;
316
+ plane_wait || exit 1 ;
317
+
318
+ # The instance set up: no sign-up screen, sign-ups closed, e-mail and password logins, no telemetry.
319
+ plane_site defaults < /dev/null || { echo "Plane's instance could not be set up." ; exit 1 ; } ;
320
+ # What a visitor meets: the login form. Plane keeps its answer for two hours, so a stale one would show the setup.
321
+ plane_setup_done || { echo "Plane still answers that its instance is not set up: a visitor would get its setup (Get started), not the login form." ; exit 1 ; } ;
322
+
323
+ if [ "${INSTALL}" == 'yes' ] ; then
324
+ echo "Making the workspace account the instance's administrator." ;
325
+ local rows ;
326
+ rows="${account_name}"$'\x1f'"${account_mail:-${account_name}@example.com}"$'\x1f'"${account_pass}"$'\x1f'"${account_name^} Workspace"$'\x1f''yes'$'\x1f'$'\x1f'$'\x1e' ;
327
+ plane_accounts_payload "${rows}" | plane_site accounts || { echo "The workspace account could not be made." ; exit 1 ; } ;
328
+ if [ "${ADD_USERS}" == 'yes' ] ; then
329
+ set_plane_users ;
330
+ add_plane_users || exit 1 ;
331
+ if [ "${SAMPLE_CONTENT}" == 'yes' ] ; then
332
+ echo "Adding the sample content: the work items of ${PLANE_PROJECT_NAME}, its cycle Sprint 1 and a comment." ;
333
+ ( cd "${project_path}" && plane_site content < /dev/null ) || exit 1 ;
334
+ fi
335
+ users=() ;
336
+ fi
337
+ echo "${doc_name} ${PROJECT_NAME} has been installed: Plane $(plane_version).";
338
+ echo "The site: ${base_url}" ;
339
+ echo "The workspace account logs in as ${account_mail:-${account_name}@example.com}, the instance's administrator (${base_url}/god-mode/)." ;
340
+ else
341
+ echo "${doc_name} ${PROJECT_NAME} is ready: Plane $(plane_version) at ${base_url}.";
342
+ echo "Build it again with --install (and --add-users) for the workspace account and the default accounts.";
343
+ fi
344
+
345
+ if [ "${LAUNCH}" == 'yes' ] ; then
346
+ ddev launch ;
347
+ fi
348
+
349
+ cd ${WORKSPACE_ROOT}/${doc_name};
350
+ }
351
+
352
+ # Make the default accounts, or give them back their names, passwords and memberships: each a member of the sample
353
+ # workspace and project in its role (Admin, Member, Guest), the stranger of none. The list is loaded by the caller
354
+ # (set_plane_users): the core's add_users, the builder, the tool.
355
+ function add_plane_users() {
356
+ plane_enter_project || return 1 ;
357
+ local user user_name user_mail user_password user_fullname user_role rows="" ;
358
+ for user in "${users[@]}" ; do
359
+ user_name="user_${user}_name"; user_mail="user_${user}_mail";
360
+ user_password="user_${user}_password"; user_fullname="user_${user}_fullname"; user_role="user_${user}_role";
361
+
362
+ echo " ---------------------------------------------------------------- ";
363
+ echo " User name: ${!user_name}";
364
+ echo " User mail: ${!user_mail}";
365
+ echo " User password: ${!user_password}";
366
+ echo " User role: ${!user_role:-no membership} of ${PLANE_PROJECT_NAME}";
367
+ echo " ================================================================= ";
368
+ rows="${rows}${!user_name}"$'\x1f'"${!user_mail}"$'\x1f'"${!user_password}"$'\x1f'"${!user_fullname}"$'\x1f''no'$'\x1f'"${!user_role}"$'\x1f'$'\x1e' ;
369
+ done
370
+ plane_accounts_payload "${rows}" | plane_site accounts ;
371
+ local status=$? ;
372
+ cd ${WORKSPACE_ROOT}/${doc_name};
373
+ return ${status} ;
374
+ }
375
+
376
+ # Keep the default accounts from logging in (their work stays), never the workspace account.
377
+ function cancel_plane_users() {
378
+ plane_enter_project || return 1 ;
379
+ local user user_name user_mail rows="" ;
380
+ for user in "${users[@]}" ; do
381
+ user_name="user_${user}_name"; user_mail="user_${user}_mail";
382
+ echo " ---------------------------------------------------------------- ";
383
+ echo " User name: ${!user_name}";
384
+ echo " User mail: ${!user_mail}";
385
+ echo " ================================================================= ";
386
+ rows="${rows}${!user_name}"$'\x1f'"${!user_mail}"$'\x1f'$'\x1f'$'\x1f''no'$'\x1f'$'\x1f''lock'$'\x1e' ;
387
+ done
388
+ plane_accounts_payload "${rows}" | plane_site accounts ;
389
+ local status=$? ;
390
+ cd ${WORKSPACE_ROOT}/${doc_name};
391
+ return ${status} ;
392
+ }
393
+
394
+ # Point the dashboard's Plane (core/config/plugin.plane.settings.yml) at this site, as an account of it: a new API key
395
+ # of that account in the sample workspace (made through Django's shell in Plane's container) goes into the settings
396
+ # file, which only you can read, never on a screen or in a log; with it the site's address, and the sample project as
397
+ # the default one when none was set.
398
+ function plane_connect() {
399
+ local login="$1" settings="${WORKSPACE_CONFIG}/plugin.plane.settings.yml" out token url ;
400
+ if ! [[ "${login}" =~ ^[A-Za-z0-9._@-]+$ ]] ; then echo "--connect takes the login or e-mail address of an account of the site." ; return 1 ; fi
401
+ if [ ! -f "${settings}" ] ; then echo "No ${settings}: install the plugin first (plugins.sh --install plane)." ; return 1 ; fi
402
+ out="$(printf '%s\n' "${login}" | plane_site token 2>/dev/null)" ;
403
+ token="$(printf '%s\n' "${out}" | tr -d '\r' | sed -n 's/^TOKEN //p' | tail -n 1)" ;
404
+ out='' ;
405
+ if ! [[ "${token}" =~ ^[A-Za-z0-9_-]{20,}$ ]] ; then echo "No API key could be made for ${login} (is it an active account and a member of the workspace ${PLANE_WORKSPACE}?)." ; return 1 ; fi
406
+ url="$(plane_url)" ;
407
+ PLANE_NEW_TOKEN="${token}" python3 - "${settings}" "${url}" "${PLANE_WORKSPACE}/${PLANE_PROJECT}" <<'PYEOF' || return 1
408
+ import os, re, sys
409
+ path, url, scope = sys.argv[1:4]
410
+ token = os.environ['PLANE_NEW_TOKEN']
411
+ text = open(path).read()
412
+ def put(text, key, value):
413
+ line = '%s: %s' % (key, value)
414
+ if re.search(r'^%s:.*$' % key, text, re.M):
415
+ return re.sub(r'^%s:.*$' % key, lambda m: line, text, count=1, flags=re.M)
416
+ return text.rstrip('\n') + '\n' + line + '\n'
417
+ text = put(text, 'url', url)
418
+ text = put(text, 'token', token)
419
+ if not re.search(r'^default_scope:[ \t]*[^ \t\n"]', text, re.M) and not re.search(r'^default_scope:[ \t]*"[^"]', text, re.M):
420
+ text = put(text, 'default_scope', scope)
421
+ fd = os.open(path + '.new', os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
422
+ with os.fdopen(fd, 'w') as f:
423
+ f.write(text)
424
+ os.replace(path + '.new', path)
425
+ PYEOF
426
+ token='' ;
427
+ echo "The dashboard reads the work items of ${url} as ${login} now: a new API key of that account is in ${settings} (not shown here), and ${PLANE_WORKSPACE}/${PLANE_PROJECT} is the default project when none was set." ;
428
+ }
429
+
430
+ # What cmd-tools-plane-cli.sh does: the first of --run, --add-users, --cancel-users, --sample-content, --tidy,
431
+ # --connect that was asked for; with none of them, what the site is and its accounts.
432
+ function plane_cli_tool() {
433
+ trim_project_name ;
434
+ plane_enter_project || exit 1 ;
435
+
436
+ ddev describe -j 2>/dev/null | grep -q '"status":"running"' || ddev start -y < /dev/null || exit 1 ;
437
+ plane_wait > /dev/null || { echo "Plane of ${PROJECT_NAME} does not answer; start it again (ddev restart in ${doc_name}/${PROJECT_NAME})." ; exit 1 ; } ;
438
+
439
+ if [ ! "${RUN}" == '_none_' ] ; then
440
+ # Plane's own commands (python manage.py <command>), in its container.
441
+ plane_exec "python manage.py ${RUN}" < /dev/null ;
442
+ exit $? ;
443
+ fi
444
+
445
+ set_plane_users ;
446
+ if [ "${ADD_USERS}" == 'yes' ] ; then
447
+ add_plane_users ;
448
+ elif [ "${CANCEL_USERS}" == 'yes' ] ; then
449
+ cancel_plane_users ;
450
+ elif [ "${SAMPLE_CONTENT}" == 'yes' ] ; then
451
+ plane_site content list < /dev/null ;
452
+ elif [ "${TIDY}" == 'yes' ] ; then
453
+ # What a test run that was stopped half way left: its work items ("Webship test …"), with their comments.
454
+ plane_site tidy list < /dev/null ;
455
+ elif [ "${CONNECT}" != '_none_' ] ; then
456
+ plane_connect "${CONNECT}" ;
457
+ else
458
+ echo "${doc_name}/${PROJECT_NAME}: Plane $(plane_version)" ;
459
+ echo " the site: $(plane_url)" ;
460
+ echo "" ;
461
+ plane_site list < /dev/null ;
462
+ fi
463
+ cd ${WORKSPACE_ROOT}/${doc_name};
464
+ }
465
+
466
+ # The step pack in the testing stack of a Plane site. The core's testing_configure calls this in the project
467
+ # folder once webship-js is scaffolded and before the dependencies are installed; for a project that is not one of
468
+ # this plugin's it changes nothing.
469
+ #
470
+ # It adds the pack to package.json, lists its step definitions in cucumber.js after webship-js's own, gives cucumber.js
471
+ # the tags that keep the pack's assertions that must fail out of a normal run, the account that prepares and removes
472
+ # test content (manager), the project the steps work in when they name none, and the users registry of the default
473
+ # accounts (a handle a scenario reads, mapped to an account; never the workspace account), and writes a first feature
474
+ # when the project has none of its own. Every part is skipped when it is already there, so a second run adds what the
475
+ # first missed and never resets a suite.
476
+ function testing_configure_plane() {
477
+ local project_path="$1";
478
+ plane_is_project "${project_path}" || return 0 ;
479
+ if [ ! -f "${project_path}/cucumber.js" ] || [ ! -f "${project_path}/package.json" ] ; then
480
+ echo " Plane: cucumber.js is not here yet — run the testing setup again once webship-js is scaffolded." ;
481
+ return 1 ;
482
+ fi
483
+
484
+ echo "Adding the Plane step pack (${PLANE_STEPS_PACKAGE} ${PLANE_STEPS_VERSION}).";
485
+ set_plane_users ;
486
+ # Plane logs in with the e-mail address, and shows the display name (the login).
487
+ local registry="" user user_name user_mail user_password ;
488
+ for user in "${users[@]}" ; do
489
+ user_name="user_${user}_name"; user_mail="user_${user}_mail"; user_password="user_${user}_password";
490
+ registry="${registry}${user}|${!user_mail}|${!user_password}|${!user_name}"$'\n' ;
491
+ done
492
+
493
+ local address ;
494
+ address="$(cd "${project_path}" && plane_url "$(sed -n 's/^name:[[:space:]]*"\{0,1\}\([A-Za-z0-9._-]*\)"\{0,1\}[[:space:]]*$/\1/p' .ddev/config.yaml 2>/dev/null | head -n 1)")" ;
495
+
496
+ python3 - "${project_path}" "${PLANE_STEPS_PACKAGE}" "${PLANE_STEPS_VERSION}" 'PLANE' "${registry}" "${address}" "${PLANE_WORKSPACE}" "${PLANE_PROJECT}" <<'PYEOF' || return 1
497
+ import json, re, sys
498
+
499
+ project, pack, version, prefix, registry, address, workspace, sample = sys.argv[1:9]
500
+
501
+ # package.json: the pack as a dev dependency, at the release line this plugin was proven on.
502
+ pkg_file = project + '/package.json'
503
+ pkg = json.load(open(pkg_file))
504
+ if pack not in pkg.get('dependencies', {}) and pack not in pkg.get('devDependencies', {}):
505
+ pkg.setdefault('devDependencies', {})[pack] = version
506
+ open(pkg_file, 'w').write(json.dumps(pkg, indent=4) + '\n')
507
+ print(' package.json: %s %s' % (pack, version))
508
+
509
+ cucumber_file = project + '/cucumber.js'
510
+ src = open(cucumber_file).read()
511
+ before = src
512
+
513
+ if ('node_modules/%s/' % pack) not in src:
514
+ line = "'node_modules/%s/tests/step-definitions/**/*.js'," % pack
515
+ core = re.search(r"^([ \t]*)'node_modules/webship-js/tests/step-definitions/[^']*',.*$", src, re.M)
516
+ opening = re.search(r"^([ \t]*)require:[ \t]*\[[ \t]*$", src, re.M)
517
+ if core:
518
+ src = src[:core.end()] + '\n' + core.group(1) + line + src[core.end():]
519
+ elif opening:
520
+ src = src[:opening.end()] + '\n' + opening.group(1) + ' ' + line + src[opening.end():]
521
+ else:
522
+ sys.exit(" cucumber.js has no require list this can add to. Add this line to it:\n " + line)
523
+ print(' cucumber.js: the step definitions of %s' % pack)
524
+
525
+ if not re.search(r"^[ \t]*tags:", src, re.M):
526
+ default = re.search(r"^([ \t]*)default:[ \t]*\{[ \t]*$", src, re.M)
527
+ if default:
528
+ pad = default.group(1) + ' '
529
+ block = [
530
+ pad + "// The pack's assertions that must fail are no part of a normal run.",
531
+ pad + "tags: process.env.CUCUMBER_TAGS || 'not @wip and not @must-fail',",
532
+ ]
533
+ src = src[:default.end()] + '\n' + '\n'.join(block) + src[default.end():]
534
+ print(' cucumber.js: the tags that keep the assertions that must fail out of a normal run')
535
+
536
+ world = re.search(r"^([ \t]*)worldParameters:[ \t]*\{[ \t]*$", src, re.M)
537
+ if not world:
538
+ sys.exit(" cucumber.js has no worldParameters block to add the users registry to.")
539
+ pad = world.group(1) + ' '
540
+ additions = []
541
+ if not re.search(r"^[ \t]*manager:", src, re.M):
542
+ additions += [
543
+ pad + "// The account that prepares and removes test content through the site's API: an Admin of the project.",
544
+ pad + "manager: process.env.MANAGER || 'owner',",
545
+ ]
546
+ if not re.search(r"^[ \t]*workspace:", src, re.M):
547
+ additions += [
548
+ pad + "// The workspace the projects are in (its slug, the part of its address after the host).",
549
+ pad + "workspace: process.env.WORKSPACE || '%s'," % workspace,
550
+ ]
551
+ if not re.search(r"^[ \t]*project:", src, re.M):
552
+ additions += [
553
+ pad + "// The project the steps work in when they name none (its identifier).",
554
+ pad + "project: process.env.PROJECT || '%s'," % sample,
555
+ ]
556
+ if not re.search(r"^[ \t]*users:[ \t]*\{", src, re.M):
557
+ rows = [r.split('|') for r in registry.splitlines() if r.strip()]
558
+ additions += [
559
+ pad + "// The users registry: a handle a scenario reads, mapped to an account of the site (the default accounts:",
560
+ pad + "// cmd-tools-plane-cli.sh --add-users). Override one with %s_<HANDLE>_USER and %s_<HANDLE>_PASSWORD." % (prefix, prefix),
561
+ pad + "users: {",
562
+ ]
563
+ for handle, mail, password, display in rows:
564
+ env = '%s_%s' % (prefix, handle.upper())
565
+ additions.append(pad + " %s: { username: process.env.%s_USER || '%s', password: process.env.%s_PASSWORD || '%s', name: '%s' }," % (handle, env, mail, env, password, display))
566
+ additions.append(pad + "},")
567
+ print(' cucumber.js: the users registry (%s)' % ', '.join(r[0] for r in rows))
568
+ if additions:
569
+ src = src[:world.end()] + '\n' + '\n'.join(additions) + src[world.end():]
570
+ print(' cucumber.js: the managing account, the workspace and the project of the steps')
571
+
572
+ # The address the suite opens when LAUNCH_URL is not set: the placeholder of the scaffold becomes the site's.
573
+ placeholder = "process.env.LAUNCH_URL || 'http://localhost:8080'"
574
+ if placeholder in src:
575
+ src = src.replace(placeholder, "process.env.LAUNCH_URL || '%s'" % address)
576
+ print(' cucumber.js: the address of the site (%s)' % address)
577
+
578
+ if src != before:
579
+ open(cucumber_file, 'w').write(src)
580
+ PYEOF
581
+
582
+ # A first feature, when the project has none for the site yet.
583
+ local feature="${WORKSPACE_SCRIPTS}/plugins/plane/site.feature" ;
584
+ if [ -f "${feature}" ] && [ ! -f "${project_path}/tests/features/site.feature" ] ; then
585
+ mkdir -p "${project_path}/tests/features" && cp "${feature}" "${project_path}/tests/features/site.feature" \
586
+ && echo " tests/features/site.feature: a first feature for the work items" ;
587
+ fi
588
+
589
+ # The scenarios log in as the default accounts and read the sample project: make sure the site has them, once it
590
+ # answers again (installing the testing stack may restart the project, and Plane with it).
591
+ if ( cd "${project_path}" && plane_wait > /dev/null 2>&1 && add_plane_users > /dev/null 2>&1 && plane_site content < /dev/null > /dev/null 2>&1 ) ; then
592
+ echo " the default accounts and the sample content are on the site (${users[*]})" ;
593
+ else
594
+ echo " The default accounts could not be written: is the site running (ddev start in ${project_path})?" ;
595
+ fi
596
+ }