@suffolklitlab/alkiln 4.11.6 → 5.0.0-experimental1

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.
@@ -23,9 +23,27 @@ module.exports = session_vars;
23
23
  // These default to null
24
24
  // TODO: How to isolate debug elsewhere so this file can use the logger?
25
25
  session_vars.get_debug = function () { return process.env.DEBUG || null; };
26
+ session_vars.is_dev_env = function () { return process.env.DEV || null; }
27
+
26
28
  session_vars.get_dev_api_key = function () { return process.env.DOCASSEMBLE_DEVELOPER_API_KEY || null; };
27
29
  session_vars.get_repo_url = function () { return process.env.REPO_URL || null; };
28
- session_vars.is_dev_env = function () { return process.env.DEV || null; }
30
+
31
+ session_vars.get_origin = function () {
32
+ /** The location from which the tests are being run. Possible
33
+ * valid return values: 'playground', 'github', or 'local'.
34
+ * Default is 'local' for ease of our own onboarding process.
35
+ */
36
+ return process.env._ORIGIN || 'local';
37
+ };
38
+ session_vars.get_user_project_name = function () { return process.env._PROJECT_NAME || null; };
39
+ session_vars.get_user_id = function () { return process.env._USER_ID || null; };
40
+ session_vars.get_tags = function () {
41
+ if ( process.env._TAGS ) {
42
+ return process.env._TAGS.split(` `);
43
+ } else {
44
+ return [];
45
+ }
46
+ };
29
47
 
30
48
  // More complex logic
31
49
  session_vars.get_server_reload_timeout = function () {
@@ -130,7 +148,10 @@ session_vars.save_project_name = function ( project_name ) {
130
148
  }; // Ends save_project_name()
131
149
 
132
150
  session_vars.get_project_name = function () {
133
- /* Get the name of the current docassemble Project */
151
+ /* Get the name of the current docassemble Project */
152
+ if ( session_vars.get_origin() === 'playground' ) {
153
+ return session_vars.get_user_project_name();
154
+ }
134
155
  let json = JSON.parse( fs.readFileSync( runtime_config_path ));
135
156
  let project_name = json[ project_name_key ] || null;
136
157
  if ( session_vars.get_debug() ) { console.log( `ALKiln debug: Project name from file is "${ project_name }".` ); }
@@ -174,7 +195,7 @@ session_vars.save_artifacts_path_name = function ( path_name ) {
174
195
  console.log( `ALKiln debug: Stored name of artifacts directory "${ path_name }" locally.` );
175
196
  }
176
197
  return file;
177
- }; // Ends get_project_name()
198
+ }; // Ends save_artifacts_path_name()
178
199
 
179
200
  session_vars.get_artifacts_path_name = function () {
180
201
  /* Get the name of the current artifacts path. */
@@ -187,48 +208,69 @@ session_vars.get_artifacts_path_name = function () {
187
208
  } catch ( error ) {
188
209
  return null;
189
210
  }
190
- }; // Ends get_project_name()
191
-
192
- /**
193
- * Ensure all required environment variables exist. Add each missing variable
194
- * to an error that will be thrown at the end.
195
- *
196
- * @throws ReferenceError with a message for all missing variables.
197
- */
198
- session_vars.validateEnvironment = function() {
199
- /** Throw a useful error for the developer if any of the required env vars
200
- * are missing. For most devs, these will be GitHub secrets. */
201
-
202
- // Note: We are checking for BASE_URL, but we are referring it as SERVER_URL as that
203
- // is the name used in GitHub secrets, which is what most devs will be using.
211
+ }; // Ends get_artifacts_path_name()
204
212
 
