From 0996c6186cfeb8e2c763d2f174b26ab276b232f7 Mon Sep 17 00:00:00 2001 From: Revanshu Paliwal Date: Wed, 28 Aug 2024 16:40:16 +0200 Subject: CODEFIX-12 Show new suggestion feature in issues page --- .../sonar-web/src/main/js/api/fix-suggestions.ts | 29 +++++++++++ .../src/main/js/api/mocks/FixIssueServiceMock.ts | 59 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 server/sonar-web/src/main/js/api/fix-suggestions.ts create mode 100644 server/sonar-web/src/main/js/api/mocks/FixIssueServiceMock.ts (limited to 'server/sonar-web/src/main/js/api') diff --git a/server/sonar-web/src/main/js/api/fix-suggestions.ts b/server/sonar-web/src/main/js/api/fix-suggestions.ts new file mode 100644 index 00000000000..84570ff31e1 --- /dev/null +++ b/server/sonar-web/src/main/js/api/fix-suggestions.ts @@ -0,0 +1,29 @@ +/* + * 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 { axiosToCatch } from '../helpers/request'; +import { SuggestedFix } from '../types/fix-suggestions'; + +export interface FixParam { + issueId: string; +} + +export function getSuggestions(data: FixParam): Promise { + return axiosToCatch.post('/api/v2/fix-suggestions/ai-suggestions', data); +} diff --git a/server/sonar-web/src/main/js/api/mocks/FixIssueServiceMock.ts b/server/sonar-web/src/main/js/api/mocks/FixIssueServiceMock.ts new file mode 100644 index 00000000000..be6dfcfcf69 --- /dev/null +++ b/server/sonar-web/src/main/js/api/mocks/FixIssueServiceMock.ts @@ -0,0 +1,59 @@ +/* + * 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 { FixParam, getSuggestions } from '../fix-suggestions'; +import { ISSUE_101 } from './data/ids'; + +jest.mock('../fix-suggestions'); + +export default class FixIssueServiceMock { + fixSuggestion = { + id: '70b14d4c-d302-4979-9121-06ac7d563c5c', + issueId: 'AYsVhClEbjXItrbcN71J', + explanation: + "Replaced 'require' statements with 'import' statements to comply with ECMAScript 2015 module management standards.", + changes: [ + { + startLine: 6, + endLine: 7, + newCode: "import { glob } from 'glob';\nimport fs from 'fs';", + }, + ], + }; + + constructor() { + jest.mocked(getSuggestions).mockImplementation(this.handleGetFixSuggestion); + } + + handleGetFixSuggestion = (data: FixParam) => { + if (data.issueId === ISSUE_101) { + return Promise.reject({ error: { msg: 'Invalid issue' } }); + } + return this.reply(this.fixSuggestion); + }; + + reply(response: T): Promise { + return new Promise((resolve) => { + setTimeout(() => { + resolve(cloneDeep(response)); + }, 10); + }); + } +} -- cgit v1.2.3