-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathutils.js
More file actions
1339 lines (1221 loc) · 41.7 KB
/
utils.js
File metadata and controls
1339 lines (1221 loc) · 41.7 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { Site as SiteModel } from '@adobe/spacecat-shared-data-access';
import { Entitlement as EntitlementModel } from '@adobe/spacecat-shared-data-access/src/models/entitlement/index.js';
import { Config } from '@adobe/spacecat-shared-data-access/src/models/site/config.js';
import { ImsPromiseClient } from '@adobe/spacecat-shared-ims-client';
import URI from 'urijs';
import {
hasText,
tracingFetch as fetch,
isValidUrl,
isObject,
isNonEmptyObject,
resolveCanonicalUrl,
isValidIMSOrgId,
detectAEMVersion,
detectLocale,
wwwUrlResolver as sharedWwwUrlResolver,
getLastNumberOfWeeks,
} from '@adobe/spacecat-shared-utils';
import TierClient from '@adobe/spacecat-shared-tier-client';
import RUMAPIClient from '@adobe/spacecat-shared-rum-api-client';
import { iso6393 } from 'iso-639-3';
import worldCountries from 'world-countries';
import { SFNClient, StartExecutionCommand } from '@aws-sdk/client-sfn';
import {
STATUS_BAD_REQUEST,
} from '../utils/constants.js';
/**
* Step Functions execution names must be 1–80 chars and may only contain
* letters, numbers, hyphens, or underscores
* (see https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html).
* This helper enforces those constraints and falls back to a timestamped name
* when input is missing or becomes empty after sanitization.
*
* When the name follows the pattern "prefix-{content}-{timestamp}", it preserves
* the full timestamp by truncating the middle content portion instead of the end.
*
* @param {string} value - The execution name to sanitize.
* @returns {string} The sanitized execution name.
*/
export const sanitizeExecutionName = (value) => {
const sanitizedInput = (value || `agent-${Date.now()}`).replace(/[^A-Za-z0-9-_]/g, '');
const executionName = sanitizedInput.length > 0 ? sanitizedInput : `agent-${Date.now()}`;
if (executionName.length <= 80) {
return executionName;
}
// Check if the name ends with a timestamp (13-digit number preceded by dash)
const timestampMatch = executionName.match(/-(\d{13})$/);
if (timestampMatch) {
// Preserve the full timestamp, truncate the middle portion
const timestamp = timestampMatch[0]; // Includes the dash: -1768507714773
const maxPrefixLength = 80 - timestamp.length; // 80 - 14 = 66
const prefix = executionName.substring(0, maxPrefixLength);
return prefix + timestamp;
}
// No timestamp pattern found, just truncate to 80
return executionName.slice(0, 80);
};
/**
* Checks if the url parameter "url" equals "ALL".
* @param {string} url - URL parameter.
* @returns {boolean} True if url equals "ALL", false otherwise.
*/
export const isAuditForAllUrls = (url) => url.toUpperCase() === 'ALL';
/**
* Checks if the deliveryType parameter "deliveryType" equals "ALL".
* @param {string} deliveryType - deliveryType parameter.
* @returns {boolean} True if deliveryType equals "ALL", false otherwise.
*/
export const isAuditForAllDeliveryTypes = (deliveryType) => deliveryType.toUpperCase() === 'ALL';
/**
* Sends an audit message for a single URL.
*
* @param {Object} sqs - The SQS service object.
* @param {string} queueUrl - The SQS queue URL.
* @param {string} type - The type of audit.
* @param {Object} auditContext - The audit context object.
* @param {string} siteId - The site ID to audit.
* @param {string} [auditData] - Optional audit data.
* @returns {Promise} A promise representing the message sending operation.
*/
export const sendAuditMessage = async (
sqs,
queueUrl,
type,
auditContext,
siteId,
auditData,
) => sqs.sendMessage(queueUrl, {
type,
siteId,
auditContext,
data: auditData,
});
// todo: prototype - untested
/* c8 ignore start */
export const sendExperimentationCandidatesMessage = async (
sqs,
queueUrl,
url,
slackContext,
) => sqs.sendMessage(queueUrl, {
processingType: 'experimentation-candidates-desktop',
urls: [{ url }],
slackContext,
});
/* c8 ignore end */
export const sentRunScraperMessage = async (
sqs,
queueUrl,
jobId,
urls,
slackContext,
allowCache = false,
) => sqs.sendMessage(queueUrl, {
processingType: 'default',
allowCache,
jobId,
urls: [...urls],
slackContext,
});
// todo: prototype - untested
/* c8 ignore start */
/**
* Sends a message to run an import job to the provided SQS queue.
*
* @param {Object} sqs
* @param {string} queueUrl
* @param {string} importType
* @param {string} siteId
* @param {string} startDate
* @param {string} endDate
* @param {Object} slackContext
* @param {string} [pageUrl] - Optional page URL for the import
* @param {Object} [data] - Optional data object for import-specific data
*/
export const sendRunImportMessage = async (
sqs,
queueUrl,
importType,
siteId,
startDate,
endDate,
slackContext,
pageUrl = undefined,
data = undefined,
) => sqs.sendMessage(queueUrl, {
type: importType,
siteId,
startDate,
endDate,
slackContext,
pageUrl,
...(data && { data }),
});
export const triggerTrafficAnalysisBackfill = async (
siteId,
config,
slackContext,
context,
weeks = 5,
) => {
const weekYearPairs = getLastNumberOfWeeks(weeks || 52);
await Promise.all(
weekYearPairs.map(async ({ week, year }) => {
const { sqs } = context;
return sqs.sendMessage(config.getQueues().imports, {
type: 'traffic-analysis',
trigger: 'backfill',
siteId,
week,
year,
allowCache: false,
slackContext,
});
}),
);
};
export const sendAutofixMessage = async (
sqs,
queueUrl,
siteId,
opportunityId,
suggestionIds,
promiseToken,
variations,
action,
customData,
{ url } = {},
) => sqs.sendMessage(queueUrl, {
opportunityId,
siteId,
suggestionIds,
promiseToken,
variations,
action,
url,
...(customData && { customData }),
});
/* c8 ignore end */
export const sendInternalReportRunMessage = async (
sqs,
queueUrl,
ReportType,
slackContext,
) => sqs.sendMessage(queueUrl, {
type: ReportType,
slackContext,
});
/**
* Sends a message to run a global import job to the provided SQS queue.
* Global imports don't require a siteId - they run across all data.
*
* @param {Object} sqs - The SQS service object.
* @param {string} queueUrl - The SQS queue URL.
* @param {string} importType - The type of global import to run.
* @param {Object} slackContext - The Slack context for notifications.
*/
export const sendGlobalImportRunMessage = async (
sqs,
queueUrl,
importType,
slackContext,
) => sqs.sendMessage(queueUrl, {
type: importType,
slackContext,
});
export const sendReportTriggerMessage = async (
sqs,
queueUrl,
data,
ReportType,
) => sqs.sendMessage(queueUrl, {
type: ReportType,
data,
});
/**
* Sends audit messages for each URL.
*
* @param {Object} sqs - The SQS service object.
* @param {string} queueUrl - The SQS queue URL.
* @param {string} type - The type of audit.
* @param {Object} auditContext - The audit context object.
* @param {Array<string>} siteIDsToAudit - An array of site IDs to audit.
* @returns {Promise<string>} A promise that resolves to a status message.
*/
export const sendAuditMessages = async (
sqs,
queueUrl,
type,
auditContext,
siteIDsToAudit,
) => {
for (const siteId of siteIDsToAudit) {
// eslint-disable-next-line no-await-in-loop
await sendAuditMessage(sqs, queueUrl, type, auditContext, siteId);
}
return `Triggered ${type} audit for ${siteIDsToAudit.length > 1 ? `all ${siteIDsToAudit.length} sites` : siteIDsToAudit[0]}`;
};
/**
* Triggers an audit for a site.
* @param {Site} site - The site to audit.
* @param {string} auditType - The type of audit.
* @param {undefined|string} auditData - Optional audit data.
* @param {Object} slackContext - The Slack context object.
* @param {Object} lambdaContext - The Lambda context object.
* @return {Promise} - A promise representing the audit trigger operation.
*/
export const triggerAuditForSite = async (
site,
auditType,
auditData,
slackContext,
lambdaContext,
) => sendAuditMessage(
lambdaContext.sqs,
lambdaContext.env.AUDIT_JOBS_QUEUE_URL,
auditType,
{
slackContext: {
channelId: slackContext.channelId,
threadTs: slackContext.threadTs,
},
},
site.getId(),
auditData,
);
/**
* Triggers the A11y codefix flow for an existing opportunity.
* This sends a message to the audit worker to process the opportunity's suggestions
* and send them to Mystique for code fix processing.
*
* @param {Site} site - The site object.
* @param {string} opportunityId - The opportunity ID to process.
* @param {string} opportunityType - The opportunity type (e.g., 'a11y-assistive').
* @param {string|null} aggregationKey - Optional aggregation key to filter suggestions.
* @param {Object} slackContext - The Slack context object.
* @param {Object} lambdaContext - The Lambda context object.
* @return {Promise} - A promise representing the trigger operation.
*/
export const triggerA11yCodefixForOpportunity = async (
site,
opportunityId,
opportunityType,
aggregationKey,
slackContext,
lambdaContext,
) => sendAuditMessage(
lambdaContext.sqs,
lambdaContext.env.AUDIT_JOBS_QUEUE_URL,
'trigger:a11y-codefix',
{
slackContext: {
channelId: slackContext.channelId,
threadTs: slackContext.threadTs,
},
},
site.getId(),
{ opportunityId, opportunityType, aggregationKey },
);
// todo: prototype - untested
/* c8 ignore start */
export const triggerExperimentationCandidates = async (
url,
slackContext,
lambdaContext,
) => sendExperimentationCandidatesMessage(
lambdaContext.sqs,
lambdaContext.env.SCRAPING_JOBS_QUEUE_URL,
url,
{
channelId: slackContext.channelId,
threadTs: slackContext.threadTs,
},
);
/* c8 ignore end */
export const triggerScraperRun = async (
jobId,
urls,
slackContext,
lambdaContext,
allowCache = false,
) => sentRunScraperMessage(
lambdaContext.sqs,
lambdaContext.env.SCRAPING_JOBS_QUEUE_URL,
jobId,
urls,
{
channelId: slackContext.channelId,
threadTs: slackContext.threadTs,
},
allowCache,
);
// todo: prototype - untested
/* c8 ignore start */
/**
* Triggers an import run by sending a message to the SQS queue.
*
* @param {Object} config
* @param {string} importType
* @param {string} siteId
* @param {string} startDate
* @param {string} endDate
* @param {Object} slackContext
* @param {Object} lambdaContext
* @param {string} [pageUrl] - Optional page URL for the import
* @param {Object} [data] - Optional data object for import-specific data
*/
export const triggerImportRun = async (
config,
importType,
siteId,
startDate,
endDate,
slackContext,
lambdaContext,
pageUrl,
data,
) => sendRunImportMessage(
lambdaContext.sqs,
config.getQueues().imports,
importType,
siteId,
startDate,
endDate,
{
channelId: slackContext.channelId,
threadTs: slackContext.threadTs,
},
pageUrl,
data,
);
/* c8 ignore end */
export const triggerInternalReportRun = async (
config,
reportType,
slackContext,
lambdaContext,
) => sendInternalReportRunMessage(
lambdaContext.sqs,
config.getQueues().reports,
reportType,
{
channelId: slackContext.channelId,
threadTs: slackContext.threadTs,
},
);
/**
* Triggers a global import run (imports that don't require a siteId).
*
* @param {Object} config - The configuration object.
* @param {string} importType - The type of global import to run.
* @param {Object} slackContext - The Slack context for notifications.
* @param {Object} lambdaContext - The Lambda context with SQS service.
*/
export const triggerGlobalImportRun = async (
config,
importType,
slackContext,
lambdaContext,
) => sendGlobalImportRunMessage(
lambdaContext.sqs,
config.getQueues().imports,
importType,
{
channelId: slackContext.channelId,
threadTs: slackContext.threadTs,
},
);
/**
* Checks if a given URL corresponds to a Helix site.
* @param {string} url - The URL to check.
* @param {Object} [edgeConfig] - The optional edge configuration object.
* @param {string} edgeConfig.hlxVersion - The Helix version of the site
* @param {string} edgeConfig.cdnProdHostname - The CDN production hostname of the site
* @param {string} edgeConfig.rso - The Helix Ref/Site/Owner information
* @param {string} edgeConfig.rso.owner - The owner of the site
* @param {string} edgeConfig.rso.ref - The ref of the site
* @param {string} edgeConfig.rso.site - The name of the site
* @returns {Promise<{ isHelix: boolean, reason?: string }>} A Promise that resolves to an object
* containing the result of the Helix site check and an optional reason if it's not a Helix site.
*/
// todo: leverage the edgeConfig for alternate verification (e.g. due to bot protection)
// eslint-disable-next-line no-unused-vars
export async function isHelixSite(url, edgeConfig = {}) {
let resp;
try {
resp = await fetch(url);
} catch (e) {
return {
isHelix: false,
reason: `Cannot fetch the site due to ${e.message}`,
};
}
const dom = await resp.text();
const { status } = resp;
const headers = resp.headers.plain();
const containsHelixDom = /<header><\/header>\s*<main>\s*<div>/.test(dom);
if (!containsHelixDom) {
return {
isHelix: false,
// if the DOM is not in helix format, log the response status, headers and first 100 chars
// of <body> for debugging purposes
reason: `DOM is not in helix format. Status: ${status}. Response headers: ${JSON.stringify(headers)}. Body: ${dom.includes('<body>') ? dom.substring(dom.indexOf('<body>'), dom.indexOf('<body>') + 100) : ''}`,
};
}
return {
isHelix: true,
};
}
/**
* Checks if a given URL corresponds to an AEM site.
* @param {string} url - The URL to check.
* @returns {Promise<{ isAEM: boolean, reason: string }>} A Promise that resolves to an object
* containing the result of the AEM site check and a reason if it's not an AEM site.
*/
export async function isAEMSite(url) {
let pageContent;
try {
const response = await fetch(url);
pageContent = await response.text();
} catch (e) {
return {
isAEM: false,
reason: `Cannot fetch the site due to ${e.message}`,
};
}
const aemTokens = ['/content/dam/', '/etc.clientlibs', 'cq:template', 'sling.resourceType'];
const isAEM = aemTokens.some((token) => pageContent.includes(token));
return {
isAEM,
reason: 'Does not contain AEM paths or meta properties',
};
}
/**
* Finds the delivery type of the site, url of which is provided.
* @param {string} url - url of the site to find the delivery type for.
* @returns {Promise<"aem_edge" | "aem_cs" | "aem_ams" | "aem_headless" | "other">}
* A Promise that resolves to the delivery type of the site
*/
export async function findDeliveryType(url) {
let resp;
try {
resp = await fetch(url);
} catch (e) {
return SiteModel.DELIVERY_TYPES.OTHER;
}
return detectAEMVersion(await resp.text());
}
/**
* Error class with a status code property.
* @extends Error
*/
export class ErrorWithStatusCode extends Error {
constructor(message, status) {
super(message);
this.status = status;
}
}
export const wwwUrlResolver = (site) => {
const baseURL = site.getBaseURL();
const uri = new URI(baseURL);
return hasText(uri.subdomain()) ? baseURL.replace(/https?:\/\//, '') : baseURL.replace(/https?:\/\//, 'www.');
};
/**
* Resolves the correct hostname for a site by checking RUM data availability.
* Adapts wwwUrlResolver from shared-utils to work with api-service context.
* @param {object} site - The site object.
* @param {object} context - The context object.
* @returns {Promise<string>} - The resolved hostname without protocol.
*/
export async function resolveWwwUrl(site, context) {
const { log } = context;
const rumApiClient = RUMAPIClient.createFrom(context);
return sharedWwwUrlResolver(site, rumApiClient, log);
}
/**
* Get the IMS user token from the context.
* @param {object} context - The context of the request.
* @returns {string} imsUserToken - The IMS User access token.
* @throws {ErrorWithStatusCode} - If the Authorization header is missing.
*/
export function getImsUserToken(context) {
const authorizationHeader = context.pathInfo?.headers?.authorization;
const BEARER_PREFIX = 'Bearer ';
if (!hasText(authorizationHeader) || !authorizationHeader.startsWith(BEARER_PREFIX)) {
throw new ErrorWithStatusCode('Missing Authorization header', STATUS_BAD_REQUEST);
}
return authorizationHeader.substring(BEARER_PREFIX.length);
}
/**
* Get an IMS promise token from the authorization header in context.
* @param {object} context - The context of the request.
* @returns {Promise<{
* promise_token: string,
* expires_in: number,
* token_type: string,
* }>} - The promise token response.
* @throws {ErrorWithStatusCode} - If the Authorization header is missing.
*/
export async function getIMSPromiseToken(context) {
// get IMS promise token and attach to queue message
let userToken;
try {
userToken = await getImsUserToken(context);
} catch (e) {
throw new ErrorWithStatusCode('Missing Authorization header', STATUS_BAD_REQUEST);
}
const imsPromiseClient = ImsPromiseClient.createFrom(
context,
ImsPromiseClient.CLIENT_TYPE.EMITTER,
);
return imsPromiseClient.getPromiseToken(
userToken,
context.env?.AUTOFIX_CRYPT_SECRET && context.env?.AUTOFIX_CRYPT_SALT,
);
}
/**
* Get the access token from the Promise Token in authInfo of context.
* @param {object} context - The context of the request.
* @returns {string} accessToken - The access token.
* @throws {ErrorWithStatusCode} - If the promise token is missing.
*/
export async function getAccessToken(context) {
const imsClient = ImsPromiseClient.createFrom(
context,
ImsPromiseClient.CLIENT_TYPE.CONSUMER,
);
const authInfo = context.attributes?.authInfo;
const promiseToken = authInfo.getPromiseToken();
if (!promiseToken) {
throw new ErrorWithStatusCode('Missing promise token', STATUS_BAD_REQUEST);
}
const accessToken = await imsClient.exchangeToken(
promiseToken,
!!context.env?.AUTOFIX_CRYPT_SECRET && !!context.env?.AUTOFIX_CRYPT_SALT,
);
return accessToken;
}
/**
* Build an S3 prefix for site content files.
* @param {string} type - The type of content (e.g., 'scrapes', 'imports', 'accessibility').
* @param {string} siteId - The site ID.
* @param {string} [path] - Optional sub-path.
* @returns {string} The S3 prefix string.
*/
export function buildS3Prefix(type, siteId, path = '') {
const normalized = path ? `${path.replace(/^\/+/g, '').replace(/\/+$/g, '')}/` : '';
return `${type}/${siteId}/${normalized}`;
}
/**
* Checks if an import type is already enabled for a site.
*
* @param {string} importType - The import type to check.
* @param {Array} imports - Array of import configurations.
* @returns {boolean} - True if import is enabled, false otherwise.
*/
const isImportEnabled = (importType, imports) => {
const foundImport = imports?.find((importConfig) => importConfig.type === importType);
// If import is found, check if it's enabled, otherwise assume it's not enabled (false)
return foundImport ? foundImport.enabled : false;
};
/**
* Derives a project name from a base URL.
*
* @param {string} baseURL - The base URL
* @returns {string} The derived project name.
*/
export const deriveProjectName = (baseURL) => {
const parsedBaseURL = new URL(baseURL);
const { hostname } = parsedBaseURL;
// Split hostname by dots, if it has 3 or more parts, we assume it has a subdomain.
const parts = hostname.split('.');
if (parts.length <= 2) {
return hostname;
}
// Remove parts of the subdomain which are 2 or 3 letters long, max first two elements
for (let i = 0; i < Math.min(parts.length, 2); i += 1) {
const part = parts[i];
if (part.length === 2 || part.length === 3) {
parts[i] = null;
}
}
return parts.filter(Boolean).join('.');
};
/**
* Creates a new project if it does not exist yet.
*
* @param {Object} context - The Lambda context object.
* @param {Object} slackContext - The Slack context object.
* @param {string} baseURL - The base URL of the site.
* @param {string} projectId - The project ID.
* @returns {Promise<Object>} - The project object.
*/
export const createProject = async (
context,
slackContext,
baseURL,
organizationId,
projectId,
) => {
const { dataAccess, log } = context;
const { say } = slackContext;
const { Project } = dataAccess;
try {
const projectName = deriveProjectName(baseURL);
// Find existing project if project id is provided
let existingProject;
if (projectId) {
const project = await Project.findById(projectId);
if (project) {
existingProject = project;
}
}
// Find existing project in the same org with the same name
if (!existingProject) {
const foundProject = (await Project.allByOrganizationId(organizationId))
.find((p) => p.getProjectName() === projectName);
if (foundProject) {
existingProject = foundProject;
}
}
if (existingProject) {
const message = `:information_source: Added site ${baseURL} to existing project ${existingProject.getProjectName()}. Project ID: ${existingProject.getId()}`;
await say(message);
return existingProject;
}
// Otherwise create new project
const newProject = await Project.create({ projectName, organizationId });
const message = `:information_source: Added site ${baseURL} to new project ${newProject.getProjectName()}. Project ID: ${newProject.getId()}`;
await say(message);
return newProject;
} catch (error) {
log.error(`Error creating project: ${error.message}`);
await say(`:x: Error creating project: ${error.message}`);
throw error;
}
};
/**
* Creates or retrieves a site and its associated organization.
*
* @param {string} baseURL - The site base URL.
* @param {string} imsOrgID - The IMS Organization ID.
* @param {string} authoringType - The authoring type of the site.
* @param {string} customDeliveryType - The delivery type of the site.
* @param {Object} slackContext - The Slack context object.
* @param {Object} reportLine - The report line object to update.
* @param {Object} context - The Lambda context containing dataAccess, log, etc.
* @param {Object} deliveryConfig - Optional delivery config to set on the site.
* @returns {Promise<Object>} - Object containing the site and organizationId.
*/
const createSiteAndOrganization = async (
baseURL,
imsOrgID,
authoringType,
customDeliveryType,
slackContext,
reportLine,
context,
deliveryConfig,
) => {
const { imsClient, dataAccess, log } = context;
const { Site, Organization } = dataAccess;
const { say } = slackContext;
// Create a local copy to avoid modifying the parameter directly
const localReportLine = { ...reportLine };
let site = await Site.findByBaseURL(baseURL);
let organizationId;
if (site) {
const siteOrgId = site.getOrganizationId();
organizationId = siteOrgId; // Set organizationId for existing sites
const message = `:information_source: Site ${baseURL} already exists. Organization ID: ${siteOrgId}`;
await say(message);
} else {
// Check if the organization with IMS Org ID already exists; create if it doesn't
let organization = await Organization.findByImsOrgId(imsOrgID);
if (!organization) {
const imsOrgDetails = await imsClient.getImsOrganizationDetails(imsOrgID);
if (!imsOrgDetails) {
localReportLine.errors = `Could not find details of IMS org with the ID *${imsOrgID}*.`;
localReportLine.status = 'Failed';
throw new Error(localReportLine.errors);
}
organization = await Organization.create({
name: imsOrgDetails.orgName,
imsOrgId: imsOrgID,
});
const message = `:white_check_mark: A new organization has been created. Organization ID: ${organization.getId()} Organization name: ${organization.getName()} IMS Org ID: ${imsOrgID}.`;
await say(message);
}
organizationId = organization.getId();
localReportLine.spacecatOrgId = organizationId;
const deliveryType = customDeliveryType || await findDeliveryType(baseURL);
localReportLine.deliveryType = deliveryType;
const isLive = deliveryType === SiteModel.DELIVERY_TYPES.AEM_EDGE;
try {
site = await Site.create({
baseURL, deliveryType, isLive, organizationId, authoringType,
});
} catch (error) {
log.error(`Error creating site: ${error.message}`);
localReportLine.errors = error.message;
localReportLine.status = 'Failed';
await say(`:x: *Errors:* ${error.message}`);
throw error;
}
}
// Set deliveryConfig and authoringType if provided (will be saved later with other site data)
if (deliveryConfig && Object.keys(deliveryConfig).length > 0) {
site.setDeliveryConfig(deliveryConfig);
if (authoringType) {
site.setAuthoringType(authoringType);
}
await say(':white_check_mark: DeliveryConfig is added/updated to site configuration');
}
Object.assign(reportLine, localReportLine);
return { site, organizationId };
};
/**
* Creates an entitlement and enrollment for a site.
*
* @param {Site} site - The site to create an entitlement and enrollment for.
* @param {Object} lambdaCtx - The Lambda context.
* @param {Object} slackCtx - The Slack context.
* @param {Object} reportLine - The report line object to update.
* @param {string} productCode - The product code to create an entitlement for.
* @param {string} tier - The tier to create an entitlement for.
* @returns {Promise<Object>} - The entitlement and site enrollment.
*/
export const createEntitlementAndEnrollment = async (
site,
lambdaCtx,
slackCtx,
reportLine,
productCode,
tier,
) => {
const { log } = lambdaCtx;
const { say } = slackCtx;
// Create a local copy to avoid modifying the parameter directly
const localReportLine = { ...reportLine };
try {
const tierClient = await TierClient.createForSite(lambdaCtx, site, productCode);
const { entitlement, siteEnrollment } = await tierClient.createEntitlement(tier);
log.info(`Successfully created ${productCode} entitlement ${entitlement.getId()} (${tier}) and enrollment ${siteEnrollment.getId()} for site ${site.getId()}`);
const message = `:white_check_mark: A new ${productCode} entitlement ${entitlement.getId()} (${tier}) and enrollment ${siteEnrollment.getId()} has been created for site ${site.getId()}`;
await say(message);
return {
entitlement,
siteEnrollment,
};
} catch (error) {
log.error(`Creating ${productCode} entitlement and enrollment failed: ${error.message}`);
await say(`❌ Creating ${productCode} entitlement and site enrollment failed`);
localReportLine.errors = `Creating ${productCode} entitlement and site enrollment failed`;
localReportLine.status = 'Failed';
throw error;
}
};
/**
* Shared onboarding function used by both modal and command implementations.
*
* @param {string} baseURLInput - The site URL input
* @param {string} imsOrganizationID - The IMS Organization ID
* @param {Object} configuration - The configuration object
* @param {Object} profile - The loaded profile configuration object
* @param {number} workflowWaitTime - Workflow wait time in seconds
* @param {Object} slackContext - Slack context object with say function
* @param {Object} context - Lambda context containing dataAccess, log, etc.
* @param {Object} additionalParams - Additional parameters
* @param {string} additionalParams.tier - Entitlement tier
* @param {Object} options - Additional options
* @param {Function} options.urlProcessor - Function to process the URL
* (e.g., extractURLFromSlackInput)
* @param {string} options.profileName - The profile name for logging and reporting
* @returns {Promise<Object>} Report line object
*/
export const onboardSingleSite = async (
baseURLInput,
imsOrganizationID,
configuration,
profile,
workflowWaitTime,
slackContext,
context,
additionalParams = {},
options = {},
) => {
const { say } = slackContext;
const {
dataAccess, log, env,
} = context;
const { Configuration } = dataAccess;
const sfnClient = new SFNClient();
const baseURL = options.urlProcessor ? options.urlProcessor(baseURLInput) : baseURLInput.trim();
const imsOrgID = imsOrganizationID || env.DEMO_IMS_ORG;
const profileName = options.profileName || 'unknown';
const tier = additionalParams.tier || EntitlementModel.TIERS.FREE_TRIAL;
await say(`:gear: Starting environment setup for site ${baseURL} with imsOrgID: ${imsOrgID} and tier: ${tier} using the ${profileName} profile`);
await say(':key: Please make sure you have access to the AEM Shared Production Demo environment. Request access here: https://demo.adobe.com/demos/internal/AemSharedProdEnv.html');
const reportLine = {
site: baseURL,
imsOrgId: imsOrgID,
spacecatOrgId: '',
siteId: '',
profile: profileName,
deliveryType: additionalParams.deliveryType || '',
authoringType: additionalParams.authoringType || '',
imports: '',
audits: '',
errors: '',
status: 'Success',
existingSite: 'No',
tier,
};
try {
if (!isValidUrl(baseURL)) {
reportLine.errors = 'Invalid site base URL';
reportLine.status = 'Failed';
log.error(`Invalid site base URL: ${baseURL}`);
await say(`:x: Invalid site base URL: ${baseURL}`);
return reportLine;
}
if (!isValidIMSOrgId(imsOrgID)) {
reportLine.errors = 'Invalid IMS Org ID';
reportLine.status = 'Failed';
log.error(`Invalid IMS Org ID: ${imsOrgID}`);
await say(`:x: Invalid IMS Org ID: ${imsOrgID}`);
return reportLine;
}
let language = additionalParams.language?.toLowerCase();
let region = additionalParams.region?.toUpperCase();
const languageValid = language && !!iso6393.find((lang) => lang.iso6301 === language);
const regionValid = region && !!worldCountries.find(
(c) => c.cca2.toLowerCase() === region.toLowerCase(),
);
// Auto-detect locale if language and/or region is not provided
if (!languageValid || !regionValid) {
try {
const locale = await detectLocale({ baseUrl: baseURL });
if (!language && locale.language) {
language = locale.language;
}
if (!region && locale.region) {
region = locale.region;
}
} catch (error) {
log.error(`Error detecting locale for site ${baseURL}: ${error.message}`);
await say(`:x: Error detecting locale for site ${baseURL}: ${error.message}`);