@backstage-community/plugin-sonarqube
Version:
111 lines (109 loc) • 3.22 kB
JavaScript
class SonarQubeClient {
discoveryApi;
fetchApi;
constructor(options) {
this.discoveryApi = options.discoveryApi;
this.fetchApi = options.fetchApi;
}
async callApi(path, query) {
const response = await this.fetchApi.fetch(
`${await this.discoveryApi.getBaseUrl(
"sonarqube"
)}/${path}?${new URLSearchParams(query).toString()}`,
{
headers: {
"Content-Type": "application/json"
}
}
);
if (response.status === 200) {
return await response.json();
}
return void 0;
}
async getFindingSummary({
componentKey,
projectInstance
} = {}) {
if (!componentKey) {
return void 0;
}
const instanceKey = projectInstance || "";
const metrics = {
alert_status: void 0,
bugs: void 0,
reliability_rating: void 0,
vulnerabilities: void 0,
security_rating: void 0,
security_hotspots_reviewed: void 0,
security_review_rating: void 0,
code_smells: void 0,
sqale_rating: void 0,
coverage: void 0,
duplicated_lines_density: void 0
};
const baseUrlWrapper = await this.callApi(
"instanceUrl",
{
instanceKey
}
);
let baseUrl = baseUrlWrapper?.instanceUrl;
if (!baseUrl) {
return void 0;
}
if (!baseUrl.endsWith("/")) {
baseUrl += "/";
}
const findings = await this.callApi("findings", {
componentKey,
instanceKey
});
if (!findings) {
return void 0;
}
findings.measures.forEach((m) => {
metrics[m.metric] = m.value;
});
return {
title: componentKey,
lastAnalysis: findings.analysisDate,
metrics,
projectUrl: `${baseUrl}dashboard?id=${encodeURIComponent(componentKey)}`,
getIssuesUrl: (identifier) => `${baseUrl}project/issues?id=${encodeURIComponent(
componentKey
)}&types=${identifier.toLocaleUpperCase("en-US")}&resolved=false`,
getComponentMeasuresUrl: (identifier) => `${baseUrl}component_measures?id=${encodeURIComponent(
componentKey
)}&metric=${identifier.toLocaleLowerCase(
"en-US"
)}&resolved=false&view=list`,
getSecurityHotspotsUrl: () => `${baseUrl}${baseUrl === "https://sonarcloud.io/" ? "project/" : ""}security_hotspots?id=${encodeURIComponent(componentKey)}`
};
}
settledResponseOf(responses) {
return responses.map(
(response) => response.status === "fulfilled" ? response.value : null
);
}
async getFindingSummaries(components) {
const map = /* @__PURE__ */ new Map();
if (components.length === 0) {
return map;
}
const promises = components.map(
({ projectInstance, componentKey }) => this.getFindingSummary({ componentKey, projectInstance })
);
const summaries = await Promise.allSettled(promises).then((results) => {
return results.map((result) => result).map((result) => result.value);
});
for await (const summary of summaries) {
if (summary?.title && !map.has(summary.title)) {
map.set(summary.title, summary);
}
}
return map;
}
}
export { SonarQubeClient };
//# sourceMappingURL=SonarQubeClient.esm.js.map