@silexlabs/silex 3.0.0-alpha.49 → 3.0.0-alpha.50

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.
@@ -33,8 +33,9 @@ export interface GitlabOptions {
33
33
  clientSecret: string
34
34
  branch: string
35
35
  assetsFolder: string
36
- metaRepo: string
37
- metaRepoFile: string
36
+ //metaRepo: string
37
+ //metaRepoFile: string
38
+ repoPrefix: string
38
39
  }
39
40
 
40
41
  interface GitlabToken {
@@ -120,8 +121,9 @@ export default class GitlabConnector implements StorageConnector {
120
121
  this.options = {
121
122
  branch: 'main',
122
123
  assetsFolder: 'assets',
123
- metaRepo: 'silex-meta',
124
- metaRepoFile: 'websites.json',
124
+ //metaRepo: 'silex-meta',
125
+ //metaRepoFile: 'websites.json',
126
+ repoPrefix: 'silex_',
125
127
  ...opts,
126
128
  } as GitlabOptions
127
129
  if(!this.options.clientId) throw new Error('Missing Gitlab client ID')
@@ -162,28 +164,28 @@ export default class GitlabConnector implements StorageConnector {
162
164
  * Get the meta repo path for the current user
163
165
  * The meta repo contains a JSON file which contains the list of websites
164
166
  */
165
- private getMetaRepoPath(session: GitlabSession): string {
166
- if(!session.gitlab?.username) throw new ApiError('Missing Gitlab user ID. User not logged in?', 401)
167
- return encodeURIComponent(`${session.gitlab.username}/${this.options.metaRepo}`)
168
- }
169
-
170
- /**
171
- * Initialize the storage with a meta repo
172
- */
173
- private async initStorage(session: GitlabSession): Promise<void> {
174
- // Create the meta repo
175
- try {
176
- const project = await this.callApi(session, 'api/v4/projects/', 'POST', {
177
- name: this.options.metaRepo,
178
- }) as any
179
- return this.createFile(session, this.getMetaRepoPath(session), this.options.metaRepoFile, JSON.stringify({
180
- websites: {}
181
- } as MetaRepoFileContent))
182
- } catch (e) {
183
- console.error('Could not init storage', e.statusCode, e.httpStatusCode, e)
184
- throw e
185
- }
186
- }
167
+ //private getMetaRepoPath(session: GitlabSession): string {
168
+ // if(!session.gitlab?.username) throw new ApiError('Missing Gitlab user ID. User not logged in?', 401)
169
+ // return encodeURIComponent(`${session.gitlab.username}/${this.options.metaRepo}`)
170
+ //}
171
+
172
+ ///**
173
+ // * Initialize the storage with a meta repo
174
+ // */
175
+ //private async initStorage(session: GitlabSession): Promise<void> {
176
+ // // Create the meta repo
177
+ // try {
178
+ // const project = await this.callApi(session, 'api/v4/projects/', 'POST', {
179
+ // name: this.options.metaRepo,
180
+ // }) as any
181
+ // return this.createFile(session, this.getMetaRepoPath(session), this.options.metaRepoFile, JSON.stringify({
182
+ // websites: {}
183
+ // } as MetaRepoFileContent))
184
+ // } catch (e) {
185
+ // console.error('Could not init storage', e.statusCode, e.httpStatusCode, e)
186
+ // throw e
187
+ // }
188
+ //}
187
189
 
188
190
  /**
189
191
  * Call the Gitlab API with the user's token and handle errors
@@ -368,36 +370,38 @@ export default class GitlabConnector implements StorageConnector {
368
370
  }
369
371
 
370
372
  async listWebsites(session: GitlabSession): Promise<WebsiteMeta[]> {
371
- try {
372
- const result = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'GET', null, {
373
- ref: this.options.branch,
374
- })
375
- const { content } = result
376
- const contentDecoded = Buffer.from(content, 'base64').toString('utf8')
377
- const websites = (JSON.parse(contentDecoded) as MetaRepoFileContent).websites
378
- return Object.entries(websites).map(([websiteId, {meta, createdAt, updatedAt}]) => ({
379
- websiteId,
380
- createdAt: new Date(createdAt),
381
- updatedAt: new Date(updatedAt),
382
- ...meta,
373
+ //try {
374
+ // const result = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'GET', null, {
375
+ // ref: this.options.branch,
376
+ // })
377
+ // const { content } = result
378
+ // const contentDecoded = Buffer.from(content, 'base64').toString('utf8')
379
+ // const websites = (JSON.parse(contentDecoded) as MetaRepoFileContent).websites
380
+ // return Object.entries(websites).map(([websiteId, {meta, createdAt, updatedAt}]) => ({
381
+ // websiteId,
382
+ // createdAt: new Date(createdAt),
383
+ // updatedAt: new Date(updatedAt),
384
+ // ...meta,
385
+ // }))
386
+ //} catch (e) {
387
+ // console.error('Could not list websites', e.statusCode, e.httpStatusCode, e.code)
388
+ // if (e.statusCode === 404 || e.httpStatusCode === 404) {
389
+ // await this.initStorage(session)
390
+ // return []
391
+ // } else {
392
+ // throw e
393
+ // }
394
+ //}
395
+ const projects = await this.callApi(session, `api/v4/users/${session.gitlab?.userId}/projects`) as any[]
396
+ return projects
397
+ .filter(p => p.name.startsWith(this.options.repoPrefix))
398
+ .map(p => ({
399
+ websiteId: p.id,
400
+ name: p.name.replace(this.options.repoPrefix, ''),
401
+ createdAt: p.created_at,
402
+ updatedAt: p.last_activity_at,
403
+ connectorUserSettings: {},
383
404
  }))
384
- } catch (e) {
385
- console.error('Could not list websites', e.statusCode, e.httpStatusCode, e.code)
386
- if (e.statusCode === 404 || e.httpStatusCode === 404) {
387
- await this.initStorage(session)
388
- return []
389
- } else {
390
- throw e
391
- }
392
- }
393
- //const projects = await this.callApi(session, `api/v4/users/${session.gitlab?.user?.id}/projects`) as any[]
394
- //return projects.map(p => ({
395
- // websiteId: p.id,
396
- // name: p.name,
397
- // createdAt: p.created_at,
398
- // updatedAt: p.last_activity_at,
399
- // connectorUserSettings: {},
400
- //}))
401
405
  }
402
406
 
403
407
  async readWebsite(session: GitlabSession, websiteId: string): Promise<WebsiteData> {
@@ -412,12 +416,12 @@ export default class GitlabConnector implements StorageConnector {
412
416
 
413
417
  async createWebsite(session: GitlabSession, websiteMeta: WebsiteMetaFileContent): Promise<WebsiteId> {
414
418
  const project = await this.callApi(session, 'api/v4/projects/', 'POST', {
415
- name: websiteMeta.name,
419
+ name: this.options.repoPrefix + websiteMeta.name,
416
420
  }) as any
417
421
  await this.createFile(session, project.id, WEBSITE_DATA_FILE, JSON.stringify({} as WebsiteData))
418
- //await this.createFile(session, project.id, WEBSITE_META_DATA_FILE, JSON.stringify(websiteMeta))
422
+ await this.createFile(session, project.id, WEBSITE_META_DATA_FILE, JSON.stringify(websiteMeta))
419
423
  //await this.updateWebsite(session, project.id, {} as WebsiteData)
420
- await this.setWebsiteMeta(session, project.id, websiteMeta)
424
+ //await this.setWebsiteMeta(session, project.id, websiteMeta)
421
425
  return project.id
422
426
  }
423
427
 
@@ -434,73 +438,84 @@ export default class GitlabConnector implements StorageConnector {
434
438
  async deleteWebsite(session: GitlabSession, websiteId: WebsiteId): Promise<void> {
435
439
  // Delete repo
436
440
  await this.callApi(session, `api/v4/projects/${websiteId}`, 'DELETE')
437
- // Load the meta repo data
438
- const file = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'GET', null, {
439
- ref: this.options.branch,
440
- })
441
- const metaRepo = JSON.parse(Buffer.from(file.content, 'base64').toString('utf8')) as MetaRepoFileContent
442
- const data = metaRepo.websites[websiteId]
443
- if(!data) throw new ApiError(`Website ${websiteId} not found`, 404)
444
- // Update or create the website meta data
445
- delete metaRepo.websites[websiteId]
446
- // Save the meta repo data
447
- const project = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'PUT', {
448
- branch: this.options.branch,
449
- commit_message: `Delete meta data of ${data.meta.name} (${websiteId}) from Silex`,
450
- content: JSON.stringify(metaRepo),
451
- file_path: this.options.metaRepoFile,
452
- })
441
+ //// Load the meta repo data
442
+ //const file = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'GET', null, {
443
+ // ref: this.options.branch,
444
+ //})
445
+ //const metaRepo = JSON.parse(Buffer.from(file.content, 'base64').toString('utf8')) as MetaRepoFileContent
446
+ //const data = metaRepo.websites[websiteId]
447
+ //if(!data) throw new ApiError(`Website ${websiteId} not found`, 404)
448
+ //// Update or create the website meta data
449
+ //delete metaRepo.websites[websiteId]
450
+ //// Save the meta repo data
451
+ //const project = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'PUT', {
452
+ // branch: this.options.branch,
453
+ // commit_message: `Delete meta data of ${data.meta.name} (${websiteId}) from Silex`,
454
+ // content: JSON.stringify(metaRepo),
455
+ // file_path: this.options.metaRepoFile,
456
+ //})
453
457
  }
454
458
 
455
459
  async getWebsiteMeta(session: GitlabSession, websiteId: WebsiteId): Promise<WebsiteMeta> {
456
- const file = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'GET', null, {
460
+ //const file = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'GET', null, {
461
+ // ref: this.options.branch,
462
+ //})
463
+ //const metaRepo = JSON.parse(Buffer.from(file.content, 'base64').toString('utf8')) as MetaRepoFileContent
464
+ //if(!metaRepo.websites[websiteId]) throw new ApiError(`Website ${websiteId} not found`, 404)
465
+ //return {
466
+ // websiteId,
467
+ // createdAt: new Date(metaRepo.websites[websiteId].createdAt),
468
+ // updatedAt: new Date(metaRepo.websites[websiteId].updatedAt),
469
+ // ...metaRepo.websites[websiteId].meta,
470
+ //}
471
+ const project = await this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${WEBSITE_META_DATA_FILE}`, 'GET', null, {
457
472
  ref: this.options.branch,
458
- })
459
- const metaRepo = JSON.parse(Buffer.from(file.content, 'base64').toString('utf8')) as MetaRepoFileContent
460
- if(!metaRepo.websites[websiteId]) throw new ApiError(`Website ${websiteId} not found`, 404)
473
+ }) as any
461
474
  return {
462
- websiteId,
463
- createdAt: new Date(metaRepo.websites[websiteId].createdAt),
464
- updatedAt: new Date(metaRepo.websites[websiteId].updatedAt),
465
- ...metaRepo.websites[websiteId].meta,
475
+ websiteId: project.id,
476
+ name: project.name,
477
+ createdAt: project.created_at,
478
+ updatedAt: project.last_activity_at,
479
+ connectorUserSettings: {},
466
480
  }
467
- //const project = await this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${WEBSITE_META_DATA_FILE}`) as any
468
- //return {
469
- // websiteId: project.id,
470
- // name: project.name,
471
- // createdAt: project.created_at,
472
- // updatedAt: project.last_activity_at,
473
- // connectorUserSettings: {},
474
- //}
475
481
  }
476
482
 
477
483
  async setWebsiteMeta(session: GitlabSession, websiteId: WebsiteId, websiteMeta: WebsiteMetaFileContent): Promise<void> {
478
- // Load the meta repo data
479
- const file = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'GET', null, {
480
- ref: this.options.branch,
481
- })
482
- const metaRepo = JSON.parse(Buffer.from(file.content, 'base64').toString('utf8')) as MetaRepoFileContent
483
- // Update or create the website meta data
484
- metaRepo.websites[websiteId] = {
485
- updatedAt: new Date().toISOString(),
486
- createdAt: metaRepo.websites[websiteId]?.createdAt ?? new Date().toISOString(),
487
- meta: websiteMeta,
484
+ //// Load the meta repo data
485
+ //const file = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'GET', null, {
486
+ // ref: this.options.branch,
487
+ //})
488
+ //const metaRepo = JSON.parse(Buffer.from(file.content, 'base64').toString('utf8')) as MetaRepoFileContent
489
+ //// Update or create the website meta data
490
+ //metaRepo.websites[websiteId] = {
491
+ // updatedAt: new Date().toISOString(),
492
+ // createdAt: metaRepo.websites[websiteId]?.createdAt ?? new Date().toISOString(),
493
+ // meta: websiteMeta,
494
+ //}
495
+ //// Save the meta repo data
496
+ //const project = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'PUT', {
497
+ // branch: this.options.branch,
498
+ // commit_message: `Update website meta data of ${websiteMeta.name} (${websiteId}) from Silex`,
499
+ // content: JSON.stringify(metaRepo),
500
+ // file_path: this.options.metaRepoFile,
501
+ //})
502
+
503
+ // Rename the repo if needed
504
+ const oldMeta = await this.getWebsiteMeta(session, websiteId)
505
+ if(websiteMeta.name !== oldMeta.name) {
506
+ await this.callApi(session, `api/v4/projects/${websiteId}`, 'PUT', {
507
+ name: this.options.repoPrefix + websiteMeta.name,
508
+ })
488
509
  }
489
- // Save the meta repo data
490
- const project = await this.callApi(session, `api/v4/projects/${this.getMetaRepoPath(session)}/repository/files/${this.options.metaRepoFile}`, 'PUT', {
510
+
511
+ // Update the metadata file
512
+ await this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${WEBSITE_META_DATA_FILE}`, 'PUT', {
491
513
  branch: this.options.branch,
492
- commit_message: `Update website meta data of ${websiteMeta.name} (${websiteId}) from Silex`,
493
- content: JSON.stringify(metaRepo),
494
- file_path: this.options.metaRepoFile,
514
+ commit_message: 'Update website meta data from Silex',
515
+ content: JSON.stringify(websiteMeta),
516
+ file_path: WEBSITE_META_DATA_FILE,
517
+ id: websiteId,
495
518
  })
496
-
497
- //const project = await this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${WEBSITE_META_DATA_FILE}`, 'PUT', {
498
- // branch: this.options.branch,
499
- // commit_message: 'Update website meta data from Silex',
500
- // content: JSON.stringify(websiteMeta),
501
- // file_path: WEBSITE_META_DATA_FILE,
502
- // id: websiteId,
503
- //})
504
519
  }
505
520
 
506
521
  async writeAssets(session: GitlabSession, websiteId: string, files: ConnectorFile[], status?: StatusCallback | undefined): Promise<void> {