Change behavior of method useProjectInfo in plugin sonarqube
Will now return undefined instead of empty string. To be consistent with how the rest of the API works. Also provide unit test for this method and the other one in the same file `isSonarQubeAvailable` Signed-off-by: Neemys <36508659+Neemys@users.noreply.github.com>
This commit is contained in:
@@ -98,7 +98,11 @@ export const SonarQubeCard = ({
|
||||
const { projectKey: projectTitle, projectInstance } = useProjectInfo(entity);
|
||||
|
||||
const { value, loading } = useAsync(
|
||||
async () => sonarQubeApi.getFindingSummary(projectTitle, projectInstance),
|
||||
async () =>
|
||||
sonarQubeApi.getFindingSummary({
|
||||
componentKey: projectTitle,
|
||||
projectInstance: projectInstance,
|
||||
}),
|
||||
[sonarQubeApi, projectTitle],
|
||||
);
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2022 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import {
|
||||
isSonarQubeAvailable,
|
||||
SONARQUBE_PROJECT_INSTANCE_SEPARATOR,
|
||||
SONARQUBE_PROJECT_KEY_ANNOTATION,
|
||||
useProjectInfo,
|
||||
} from './useProjectKey';
|
||||
import { Entity } from '../../../../packages/catalog-model';
|
||||
|
||||
const createDummyEntity = (sonarqubeAnnotationValue: string): Entity => {
|
||||
return {
|
||||
apiVersion: '',
|
||||
kind: '',
|
||||
metadata: {
|
||||
name: 'dummy',
|
||||
annotations: {
|
||||
[SONARQUBE_PROJECT_KEY_ANNOTATION]: sonarqubeAnnotationValue,
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
describe('isSonarQubeAvailable', () => {
|
||||
it('returns true if sonarqube annotation defined', () => {
|
||||
const entity = createDummyEntity('dummy');
|
||||
expect(isSonarQubeAvailable(entity)).toBe(true);
|
||||
});
|
||||
it('returns false if sonarqube annotation empty', () => {
|
||||
const entity = createDummyEntity('');
|
||||
expect(isSonarQubeAvailable(entity)).toBe(false);
|
||||
});
|
||||
it('returns false if sonarqube annotation not defined', () => {
|
||||
const entity = {
|
||||
apiVersion: '',
|
||||
kind: '',
|
||||
metadata: {
|
||||
name: 'dummy',
|
||||
annotations: {},
|
||||
},
|
||||
};
|
||||
expect(isSonarQubeAvailable(entity)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('useProjectInfo', () => {
|
||||
const DUMMY_INSTANCE = 'dummyInstance';
|
||||
const DUMMY_KEY = 'dummyKey';
|
||||
it('parse annotation with key and instance', () => {
|
||||
const entity = createDummyEntity(
|
||||
DUMMY_INSTANCE + SONARQUBE_PROJECT_INSTANCE_SEPARATOR + DUMMY_KEY,
|
||||
);
|
||||
expect(useProjectInfo(entity)).toEqual({
|
||||
projectInstance: DUMMY_INSTANCE,
|
||||
projectKey: DUMMY_KEY,
|
||||
});
|
||||
});
|
||||
// compatibility with previous mono-instance sonarqube config
|
||||
it('parse annotation with only key', () => {
|
||||
const entity = createDummyEntity(DUMMY_KEY);
|
||||
expect(useProjectInfo(entity)).toEqual({
|
||||
projectInstance: undefined,
|
||||
projectKey: DUMMY_KEY,
|
||||
});
|
||||
});
|
||||
it('handle empty annotation', () => {
|
||||
const entity = createDummyEntity('');
|
||||
expect(useProjectInfo(entity)).toEqual({
|
||||
projectInstance: undefined,
|
||||
projectKey: undefined,
|
||||
});
|
||||
});
|
||||
it('handle non-existent annotation', () => {
|
||||
const entity = {
|
||||
apiVersion: '',
|
||||
kind: '',
|
||||
metadata: {
|
||||
name: 'dummy',
|
||||
annotations: {},
|
||||
},
|
||||
};
|
||||
expect(useProjectInfo(entity)).toEqual({
|
||||
projectInstance: undefined,
|
||||
projectKey: undefined,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -25,7 +25,7 @@ export const isSonarQubeAvailable = (entity: Entity) =>
|
||||
/**
|
||||
* Try to parse sonarqube information from an entity.
|
||||
*
|
||||
* If part or all info are not found, they will default to an empty string
|
||||
* If part or all info are not found, they will default to undefined
|
||||
*
|
||||
* @param entity entity to find the sonarqube information from.
|
||||
* @return a ProjectInfo properly populated.
|
||||
@@ -33,11 +33,11 @@ export const isSonarQubeAvailable = (entity: Entity) =>
|
||||
export const useProjectInfo = (
|
||||
entity: Entity,
|
||||
): {
|
||||
projectInstance: string;
|
||||
projectKey: string;
|
||||
projectInstance: string | undefined;
|
||||
projectKey: string | undefined;
|
||||
} => {
|
||||
let projectInstance = '';
|
||||
let projectKey = '';
|
||||
let projectInstance = undefined;
|
||||
let projectKey = undefined;
|
||||
const annotation =
|
||||
entity?.metadata.annotations?.[SONARQUBE_PROJECT_KEY_ANNOTATION];
|
||||
if (annotation) {
|
||||
|
||||
Reference in New Issue
Block a user