aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/api
diff options
context:
space:
mode:
authorLucas <97296331+lucas-paulger-sonarsource@users.noreply.github.com>2024-07-22 12:25:55 +0300
committersonartech <sonartech@sonarsource.com>2024-08-13 20:02:46 +0000
commitfe9c1ab62eec9e15f806c884f274c6d32a6c8f92 (patch)
tree43ecda108f514bb3b1d54c8619a64ffa41e5075d /server/sonar-web/src/main/js/api
parente6adb0980a1db8a356a7283c240a95b01a90a472 (diff)
downloadsonarqube-fe9c1ab62eec9e15f806c884f274c6d32a6c8f92.tar.gz
sonarqube-fe9c1ab62eec9e15f806c884f274c6d32a6c8f92.zip
SONAR-22499 CodeViewer supports ipynb files (#11371)
Diffstat (limited to 'server/sonar-web/src/main/js/api')
-rw-r--r--server/sonar-web/src/main/js/api/mocks/SourcesServiceMock.ts43
-rw-r--r--server/sonar-web/src/main/js/api/mocks/data/sources.ts87
-rw-r--r--server/sonar-web/src/main/js/api/sources.ts24
3 files changed, 154 insertions, 0 deletions
diff --git a/server/sonar-web/src/main/js/api/mocks/SourcesServiceMock.ts b/server/sonar-web/src/main/js/api/mocks/SourcesServiceMock.ts
new file mode 100644
index 00000000000..660eca474fe
--- /dev/null
+++ b/server/sonar-web/src/main/js/api/mocks/SourcesServiceMock.ts
@@ -0,0 +1,43 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+import { cloneDeep } from 'lodash';
+import { getRawSource } from '../sources';
+import { mockIpynbFile } from './data/sources';
+
+jest.mock('../sources');
+
+export default class SourcesServiceMock {
+ constructor() {
+ jest.mocked(getRawSource).mockImplementation(this.handleGetRawSource);
+ }
+
+ handleGetRawSource = () => {
+ return this.reply(mockIpynbFile);
+ };
+
+ reply<T>(response: T): Promise<T> {
+ return Promise.resolve(cloneDeep(response));
+ }
+
+ reset = () => {
+ return this;
+ };
+}
diff --git a/server/sonar-web/src/main/js/api/mocks/data/sources.ts b/server/sonar-web/src/main/js/api/mocks/data/sources.ts
new file mode 100644
index 00000000000..d77794ecf00
--- /dev/null
+++ b/server/sonar-web/src/main/js/api/mocks/data/sources.ts
@@ -0,0 +1,87 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+export const mockIpynbFile = JSON.stringify({
+ cells: [
+ {
+ cell_type: 'markdown',
+ metadata: {},
+ source: ['# Learning a cosine with keras'],
+ },
+ {
+ cell_type: 'code',
+ execution_count: 2,
+ metadata: {
+ collapsed: false,
+ jupyter: {
+ outputs_hidden: false,
+ },
+ },
+ outputs: [
+ {
+ name: 'stdout',
+ output_type: 'stream',
+ text: ['(7500,)\n', '(2500,)\n'],
+ },
+ ],
+ source: [
+ 'import numpy as np\n',
+ 'import sklearn.cross_validation as skcv\n',
+ '#x = np.linspace(0, 5*np.pi, num=10000, dtype=np.float32)\n',
+ 'x = np.linspace(0, 4*np.pi, num=10000, dtype=np.float32)\n',
+ 'y = np.cos(x)\n',
+ '\n',
+ 'train, test = skcv.train_test_split(np.arange(x.shape[0]))\n',
+ 'print train.shape\n',
+ 'print test.shape',
+ ],
+ },
+ {
+ cell_type: 'code',
+ execution_count: 3,
+ metadata: {
+ collapsed: false,
+ jupyter: {
+ outputs_hidden: false,
+ },
+ },
+ outputs: [
+ {
+ data: {
+ 'text/plain': ['[<matplotlib.lines.Line2D at 0x7fb588176b90>]'],
+ },
+ execution_count: 3,
+ metadata: {},
+ output_type: 'execute_result',
+ },
+ {
+ data: {
+ 'image/png':
+ 'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAG0lEQVR4nGIJn1mo28/GzPDiV+yTNYAAAAD//yPBBfrGshAGAAAAAElFTkSuQmCC',
+ 'text/plain': ['<matplotlib.figure.Figure at 0x7fb58e57c850>'],
+ },
+ metadata: {},
+ output_type: 'display_data',
+ },
+ ],
+ source: ['import pylab as pl\n', '%matplotlib inline\n', 'pl.plot(x, y)'],
+ },
+ ],
+});
diff --git a/server/sonar-web/src/main/js/api/sources.ts b/server/sonar-web/src/main/js/api/sources.ts
new file mode 100644
index 00000000000..fade6f713a1
--- /dev/null
+++ b/server/sonar-web/src/main/js/api/sources.ts
@@ -0,0 +1,24 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+import { get, parseText, RequestData } from '../helpers/request';
+
+export function getRawSource(data: RequestData): Promise<string> {
+ return get('/api/sources/raw', data).then(parseText);
+}