@webship.co/workspace-redmine 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,41 @@
1
+ # Redmine of this project: the official image on the project's own PostgreSQL (the container tool's db service),
2
+ # served at the project's address; the project's web container keeps the mail catcher. Written by the Redmine builder of
3
+ # the workspace (the plugin's docker-compose.redmine.yaml); build again rather than editing by hand. The site's own
4
+ # values (its secret key) are in redmine/env, which only you can read.
5
+ services:
6
+ web:
7
+ environment:
8
+ - HTTP_EXPOSE=8093:80,8025:8025
9
+ - HTTPS_EXPOSE=8457:80,8026:8025
10
+ redmine:
11
+ image: "redmine:${REDMINE_VERSION:-7.0.3}"
12
+ container_name: ddev-${DDEV_SITENAME}-redmine
13
+ labels:
14
+ com.ddev.site-name: ${DDEV_SITENAME}
15
+ com.ddev.approot: ${DDEV_APPROOT}
16
+ restart: "no"
17
+ expose:
18
+ - "3000"
19
+ # The image migrates the database once as it starts, and stops if PostgreSQL does not answer yet: a new project's
20
+ # database is still being made when its container starts, so Redmine waits for it to be healthy.
21
+ depends_on:
22
+ db:
23
+ condition: service_healthy
24
+ env_file:
25
+ - ${DDEV_APPROOT}/redmine/env
26
+ volumes:
27
+ - redmine_files:/usr/src/redmine/files
28
+ # The plugin's scripts, which the builder and the tool run in this container.
29
+ - ${DDEV_APPROOT}/redmine:/opt/webship:ro
30
+ environment:
31
+ VIRTUAL_HOST: ${DDEV_HOSTNAME}
32
+ HTTP_EXPOSE: "80:3000"
33
+ HTTPS_EXPOSE: "443:3000"
34
+ REDMINE_DB_POSTGRES: db
35
+ REDMINE_DB_USERNAME: db
36
+ REDMINE_DB_PASSWORD: db
37
+ REDMINE_DB_DATABASE: db
38
+ # Named after the project, so removing the project removes it with its database, and a new build starts empty.
39
+ volumes:
40
+ redmine_files:
41
+ name: ddev-${DDEV_SITENAME}-redmine-files
@@ -0,0 +1,576 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # Redmine for the Webship Workspace: build a site of Redmine (the open-source project management and issue tracking
4
+ # software) from the official image, run its own tools in its container, add its default accounts and their
5
+ # memberships, add its sample content, and wire its step pack into the testing setup.
6
+ #
7
+ # Placed by `plugins.sh --install redmine` in core/scripts/plugins/redmine/functions/, where bootstrap sources it after
8
+ # the core's own functions. Everything runs in containers, never on the host:
9
+ # - DDEV (the default): Redmine's image is a service of a generic project, on the project's own PostgreSQL (the db
10
+ # service), at https://<name>.ddev.site; the project's web container keeps the mail catcher.
11
+ # - Docker Compose (--runtime docker): the official image is the web container's (rt_stack image), on the project's
12
+ # PostgreSQL, its server started by the stack (rt_serve) through the image's own entrypoint, at http://127.0.0.1:<port>.
13
+ # Podman, Kubernetes, Lando and Docksal are not proven yet: asked for one, the builder says so and changes nothing.
14
+ #
15
+ # The container tool has no project type for Redmine, so a site of this plugin is recognised by a file on DDEV
16
+ # (.ddev/config.redmine.yaml, written by the builder) and by the plugin's claim on the other runtimes (rt_claim).
17
+ # Four names are the ones the core's tools look for:
18
+ # set_redmine_users the default accounts (cmd-tools-add-users.sh <project> redmine)
19
+ # add_redmine_users make them, and their memberships (the same tool, and --add-users of the builder)
20
+ # cancel_redmine_users lock them (cmd-tools-cancel-users.sh <project> redmine)
21
+ # testing_configure_redmine the step pack in the testing stack (cmd-tools-testing.sh <project>)
22
+
23
+ # The step pack a site is tested with, and the release line it is required at.
24
+ readonly REDMINE_STEPS_PACKAGE='@webship-js/redmine-steps';
25
+ readonly REDMINE_STEPS_VERSION='^2.0.0';
26
+
27
+ # The release the builder runs when none is asked for (a tag of the official image).
28
+ readonly REDMINE_DEFAULT_VERSION='7.0.3';
29
+
30
+ # The sample project the default accounts are members of (its identifier and its name), and the folder of a site with
31
+ # the plugin's scripts and the site's own values (its container reads them at /opt/webship on DDEV).
32
+ readonly REDMINE_PROJECT='webship-site';
33
+ readonly REDMINE_PROJECT_NAME='Webship site';
34
+ readonly REDMINE_FILES='redmine';
35
+
36
+ # The default accounts, the ones a scenario logs in as: a Manager of the sample project (owner), a Developer (member),
37
+ # a Reporter (reader), and an account with no membership (stranger), whose issue the tracker treats as from outside
38
+ # the project. Accounts are configuration, made once on the site; a test suite never creates or deletes
39
+ # one. The workspace account the builder makes with --install is an administrator of its own and is not in this list.
40
+ function set_redmine_users() {
41
+ users=(owner member reader stranger);
42
+ local user ;
43
+ for user in "${users[@]}" ; do
44
+ printf -v "user_${user}_name" '%s' "${user}" ;
45
+ printf -v "user_${user}_mail" '%s' "${user}@example.com" ;
46
+ printf -v "user_${user}_password" '%s' 'dD.123123ddd' ;
47
+ printf -v "user_${user}_fullname" '%s' "${user^} Test" ;
48
+ done
49
+ user_owner_role='Manager' ;
50
+ user_member_role='Developer' ;
51
+ user_reader_role='Reporter' ;
52
+ user_stranger_role='' ;
53
+ }
54
+
55
+ # The runtime of the project in this folder, or of a new build (--runtime, else the workspace's setting, else DDEV).
56
+ # A core without the runtime layer has DDEV only.
57
+ function redmine_runtime() {
58
+ if declare -f runtime_of > /dev/null ; then runtime_of "$@" ; else echo 'ddev' ; fi
59
+ }
60
+
61
+ # The address of the site in this folder (or of a new build named PROJECT_NAME, on DDEV).
62
+ function redmine_url() {
63
+ if [ "$(redmine_runtime)" != 'ddev' ] && declare -f rt_url > /dev/null ; then rt_url ; return ; fi
64
+ echo "https://${1:-${PROJECT_NAME}}.ddev.site" ;
65
+ }
66
+
67
+ # Is this folder a Redmine site of this plugin? On DDEV the builder leaves .ddev/config.redmine.yaml in it;
68
+ # on another runtime the project carries the plugin's claim.
69
+ function redmine_is_project() {
70
+ local project_path="$1" ;
71
+ [ -f "${project_path}/.ddev/config.redmine.yaml" ] && return 0 ;
72
+ if declare -f rt_is_platform > /dev/null ; then rt_is_platform "${project_path}" redmine ; return $? ; fi
73
+ return 1 ;
74
+ }
75
+
76
+ # Go to the project a tool was asked about, or say why not.
77
+ function redmine_enter_project() {
78
+ local project_path="${WORKSPACE_ROOT}/${doc_name}/${PROJECT_NAME}";
79
+ if [ ! -d "${project_path}" ] ; then
80
+ echo "No project ${PROJECT_NAME} in ${WORKSPACE_ROOT}/${doc_name}." ;
81
+ return 1 ;
82
+ fi
83
+ if ! redmine_is_project "${project_path}" ; then
84
+ echo "${doc_name}/${PROJECT_NAME} is not a Redmine site of this plugin." ;
85
+ return 1 ;
86
+ fi
87
+ redmine_refresh_files "${project_path}" || return 1 ;
88
+ cd "${project_path}" || return 1 ;
89
+ }
90
+
91
+ # The site's copy of the plugin's scripts (redmine/), brought up to the installed plugin's, so a plugin updated
92
+ # with --install reaches the sites built before it. The site's own values (redmine/env) are left as they are.
93
+ function redmine_refresh_files() {
94
+ local project_path="$1" from="${WORKSPACE_SCRIPTS}/plugins/redmine" f ;
95
+ [ -d "${project_path}/${REDMINE_FILES}" ] || return 0 ;
96
+ for f in redmine-site.rb redmine-serve.sh ; do
97
+ if [ -f "${from}/${f}" ] && ! cmp -s "${from}/${f}" "${project_path}/${REDMINE_FILES}/${f}" ; then
98
+ cp "${from}/${f}" "${project_path}/${REDMINE_FILES}/${f}" || return 1 ;
99
+ fi
100
+ done
101
+ }
102
+
103
+ # A value of the site's own values (redmine/env), never printed by the tools: REDMINE_VERSION.
104
+ function redmine_setting() {
105
+ sed -n "s/^$1=\\(.*\\)\$/\\1/p" "${2:-.}/${REDMINE_FILES}/env" 2>/dev/null | head -n 1 ;
106
+ }
107
+
108
+ # A value made for this site only: letters and digits.
109
+ function redmine_secret() {
110
+ head -c 96 /dev/urandom | base64 -w0 | tr -dc 'A-Za-z0-9' | head -c "${1:-64}" ;
111
+ }
112
+
113
+ # Write the project's files: on DDEV Redmine's service (.ddev/docker-compose.redmine.yaml), the release in
114
+ # .ddev/.env and the claim (.ddev/config.redmine.yaml); on another runtime the claim, the official image as the
115
+ # web container's and its start (rt_claim, rt_stack, rt_web_build, rt_serve). Then the plugin's scripts and the site's
116
+ # own values (redmine/env, which only you can read): its secret key, the first administrator's password, the mail.
117
+ function redmine_write_config() {
118
+ local project_path="$1" version="$2" runtime="$3" ;
119
+ local from="${WORKSPACE_SCRIPTS}/plugins/redmine" f ;
120
+ for f in docker-compose.redmine.yaml redmine-site.rb redmine-serve.sh ; do
121
+ if [ ! -f "${from}/${f}" ] ; then
122
+ echo "${f} is not in ${from}: install the plugin (plugins.sh --install redmine)." ;
123
+ return 1 ;
124
+ fi
125
+ done
126
+ mkdir -p "${project_path}/${REDMINE_FILES}" || return 1 ;
127
+ for f in redmine-site.rb redmine-serve.sh ; do
128
+ cp "${from}/${f}" "${project_path}/${REDMINE_FILES}/${f}" || return 1 ;
129
+ done
130
+
131
+ local env="${project_path}/${REDMINE_FILES}/env" ;
132
+ ( umask 077 && cat > "${env}" <<ENVEOF
133
+ # The values of this Redmine site, made by the Redmine builder of the workspace: its secret key is of this site only.
134
+ # Read by the site's container; never printed. REDMINE_ADMIN_PASSWORD is the password of its first administrator.
135
+ REDMINE_VERSION=${version}
136
+ SECRET_KEY_BASE=$(redmine_secret 96)
137
+ REDMINE_ADMIN_PASSWORD=$(redmine_secret 24)
138
+ REDMINE_DB_POSTGRES=db
139
+ REDMINE_DB_USERNAME=db
140
+ REDMINE_DB_PASSWORD=db
141
+ REDMINE_DB_DATABASE=db
142
+ ENVEOF
143
+ ) || return 1 ;
144
+
145
+ if [ "${runtime}" = 'ddev' ] ; then
146
+ cp "${from}/docker-compose.redmine.yaml" "${project_path}/.ddev/docker-compose.redmine.yaml" || return 1 ;
147
+ printf 'REDMINE_VERSION=%s\n' "${version}" > "${project_path}/.ddev/.env" || return 1 ;
148
+ cat > "${project_path}/.ddev/config.redmine.yaml" <<YAMLEOF
149
+ # Redmine in this project: its service (docker-compose.redmine.yaml), the release in .env, the site's own values in
150
+ # ../redmine/env. Written by the Redmine builder of the workspace; build again rather than editing.
151
+ web_environment:
152
+ - REDMINE_VERSION=${version}
153
+ YAMLEOF
154
+ return 0 ;
155
+ fi
156
+
157
+ # Another runtime: the official image is the web container's, and the stack starts it (redmine-serve.sh).
158
+ ( cd "${project_path}" \
159
+ && rt_claim redmine \
160
+ && rt_stack image "redmine:${version}" \
161
+ && rt_web_build redmine <<'DOCKEREOF'
162
+ # What Redmine writes while it runs (its files, its plugins' assets) belongs to the host user, www-data with the host's
163
+ # UID by now; the folders the image leaves open to everyone (config, log, tmp, public/assets) stay as they are.
164
+ RUN chown -R www-data:www-data /usr/src/redmine/files /usr/src/redmine/public/plugin_assets
165
+ DOCKEREOF
166
+ ) || return 1 ;
167
+ ( cd "${project_path}" && rt_serve 3000 "bash ${REDMINE_FILES}/redmine-serve.sh" ) || return 1 ;
168
+ }
169
+
170
+ # Run a command line in Redmine's container, as Redmine's own user, in its folder (/usr/src/redmine), with the site's
171
+ # values; what is piped in reaches it.
172
+ function redmine_exec() {
173
+ local line="$1" ;
174
+ if [ "$(redmine_runtime)" = 'ddev' ] ; then
175
+ ddev exec -s redmine "gosu redmine bash -c $(printf '%q' "cd /usr/src/redmine && ${line}")" ;
176
+ else
177
+ ddev exec "set -a ; . /var/www/html/${REDMINE_FILES}/env ; set +a ; cd /usr/src/redmine && ${line}" ;
178
+ fi
179
+ }
180
+
181
+ # The folder the plugin's scripts are read from in Redmine's container.
182
+ function redmine_scripts_dir() {
183
+ if [ "$(redmine_runtime)" = 'ddev' ] ; then echo '/opt/webship' ; else echo "/var/www/html/${REDMINE_FILES}" ; fi
184
+ }
185
+
186
+ # The answer of Redmine's login page (200 when it serves and reaches its database), asked inside the project's network:
187
+ # on DDEV from the web container, elsewhere inside its own container.
188
+ function redmine_probe() {
189
+ if [ "$(redmine_runtime)" = 'ddev' ] ; then
190
+ ddev exec "curl -s -o /dev/null -m 10 -w '%{http_code}' http://redmine:3000/login" < /dev/null 2>/dev/null | tr -d '\r' | tail -1 ;
191
+ else
192
+ ddev exec "curl -s -o /dev/null -m 10 -w '%{http_code}' http://127.0.0.1:3000/login" < /dev/null 2>/dev/null | tr -d '\r' | tail -1 ;
193
+ fi
194
+ }
195
+
196
+ # Wait until the site answers, in the project's folder: up to fifteen minutes, as the first start migrates the
197
+ # database.
198
+ function redmine_wait() {
199
+ local attempt code="" ;
200
+ for attempt in $(seq 1 300) ; do
201
+ code="$(redmine_probe)" ;
202
+ [ "${code}" == '200' ] && return 0 ;
203
+ sleep 3 ;
204
+ done
205
+ echo "Redmine did not answer (its login page: ${code:-none}); the end of its log:" ;
206
+ if [ "$(redmine_runtime)" = 'ddev' ] ; then ddev logs -s redmine --tail 40 < /dev/null 2>/dev/null ; else ddev logs < /dev/null 2>/dev/null | tail -n 40 ; fi
207
+ return 1 ;
208
+ }
209
+
210
+ # The release the site runs, as Redmine says it.
211
+ function redmine_version() {
212
+ ( cd "${WORKSPACE_ROOT}/${doc_name}/${PROJECT_NAME}" 2>/dev/null && redmine_exec "sed -n 's/^ *\\(MAJOR\\|MINOR\\|TINY\\) *= *\\([0-9]*\\).*/\\2/p' lib/redmine/version.rb | paste -sd." < /dev/null 2>/dev/null ) | tr -d '\r' | tail -n 1 ;
213
+ }
214
+
215
+ # Run verbs of redmine-site.rb (defaults, accounts, content, tidy, list, token) with Redmine's own Rails runner in its
216
+ # container. A payload (accounts, passwords) travels on the input, never on a command line.
217
+ function redmine_site() {
218
+ local verbs="$*" ;
219
+ # The log to its file, not on the screen: the runner of the production environment logs every mail it queues.
220
+ redmine_exec "RAILS_LOG_TO_STDOUT= bundle exec rails runner $(redmine_scripts_dir)/redmine-site.rb ${verbs}" ;
221
+ }
222
+
223
+ # Rows of accounts for redmine-site.rb, as the base64 of its JSON list: "login|mail|password|first|last|admin|
224
+ # role|action" lines in REDMINE_ROWS, which travel to Python in the environment, never on a command line.
225
+ function redmine_accounts_payload() {
226
+ REDMINE_ROWS="$1" python3 -c '
227
+ import base64, json, os
228
+ rows = []
229
+ for line in os.environ["REDMINE_ROWS"].split("\x1e"):
230
+ if not line:
231
+ continue
232
+ login, mail, password, first, last, admin, role, action = line.split("\x1f")
233
+ rows.append({"login": login, "mail": mail, "password": password, "firstname": first, "lastname": last,
234
+ "admin": admin == "yes", "role": role, "action": action})
235
+ print(base64.b64encode(json.dumps(rows).encode()).decode())' ;
236
+ }
237
+
238
+ # Build a Redmine site from nothing: the project and Redmine; with --install the workspace account as an
239
+ # administrator; with --add-users the default accounts, members of the sample project; with --sample-content the
240
+ # project's issues, version and note the step pack is tested on.
241
+ function redmine_build() {
242
+ trim_project_name ;
243
+ local version="${REDMINE_VERSION:-${site_redmine_version:-${REDMINE_DEFAULT_VERSION}}}" ;
244
+ local project_path="${WORKSPACE_ROOT}/${doc_name}/${PROJECT_NAME}" ;
245
+ local runtime ; runtime="$(cd "${WORKSPACE_ROOT}/${doc_name}" && redmine_runtime)" ;
246
+
247
+ case "${runtime}" in
248
+ ddev|docker) ;;
249
+ *) echo "Redmine builds on DDEV and on Docker Compose for now (--runtime ddev or docker), not on ${runtime}: that runtime is not proven for it yet. Nothing was changed." ; exit 1 ;;
250
+ esac
251
+ if [ "${runtime}" != 'ddev' ] && ! declare -f rt_stack > /dev/null ; then
252
+ echo "--runtime ${runtime} needs a Webship Workspace with the container runtimes (webship/workspace#694, #841): update the workspace, or build on DDEV." ;
253
+ exit 1 ;
254
+ fi
255
+ if ! [[ "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] ; then
256
+ echo "--redmine-version takes a release (7.0.3), not ${version}. Nothing was changed." ;
257
+ exit 1 ;
258
+ fi
259
+ if [ "${ADD_USERS}" == 'yes' ] && [ "${INSTALL}" != 'yes' ] ; then
260
+ echo "--add-users needs --install: the default accounts go into the site --install sets up." ;
261
+ exit 1 ;
262
+ fi
263
+ if [ "${SAMPLE_CONTENT}" == 'yes' ] && [ "${ADD_USERS}" != 'yes' ] ; then
264
+ echo "--sample-content needs --add-users: the sample project's members are the default accounts." ;
265
+ exit 1 ;
266
+ fi
267
+ if [ ${#PROJECT_NAME} -gt 40 ] || ! [[ "${PROJECT_NAME}" =~ ^[a-z0-9][a-z0-9-]*$ ]] ; then
268
+ echo "The project name ${PROJECT_NAME} must be small letters, digits and dashes (its address and its containers take it). Nothing was changed." ;
269
+ exit 1 ;
270
+ fi
271
+ if [ "${INSTALL}" == 'yes' ] && [ ${#account_pass} -lt 8 ] ; then
272
+ echo "The workspace account's password (core/config/settings.yml) has fewer than eight characters, which Redmine asks of a password: --install makes it an account of the site. Nothing was changed." ;
273
+ exit 1 ;
274
+ fi
275
+
276
+ cd ${WORKSPACE_ROOT}/${doc_name} || exit 1 ;
277
+
278
+ # A project of the same name goes first, with its containers and its volumes (Redmine's database and files).
279
+ drop_database ;
280
+ rm -rf "${PROJECT_NAME:?}" ;
281
+ mkdir -p "${PROJECT_NAME}" ;
282
+ cd "${project_path}" || exit 1 ;
283
+
284
+ # A generic project on PostgreSQL 17.
285
+ if [ "${runtime}" = 'ddev' ] ; then
286
+ ddev config --project-type=generic --project-name=${PROJECT_NAME} --database=postgres:17 \
287
+ --nodejs-version=$(ddev_nodejs_version) < /dev/null || exit 1 ;
288
+ else
289
+ ddev config --project-type=generic --webserver-type=generic --project-name=${PROJECT_NAME} --database=postgres:17 --auto < /dev/null || exit 1 ;
290
+ fi
291
+ redmine_write_config "${project_path}" "${version}" "${runtime}" || exit 1 ;
292
+ local base_url ; base_url="$(redmine_url)" ;
293
+
294
+ echo "Starting the project and Redmine ${version}: its image is about 600 MB the first time, and the first start migrates the database." ;
295
+ ddev_up start -y < /dev/null || exit 1 ;
296
+ redmine_wait || exit 1 ;
297
+
298
+ # Redmine's default data (trackers, statuses, roles, workflows), the REST API on, the address of its links, and the
299
+ # first administrator's password, of this site only.
300
+ local scheme="${base_url%%://*}" host="${base_url#*://}" ;
301
+ REDMINE_ADMIN="$(redmine_setting REDMINE_ADMIN_PASSWORD "${project_path}")" REDMINE_HOST="${host}" REDMINE_SCHEME="${scheme}" python3 -c '
302
+ import base64, json, os
303
+ print(base64.b64encode(json.dumps({"admin_password": os.environ["REDMINE_ADMIN"], "host": os.environ["REDMINE_HOST"], "protocol": os.environ["REDMINE_SCHEME"]}).encode()).decode())' \
304
+ | redmine_site defaults || { echo "Redmine's default data could not be loaded." ; exit 1 ; } ;
305
+
306
+ if [ "${INSTALL}" == 'yes' ] ; then
307
+ echo "Making the workspace account an administrator of the site." ;
308
+ local rows ;
309
+ rows="${account_name}"$'\x1f'"${account_mail:-${account_name}@example.com}"$'\x1f'"${account_pass}"$'\x1f'"${account_name^}"$'\x1f''Workspace'$'\x1f''yes'$'\x1f'$'\x1f'$'\x1e' ;
310
+ redmine_accounts_payload "${rows}" | redmine_site accounts || { echo "The workspace account could not be made." ; exit 1 ; } ;
311
+ if [ "${ADD_USERS}" == 'yes' ] ; then
312
+ set_redmine_users ;
313
+ add_redmine_users || exit 1 ;
314
+ if [ "${SAMPLE_CONTENT}" == 'yes' ] ; then
315
+ echo "Adding the sample content: the project ${REDMINE_PROJECT_NAME}'s issues, its version Sprint 1 and a note." ;
316
+ ( cd "${project_path}" && redmine_site content < /dev/null ) || exit 1 ;
317
+ fi
318
+ users=() ;
319
+ fi
320
+ echo "${doc_name} ${PROJECT_NAME} has been installed: Redmine $(redmine_version).";
321
+ echo "The site: ${base_url}" ;
322
+ echo "The workspace account logs in as ${account_name}; the first administrator (admin) has a password of this site only, in ${doc_name}/${PROJECT_NAME}/${REDMINE_FILES}/env (REDMINE_ADMIN_PASSWORD).";
323
+ else
324
+ echo "${doc_name} ${PROJECT_NAME} is ready: Redmine $(redmine_version) at ${base_url}.";
325
+ echo "Its first administrator (admin) has a password of this site only, in ${doc_name}/${PROJECT_NAME}/${REDMINE_FILES}/env (REDMINE_ADMIN_PASSWORD). Build it again with --install (and --add-users) for the workspace account and the default accounts.";
326
+ fi
327
+
328
+ if [ "${LAUNCH}" == 'yes' ] ; then
329
+ ddev launch ;
330
+ fi
331
+
332
+ cd ${WORKSPACE_ROOT}/${doc_name};
333
+ }
334
+
335
+ # Make the default accounts, or give them back their names, passwords and status, with the first login done (no
336
+ # welcome dialog), and each a member of the sample project in its role (the stranger in none). The list is loaded by
337
+ # the caller (set_redmine_users): the core's add_users, the builder, the tool.
338
+ function add_redmine_users() {
339
+ redmine_enter_project || return 1 ;
340
+ local user user_name user_mail user_password user_fullname user_role rows="" ;
341
+ for user in "${users[@]}" ; do
342
+ user_name="user_${user}_name"; user_mail="user_${user}_mail";
343
+ user_password="user_${user}_password"; user_fullname="user_${user}_fullname"; user_role="user_${user}_role";
344
+
345
+ echo " ---------------------------------------------------------------- ";
346
+ echo " User name: ${!user_name}";
347
+ echo " User mail: ${!user_mail}";
348
+ echo " User password: ${!user_password}";
349
+ echo " User role: ${!user_role:-no membership} of ${REDMINE_PROJECT_NAME}";
350
+ echo " ================================================================= ";
351
+ rows="${rows}${!user_name}"$'\x1f'"${!user_mail}"$'\x1f'"${!user_password}"$'\x1f'"${!user_fullname% *}"$'\x1f'"${!user_fullname##* }"$'\x1f''no'$'\x1f'"${!user_role}"$'\x1f'$'\x1e' ;
352
+ done
353
+ redmine_accounts_payload "${rows}" | redmine_site accounts ;
354
+ local status=$? ;
355
+ cd ${WORKSPACE_ROOT}/${doc_name};
356
+ return ${status} ;
357
+ }
358
+
359
+ # Lock the default accounts (they cannot log in any more; their work stays), never the workspace account.
360
+ function cancel_redmine_users() {
361
+ redmine_enter_project || return 1 ;
362
+ local user user_name user_mail rows="" ;
363
+ for user in "${users[@]}" ; do
364
+ user_name="user_${user}_name"; user_mail="user_${user}_mail";
365
+ echo " ---------------------------------------------------------------- ";
366
+ echo " User name: ${!user_name}";
367
+ echo " User mail: ${!user_mail}";
368
+ echo " ================================================================= ";
369
+ rows="${rows}${!user_name}"$'\x1f'"${!user_mail}"$'\x1f'$'\x1f'$'\x1f'$'\x1f''no'$'\x1f'$'\x1f''lock'$'\x1e' ;
370
+ done
371
+ redmine_accounts_payload "${rows}" | redmine_site accounts ;
372
+ local status=$? ;
373
+ cd ${WORKSPACE_ROOT}/${doc_name};
374
+ return ${status} ;
375
+ }
376
+
377
+ # Point the dashboard's Redmine (core/config/plugin.redmine.settings.yml) at this site, as an account of it:
378
+ # a new API key of that account goes into the settings file, which only you can read, never on a screen or in a
379
+ # log; with it the site's address, and the sample project as the default one when none was set.
380
+ function redmine_connect() {
381
+ local login="$1" settings="${WORKSPACE_CONFIG}/plugin.redmine.settings.yml" out token url ;
382
+ if ! [[ "${login}" =~ ^[A-Za-z0-9._@-]+$ ]] ; then echo "--connect takes the login of an account of the site." ; return 1 ; fi
383
+ if [ ! -f "${settings}" ] ; then echo "No ${settings}: install the plugin first (plugins.sh --install redmine)." ; return 1 ; fi
384
+ out="$(printf '%s\n' "${login}" | redmine_site token 2>/dev/null)" ;
385
+ token="$(printf '%s\n' "${out}" | tr -d '\r' | sed -n 's/^TOKEN //p' | tail -n 1)" ;
386
+ out='' ;
387
+ if ! [[ "${token}" =~ ^[A-Za-z0-9_-]{20,}$ ]] ; then echo "No API key could be made for ${login} (is it an active account of the site?)." ; return 1 ; fi
388
+ url="$(redmine_url)" ;
389
+ REDMINE_NEW_TOKEN="${token}" python3 - "${settings}" "${url}" "${REDMINE_PROJECT}" <<'PYEOF' || return 1
390
+ import os, re, sys
391
+ path, url, project = sys.argv[1:4]
392
+ token = os.environ['REDMINE_NEW_TOKEN']
393
+ text = open(path).read()
394
+ def put(text, key, value):
395
+ line = '%s: %s' % (key, value)
396
+ if re.search(r'^%s:.*$' % key, text, re.M):
397
+ return re.sub(r'^%s:.*$' % key, lambda m: line, text, count=1, flags=re.M)
398
+ return text.rstrip('\n') + '\n' + line + '\n'
399
+ text = put(text, 'url', url)
400
+ text = put(text, 'token', token)
401
+ if not re.search(r'^default_scope:[ \t]*[^ \t\n"]', text, re.M) and not re.search(r'^default_scope:[ \t]*"[^"]', text, re.M):
402
+ text = put(text, 'default_scope', project)
403
+ fd = os.open(path + '.new', os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
404
+ with os.fdopen(fd, 'w') as f:
405
+ f.write(text)
406
+ os.replace(path + '.new', path)
407
+ PYEOF
408
+ token='' ;
409
+ echo "The dashboard reads the issues of ${url} as ${login} now: a new API key of that account is in ${settings} (not shown here), and ${REDMINE_PROJECT} is the default project when none was set." ;
410
+ }
411
+
412
+ # What cmd-tools-redmine-cli.sh does: the first of --run, --add-users, --cancel-users, --sample-content, --tidy,
413
+ # --connect that was asked for; with none of them, what the site is and its accounts.
414
+ function redmine_cli_tool() {
415
+ trim_project_name ;
416
+ redmine_enter_project || exit 1 ;
417
+
418
+ if [ "$(redmine_runtime)" = 'ddev' ] ; then
419
+ ddev describe -j 2>/dev/null | grep -q '"status":"running"' || ddev start -y < /dev/null || exit 1 ;
420
+ else
421
+ ddev start < /dev/null > /dev/null 2>&1 || ddev start -y < /dev/null || exit 1 ;
422
+ fi
423
+ redmine_wait > /dev/null || { echo "Redmine of ${PROJECT_NAME} does not answer; start it again (ddev restart in ${doc_name}/${PROJECT_NAME})." ; exit 1 ; } ;
424
+
425
+ if [ ! "${RUN}" == '_none_' ] ; then
426
+ # Redmine's own tasks (bundle exec rake <task>), in its container, as its user.
427
+ redmine_exec "bundle exec rake ${RUN}" < /dev/null ;
428
+ exit $? ;
429
+ fi
430
+
431
+ set_redmine_users ;
432
+ if [ "${ADD_USERS}" == 'yes' ] ; then
433
+ add_redmine_users ;
434
+ elif [ "${CANCEL_USERS}" == 'yes' ] ; then
435
+ cancel_redmine_users ;
436
+ elif [ "${SAMPLE_CONTENT}" == 'yes' ] ; then
437
+ redmine_site content list < /dev/null ;
438
+ elif [ "${TIDY}" == 'yes' ] ; then
439
+ # What a test run that was stopped half way left: its issues ("Webship test …"), with their time.
440
+ redmine_site tidy list < /dev/null ;
441
+ elif [ "${CONNECT}" != '_none_' ] ; then
442
+ redmine_connect "${CONNECT}" ;
443
+ else
444
+ echo "${doc_name}/${PROJECT_NAME}: Redmine $(redmine_version)" ;
445
+ echo " the site: $(redmine_url)" ;
446
+ echo "" ;
447
+ redmine_site list < /dev/null ;
448
+ fi
449
+ cd ${WORKSPACE_ROOT}/${doc_name};
450
+ }
451
+
452
+ # The step pack in the testing stack of a Redmine site. The core's testing_configure calls this in the project
453
+ # folder once webship-js is scaffolded and before the dependencies are installed; for a project that is not one of
454
+ # this plugin's it changes nothing.
455
+ #
456
+ # It adds the pack to package.json, lists its step definitions in cucumber.js after webship-js's own, gives cucumber.js
457
+ # the tags that keep the pack's assertions that must fail out of a normal run, the account that prepares and removes
458
+ # test content (manager), the project the steps work in when they name none, and the users registry of the default
459
+ # accounts (a handle a scenario reads, mapped to an account; never the workspace account), and writes a first feature
460
+ # when the project has none of its own. Every part is skipped when it is already there, so a second run adds what the
461
+ # first missed and never resets a suite.
462
+ function testing_configure_redmine() {
463
+ local project_path="$1";
464
+ redmine_is_project "${project_path}" || return 0 ;
465
+ if [ ! -f "${project_path}/cucumber.js" ] || [ ! -f "${project_path}/package.json" ] ; then
466
+ echo " Redmine: cucumber.js is not here yet — run the testing setup again once webship-js is scaffolded." ;
467
+ return 1 ;
468
+ fi
469
+
470
+ echo "Adding the Redmine step pack (${REDMINE_STEPS_PACKAGE} ${REDMINE_STEPS_VERSION}).";
471
+ set_redmine_users ;
472
+ local registry="" user user_name user_password user_fullname ;
473
+ for user in "${users[@]}" ; do
474
+ user_name="user_${user}_name"; user_password="user_${user}_password"; user_fullname="user_${user}_fullname";
475
+ registry="${registry}${user}|${!user_name}|${!user_password}|${!user_fullname}"$'\n' ;
476
+ done
477
+
478
+ local address ;
479
+ address="$(cd "${project_path}" && redmine_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)")" ;
480
+
481
+ python3 - "${project_path}" "${REDMINE_STEPS_PACKAGE}" "${REDMINE_STEPS_VERSION}" 'REDMINE' "${registry}" "${address}" "${REDMINE_PROJECT}" <<'PYEOF' || return 1
482
+ import json, re, sys
483
+
484
+ project, pack, version, prefix, registry, address, sample = sys.argv[1:8]
485
+
486
+ # package.json: the pack as a dev dependency, at the release line this plugin was proven on.
487
+ pkg_file = project + '/package.json'
488
+ pkg = json.load(open(pkg_file))
489
+ if pack not in pkg.get('dependencies', {}) and pack not in pkg.get('devDependencies', {}):
490
+ pkg.setdefault('devDependencies', {})[pack] = version
491
+ open(pkg_file, 'w').write(json.dumps(pkg, indent=4) + '\n')
492
+ print(' package.json: %s %s' % (pack, version))
493
+
494
+ cucumber_file = project + '/cucumber.js'
495
+ src = open(cucumber_file).read()
496
+ before = src
497
+
498
+ if ('node_modules/%s/' % pack) not in src:
499
+ line = "'node_modules/%s/tests/step-definitions/**/*.js'," % pack
500
+ core = re.search(r"^([ \t]*)'node_modules/webship-js/tests/step-definitions/[^']*',.*$", src, re.M)
501
+ opening = re.search(r"^([ \t]*)require:[ \t]*\[[ \t]*$", src, re.M)
502
+ if core:
503
+ src = src[:core.end()] + '\n' + core.group(1) + line + src[core.end():]
504
+ elif opening:
505
+ src = src[:opening.end()] + '\n' + opening.group(1) + ' ' + line + src[opening.end():]
506
+ else:
507
+ sys.exit(" cucumber.js has no require list this can add to. Add this line to it:\n " + line)
508
+ print(' cucumber.js: the step definitions of %s' % pack)
509
+
510
+ if not re.search(r"^[ \t]*tags:", src, re.M):
511
+ default = re.search(r"^([ \t]*)default:[ \t]*\{[ \t]*$", src, re.M)
512
+ if default:
513
+ pad = default.group(1) + ' '
514
+ block = [
515
+ pad + "// The pack's assertions that must fail are no part of a normal run.",
516
+ pad + "tags: process.env.CUCUMBER_TAGS || 'not @wip and not @must-fail',",
517
+ ]
518
+ src = src[:default.end()] + '\n' + '\n'.join(block) + src[default.end():]
519
+ print(' cucumber.js: the tags that keep the assertions that must fail out of a normal run')
520
+
521
+ world = re.search(r"^([ \t]*)worldParameters:[ \t]*\{[ \t]*$", src, re.M)
522
+ if not world:
523
+ sys.exit(" cucumber.js has no worldParameters block to add the users registry to.")
524
+ pad = world.group(1) + ' '
525
+ additions = []
526
+ if not re.search(r"^[ \t]*manager:", src, re.M):
527
+ additions += [
528
+ pad + "// The account that prepares and removes test content through the site's API: a Manager of the project.",
529
+ pad + "manager: process.env.MANAGER || 'owner',",
530
+ ]
531
+ if not re.search(r"^[ \t]*project:", src, re.M):
532
+ additions += [
533
+ pad + "// The project the steps work in when they name none (its identifier).",
534
+ pad + "project: process.env.PROJECT || '%s'," % sample,
535
+ ]
536
+ if not re.search(r"^[ \t]*users:[ \t]*\{", src, re.M):
537
+ rows = [r.split('|') for r in registry.splitlines() if r.strip()]
538
+ additions += [
539
+ pad + "// The users registry: a handle a scenario reads, mapped to an account of the site (the default accounts:",
540
+ pad + "// cmd-tools-redmine-cli.sh --add-users). Override one with %s_<HANDLE>_USER and %s_<HANDLE>_PASSWORD." % (prefix, prefix),
541
+ pad + "users: {",
542
+ ]
543
+ for handle, login, password, fullname in rows:
544
+ env = '%s_%s' % (prefix, handle.upper())
545
+ additions.append(pad + " %s: { username: process.env.%s_USER || '%s', password: process.env.%s_PASSWORD || '%s', name: '%s' }," % (handle, env, login, env, password, fullname))
546
+ additions.append(pad + "},")
547
+ print(' cucumber.js: the users registry (%s)' % ', '.join(r[0] for r in rows))
548
+ if additions:
549
+ src = src[:world.end()] + '\n' + '\n'.join(additions) + src[world.end():]
550
+ print(' cucumber.js: the managing account and the project of the steps')
551
+
552
+ # The address the suite opens when LAUNCH_URL is not set: the placeholder of the scaffold becomes the site's.
553
+ placeholder = "process.env.LAUNCH_URL || 'http://localhost:8080'"
554
+ if placeholder in src:
555
+ src = src.replace(placeholder, "process.env.LAUNCH_URL || '%s'" % address)
556
+ print(' cucumber.js: the address of the site (%s)' % address)
557
+
558
+ if src != before:
559
+ open(cucumber_file, 'w').write(src)
560
+ PYEOF
561
+
562
+ # A first feature, when the project has none for the site yet.
563
+ local feature="${WORKSPACE_SCRIPTS}/plugins/redmine/site.feature" ;
564
+ if [ -f "${feature}" ] && [ ! -f "${project_path}/tests/features/site.feature" ] ; then
565
+ mkdir -p "${project_path}/tests/features" && cp "${feature}" "${project_path}/tests/features/site.feature" \
566
+ && echo " tests/features/site.feature: a first feature for the issues" ;
567
+ fi
568
+
569
+ # The scenarios log in as the default accounts and read the sample project: make sure the site has them, once it
570
+ # answers again (installing the testing stack may restart the project, and Redmine with it).
571
+ if ( cd "${project_path}" && redmine_wait > /dev/null 2>&1 && add_redmine_users > /dev/null 2>&1 && redmine_site content < /dev/null > /dev/null 2>&1 ) ; then
572
+ echo " the default accounts and the sample content are on the site (${users[*]})" ;
573
+ else
574
+ echo " The default accounts could not be written: is the site running (ddev start in ${project_path})?" ;
575
+ fi
576
+ }
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # Redmine's own server on a runtime other than DDEV (Docker Compose): the web container is the official image with the
4
+ # workspace's layer, and runs this as the host user from the project's root (rt_serve) once the project's PostgreSQL
5
+ # takes connections. The image's own entrypoint writes the database settings from the site's values, migrates the
6
+ # database, then starts the server. Written by the Redmine builder of the workspace; build again rather than editing.
7
+
8
+ set -a ;
9
+ # shellcheck disable=SC1091
10
+ . "$(dirname "$0")/env" || exit 1 ;
11
+ set +a ;
12
+ cd /usr/src/redmine || exit 1 ;
13
+ exec /docker-entrypoint.sh rails server -b 0.0.0.0 -p 3000