diff options
author | stanislavh <stanislav.honcharov@sonarsource.com> | 2023-12-06 11:31:21 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-12-08 20:03:05 +0000 |
commit | 5ed270ff91f07132ac004af022b0d4b5b441fcf7 (patch) | |
tree | a6225e4c5f9e28f09f91d6a7b24406975c69369e /server/sonar-web/src/main/js/api | |
parent | 3c3ee9dbc3e54a3b1920e1db3006174b205dee91 (diff) | |
download | sonarqube-5ed270ff91f07132ac004af022b0d4b5b441fcf7.tar.gz sonarqube-5ed270ff91f07132ac004af022b0d4b5b441fcf7.zip |
SONAR-21131 Use api/v2 for custom rule creation
Diffstat (limited to 'server/sonar-web/src/main/js/api')
-rw-r--r-- | server/sonar-web/src/main/js/api/mocks/CodingRulesServiceMock.ts | 51 | ||||
-rw-r--r-- | server/sonar-web/src/main/js/api/rules.ts | 39 |
2 files changed, 45 insertions, 45 deletions
diff --git a/server/sonar-web/src/main/js/api/mocks/CodingRulesServiceMock.ts b/server/sonar-web/src/main/js/api/mocks/CodingRulesServiceMock.ts index d0c9612a95a..aa3c90bc886 100644 --- a/server/sonar-web/src/main/js/api/mocks/CodingRulesServiceMock.ts +++ b/server/sonar-web/src/main/js/api/mocks/CodingRulesServiceMock.ts @@ -20,12 +20,13 @@ import { HttpStatusCode } from 'axios'; import { cloneDeep, countBy, pick, trim } from 'lodash'; import { RuleDescriptionSections } from '../../apps/coding-rules/rule'; +import { mapRestRuleToRule } from '../../apps/coding-rules/utils'; import { getStandards } from '../../helpers/security-standard'; import { mockCurrentUser, mockPaging, + mockRestRuleDetails, mockRuleActivation, - mockRuleDetails, mockRuleRepository, } from '../../helpers/testMocks'; import { RuleRepository, SearchRulesResponse } from '../../types/coding-rules'; @@ -33,7 +34,14 @@ import { ComponentQualifier, Visibility } from '../../types/component'; import { RawIssuesResponse } from '../../types/issues'; import { RuleStatus, SearchRulesQuery } from '../../types/rules'; import { SecurityStandard } from '../../types/security'; -import { Dict, Rule, RuleActivation, RuleDetails, RulesUpdateRequest } from '../../types/types'; +import { + Dict, + Rule, + RuleActivation, + RuleDetails, + RuleParameter, + RulesUpdateRequest, +} from '../../types/types'; import { NoticeType } from '../../types/users'; import { getComponentData } from '../components'; import { getFacet } from '../issues'; @@ -338,9 +346,9 @@ export default class CodingRulesServiceMock { const template = this.rules.find((r) => r.key === rule.templateKey); // Lets not convert the md to html in test. - rule.mdDesc = data.markdown_description !== undefined ? data.markdown_description : rule.mdDesc; + rule.mdDesc = data.markdownDescription !== undefined ? data.markdownDescription : rule.mdDesc; rule.htmlDesc = - data.markdown_description !== undefined ? data.markdown_description : rule.htmlDesc; + data.markdownDescription !== undefined ? data.markdownDescription : rule.htmlDesc; rule.mdNote = data.markdown_note !== undefined ? data.markdown_note : rule.mdNote; rule.htmlNote = data.markdown_note !== undefined ? data.markdown_note : rule.htmlNote; rule.name = data.name !== undefined ? data.name : rule.name; @@ -372,41 +380,30 @@ export default class CodingRulesServiceMock { }; handleCreateRule = (data: CreateRuleData) => { - const newRule = mockRuleDetails({ + const newRule = mockRestRuleDetails({ descriptionSections: [ { key: RuleDescriptionSections.DEFAULT, content: data.markdownDescription }, ], - ...pick(data, ['templateKey', 'name', 'status', 'cleanCodeAttribute', 'impacts']), - key: data.customKey, - params: - data.params?.split(';').map((param: string) => { - const [key, value] = param.split('='); - return { key, defaultValue: value, type: 'TEXT' }; - }) ?? [], + ...pick(data, ['templateKey', 'name', 'status', 'cleanCodeAttribute', 'impacts', 'key']), + parameters: data.parameters as RuleParameter[], }); - const rulesFromTemplateWithSameKeys = this.rules.filter( - (rule) => rule.templateKey === newRule.templateKey && rule.key === newRule.key, + const ruleFromTemplateWithSameKey = this.rules.find( + (rule) => rule.templateKey === newRule.templateKey && newRule.key.split(':')[1] === rule.key, ); - if (rulesFromTemplateWithSameKeys.length > 0) { - if ( - rulesFromTemplateWithSameKeys.find( - (rule) => rule.status === RuleStatus.Removed && rule.key === newRule.key, - ) - ) { - return Promise.reject({ - status: HttpStatusCode.Conflict, - errors: [{ msg: `Rule with the same was removed before` }], - }); - } - + if (ruleFromTemplateWithSameKey?.status === RuleStatus.Removed) { + return Promise.reject({ + status: HttpStatusCode.Conflict, + errors: [{ msg: `Rule with the same was removed before` }], + }); + } else if (ruleFromTemplateWithSameKey) { return Promise.reject({ errors: [{ msg: `A rule with key ${newRule.key} already exists` }], }); } - this.rules.push(newRule); + this.rules.push(mapRestRuleToRule(newRule)); return this.reply(newRule); }; diff --git a/server/sonar-web/src/main/js/api/rules.ts b/server/sonar-web/src/main/js/api/rules.ts index f8cbf1c301e..ec8c67c1563 100644 --- a/server/sonar-web/src/main/js/api/rules.ts +++ b/server/sonar-web/src/main/js/api/rules.ts @@ -18,18 +18,25 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import { throwGlobalError } from '../helpers/error'; -import { getJSON, post, postJSON } from '../helpers/request'; +import { axiosToCatch, getJSON, post, postJSON } from '../helpers/request'; import { CleanCodeAttribute, SoftwareImpact } from '../types/clean-code-taxonomy'; import { GetRulesAppResponse, SearchRulesResponse } from '../types/coding-rules'; import { SearchRulesQuery } from '../types/rules'; -import { RuleActivation, RuleDetails, RulesUpdateRequest } from '../types/types'; +import { + RestRuleDetails, + RestRuleParameter, + RuleActivation, + RuleDetails, + RulesUpdateRequest, +} from '../types/types'; + +const RULES_ENDPOINT = '/api/v2/clean-code-policy/rules'; export interface CreateRuleData { - customKey: string; + key: string; markdownDescription: string; name: string; - params?: string; - preventReactivation?: boolean; + parameters?: Partial<RestRuleParameter>[]; status?: string; templateKey: string; cleanCodeAttribute: CleanCodeAttribute; @@ -68,19 +75,15 @@ export function getRuleTags(parameters: { ps?: number; q: string }): Promise<str return getJSON('/api/rules/tags', parameters).then((r) => r.tags, throwGlobalError); } -export function createRule(data: CreateRuleData): Promise<RuleDetails> { - return postJSON('/api/rules/create', data).then( - (r) => r.rule, - (response) => { - // do not show global error if the status code is 409 - // this case should be handled inside a component - if (response && response.status === 409) { - return Promise.reject(response); - } else { - return throwGlobalError(response); - } - }, - ); +export function createRule(data: CreateRuleData): Promise<RestRuleDetails> { + return axiosToCatch.post<RuleDetails>(RULES_ENDPOINT, data).catch(({ response }) => { + // do not show global error if the status code is 409 + // this case should be handled inside a component + if (response && response.status === 409) { + return Promise.reject(response); + } + return throwGlobalError(response); + }); } export function deleteRule(parameters: { key: string }) { |