Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Integration][SonarQube] Bug | Subsequent resync in SonarQube causes deletion of entities #1359

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion integrations/sonarqube/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- towncrier release notes start -->

## 0.1.142 (2025-01-29)
## 0.1.143 (2025-02-03)


### Improvements

- Bumped ocean version to ^0.18.6


## 0.1.142 (2025-01-29)


### Bug Fixes

- Fixed a bug where the global sonar_client loses its HTTP header context during scheduled resyncs, triggering 403 errors that ultimately leads to the unintended deletion of ingested entities


## 0.1.141 (2025-01-28)


Expand Down
11 changes: 8 additions & 3 deletions integrations/sonarqube/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ def init_sonar_client() -> SonarQubeClient:
)


sonar_client = init_sonar_client()


@ocean.on_resync(ObjectKind.PROJECTS)
async def on_project_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
sonar_client = init_sonar_client()
logger.warning(
"The `project` resource is deprecated. Please use `projects_ga` instead."
)
Expand All @@ -53,6 +51,7 @@ async def on_project_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:

@ocean.on_resync(ObjectKind.PROJECTS_GA)
async def on_ga_project_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
sonar_client = init_sonar_client()
selector = cast(SonarQubeGAProjectResourceConfig, event.resource_config).selector
sonar_client.metrics = selector.metrics
params = {}
Expand All @@ -66,6 +65,7 @@ async def on_ga_project_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:

@ocean.on_resync(ObjectKind.ISSUES)
async def on_issues_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
sonar_client = init_sonar_client()
selector = cast(SonarQubeIssueResourceConfig, event.resource_config).selector
query_params = selector.generate_request_params()
project_query_params = (
Expand All @@ -84,6 +84,7 @@ async def on_issues_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
@ocean.on_resync(ObjectKind.ANALYSIS)
@ocean.on_resync(ObjectKind.SASS_ANALYSIS)
async def on_saas_analysis_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
sonar_client = init_sonar_client()
if not ocean.integration_config["sonar_is_on_premise"]:
logger.info("Sonar is not on-premise, processing SonarCloud on saas analysis")
async for analyses_list in sonar_client.get_all_sonarcloud_analyses():
Expand All @@ -93,6 +94,7 @@ async def on_saas_analysis_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:

@ocean.on_resync(ObjectKind.ONPREM_ANALYSIS)
async def on_onprem_analysis_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
sonar_client = init_sonar_client()
if ocean.integration_config["sonar_is_on_premise"]:
logger.info("Sonar is on-premise, processing on-premise SonarQube analysis")
async for analyses_list in sonar_client.get_all_sonarqube_analyses():
Expand All @@ -102,6 +104,7 @@ async def on_onprem_analysis_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:

@ocean.on_resync(ObjectKind.PORTFOLIOS)
async def on_portfolio_resync(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
sonar_client = init_sonar_client()
async for portfolio_list in sonar_client.get_all_portfolios():
logger.info(f"Received portfolio batch of size: {len(portfolio_list)}")
yield portfolio_list
Expand All @@ -112,6 +115,7 @@ async def handle_sonarqube_webhook(webhook_data: dict[str, Any]) -> None:
logger.info(
f"Processing Sonarqube webhook for event type: {webhook_data.get('project', {}).get('key')}"
)
sonar_client = init_sonar_client()

project = await sonar_client.get_single_component(
webhook_data.get("project", {})
Expand Down Expand Up @@ -145,6 +149,7 @@ async def on_start() -> None:
"Organization ID is required for SonarCloud. Please specify a valid sonarOrganizationId"
)

sonar_client = init_sonar_client()
sonar_client.sanity_check()

if ocean.event_listener_type == "ONCE":
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.142"
version = "0.1.143"
description = "SonarQube projects and code quality analysis integration"
authors = ["Port Team <support@getport.io>"]

Expand Down