213
+ session_vars.validateEnvironment = function() {
214
+ /** Throw a useful error for the developer if any of the env vars
215
+ * required by absolutely all tests are missing. Gather all
216
+ * missing var names into one message to ease dev process.
217
+ *
218
+ * Rational for doing it all in here:
219
+ * 1. Easier to test
220
+ * 2. Will avoid piecemeal errors in separate locations
221
+ *
222
+ * TODO: Also validate format of variables?
223
+ *
224
+ * @throws ReferenceError with a message for all missing variables.
225
+ */
205
226
  let errorMessage = '';
206
227
 
207
- // Since this code will most often be run in GitHub, I'm going to build a single error message that outlines all missing variables. Otherwise, a developer
208
- // will have to run their pipeline an incredible number of times to discover all the missing variables.
209
- if ( !session_vars.get_dev_api_key() )
210
- {
211
- errorMessage += `\nThe DOCASSEMBLE_DEVELOPER_API_KEY GitHub secret must be defined. The DOCASSEMBLE_DEVELOPER_API_KEY is a docassemble API key you created for your server's testing account. It should have developer permissions.`
212
- }
213
- if ( !session_vars.get_da_server_url() )
214
- {
228
+ // Env vars required for any tests
229
+ if ( !session_vars.get_da_server_url() ) {
215
230
  errorMessage += `\nThe SERVER_URL GitHub secret must be defined. The SERVER_URL is the URL of the Docassemble Server where the tests should be run.`;
216
231
  }
217
- if ( !session_vars.get_repo_url() )
218
- {
219
- errorMessage += `\nThe REPO_URL GitHub secret must be defined. The REPO_URL is the URL to the Git repo whose Actions are being run.`;
220
- }
221
- // The branch name is always automatically defined in Github, it'll be missing only if you're running locally and missing an env var.
222
- if ( !session_vars.get_branch_name() )
223
- {
224
- errorMessage += `\nThe BRANCH_NAME environment variable must be defined. The BRANCH_NAME is the name of the repository branch that should be pulled into the docassemble project.`;
225
- }
232
+
233
+ let origin = session_vars.get_origin();
234
+ if ( origin !== 'github' && origin !== 'local' && origin !== 'playground' ) {
235
+ errorMessage += `\nThe _ORIGIN environment variable should be set to a valid value automatically. If you see this error, file an issue at https://github.com/suffolkLITLab/docassemble-ALKilnInThePlayground. It must be defined as either 'playground', 'github', or 'local', but its value was ${origin}`;
236
+
237
+ // If origin has one of the allowed values, we can do the rest of the validations
238
+ } else {
239
+
240
+ // Env vars required for github or local runs, as both create Projects
241
+ // in the Playground and pull the repo from GitHub
242
+ if ( origin === 'github' || origin === 'local' ) {
243
+ if ( !session_vars.get_dev_api_key() ) {
244
+ errorMessage += `\nThe DOCASSEMBLE_DEVELOPER_API_KEY GitHub secret must be defined. The DOCASSEMBLE_DEVELOPER_API_KEY is a docassemble API key you created for your server's testing account. It should have developer permissions.`
245
+ }
246
+ // Github automatically gives this, but may be missing locally.
247
+ if ( !session_vars.get_repo_url() ) {
248
+ errorMessage += `\nThe REPO_URL environment variable must be defined. The REPO_URL is the URL to the GitHub repo whose Actions are being run.`;
249
+ }
250
+ // Github automatically gives this, but may be missing locally.
251
+ if ( !session_vars.get_branch_name() ) {
252
+ errorMessage += `\nThe BRANCH_NAME environment variable must be defined. The BRANCH_NAME is the name of the repository branch that should be pulled into the docassemble project.`;
253
+ }
254
+ } // ends github and local env vars
255
+
256
+ // We're the ones ensuring these exist for Playground runs
257
+ if ( origin === 'playground' ) {
258
+ if ( !session_vars.get_user_project_name() ) {
259
+ errorMessage += `\nThe _PROJECT_NAME environment variable should automatically exist. If you see this error, file an issue at https://github.com/suffolkLITLab/docassemble-ALKilnInThePlayground. It is the name of the Project the developer chose to test.`
260
+ }
261
+ if ( !session_vars.get_user_id() ) {
262
+ errorMessage += `\nThe _USER_ID environment variable should automatically exist. If you see this error, file an issue at https://github.com/suffolkLITLab/docassemble-ALKilnInThePlayground. It is the user ID of the developer on the server.`
263
+ }
264
+ // Tags are optional
265
+ } // ends Playground env vars
266
+
267
+ } // ends validation based on origin
226
268
 
227
269
  errorMessage = errorMessage.trim();
228
270
 
229
271
  if (errorMessage !== '') {
230
- throw new ReferenceError(errorMessage);
272
+ throw new ReferenceError(`ALKiln error: ${errorMessage}`);
231
273
  }
232
274
  };
233
-
275
+
234
276
  session_vars.validateEnvironment();