|
| 1 | +import { defineSmartComponent } from '../../lib/smart/define-smart-component.js'; |
| 2 | +// @ts-expect-error FIXME: remove when clever-client exports types |
| 3 | +import { getAddon as getAddonProvider } from '@clevercloud/client/esm/api/v2/providers.js'; |
| 4 | +// @ts-expect-error FIXME: remove when clever-client exports types |
| 5 | +import { ONE_SECOND } from '@clevercloud/client/esm/with-cache.js'; |
| 6 | +import { getDocUrl } from '../../lib/dev-hub-url.js'; |
| 7 | +import { formatAddonFeatures } from '../../lib/product.js'; |
| 8 | +import { sendToApi } from '../../lib/send-to-api.js'; |
| 9 | +import { i18n } from '../../translations/translation.js'; |
| 10 | +import '../cc-smart-container/cc-smart-container.js'; |
| 11 | +import { CcAddonInfoClient } from './cc-addon-info.client.js'; |
| 12 | +import './cc-addon-info.js'; |
| 13 | + |
| 14 | +const PROVIDER_ID = 'jenkins'; |
| 15 | + |
| 16 | +/** |
| 17 | + * @typedef {import('./cc-addon-info.js').CcAddonInfo} CcAddonInfo |
| 18 | + * @typedef {import('./cc-addon-info.types.js').AddonInfoStateLoading} AddonInfoStateLoading |
| 19 | + * @typedef {import('./cc-addon-info.types.js').JenkinsProviderInfo} JenkinsProviderInfo |
| 20 | + * @typedef {import('./cc-addon-info.types.js').RawAddon} RawAddon |
| 21 | + * @typedef {import('../../lib/smart/smart-component.types.js').OnContextUpdateArgs<CcAddonInfo>} OnContextUpdateArgs |
| 22 | + * @typedef {import('../../lib/send-to-api.types.js').ApiConfig} ApiConfig |
| 23 | + * @typedef {import('../../lib/send-to-api.types.js').AuthBridgeConfig} AuthBridgeConfig |
| 24 | + */ |
| 25 | + |
| 26 | +defineSmartComponent({ |
| 27 | + selector: 'cc-addon-info[smart-mode="jenkins"]', |
| 28 | + params: { |
| 29 | + apiConfig: { type: Object }, |
| 30 | + ownerId: { type: String }, |
| 31 | + addonId: { type: String }, |
| 32 | + }, |
| 33 | + /** |
| 34 | + * @param {OnContextUpdateArgs} _ |
| 35 | + */ |
| 36 | + onContextUpdate({ context, updateComponent, signal }) { |
| 37 | + const { apiConfig, ownerId, addonId } = context; |
| 38 | + |
| 39 | + const api = new Api({ apiConfig, ownerId, addonId, signal }); |
| 40 | + |
| 41 | + /** |
| 42 | + * @type {AddonInfoStateLoading} |
| 43 | + */ |
| 44 | + const LOADING_STATE = { |
| 45 | + type: 'loading', |
| 46 | + version: { |
| 47 | + stateType: 'up-to-date', |
| 48 | + installed: '0.0.0', |
| 49 | + latest: '0.0.0', |
| 50 | + }, |
| 51 | + creationDate: '2025-08-06 15:03:00', |
| 52 | + plan: 'XS', |
| 53 | + // Remove encryption since it's not part of the addon features |
| 54 | + features: [ |
| 55 | + { |
| 56 | + code: 'cpu', |
| 57 | + type: 'number', |
| 58 | + value: 2, |
| 59 | + }, |
| 60 | + { |
| 61 | + code: 'memory', |
| 62 | + type: 'bytes', |
| 63 | + value: 4, |
| 64 | + }, |
| 65 | + { |
| 66 | + code: 'disk-size', |
| 67 | + type: 'bytes', |
| 68 | + value: 40, |
| 69 | + }, |
| 70 | + { |
| 71 | + code: 'encryption-at-rest', |
| 72 | + type: 'boolean', |
| 73 | + value: 'false', |
| 74 | + }, |
| 75 | + ], |
| 76 | + }; |
| 77 | + |
| 78 | + updateComponent('state', { type: 'loading', ...LOADING_STATE }); |
| 79 | + updateComponent('docLink', { |
| 80 | + text: i18n('cc-addon-info.doc-link.jenkins'), |
| 81 | + href: getDocUrl('/addons/jenkins'), |
| 82 | + }); |
| 83 | + |
| 84 | + api |
| 85 | + .getJenkinsAddonInfo() |
| 86 | + .then(({ rawAddon, addonProvider }) => { |
| 87 | + // Get standard features from plan |
| 88 | + const features = formatAddonFeatures(rawAddon.plan.features, ['cpu', 'memory', 'disk-size']); |
| 89 | + |
| 90 | + // Add encryption feature from addonProvider |
| 91 | + const encryptionFeature = addonProvider.features.find((f) => f.name === 'encryption'); |
| 92 | + if (encryptionFeature) { |
| 93 | + features.push({ |
| 94 | + code: 'encryption-at-rest', |
| 95 | + type: 'boolean', |
| 96 | + value: `encryptionFeature.enabled`, |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + updateComponent('state', { |
| 101 | + type: 'loaded', |
| 102 | + version: { |
| 103 | + stateType: 'up-to-date', |
| 104 | + installed: addonProvider.version, |
| 105 | + latest: addonProvider.version, |
| 106 | + }, |
| 107 | + creationDate: rawAddon.creationDate, |
| 108 | + plan: rawAddon.plan.name, |
| 109 | + features, |
| 110 | + }); |
| 111 | + }) |
| 112 | + .catch((error) => { |
| 113 | + console.error(error); |
| 114 | + updateComponent('state', { type: 'error' }); |
| 115 | + }); |
| 116 | + }, |
| 117 | +}); |
| 118 | + |
| 119 | +class Api extends CcAddonInfoClient { |
| 120 | + /** |
| 121 | + * @param {Object} config - Configuration object |
| 122 | + * @param {ApiConfig} config.apiConfig - API configuration |
| 123 | + * @param {string} config.ownerId - Owner identifier |
| 124 | + * @param {string} config.addonId - Addon identifier |
| 125 | + * @param {{ base: string, console: string }} [config.grafanaLink] - Base url to build a grafana link to the app |
| 126 | + * @param {AbortSignal} config.signal - Signal to abort calls |
| 127 | + */ |
| 128 | + constructor({ apiConfig, ownerId, addonId, grafanaLink, signal }) { |
| 129 | + super({ apiConfig, ownerId, addonId, providerId: PROVIDER_ID, grafanaLink, signal }); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * |
| 134 | + * @param {string} providerId |
| 135 | + * @returns {Promise<JenkinsProviderInfo>} |
| 136 | + */ |
| 137 | + _getAddonProvider(providerId) { |
| 138 | + return getAddonProvider({ providerId, addonId: this._addonId }).then( |
| 139 | + sendToApi({ apiConfig: this._apiConfig, signal: this._signal, cacheDelay: ONE_SECOND }), |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @returns {Promise<{ rawAddon: RawAddon, addonProvider: JenkinsProviderInfo }>} |
| 145 | + */ |
| 146 | + async getJenkinsAddonInfo() { |
| 147 | + const rawAddon = await this._getAddon(); |
| 148 | + const addonProvider = await this._getAddonProvider(rawAddon.provider.id); |
| 149 | + |
| 150 | + return { rawAddon, addonProvider }; |
| 151 | + } |
| 152 | +} |
0 commit comments