-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
fix(preprod): truncate max status check length #102987
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
Merged
+132
−5
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c3b708c
fix
trevor-e d1859e9
fix
trevor-e 5350e6e
fix
trevor-e f774eb7
truncate
trevor-e 493ae32
fix
trevor-e acc2cb6
oops
trevor-e 65dab2c
types
trevor-e fca94cc
fix
trevor-e dea1b55
tests
trevor-e b33374a
Merge branch 'telkins/status-check-permissions' into telkins/status-c…
trevor-e a3fa462
tests
trevor-e f50d6d7
link
trevor-e aa97ec9
Merge branch 'master' into telkins/status-check-truncate
trevor-e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| from sentry.preprod.models import PreprodArtifact, PreprodArtifactSizeMetrics | ||
| from sentry.preprod.url_utils import get_preprod_artifact_url | ||
| from sentry.preprod.vcs.status_checks.size.templates import format_status_check_messages | ||
| from sentry.shared_integrations.exceptions import ApiError, IntegrationConfigurationError | ||
| from sentry.silo.base import SiloMode | ||
| from sentry.tasks.base import instrumented_task | ||
| from sentry.taskworker.namespaces import integrations_tasks | ||
|
|
@@ -36,7 +37,7 @@ | |
| name="sentry.preprod.tasks.create_preprod_status_check", | ||
| namespace=integrations_tasks, | ||
| processing_deadline_duration=30, | ||
| retry=Retry(times=3), | ||
| retry=Retry(times=3, ignore=(IntegrationConfigurationError,)), | ||
| silo_mode=SiloMode.REGION, | ||
| ) | ||
| def create_preprod_status_check_task(preprod_artifact_id: int) -> None: | ||
|
|
@@ -321,19 +322,44 @@ def create_status_check( | |
| ) | ||
| return None | ||
|
|
||
| truncated_text = _truncate_to_byte_limit(text, GITHUB_MAX_TEXT_FIELD_LENGTH) | ||
| truncated_summary = _truncate_to_byte_limit(summary, GITHUB_MAX_SUMMARY_FIELD_LENGTH) | ||
|
|
||
| if text and len(truncated_text) != len(text): | ||
| logger.warning( | ||
| "preprod.status_checks.create.text_truncated", | ||
| extra={ | ||
| "original_bytes": len(text.encode("utf-8")), | ||
| "truncated_bytes": len(truncated_text.encode("utf-8")), | ||
| "organization_id": self.organization_id, | ||
| "organization_slug": self.organization_slug, | ||
| }, | ||
| ) | ||
|
|
||
| if summary and len(truncated_summary) != len(summary): | ||
| logger.warning( | ||
| "preprod.status_checks.create.summary_truncated", | ||
| extra={ | ||
| "original_bytes": len(summary.encode("utf-8")), | ||
| "truncated_bytes": len(truncated_summary.encode("utf-8")), | ||
| "organization_id": self.organization_id, | ||
| "organization_slug": self.organization_slug, | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| ) | ||
|
|
||
| check_data: dict[str, Any] = { | ||
| "name": title, | ||
| "head_sha": sha, | ||
| "external_id": external_id, | ||
| "output": { | ||
| "title": subtitle, | ||
| "summary": summary, | ||
| "summary": truncated_summary, | ||
| }, | ||
| "status": mapped_status.value, | ||
| } | ||
|
|
||
| if text: | ||
| check_data["output"]["text"] = text | ||
| if truncated_text: | ||
| check_data["output"]["text"] = truncated_text | ||
|
|
||
| if mapped_conclusion: | ||
| check_data["conclusion"] = mapped_conclusion.value | ||
|
|
@@ -351,9 +377,70 @@ def create_status_check( | |
| response = self.client.create_check_run(repo=repo, data=check_data) | ||
| check_id = response.get("id") | ||
| return str(check_id) if check_id else None | ||
| except Exception as e: | ||
| except ApiError as e: | ||
| lifecycle.record_failure(e) | ||
| return None | ||
|
|
||
| # 4xx client errors (except 429) are not transient (our fault or configuration issues) | ||
| # Convert them to IntegrationConfigurationError to prevent retries | ||
| # 429 rate limits, 5xx server errors, and other transient issues will bubble up and be retriable. | ||
| if e.code and 400 <= e.code < 500 and e.code != 429: | ||
| if e.code == 403: | ||
| error_message = str(e).lower() | ||
| if ( | ||
| "resource not accessible" in error_message | ||
| or "insufficient" in error_message | ||
| or "permission" in error_message | ||
| ): | ||
| logger.exception( | ||
| "preprod.status_checks.create.insufficient_permissions", | ||
| extra={ | ||
| "organization_id": self.organization_id, | ||
| "integration_id": self.integration_id, | ||
| "repo": repo, | ||
| }, | ||
| ) | ||
| raise IntegrationConfigurationError( | ||
| "GitHub App lacks permissions to create check runs. " | ||
| "Please ensure the app has the required permissions and that " | ||
| "the organization has accepted any updated permissions." | ||
| ) from e | ||
|
|
||
| logger.exception( | ||
| "preprod.status_checks.create.client_error", | ||
| extra={ | ||
| "organization_id": self.organization_id, | ||
| "integration_id": self.integration_id, | ||
| "repo": repo, | ||
| "status_code": e.code, | ||
| }, | ||
| ) | ||
| raise IntegrationConfigurationError( | ||
| f"GitHub API returned {e.code} client error when creating check run" | ||
| ) from e | ||
|
|
||
| # For 5xx or other errors, re-raise to allow retries | ||
| raise | ||
|
|
||
|
|
||
| GITHUB_MAX_SUMMARY_FIELD_LENGTH = 65535 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where'd you find references to these limits? Would rec commenting the links to their docs if there is one.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea, added |
||
| GITHUB_MAX_TEXT_FIELD_LENGTH = 65535 | ||
|
|
||
|
|
||
| def _truncate_to_byte_limit(text: str | None, byte_limit: int) -> str | None: | ||
| """Truncate text to fit within byte limit while ensuring valid UTF-8.""" | ||
| if not text: | ||
| return text | ||
|
|
||
| encoded = text.encode("utf-8") | ||
| if len(encoded) <= byte_limit: | ||
| return text | ||
|
|
||
| # Truncate to byte_limit - 10 (a bit of wiggle room) to make room for "..." | ||
| # Note: this can break formatting you have and is more of a catch-all, | ||
| # broken formatting is better than silently erroring for the user. | ||
| # Templating logic itself should try to more contextually trim the content if possible. | ||
| truncated = encoded[: byte_limit - 10].decode("utf-8", errors="ignore") | ||
| return truncated + "..." | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| GITHUB_STATUS_CHECK_STATUS_MAPPING: dict[StatusCheckStatus, GitHubCheckStatus] = { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.