You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plugins.ts 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { findLastIndex } from 'lodash';
  21. import throwGlobalError from '../app/utils/throwGlobalError';
  22. import { getJSON, post } from '../helpers/request';
  23. import { isDefined } from '../helpers/types';
  24. import {
  25. AvailablePlugin,
  26. InstalledPlugin,
  27. PendingPluginResult,
  28. PluginType,
  29. Update
  30. } from '../types/plugins';
  31. export function getAvailablePlugins(): Promise<{
  32. plugins: AvailablePlugin[];
  33. updateCenterRefresh: string;
  34. }> {
  35. return getJSON('/api/plugins/available').catch(throwGlobalError);
  36. }
  37. export function getPendingPlugins(): Promise<PendingPluginResult> {
  38. return getJSON('/api/plugins/pending').catch(throwGlobalError);
  39. }
  40. function getLastUpdates(updates: undefined | Update[]): Update[] {
  41. if (!updates) {
  42. return [];
  43. }
  44. const lastUpdate = ['COMPATIBLE', 'REQUIRES_SYSTEM_UPGRADE', 'DEPS_REQUIRE_SYSTEM_UPGRADE'].map(
  45. status => {
  46. const index = findLastIndex(updates, update => update.status === status);
  47. return index > -1 ? updates[index] : undefined;
  48. }
  49. );
  50. return lastUpdate.filter(isDefined);
  51. }
  52. function addChangelog(update: Update, updates?: Update[]) {
  53. if (!updates) {
  54. return update;
  55. }
  56. const index = updates.indexOf(update);
  57. const previousUpdates = index > 0 ? updates.slice(0, index) : [];
  58. return { ...update, previousUpdates };
  59. }
  60. function getInstalledPluginApi(type = PluginType.External) {
  61. return getJSON('/api/plugins/installed', { f: 'category', type });
  62. }
  63. function getUpdatesPluginApi() {
  64. return getJSON('/api/plugins/updates');
  65. }
  66. export function getInstalledPlugins(
  67. type: PluginType = PluginType.External
  68. ): Promise<InstalledPlugin[]> {
  69. return getInstalledPluginApi(type).then(({ plugins }) => plugins, throwGlobalError);
  70. }
  71. export function getInstalledPluginsWithUpdates(): Promise<InstalledPlugin[]> {
  72. return Promise.all([getInstalledPluginApi(), getUpdatesPluginApi()])
  73. .then(([installed, updates]) =>
  74. installed.plugins.map((plugin: InstalledPlugin) => {
  75. const updatePlugin: InstalledPlugin = updates.plugins.find(
  76. (p: InstalledPlugin) => p.key === plugin.key
  77. );
  78. if (updatePlugin) {
  79. return {
  80. ...updatePlugin,
  81. ...plugin,
  82. updates: getLastUpdates(updatePlugin.updates).map(update =>
  83. addChangelog(update, updatePlugin.updates)
  84. )
  85. };
  86. }
  87. return plugin;
  88. })
  89. )
  90. .catch(throwGlobalError);
  91. }
  92. export function getPluginUpdates(): Promise<InstalledPlugin[]> {
  93. return Promise.all([getUpdatesPluginApi(), getInstalledPluginApi()])
  94. .then(([updates, installed]) =>
  95. updates.plugins.map((updatePlugin: InstalledPlugin) => {
  96. const updates = getLastUpdates(updatePlugin.updates).map(update =>
  97. addChangelog(update, updatePlugin.updates)
  98. );
  99. const plugin = installed.plugins.find((p: InstalledPlugin) => p.key === updatePlugin.key);
  100. if (plugin) {
  101. return {
  102. ...plugin,
  103. ...updatePlugin,
  104. updates
  105. };
  106. }
  107. return { ...updatePlugin, updates };
  108. })
  109. )
  110. .catch(throwGlobalError);
  111. }
  112. export function installPlugin(data: { key: string }): Promise<void | Response> {
  113. return post('/api/plugins/install', data).catch(throwGlobalError);
  114. }
  115. export function uninstallPlugin(data: { key: string }): Promise<void | Response> {
  116. return post('/api/plugins/uninstall', data).catch(throwGlobalError);
  117. }
  118. export function updatePlugin(data: { key: string }): Promise<void | Response> {
  119. return post('/api/plugins/update', data).catch(throwGlobalError);
  120. }
  121. export function cancelPendingPlugins(): Promise<void | Response> {
  122. return post('/api/plugins/cancel_all').catch(throwGlobalError);
  123. }