From 47bdeede2e968ec828a231fe5679ea37fbe00a4d Mon Sep 17 00:00:00 2001 From: Wouter Admiraal Date: Tue, 14 Apr 2020 15:40:45 +0200 Subject: [PATCH] Remove deprecated period parameter for /api/measures/component --- server/sonar-web/src/main/js/api/measures.ts | 49 ++++++++++++++++--- .../component-measures/components/App.tsx | 18 +++---- .../components/__tests__/App-test.tsx | 12 ++--- .../apps/overview/branches/BranchOverview.tsx | 29 ++++++----- .../__tests__/BranchOverview-test.tsx | 12 ++--- .../pullRequests/PullRequestOverview.tsx | 9 ++-- .../__tests__/PullRequestOverview-test.tsx | 10 ++-- server/sonar-web/src/main/js/types/types.d.ts | 2 +- 8 files changed, 84 insertions(+), 57 deletions(-) diff --git a/server/sonar-web/src/main/js/api/measures.ts b/server/sonar-web/src/main/js/api/measures.ts index 22d2d4c5277..712ba286f0f 100644 --- a/server/sonar-web/src/main/js/api/measures.ts +++ b/server/sonar-web/src/main/js/api/measures.ts @@ -17,7 +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 { getJSON, post, postJSON, RequestData } from 'sonar-ui-common/helpers/request'; +import { getJSON, post, postJSON } from 'sonar-ui-common/helpers/request'; import throwGlobalError from '../app/utils/throwGlobalError'; import { BranchParameters } from '../types/branch-like'; @@ -27,15 +27,52 @@ export function getMeasures( return getJSON('/api/measures/component', data).then(r => r.component.measures, throwGlobalError); } -export function getMeasuresAndMeta( +interface MeasuresAndMetaWithMetrics { + component: T.ComponentMeasure; + metrics: T.Metric[]; +} + +export function getMeasuresWithMetrics( + component: string, + metrics: string[], + branchParameters?: BranchParameters +): Promise { + return getJSON('/api/measures/component', { + additionalFields: 'metrics', + component, + metricKeys: metrics.join(','), + ...branchParameters + }).catch(throwGlobalError); +} + +interface MeasuresAndMetaWithPeriod { + component: T.ComponentMeasure; + period: T.Period; +} + +export function getMeasuresWithPeriod( + component: string, + metrics: string[], + branchParameters?: BranchParameters +): Promise { + return getJSON('/api/measures/component', { + additionalFields: 'period', + component, + metricKeys: metrics.join(','), + ...branchParameters + }).catch(throwGlobalError); +} + +export function getMeasuresWithPeriodAndMetrics( component: string, metrics: string[], - additional: RequestData = {} -): Promise<{ component: T.ComponentMeasure; metrics?: T.Metric[]; periods?: T.Period[] }> { + branchParameters?: BranchParameters +): Promise { return getJSON('/api/measures/component', { - ...additional, + additionalFields: 'period,metrics', component, - metricKeys: metrics.join(',') + metricKeys: metrics.join(','), + ...branchParameters }).catch(throwGlobalError); } diff --git a/server/sonar-web/src/main/js/apps/component-measures/components/App.tsx b/server/sonar-web/src/main/js/apps/component-measures/components/App.tsx index dcd2aca7ea4..679f94670e0 100644 --- a/server/sonar-web/src/main/js/apps/component-measures/components/App.tsx +++ b/server/sonar-web/src/main/js/apps/component-measures/components/App.tsx @@ -34,16 +34,16 @@ import { removeSideBarClass, removeWhitePageClass } from 'sonar-ui-common/helpers/pages'; -import { getMeasuresAndMeta } from '../../../api/measures'; +import { getMeasuresWithPeriod } from '../../../api/measures'; import { getAllMetrics } from '../../../api/metrics'; import Suggestions from '../../../app/components/embed-docs-modal/Suggestions'; import ScreenPositionHelper from '../../../components/common/ScreenPositionHelper'; import { enhanceMeasure } from '../../../components/measure/utils'; import '../../../components/search-navigator.css'; import { getBranchLikeQuery, isPullRequest, isSameBranchLike } from '../../../helpers/branch-like'; -import { getLeakPeriod } from '../../../helpers/periods'; import { fetchBranchStatus } from '../../../store/rootActions'; import { BranchLike } from '../../../types/branch-like'; +import { ComponentQualifier } from '../../../types/component'; import Sidebar from '../sidebar/Sidebar'; import '../style.css'; import { @@ -137,21 +137,15 @@ export class App extends React.PureComponent { const filteredKeys = getMeasuresPageMetricKeys(metrics, branchLike); - getMeasuresAndMeta(componentKey, filteredKeys, { - additionalFields: 'periods', - ...getBranchLikeQuery(branchLike) - }).then( - ({ component, periods }) => { + getMeasuresWithPeriod(componentKey, filteredKeys, getBranchLikeQuery(branchLike)).then( + ({ component, period }) => { if (this.mounted) { const measures = banQualityGateMeasure(component).map(measure => enhanceMeasure(measure, metrics) ); - const newBugs = measures.find(measure => measure.metric.key === 'new_bugs'); - const applicationPeriods = newBugs ? [{ index: 1 } as T.Period] : []; - const leakPeriod = getLeakPeriod( - component.qualifier === 'APP' ? applicationPeriods : periods - ); + const leakPeriod = + component.qualifier === ComponentQualifier.Project ? period : undefined; this.setState({ loading: false, diff --git a/server/sonar-web/src/main/js/apps/component-measures/components/__tests__/App-test.tsx b/server/sonar-web/src/main/js/apps/component-measures/components/__tests__/App-test.tsx index 0fb908851a2..37c50c0c4ea 100644 --- a/server/sonar-web/src/main/js/apps/component-measures/components/__tests__/App-test.tsx +++ b/server/sonar-web/src/main/js/apps/component-measures/components/__tests__/App-test.tsx @@ -20,7 +20,7 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils'; -import { getMeasuresAndMeta } from '../../../../api/measures'; +import { getMeasuresWithPeriod } from '../../../../api/measures'; import { mockMainBranch, mockPullRequest } from '../../../../helpers/mocks/branch-like'; import { mockComponent, mockIssue, mockLocation, mockRouter } from '../../../../helpers/testMocks'; import { App } from '../App'; @@ -59,13 +59,13 @@ jest.mock('../../../../api/metrics', () => ({ })); jest.mock('../../../../api/measures', () => ({ - getMeasuresAndMeta: jest.fn() + getMeasuresWithPeriod: jest.fn() })); beforeEach(() => { - (getMeasuresAndMeta as jest.Mock).mockResolvedValue({ + (getMeasuresWithPeriod as jest.Mock).mockResolvedValue({ component: { measures: [{ metric: 'coverage', value: '80.0' }] }, - periods: [{ index: '1' }] + period: { mode: 'previous_version' } }); }); @@ -86,9 +86,9 @@ it('should render a measure overview', async () => { }); it('should render a message when there are no measures', async () => { - (getMeasuresAndMeta as jest.Mock).mockResolvedValue({ + (getMeasuresWithPeriod as jest.Mock).mockResolvedValue({ component: { measures: [] }, - periods: [{ index: '1' }] + period: { mode: 'previous_version' } }); const wrapper = shallowRender(); await waitAndUpdate(wrapper); diff --git a/server/sonar-web/src/main/js/apps/overview/branches/BranchOverview.tsx b/server/sonar-web/src/main/js/apps/overview/branches/BranchOverview.tsx index 0baa63ff420..e78b2785fcf 100644 --- a/server/sonar-web/src/main/js/apps/overview/branches/BranchOverview.tsx +++ b/server/sonar-web/src/main/js/apps/overview/branches/BranchOverview.tsx @@ -22,7 +22,7 @@ import * as React from 'react'; import { parseDate, toNotSoISOString } from 'sonar-ui-common/helpers/dates'; import { isDefined } from 'sonar-ui-common/helpers/types'; import { getApplicationLeak } from '../../../api/application'; -import { getMeasuresAndMeta } from '../../../api/measures'; +import { getMeasuresWithPeriodAndMetrics } from '../../../api/measures'; import { getProjectActivity } from '../../../api/projectActivity'; import { getApplicationQualityGate, getQualityGateProjectStatus } from '../../../api/quality-gates'; import { getTimeMachineData } from '../../../api/time-machine'; @@ -37,7 +37,6 @@ import { isSameBranchLike } from '../../../helpers/branch-like'; import { enhanceConditionWithMeasure, enhanceMeasuresWithMetrics } from '../../../helpers/measures'; -import { getLeakPeriod } from '../../../helpers/periods'; import { extractStatusConditionsFromApplicationStatusChildProject, extractStatusConditionsFromProjectStatus @@ -66,7 +65,7 @@ interface State { measures?: T.MeasureEnhanced[]; measuresHistory?: MeasureHistory[]; metrics?: T.Metric[]; - periods?: T.Period[]; + period?: T.Period; qgStatuses?: QualityGateStatus[]; } @@ -124,7 +123,7 @@ export default class BranchOverview extends React.PureComponent { application: component.key, ...getBranchLikeQuery(branchLike) }); - const { measures: appMeasures, metrics, periods } = await this.loadMeasuresAndMeta( + const { measures: appMeasures, metrics, period } = await this.loadMeasuresAndMeta( component.key ); @@ -183,7 +182,7 @@ export default class BranchOverview extends React.PureComponent { loadingStatus: false, measures: appMeasures, metrics, - periods, + period, qgStatuses }); } @@ -216,7 +215,7 @@ export default class BranchOverview extends React.PureComponent { : METRICS; this.loadMeasuresAndMeta(key, metricKeys).then( - ({ measures, metrics, periods }) => { + ({ measures, metrics, period }) => { if (this.mounted && measures) { const { ignoredConditions, status } = projectStatus; const conditions = extractStatusConditionsFromProjectStatus(projectStatus); @@ -234,7 +233,7 @@ export default class BranchOverview extends React.PureComponent { loadingStatus: false, measures, metrics, - periods, + period, qgStatuses: [qgStatus] }); } else if (this.mounted) { @@ -252,14 +251,15 @@ export default class BranchOverview extends React.PureComponent { loadMeasuresAndMeta = (componentKey: string, metricKeys: string[] = []) => { const { branchLike } = this.props; - return getMeasuresAndMeta(componentKey, metricKeys.length > 0 ? metricKeys : METRICS, { - additionalFields: 'metrics,periods', - ...getBranchLikeQuery(branchLike) - }).then(({ component: { measures }, metrics, periods }) => { + return getMeasuresWithPeriodAndMetrics( + componentKey, + metricKeys.length > 0 ? metricKeys : METRICS, + getBranchLikeQuery(branchLike) + ).then(({ component: { measures }, metrics, period }) => { return { measures: enhanceMeasuresWithMetrics(measures || [], metrics || []), metrics, - periods + period }; }); }; @@ -381,12 +381,11 @@ export default class BranchOverview extends React.PureComponent { measures, measuresHistory, metrics, - periods, + period, qgStatuses } = this.state; - const leakPeriod = - component.qualifier === ComponentQualifier.Application ? appLeak : getLeakPeriod(periods); + const leakPeriod = component.qualifier === ComponentQualifier.Application ? appLeak : period; const projectIsEmpty = loadingStatus === false && diff --git a/server/sonar-web/src/main/js/apps/overview/branches/__tests__/BranchOverview-test.tsx b/server/sonar-web/src/main/js/apps/overview/branches/__tests__/BranchOverview-test.tsx index 56e0a1bce43..37c3a8f3fe9 100644 --- a/server/sonar-web/src/main/js/apps/overview/branches/__tests__/BranchOverview-test.tsx +++ b/server/sonar-web/src/main/js/apps/overview/branches/__tests__/BranchOverview-test.tsx @@ -23,7 +23,7 @@ import * as React from 'react'; import { isDiffMetric } from 'sonar-ui-common/helpers/measures'; import { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils'; import { getApplicationLeak } from '../../../../api/application'; -import { getMeasuresAndMeta } from '../../../../api/measures'; +import { getMeasuresWithPeriodAndMetrics } from '../../../../api/measures'; import { getProjectActivity } from '../../../../api/projectActivity'; import { getApplicationQualityGate, @@ -47,7 +47,7 @@ jest.mock('sonar-ui-common/helpers/dates', () => ({ jest.mock('../../../../api/measures', () => { const { mockMeasure, mockMetric } = require.requireActual('../../../../helpers/testMocks'); return { - getMeasuresAndMeta: jest.fn((_, metricKeys: string[]) => { + getMeasuresWithPeriodAndMetrics: jest.fn((_, metricKeys: string[]) => { const metrics: T.Metric[] = []; const measures: T.Measure[] = []; metricKeys.forEach(key => { @@ -184,7 +184,7 @@ describe('project overview', () => { const wrapper = shallowRender(); await waitAndUpdate(wrapper); expect(getQualityGateProjectStatus).toBeCalled(); - expect(getMeasuresAndMeta).toBeCalled(); + expect(getMeasuresWithPeriodAndMetrics).toBeCalled(); // Check the conditions got correctly enhanced with measure meta data. const { qgStatuses } = wrapper.state(); @@ -220,7 +220,7 @@ describe('project overview', () => { }); it('should correctly flag a project as empty', async () => { - (getMeasuresAndMeta as jest.Mock).mockResolvedValueOnce({ component: {} }); + (getMeasuresWithPeriodAndMetrics as jest.Mock).mockResolvedValueOnce({ component: {} }); const wrapper = shallowRender(); await waitAndUpdate(wrapper); @@ -245,7 +245,7 @@ describe('application overview', () => { await waitAndUpdate(wrapper); expect(getApplicationQualityGate).toBeCalled(); expect(getApplicationLeak).toBeCalled(); - expect(getMeasuresAndMeta).toBeCalled(); + expect(getMeasuresWithPeriodAndMetrics).toBeCalled(); // Check the conditions got correctly enhanced with measure meta data. const { qgStatuses } = wrapper.state(); @@ -300,7 +300,7 @@ describe('application overview', () => { }); it('should correctly flag an application as empty', async () => { - (getMeasuresAndMeta as jest.Mock).mockResolvedValueOnce({ component: {} }); + (getMeasuresWithPeriodAndMetrics as jest.Mock).mockResolvedValueOnce({ component: {} }); const wrapper = shallowRender({ component }); await waitAndUpdate(wrapper); diff --git a/server/sonar-web/src/main/js/apps/overview/pullRequests/PullRequestOverview.tsx b/server/sonar-web/src/main/js/apps/overview/pullRequests/PullRequestOverview.tsx index 4be5783d3af..176ede6361f 100644 --- a/server/sonar-web/src/main/js/apps/overview/pullRequests/PullRequestOverview.tsx +++ b/server/sonar-web/src/main/js/apps/overview/pullRequests/PullRequestOverview.tsx @@ -25,7 +25,7 @@ import HelpTooltip from 'sonar-ui-common/components/controls/HelpTooltip'; import { Alert } from 'sonar-ui-common/components/ui/Alert'; import { translate } from 'sonar-ui-common/helpers/l10n'; import { isDefined } from 'sonar-ui-common/helpers/types'; -import { getMeasuresAndMeta } from '../../../api/measures'; +import { getMeasuresWithMetrics } from '../../../api/measures'; import DocTooltip from '../../../components/docs/DocTooltip'; import { getBranchLikeQuery } from '../../../helpers/branch-like'; import { enhanceConditionWithMeasure, enhanceMeasuresWithMetrics } from '../../../helpers/measures'; @@ -128,15 +128,12 @@ export class PullRequestOverview extends React.PureComponent { uniq([...PR_METRICS, ...conditions.filter(c => c.level !== 'OK').map(c => c.metric)]) : PR_METRICS; - getMeasuresAndMeta(key, metricKeys, { - additionalFields: 'metrics', - ...getBranchLikeQuery(branchLike) - }).then( + getMeasuresWithMetrics(key, metricKeys, getBranchLikeQuery(branchLike)).then( ({ component, metrics }) => { if (this.mounted && component.measures) { this.setState({ loading: false, - measures: enhanceMeasuresWithMetrics(component.measures || [], metrics || []) + measures: enhanceMeasuresWithMetrics(component.measures || [], metrics) }); } }, diff --git a/server/sonar-web/src/main/js/apps/overview/pullRequests/__tests__/PullRequestOverview-test.tsx b/server/sonar-web/src/main/js/apps/overview/pullRequests/__tests__/PullRequestOverview-test.tsx index ae06e66200a..60bc3122c57 100644 --- a/server/sonar-web/src/main/js/apps/overview/pullRequests/__tests__/PullRequestOverview-test.tsx +++ b/server/sonar-web/src/main/js/apps/overview/pullRequests/__tests__/PullRequestOverview-test.tsx @@ -20,7 +20,7 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils'; -import { getMeasuresAndMeta } from '../../../../api/measures'; +import { getMeasuresWithMetrics } from '../../../../api/measures'; import { mockPullRequest } from '../../../../helpers/mocks/branch-like'; import { mockQualityGateStatusCondition } from '../../../../helpers/mocks/quality-gates'; import { mockComponent } from '../../../../helpers/testMocks'; @@ -30,7 +30,7 @@ import { PullRequestOverview } from '../PullRequestOverview'; jest.mock('../../../../api/measures', () => { const { mockMeasure, mockMetric } = require.requireActual('../../../../helpers/testMocks'); return { - getMeasuresAndMeta: jest.fn().mockResolvedValue({ + getMeasuresWithMetrics: jest.fn().mockResolvedValue({ component: { measures: [ mockMeasure({ metric: 'new_bugs' }), @@ -73,7 +73,7 @@ it('should render correctly for a passed QG', async () => { expect(wrapper.find('QualityGateConditions').exists()).toBe(false); - expect(getMeasuresAndMeta).toBeCalled(); + expect(getMeasuresWithMetrics).toBeCalled(); expect(fetchBranchStatus).toBeCalled(); }); @@ -107,7 +107,7 @@ it('should render correctly for a failed QG', async () => { it('should correctly fetch all required metrics for a passing QG', async () => { const wrapper = shallowRender({ conditions: [] }); await waitAndUpdate(wrapper); - expect(getMeasuresAndMeta).toBeCalledWith('my-project', PR_METRICS, expect.any(Object)); + expect(getMeasuresWithMetrics).toBeCalledWith('my-project', PR_METRICS, expect.any(Object)); }); it('should correctly fetch all required metrics for a failing QG', async () => { @@ -115,7 +115,7 @@ it('should correctly fetch all required metrics for a failing QG', async () => { conditions: [mockQualityGateStatusCondition({ level: 'ERROR', metric: 'foo' })] }); await waitAndUpdate(wrapper); - expect(getMeasuresAndMeta).toBeCalledWith( + expect(getMeasuresWithMetrics).toBeCalledWith( 'my-project', [...PR_METRICS, 'foo'], expect.any(Object) diff --git a/server/sonar-web/src/main/js/types/types.d.ts b/server/sonar-web/src/main/js/types/types.d.ts index 365b9e59c18..b1dcc3c788c 100644 --- a/server/sonar-web/src/main/js/types/types.d.ts +++ b/server/sonar-web/src/main/js/types/types.d.ts @@ -556,7 +556,7 @@ declare namespace T { export interface Period { date: string; - index: number; + index?: number; mode: PeriodMode | NewCodePeriodSettingType; modeParam?: string; parameter?: string; -- 2.39.5