|
| 1 | +/** |
| 2 | + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH |
| 3 | + * under one or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information regarding copyright |
| 5 | + * ownership. |
| 6 | + * |
| 7 | + * Camunda licenses this file to you under the MIT; you may not use this file |
| 8 | + * except in compliance with the MIT License. |
| 9 | + */ |
| 10 | + |
| 11 | +import { writeFileSync as writeFile } from 'fs'; |
| 12 | +import { resolve as pathResolve } from 'path'; |
| 13 | + |
| 14 | +const URL = 'https://marketplace.cloud.camunda.io/api/v1/ootb-connectors'; |
| 15 | + |
| 16 | +const templates = await fetchTemplates(); |
| 17 | + |
| 18 | +writeFile( |
| 19 | + pathResolve('packages/zeebe-element-templates-json-schema/test/integration/ootb-connector-templates.json'), |
| 20 | + JSON.stringify(templates, null, 2) |
| 21 | +); |
| 22 | + |
| 23 | +/** |
| 24 | + * Fetch OOTB connector templates from the Camunda Marketplace. |
| 25 | + * |
| 26 | + * @returns {Promise<Template[]>} templates |
| 27 | + */ |
| 28 | +async function fetchTemplates() { |
| 29 | + console.info('Fetching and updating templates'); |
| 30 | + |
| 31 | + let response = await fetch(URL); |
| 32 | + |
| 33 | + if (!response.ok) { |
| 34 | + console.warn(`Failed to fetch templates from ${ URL } (HTTP ${ response.status })`); |
| 35 | + |
| 36 | + return []; |
| 37 | + } |
| 38 | + |
| 39 | + /** @type {TemplatesByIdMetadata} */ |
| 40 | + const templatesByIdMetadata = await response.json(); |
| 41 | + |
| 42 | + const tasks = []; |
| 43 | + |
| 44 | + for (const id in templatesByIdMetadata) { |
| 45 | + const templatesMetadata = templatesByIdMetadata[ id ]; |
| 46 | + |
| 47 | + for (const templateMetadata of templatesMetadata) { |
| 48 | + tasks.push(fetchTemplate(templateMetadata, id)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return (await Promise.all(tasks)).filter(template => template !== null); |
| 53 | +} |
| 54 | + |
| 55 | +async function fetchTemplate(templateMetadata, id) { |
| 56 | + const { ref } = templateMetadata; |
| 57 | + |
| 58 | + const response = await fetch(ref); |
| 59 | + |
| 60 | + if (!response.ok) { |
| 61 | + console.warn(`Failed to fetch template ${ id } version ${ templateMetadata.version } from ${ ref } (HTTP ${ response.status })`); |
| 62 | + |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + const templateText = await response.text(); |
| 68 | + let templateJson = JSON.parse(templateText); |
| 69 | + |
| 70 | + console.info(`Fetched template ${ id } version ${ templateMetadata.version } from ${ ref }`); |
| 71 | + |
| 72 | + return templateJson; |
| 73 | + } catch (error) { |
| 74 | + console.warn(`Failed to parse template ${ id } version ${ templateMetadata.version } fetched from ${ ref }`, error); |
| 75 | + |
| 76 | + return null; |
| 77 | + } |
| 78 | +} |
0 commit comments