Skip to content

Commit

Permalink
fix: Fix sonar metrics api url (#140)
Browse files Browse the repository at this point in the history
Jira: EPMDEDP-13134
Related: #140
Change-Id: I2171f2e42e9f41d70b61d8457f622c92adb7bbc4
  • Loading branch information
callmevladik committed Jan 29, 2024
1 parent 3887989 commit a9d9d4d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ export const Details = ({ codebaseData, codebaseBranchData, pipelineRuns }: Deta
<Grid item xs={12}>
<Grid container alignItems={'center'}>
<Grid item>
<SonarQubeMetrics codebaseName={codebaseData.metadata.name} namespace={namespace} />
<SonarQubeMetrics
codebaseName={codebaseData.metadata.name}
branchName={codebaseBranchData.spec.branchName}
namespace={namespace}
/>
</Grid>
<Grid item style={{ marginLeft: 'auto' }}>
<DependencyTrackMetrics
Expand Down
12 changes: 10 additions & 2 deletions src/services/link-creation/sonar/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @jest-environment jsdom
*/

import { SonarQubeURLService } from './index';

describe('testing link-creation SonarQubeURLService', () => {
Expand Down Expand Up @@ -33,9 +37,13 @@ describe('testing link-creation SonarQubeURLService', () => {

it('should successfully create metrics api url based on given sonarURLOrigin and codebaseName params', () => {
expect(
SonarQubeURLService.createMetricsApiUrl('https://sonar-test.com', 'test-codebase-name')
SonarQubeURLService.createMetricsApiUrl(
'https://sonar-test.com',
'test-codebase-name',
'test-branch-name'
)
).toEqual(
'https://sonar-test.com/api/measures/component?component=test-codebase-name&metricKeys=bugs,code_smells,coverage,duplicated_lines_density,ncloc,sqale_rating,alert_status,reliability_rating,security_hotspots,security_rating,sqale_index,vulnerabilities'
'https://sonar-test.com/api/measures/component?component=test-codebase-name&branch=test-branch-name&metricKeys=bugs,code_smells,coverage,duplicated_lines_density,ncloc,sqale_rating,alert_status,reliability_rating,security_hotspots,security_rating,sqale_index,vulnerabilities'
);
});
});
8 changes: 6 additions & 2 deletions src/services/link-creation/sonar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ export const SonarQubeURLService = {

return `${sonarURLOrigin}/component_measures?id=${projectID}&metric=${metricName}`;
},
createMetricsApiUrl: (sonarURLOrigin: string, codebaseName: string) => {
createMetricsApiUrl: (sonarURLOrigin: string, codebaseName: string, branchName: string) => {
if (!sonarURLOrigin) {
return undefined;
}

return `${sonarURLOrigin}/api/measures/component?component=${codebaseName}&metricKeys=bugs,code_smells,coverage,duplicated_lines_density,ncloc,sqale_rating,alert_status,reliability_rating,security_hotspots,security_rating,sqale_index,vulnerabilities`;
return `${sonarURLOrigin}/api/measures/component?component=${window.encodeURIComponent(
codebaseName
)}&branch=${window.encodeURIComponent(
branchName
)}&metricKeys=bugs,code_smells,coverage,duplicated_lines_density,ncloc,sqale_rating,alert_status,reliability_rating,security_hotspots,security_rating,sqale_index,vulnerabilities`;
},
};
12 changes: 10 additions & 2 deletions src/widgets/SonarQubeMetrics/hooks/useSonarQubeMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const getMetricsValues = (metrics: SonarQubeMetricsResponse) => {
return values;
};

export const useSonarQubeMetrics = (sonarQubeBaseURL: string, codebaseName: string) => {
export const useSonarQubeMetrics = (
sonarQubeBaseURL: string,
codebaseName: string,
branchName: string
) => {
const { namespace } = useParams<EDPComponentDetailsRouteParams>();

const { data: ciSonarQubeToken } = useSecretByNameQuery<string>({
Expand All @@ -31,7 +35,11 @@ export const useSonarQubeMetrics = (sonarQubeBaseURL: string, codebaseName: stri
},
});

const fetchURL = LinkCreationService.sonar.createMetricsApiUrl(sonarQubeBaseURL, codebaseName);
const fetchURL = LinkCreationService.sonar.createMetricsApiUrl(
sonarQubeBaseURL,
codebaseName,
branchName
);

const { data, isFetched } = useQuery(['sonarQubeMetrics', { codebaseName: codebaseName }], {
queryFn: async () =>
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/SonarQubeMetrics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ const getIconCodeSmells = (value: string) =>
<Icon icon={'material-symbols:sentiment-dissatisfied-outline'} />
);

export const SonarQubeMetrics = ({ codebaseName, namespace }: SonarQubeMetricsProps) => {
export const SonarQubeMetrics = ({ codebaseName, namespace, branchName }: SonarQubeMetricsProps) => {
const classes = useStyles();
const projectID = codebaseName;

const { data: EDPComponentsURLS } = useEDPComponentsURLsQuery(namespace);
const sonarQubeBaseURL = EDPComponentsURLS?.[SYSTEM_EDP_COMPONENTS.SONAR];

const { data: metrics, isLoading } = useSonarQubeMetrics(sonarQubeBaseURL, codebaseName);
const { data: metrics, isLoading } = useSonarQubeMetrics(sonarQubeBaseURL, codebaseName, branchName);
const sonarConfigurationPage = Router.createRouteURL(routeEDPSonarIntegration.path);
const history = useHistory();

Expand Down
1 change: 1 addition & 0 deletions src/widgets/SonarQubeMetrics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export type DuplicationRating = {
export interface SonarQubeMetricsProps {
codebaseName: string;
namespace: string;
branchName: string;
}

0 comments on commit a9d9d4d

Please sign in to comment.