Make instanceName an optional parameter in plugin sonarqube-backend APIs
Signed-off-by: Neemys <36508659+Neemys@users.noreply.github.com>
This commit is contained in:
@@ -13,12 +13,12 @@ export function createRouter(options: RouterOptions): Promise<express.Router>;
|
||||
// @public
|
||||
export class DefaultSonarqubeInfoProvider implements SonarqubeInfoProvider {
|
||||
static fromConfig(config: Config): DefaultSonarqubeInfoProvider;
|
||||
getBaseUrl({ instanceName }: { instanceName: string }): {
|
||||
getBaseUrl({ instanceName }?: { instanceName?: string }): {
|
||||
baseUrl: string;
|
||||
};
|
||||
getFindings(
|
||||
componentKey: string,
|
||||
instanceName: string,
|
||||
instanceName?: string,
|
||||
): Promise<SonarqubeFindings | undefined>;
|
||||
}
|
||||
|
||||
@@ -45,12 +45,12 @@ export interface SonarqubeFindings {
|
||||
|
||||
// @public
|
||||
export interface SonarqubeInfoProvider {
|
||||
getBaseUrl({ instanceName }: { instanceName: string }): {
|
||||
getBaseUrl({ instanceName }?: { instanceName?: string }): {
|
||||
baseUrl: string;
|
||||
};
|
||||
getFindings(
|
||||
componentKey: string,
|
||||
instanceName: string,
|
||||
instanceName?: string,
|
||||
): Promise<SonarqubeFindings | undefined>;
|
||||
}
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ describe('createRouter', () => {
|
||||
expect(response.status).toEqual(400);
|
||||
});
|
||||
|
||||
it('use an empty string as instance name when instance key not provided', async () => {
|
||||
it('use the value as instance name when instance key not provided', async () => {
|
||||
const measures = {
|
||||
analysisDate: '2021-04-08',
|
||||
measures: [{ metric: 'vulnerabilities', value: '54' }],
|
||||
@@ -94,11 +94,12 @@ describe('createRouter', () => {
|
||||
.get('/findings')
|
||||
.query({
|
||||
componentKey: DUMMY_COMPONENT_KEY,
|
||||
instanceKey: undefined,
|
||||
})
|
||||
.send();
|
||||
|
||||
expect(getFindingsMock).toBeCalledTimes(1);
|
||||
expect(getFindingsMock).toBeCalledWith(DUMMY_COMPONENT_KEY, '');
|
||||
expect(getFindingsMock).toBeCalledWith(DUMMY_COMPONENT_KEY, undefined);
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.body).toEqual(measures);
|
||||
});
|
||||
@@ -121,5 +122,16 @@ describe('createRouter', () => {
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.body).toEqual({ instanceUrl: DUMMY_INSTANCE_URL });
|
||||
});
|
||||
|
||||
it('query default instance when instanceKey not provided', async () => {
|
||||
getBaseUrlMock.mockReturnValue({ baseUrl: DUMMY_INSTANCE_URL });
|
||||
const response = await request(app).get('/instanceUrl').send();
|
||||
expect(getBaseUrlMock).toBeCalledTimes(1);
|
||||
expect(getBaseUrlMock).toBeCalledWith({
|
||||
instanceName: undefined,
|
||||
});
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.body).toEqual({ instanceUrl: DUMMY_INSTANCE_URL });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -54,21 +54,16 @@ export async function createRouter(
|
||||
router.use(express.json());
|
||||
router.get('/findings', async (request, response) => {
|
||||
const componentKey = request.query.componentKey as string;
|
||||
let instanceKey = request.query.instanceKey as string;
|
||||
const instanceKey = request.query.instanceKey as string;
|
||||
|
||||
if (!componentKey)
|
||||
throw new InputError('ComponentKey must be provided as a single string.');
|
||||
|
||||
if (!instanceKey) {
|
||||
instanceKey = '';
|
||||
logger.info(
|
||||
`Retrieving findings for component ${componentKey} in default sonarqube instance`,
|
||||
);
|
||||
} else {
|
||||
logger.info(
|
||||
`Retrieving findings for component ${componentKey} in sonarqube instance name ${instanceKey}`,
|
||||
);
|
||||
}
|
||||
logger.info(
|
||||
instanceKey
|
||||
? `Retrieving findings for component ${componentKey} in sonarqube instance name ${instanceKey}`
|
||||
: `Retrieving findings for component ${componentKey} in default sonarqube instance`,
|
||||
);
|
||||
|
||||
response.json(
|
||||
await sonarqubeInfoProvider.getFindings(componentKey, instanceKey),
|
||||
@@ -76,19 +71,15 @@ export async function createRouter(
|
||||
});
|
||||
|
||||
router.get('/instanceUrl', (request, response) => {
|
||||
let requestedInstanceKey = request.query.instanceKey as string;
|
||||
if (requestedInstanceKey) {
|
||||
logger.info(
|
||||
`Retrieving sonarqube instance URL for key ${requestedInstanceKey}`,
|
||||
);
|
||||
} else {
|
||||
requestedInstanceKey = '';
|
||||
logger.info(
|
||||
`Retrieving default sonarqube instance URL as parameter is inexistant, empty or malformed`,
|
||||
);
|
||||
}
|
||||
const instanceKey = request.query.instanceKey as string;
|
||||
|
||||
logger.info(
|
||||
instanceKey
|
||||
? `Retrieving sonarqube instance URL for key ${instanceKey}`
|
||||
: `Retrieving default sonarqube instance URL as instanceKey is not provided`,
|
||||
);
|
||||
const { baseUrl } = sonarqubeInfoProvider.getBaseUrl({
|
||||
instanceName: requestedInstanceKey,
|
||||
instanceName: instanceKey,
|
||||
});
|
||||
response.json({
|
||||
instanceUrl: baseUrl,
|
||||
|
||||
@@ -229,6 +229,19 @@ describe('DefaultSonarqubeInfoProvider', () => {
|
||||
}
|
||||
|
||||
describe('getBaseUrl', () => {
|
||||
it('Provide base url for default from simple config and non provided instanceName', async () => {
|
||||
const provider = configureProvider({
|
||||
sonarqube: {
|
||||
baseUrl: 'https://sonarqube.example.com',
|
||||
apiKey: '123456789abcdef0123456789abcedf012',
|
||||
},
|
||||
});
|
||||
|
||||
expect(provider.getBaseUrl()).toEqual({
|
||||
baseUrl: 'https://sonarqube.example.com',
|
||||
});
|
||||
});
|
||||
|
||||
it('Provide base url for default from simple config and empty string', async () => {
|
||||
const provider = configureProvider({
|
||||
sonarqube: {
|
||||
|
||||
@@ -23,18 +23,21 @@ import fetch from 'node-fetch';
|
||||
*/
|
||||
export interface SonarqubeInfoProvider {
|
||||
/**
|
||||
* Get the sonarqube URL in configuration from a provided name.
|
||||
* Get the sonarqube URL in configuration from a provided instanceName.
|
||||
*
|
||||
* If name is omitted, default sonarqube instance is queried in config
|
||||
* If instanceName is omitted, default sonarqube instance is queried in config
|
||||
*
|
||||
* @param instanceName - Name of the sonarqube instance to get the info from
|
||||
* @returns the url of the instance
|
||||
*/
|
||||
getBaseUrl({ instanceName }: { instanceName: string }): { baseUrl: string };
|
||||
getBaseUrl({ instanceName }?: { instanceName?: string }): { baseUrl: string };
|
||||
|
||||
/**
|
||||
* Query the sonarqube instance corresponding to the instanceName to get all
|
||||
* measures for the component of key componentKey.
|
||||
*
|
||||
* If instanceName is omitted, default sonarqube instance is queried in config
|
||||
*
|
||||
* @param componentKey - component key of the project we want to get measure from.
|
||||
* @param instanceName - name of the instance (in config) where the project is hosted.
|
||||
* @returns All measures with the analysis date. Will return undefined if we
|
||||
@@ -42,7 +45,7 @@ export interface SonarqubeInfoProvider {
|
||||
*/
|
||||
getFindings(
|
||||
componentKey: string,
|
||||
instanceName: string,
|
||||
instanceName?: string,
|
||||
): Promise<SonarqubeFindings | undefined>;
|
||||
}
|
||||
|
||||
@@ -295,8 +298,10 @@ export class DefaultSonarqubeInfoProvider implements SonarqubeInfoProvider {
|
||||
* {@inheritDoc SonarqubeInfoProvider.getBaseUrl}
|
||||
* @throws Error If configuration can't be retrieved.
|
||||
*/
|
||||
getBaseUrl({ instanceName }: { instanceName: string }): { baseUrl: string } {
|
||||
const instanceConfig = this.config.getInstanceConfig(instanceName ?? '');
|
||||
getBaseUrl({ instanceName }: { instanceName?: string } = {}): {
|
||||
baseUrl: string;
|
||||
} {
|
||||
const instanceConfig = this.config.getInstanceConfig(instanceName);
|
||||
return { baseUrl: instanceConfig.baseUrl };
|
||||
}
|
||||
|
||||
@@ -306,11 +311,9 @@ export class DefaultSonarqubeInfoProvider implements SonarqubeInfoProvider {
|
||||
*/
|
||||
async getFindings(
|
||||
componentKey: string,
|
||||
instanceName: string,
|
||||
instanceName?: string,
|
||||
): Promise<SonarqubeFindings | undefined> {
|
||||
const { baseUrl, apiKey } = this.config.getInstanceConfig(
|
||||
instanceName ?? '',
|
||||
);
|
||||
const { baseUrl, apiKey } = this.config.getInstanceConfig(instanceName);
|
||||
|
||||
// get component info to retrieve analysis date
|
||||
const component =
|
||||
|
||||
Reference in New Issue
Block a user