debian-mirror-gitlab/app/assets/javascripts/gitlab_version_check/index.js
2023-03-04 22:38:38 +05:30

98 lines
2.5 KiB
JavaScript

import Vue from 'vue';
import { parseBoolean, convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import GitlabVersionCheckBadge from './components/gitlab_version_check_badge.vue';
import SecurityPatchUpgradeAlert from './components/security_patch_upgrade_alert.vue';
import SecurityPatchUpgradeAlertModal from './components/security_patch_upgrade_alert_modal.vue';
const mountGitlabVersionCheckBadge = (el) => {
const { size, version } = el.dataset;
const actionable = parseBoolean(el.dataset.actionable);
try {
const { severity } = JSON.parse(version);
// If no severity (status) data don't worry about rendering
if (!severity) {
return null;
}
return new Vue({
el,
render(createElement) {
return createElement(GitlabVersionCheckBadge, {
props: {
size,
actionable,
status: severity,
},
});
},
});
} catch {
return null;
}
};
const mountSecurityPatchUpgradeAlert = (el) => {
const { currentVersion } = el.dataset;
try {
return new Vue({
el,
render(createElement) {
return createElement(SecurityPatchUpgradeAlert, {
props: {
currentVersion,
},
});
},
});
} catch {
return null;
}
};
const mountSecurityPatchUpgradeAlertModal = (el) => {
const { currentVersion, version } = el.dataset;
try {
const { details, latestStableVersions } = convertObjectPropsToCamelCase(JSON.parse(version));
return new Vue({
el,
render(createElement) {
return createElement(SecurityPatchUpgradeAlertModal, {
props: {
currentVersion,
details,
latestStableVersions,
},
});
},
});
} catch {
return null;
}
};
export default () => {
const renderedApps = [];
const securityPatchUpgradeAlert = document.getElementById('js-security-patch-upgrade-alert');
const securityPatchUpgradeAlertModal = document.getElementById(
'js-security-patch-upgrade-alert-modal',
);
const versionCheckBadges = [...document.querySelectorAll('.js-gitlab-version-check-badge')];
if (securityPatchUpgradeAlert) {
renderedApps.push(mountSecurityPatchUpgradeAlert(securityPatchUpgradeAlert));
}
if (securityPatchUpgradeAlertModal) {
renderedApps.push(mountSecurityPatchUpgradeAlertModal(securityPatchUpgradeAlertModal));
}
renderedApps.push(...versionCheckBadges.map((el) => mountGitlabVersionCheckBadge(el)));
return renderedApps;
};