Skip to content

Commit

Permalink
[Integration][SonarQube] Added defensive mechanism to fail resync eve…
Browse files Browse the repository at this point in the history
…nt when no data is fetched from Sonar API `components/search_project` (#1147)
  • Loading branch information
mk-armah authored Nov 20, 2024
1 parent 454f7da commit 9052f6e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
12 changes: 12 additions & 0 deletions integrations/sonarqube/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- towncrier release notes start -->

## 0.1.111 (2024-11-20)


### Bug Fixes

- Added defensive mechanism to fail the resyn event for the kind when no data is fetched from Sonar API

### Improvements

- Added more logs to track the request and response object made to the Sonar API


## 0.1.110 (2024-11-12)


Expand Down
15 changes: 9 additions & 6 deletions integrations/sonarqube/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,11 @@ async def send_paginated_api_request(
query_params = query_params or {}
query_params["ps"] = PAGE_SIZE
all_resources = [] # List to hold all fetched resources

try:
logger.debug(
f"Sending API request to {method} {endpoint} with query params: {query_params}"
)

while True:
logger.info(
f"Sending API request to {method} {endpoint} with query params: {query_params}"
)
response = await self.http_client.request(
method=method,
url=f"{self.base_url}/api/{endpoint}",
Expand All @@ -141,6 +139,9 @@ async def send_paginated_api_request(
response.raise_for_status()
response_json = response.json()
resource = response_json.get(data_key, [])
if not resource:
logger.warning(f"No {data_key} found in response: {response_json}")

all_resources.extend(resource)

# Check for paging information and decide whether to fetch more pages
Expand Down Expand Up @@ -210,7 +211,9 @@ async def get_components(
data_key="components",
query_params=query_params,
)

logger.info(
f"Fetched {len(response)} components {[item.get("key") for item in response]} from SonarQube"
)
return response
except Exception as e:
logger.error(f"Error occurred while fetching components: {e}")
Expand Down
25 changes: 24 additions & 1 deletion integrations/sonarqube/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,46 @@ def init_sonar_client() -> SonarQubeClient:
@ocean.on_resync(ObjectKind.PROJECTS)
async def on_project_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
logger.info(f"Listing Sonarqube resource: {kind}")

fetched_projects = False
async for project_list in sonar_client.get_all_projects():
yield project_list
fetched_projects = True

if not fetched_projects:
logger.error("No projects found in Sonarqube")
raise RuntimeError(
"No projects found in Sonarqube, failing the resync to avoid data loss"
)


@ocean.on_resync(ObjectKind.ISSUES)
async def on_issues_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
fetched_issues = False
async for issues_list in sonar_client.get_all_issues():
yield issues_list
fetched_issues = True

if not fetched_issues:
logger.error("No issues found in Sonarqube")
raise RuntimeError(
"No issues found in Sonarqube, failing the resync to avoid data loss"
)


@ocean.on_resync(ObjectKind.ANALYSIS)
@ocean.on_resync(ObjectKind.SASS_ANALYSIS)
async def on_saas_analysis_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
if not ocean.integration_config["sonar_is_on_premise"]:
fetched_analyses = False
async for analyses_list in sonar_client.get_all_sonarcloud_analyses():
yield analyses_list
fetched_analyses = True

if not fetched_analyses:
logger.error("No analysis found in Sonarqube")
raise RuntimeError(
"No analysis found in Sonarqube, failing the resync to avoid data loss"
)


@ocean.on_resync(ObjectKind.ONPREM_ANALYSIS)
Expand Down
2 changes: 1 addition & 1 deletion integrations/sonarqube/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sonarqube"
version = "0.1.110"
version = "0.1.111"
description = "SonarQube projects and code quality analysis integration"
authors = ["Port Team <support@getport.io>"]

Expand Down

0 comments on commit 9052f6e

Please sign in to comment.