|
| 1 | +import { EnvContext, RunMode } from "../Context" |
| 2 | +import { EnvProvider } from "../EnvProvider" |
| 3 | +import { |
| 4 | + ConflictStrategy, |
| 5 | + DriveUtils, |
| 6 | + FileContent, |
| 7 | +} from "../adapter/GDriveAdapter" |
| 8 | + |
| 9 | +export type FileConfig = { |
| 10 | + name: string |
| 11 | + type: string |
| 12 | + filename: string |
| 13 | + ref: string |
| 14 | + destFolder: string |
| 15 | +} |
| 16 | + |
| 17 | +export type E2EConfig = { |
| 18 | + globals: { |
| 19 | + repoBaseUrl: string |
| 20 | + subjectPrefix: string |
| 21 | + to: string |
| 22 | + } |
| 23 | + folders: { |
| 24 | + name: string |
| 25 | + location: string |
| 26 | + }[] |
| 27 | + files: FileConfig[] |
| 28 | + mails: { |
| 29 | + name: string |
| 30 | + subject: string |
| 31 | + htmlBody: string |
| 32 | + files: string[] |
| 33 | + }[] |
| 34 | +} |
| 35 | + |
| 36 | +export class E2E { |
| 37 | + public static getBlobFromFileEntry( |
| 38 | + config: E2EConfig, |
| 39 | + file: FileConfig, |
| 40 | + ): GoogleAppsScript.Base.Blob | undefined { |
| 41 | + let blob |
| 42 | + switch (file.type) { |
| 43 | + case "repo": { |
| 44 | + const url = `${config.globals.repoBaseUrl}/${file.ref}` |
| 45 | + console.log(`Fetching repo file from ${url} ...`) |
| 46 | + blob = UrlFetchApp.fetch(url).getBlob() |
| 47 | + break |
| 48 | + } |
| 49 | + case "url": |
| 50 | + console.log(`Fetching URL file from ${file.ref} ...`) |
| 51 | + blob = UrlFetchApp.fetch(file.ref).getBlob() |
| 52 | + break |
| 53 | + case "gdrive": |
| 54 | + console.log(`Fetching GDrive file from ${file.ref} ...`) |
| 55 | + blob = DriveApp.getFileById(file.ref).getBlob() |
| 56 | + break |
| 57 | + } |
| 58 | + return blob |
| 59 | + } |
| 60 | + |
| 61 | + public static initMails(ctx: EnvContext, config: E2EConfig) { |
| 62 | + ctx.log.info("GMail initialization started.") |
| 63 | + config.mails.forEach((mail) => { |
| 64 | + const files: string[] = mail.files ?? [] |
| 65 | + const attachments: GoogleAppsScript.Base.Blob[] = files.map((name) => { |
| 66 | + const file = config.files.reduce((prev, curr) => |
| 67 | + name === curr.name ? curr : prev, |
| 68 | + ) |
| 69 | + return this.getBlobFromFileEntry(config, file) |
| 70 | + }) as GoogleAppsScript.Base.Blob[] |
| 71 | + ctx.env.mailApp.sendEmail({ |
| 72 | + to: config.globals.to, |
| 73 | + subject: `${config.globals.subjectPrefix}${mail.subject}`, |
| 74 | + htmlBody: mail.htmlBody, |
| 75 | + attachments: attachments, |
| 76 | + }) |
| 77 | + }) |
| 78 | + ctx.log.info("GMail initialization finished.") |
| 79 | + } |
| 80 | + |
| 81 | + public static initDrive(ctx: EnvContext, config: E2EConfig) { |
| 82 | + ctx.log.info("GDrive initialization started.") |
| 83 | + config.files |
| 84 | + .filter((file) => file.destFolder !== undefined) |
| 85 | + .forEach((file) => { |
| 86 | + const blob = this.getBlobFromFileEntry(config, file) |
| 87 | + if (!blob) return |
| 88 | + const folderLocation = config.folders.reduce((prev, current) => |
| 89 | + current.name === file.destFolder ? current : prev, |
| 90 | + ) |
| 91 | + DriveUtils.createFile( |
| 92 | + ctx, |
| 93 | + `${folderLocation.location}/${file.filename}`, |
| 94 | + new FileContent(blob), |
| 95 | + ConflictStrategy.REPLACE, |
| 96 | + ) |
| 97 | + }) |
| 98 | + ctx.log.info("GDrive initialization finished.") |
| 99 | + } |
| 100 | + |
| 101 | + public static initAll( |
| 102 | + config: E2EConfig, |
| 103 | + ctx: EnvContext = EnvProvider.defaultContext(RunMode.DANGEROUS), |
| 104 | + ) { |
| 105 | + ctx.log.info("E2E initialization started.") |
| 106 | + this.initDrive(ctx, config) |
| 107 | + this.initMails(ctx, config) |
| 108 | + ctx.log.info("E2E initialization finished.") |
| 109 | + } |
| 110 | +} |
0 commit comments