1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/*
* 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 { ICell, INotebookContent, isCode, isMarkdown } from '@jupyterlab/nbformat';
import { Spinner } from '@sonarsource/echoes-react';
import {
Card,
FlagMessage,
hljsIssueIndicatorPlugin,
hljsUnderlinePlugin,
UnderlineRangePosition,
} from '~design-system';
import React, { forwardRef, useEffect, useMemo, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import { To } from 'react-router-dom';
import { useLocation, useRouter } from '~sonar-aligned/components/hoc/withRouter';
import {
JupyterCodeCell,
JupyterMarkdownCell,
} from '~sonar-aligned/components/SourceViewer/JupyterNotebookViewer';
import { getBranchLikeQuery } from '~sonar-aligned/helpers/branch-like';
import { getOffsetsForIssue } from '~sonar-aligned/helpers/json-issue-mapper';
import { getComponentIssuesUrl } from '~sonar-aligned/helpers/urls';
import { ComponentContext } from '../../app/components/componentContext/ComponentContext';
import { parseQuery, serializeQuery } from '../../apps/issues/utils';
import { translate } from '../../helpers/l10n';
import { getIssuesUrl } from '../../helpers/urls';
import { useRawSourceQuery } from '../../queries/sources';
import { BranchLike } from '../../types/branch-like';
import { Component, Issue } from '../../types/types';
import LineIssuesIndicator from './components/LineIssuesIndicator';
import loadIssues from './helpers/loadIssues';
export interface Props {
branchLike: BranchLike | undefined;
component: string;
}
type IssuesByCell = { [key: number]: IssuesByLine };
type IssueByLine = {
end: UnderlineRangePosition;
issue: Issue;
start: UnderlineRangePosition;
};
type IssuesByLine = {
[line: number]: IssueByLine[];
};
type IssueKeysByLine = { [line: number]: string[] };
type IssueIndicatorsProps = {
branchLike: BranchLike;
component: Component;
issuesByCell: IssuesByCell;
jupyterRef: React.RefObject<HTMLDivElement>;
};
type IssueMapper = {
issueUrl: To;
key: string;
lineIndex: number;
onlyIssues: Issue[];
};
export default function SourceViewerPreview(props: Readonly<Props>) {
const { component, branchLike } = props;
const [issues, setIssues] = useState<Issue[]>([]);
const [issuesByCell, setIssuesByCell] = useState<IssuesByCell>({});
const jupyterNotebookRef = useRef<HTMLDivElement>(null);
const { data, isLoading } = useRawSourceQuery({
key: component,
...getBranchLikeQuery(branchLike),
});
const { component: componentContext } = React.useContext(ComponentContext);
const jupyterNotebook = useMemo(() => {
if (typeof data !== 'string') {
return null;
}
try {
return JSON.parse(data) as INotebookContent;
} catch (error) {
return null;
}
}, [data]);
useEffect(() => {
const fetchData = async () => {
const issues = await loadIssues(component, branchLike);
setIssues(issues);
};
fetchData();
}, [component, branchLike]);
useEffect(() => {
processIssuesByCell(issues, data, setIssuesByCell);
}, [issues, data]);
if (isLoading) {
return <Spinner isLoading={isLoading} />;
}
if (typeof data !== 'string') {
return (
<FlagMessage className="sw-mt-2" variant="warning">
{translate('component_viewer.no_component')}
</FlagMessage>
);
}
if (!jupyterNotebook?.cells) {
return (
<FlagMessage className="sw-mt-2" variant="warning">
{translate('source_viewer.jupyter.preview.error')}
</FlagMessage>
);
}
return (
<Card>
<JupyterNotebookSourceViewer
cells={jupyterNotebook.cells}
issuesByCell={issuesByCell}
ref={jupyterNotebookRef}
/>
{issues && componentContext && branchLike && (
<IssueIndicators
issuesByCell={issuesByCell}
component={componentContext}
branchLike={branchLike}
jupyterRef={jupyterNotebookRef}
/>
)}
</Card>
);
}
type JupyterNotebookProps = {
cells: ICell[];
issuesByCell: IssuesByCell;
};
function mapIssuesToIssueKeys(issuesByLine: IssuesByLine): IssueKeysByLine {
return Object.entries(issuesByLine).reduce((acc, [line, issues]) => {
acc[Number(line)] = issues.map(({ issue }) => issue.key);
return acc;
}, {} as IssueKeysByLine);
}
const JupyterNotebookSourceViewer = forwardRef<HTMLDivElement, JupyterNotebookProps>(
({ cells, issuesByCell }, ref) => {
const buildCellsBlocks = useMemo(() => {
return cells.map((cell: ICell, index: number) => {
let sourceLines = Array.isArray(cell.source) ? cell.source : [cell.source];
const issuesByLine = issuesByCell[index];
if (!issuesByLine) {
return {
cell,
sourceLines,
};
}
const issues = mapIssuesToIssueKeys(issuesByLine);
const flatIssues = Object.entries(issuesByLine).flatMap(([, issues]) => issues);
sourceLines = hljsUnderlinePlugin.tokenize(sourceLines, flatIssues);
sourceLines = hljsIssueIndicatorPlugin.addIssuesToLines(sourceLines, issues);
return {
cell,
sourceLines,
};
});
}, [cells, issuesByCell]);
return (
<div ref={ref}>
{buildCellsBlocks.map((element, index) => {
const { cell, sourceLines } = element;
if (isCode(cell)) {
return (
<JupyterCodeCell
source={sourceLines}
outputs={cell.outputs}
key={`${cell.cell_type}-${index}`}
/>
);
} else if (isMarkdown(cell)) {
return <JupyterMarkdownCell cell={cell} key={`${cell.cell_type}-${index}`} />;
}
return null;
})}
</div>
);
},
);
JupyterNotebookSourceViewer.displayName = 'JupyterNotebookSourceViewer';
function IssueIndicators({
issuesByCell,
component,
branchLike,
jupyterRef,
}: Readonly<IssueIndicatorsProps>) {
const location = useLocation();
const query = parseQuery(location.query);
const onlyIssuesMap = (issues: IssueByLine[]) => issues.map(({ issue }) => issue);
const mappedIssues = useMemo(() => {
return Object.entries(issuesByCell).flatMap(([, issuesByLine]) =>
Object.entries(issuesByLine).map(([, issues]) => {
const firstIssue = issues[0].issue;
const onlyIssues = onlyIssuesMap(issues);
const urlQuery = {
...getBranchLikeQuery(branchLike),
...serializeQuery(query),
open: firstIssue.key,
};
const issueUrl = component?.key
? getComponentIssuesUrl(component?.key, urlQuery)
: getIssuesUrl(urlQuery);
return { key: firstIssue.key, issueUrl, onlyIssues, lineIndex: issues[0].start.line };
}),
);
// we only need to recompute this with new issues
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [issuesByCell]);
return mappedIssues.map((mappedIssues) => (
<PortalLineIssuesIndicator
key={mappedIssues.key}
issueMapper={mappedIssues}
jupyterRef={jupyterRef}
/>
));
}
/*
Creates a react portal bound to an element id `issue-key-${key}` that represents the first issue
present on a virtual line of code. The element id is generated from executing the
HljsIssueIndicatorPlugin.addIssuesToLines function on the source code of a Jupyter notebook cell.
*/
function PortalLineIssuesIndicator(props: {
issueMapper: IssueMapper;
jupyterRef: React.RefObject<HTMLDivElement>;
}) {
const { jupyterRef, issueMapper } = props;
const router = useRouter();
// We use this state to force-re-render the component when the jupyterRef changes
// eslint-disable-next-line react/hook-use-state
const [, setMutationCount] = useState(0);
useEffect(() => {
if (!jupyterRef.current) {
return;
}
setMutationCount((count) => count + 1);
}, [jupyterRef]);
const { key, lineIndex, onlyIssues, issueUrl } = issueMapper;
const element = document.getElementById(`issue-key-${key}`);
if (!element) {
return null;
}
return createPortal(
<LineIssuesIndicator
issues={onlyIssues}
onClick={() => router.navigate(issueUrl)}
line={{ line: Number(lineIndex) }}
as="span"
/>,
element,
);
}
/**
* Processes issues and maps them to their corresponding cells and lines in a Jupyter notebook.
* This function updates the state with a new mapping of issues by cell.
*
* @param {Array} issues - The list of issues to process.
* @param {string} data - The JSON data representing the notebook.
* @param {Function} setIssuesByCell - Function to update the state with the new issues mapping.
*/
function processIssuesByCell(
issues: Issue[],
data: string | null | undefined,
setIssuesByCell: Function,
) {
const newIssuesByCell: IssuesByCell = {};
issues.forEach((issue) => {
if (typeof data !== 'string') {
return;
}
const { startOffset, endOffset } = getOffsetsForIssue(issue, data);
// failed to parse the issue offsets, skip the issue
if (!startOffset || !endOffset) {
return;
}
const { cell } = startOffset;
if (!newIssuesByCell[cell]) {
newIssuesByCell[cell] = {};
}
if (!newIssuesByCell[cell][startOffset.line]) {
newIssuesByCell[cell][startOffset.line] = [{ issue, start: startOffset, end: endOffset }];
}
const existingIssues = newIssuesByCell[cell][startOffset.line];
const issueExists = existingIssues.some(
({ issue: existingIssue }) => existingIssue.key === issue.key,
);
if (!issueExists) {
newIssuesByCell[cell][startOffset.line].push({ issue, start: startOffset, end: endOffset });
}
});
setIssuesByCell(newIssuesByCell);
}
|