diff options
author | stanislavh <stanislav.honcharov@sonarsource.com> | 2023-12-06 11:08:25 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-12-08 20:03:05 +0000 |
commit | 3c3ee9dbc3e54a3b1920e1db3006174b205dee91 (patch) | |
tree | 5812207ccd2fc9f173838691fb12ff83638c3d74 /server/sonar-web/src/main/js/api/mocks | |
parent | f858d78968e5a82dbfcd494838cf46d4577a2211 (diff) | |
download | sonarqube-3c3ee9dbc3e54a3b1920e1db3006174b205dee91.tar.gz sonarqube-3c3ee9dbc3e54a3b1920e1db3006174b205dee91.zip |
SONAR-21179 Use update endpoint when reactivating a rule
Diffstat (limited to 'server/sonar-web/src/main/js/api/mocks')
3 files changed, 53 insertions, 9 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 e7f50d93529..d0c9612a95a 100644 --- a/server/sonar-web/src/main/js/api/mocks/CodingRulesServiceMock.ts +++ b/server/sonar-web/src/main/js/api/mocks/CodingRulesServiceMock.ts @@ -17,6 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +import { HttpStatusCode } from 'axios'; import { cloneDeep, countBy, pick, trim } from 'lodash'; import { RuleDescriptionSections } from '../../apps/coding-rules/rule'; import { getStandards } from '../../helpers/security-standard'; @@ -30,7 +31,7 @@ import { import { RuleRepository, SearchRulesResponse } from '../../types/coding-rules'; import { ComponentQualifier, Visibility } from '../../types/component'; import { RawIssuesResponse } from '../../types/issues'; -import { SearchRulesQuery } from '../../types/rules'; +import { RuleStatus, SearchRulesQuery } from '../../types/rules'; import { SecurityStandard } from '../../types/security'; import { Dict, Rule, RuleActivation, RuleDetails, RulesUpdateRequest } from '../../types/types'; import { NoticeType } from '../../types/users'; @@ -174,7 +175,7 @@ export default class CodingRulesServiceMock { cwe, activation, }: FacetFilter) { - let filteredRules = this.rules; + let filteredRules = this.getRulesFilteredByRemovedStatus(); if (cleanCodeAttributeCategories) { filteredRules = filteredRules.filter( (r) => @@ -266,12 +267,16 @@ export default class CodingRulesServiceMock { this.rulesActivations = mockRulesActivationsInQP(); } + getRulesFilteredByRemovedStatus() { + return this.rules.filter((r) => r.status !== RuleStatus.Removed); + } + allRulesCount() { - return this.rules.length; + return this.getRulesFilteredByRemovedStatus().length; } allRulesName() { - return this.rules.map((r) => r.name); + return this.getRulesFilteredByRemovedStatus().map((r) => r.name); } allQualityProfile(language: string) { @@ -322,12 +327,14 @@ export default class CodingRulesServiceMock { }; handleUpdateRule = (data: RulesUpdateRequest): Promise<RuleDetails> => { - const rule = this.rules.find((r) => r.key === data.key); + // find rule if key is in the list of rules or key is a part of 'repo:key' + const rule = this.rules.find((r) => data.key.split(':').some((part) => part === r.key)); if (rule === undefined) { return Promise.reject({ errors: [{ msg: `No rule has been found for id ${data.key}` }], }); } + const template = this.rules.find((r) => r.key === rule.templateKey); // Lets not convert the md to html in test. @@ -337,6 +344,7 @@ export default class CodingRulesServiceMock { 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; + rule.status = rule.status === RuleStatus.Removed ? RuleStatus.Ready : rule.status; if (template && data.params) { rule.params = []; data.params.split(';').forEach((param) => { @@ -377,6 +385,27 @@ export default class CodingRulesServiceMock { }) ?? [], }); + const rulesFromTemplateWithSameKeys = this.rules.filter( + (rule) => rule.templateKey === newRule.templateKey && rule.key === newRule.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` }], + }); + } + + return Promise.reject({ + errors: [{ msg: `A rule with key ${newRule.key} already exists` }], + }); + } + this.rules.push(newRule); return this.reply(newRule); @@ -469,6 +498,7 @@ export default class CodingRulesServiceMock { activation, }); } + const responseRules = filteredRules.slice((currentP - 1) * currentPs, currentP * currentPs); return this.reply({ actives: qprofile ? this.rulesActivations : undefined, @@ -485,13 +515,13 @@ export default class CodingRulesServiceMock { handleBulkActivateRules = () => { if (this.applyWithWarning) { return this.reply({ - succeeded: this.rules.length - 1, + succeeded: this.getRulesFilteredByRemovedStatus().length - 1, failed: 1, errors: [{ msg: 'c rule c:S6069 cannot be activated on cpp profile SonarSource' }], }); } return this.reply({ - succeeded: this.rules.length, + succeeded: this.getRulesFilteredByRemovedStatus().length, failed: 0, errors: [], }); @@ -499,7 +529,7 @@ export default class CodingRulesServiceMock { handleBulkDeactivateRules = () => { return this.reply({ - succeeded: this.rules.length, + succeeded: this.getRulesFilteredByRemovedStatus().length, failed: 0, }); }; diff --git a/server/sonar-web/src/main/js/api/mocks/data/ids.ts b/server/sonar-web/src/main/js/api/mocks/data/ids.ts index f680b17e2a8..502e5600ffd 100644 --- a/server/sonar-web/src/main/js/api/mocks/data/ids.ts +++ b/server/sonar-web/src/main/js/api/mocks/data/ids.ts @@ -50,6 +50,7 @@ export const RULE_8 = 'rule8'; export const RULE_9 = 'rule9'; export const RULE_10 = 'rule10'; export const RULE_11 = 'rule11'; +export const RULE_12 = 'rule12'; // Quality Profiles. export const QP_1 = 'p1'; diff --git a/server/sonar-web/src/main/js/api/mocks/data/rules.ts b/server/sonar-web/src/main/js/api/mocks/data/rules.ts index f7a1aca3dbf..3616cb845e4 100644 --- a/server/sonar-web/src/main/js/api/mocks/data/rules.ts +++ b/server/sonar-web/src/main/js/api/mocks/data/rules.ts @@ -25,6 +25,7 @@ import { SoftwareImpactSeverity, SoftwareQuality, } from '../../../types/clean-code-taxonomy'; +import { RuleStatus } from '../../../types/rules'; import { ADVANCED_RULE, QP_1, @@ -34,6 +35,7 @@ import { RULE_1, RULE_10, RULE_11, + RULE_12, RULE_2, RULE_3, RULE_4, @@ -214,7 +216,7 @@ export function mockRuleDetailsList() { { key: '1', type: 'TEXT', htmlDesc: 'html description for key 1' }, { key: '2', type: 'NUMBER', defaultValue: 'default value for key 2' }, ], - templateKey: 'rule8', + templateKey: RULE_8, }), mockRuleDetails({ createdAt: '2022-12-16T17:26:54+0100', @@ -242,6 +244,17 @@ export function mockRuleDetailsList() { langName: 'Java', name: 'Common java rule', }), + mockRuleDetails({ + key: RULE_12, + type: 'BUG', + severity: 'MINOR', + lang: 'py', + langName: 'Python', + tags: ['awesome'], + name: 'Deleted custom rule based on rule8', + templateKey: RULE_8, + status: RuleStatus.Removed, + }), ]; } |