Skip to content

Commit

Permalink
Sweep: Fix this sentry error PREdited pull_request.body Input should …
Browse files Browse the repository at this point in the history
…be a valid string (#3901)

# Description
This pull request addresses an issue where the `body` field of a pull
request event could be `None`, which led to errors when attempting to
create a `PREdited` object. The changes ensure that the `body` field is
always a valid string, even if the original input is `None`.

# Summary
- Updated the handling of the `body` field in `pull_request` events to
ensure it defaults to an empty string if `None`.
- Modified the conditional check in `sweepai/api.py` to simplify the
logic and prevent potential errors related to type handling.
- Ensured that all `pull_request` events processed by the `handle_event`
function in `sweepai/api.py` have a non-null `body` field.

Fixes #3899.

---

### 💡 To get Sweep to edit this pull request, you can:
* Comment below, and Sweep can edit the entire PR
* Comment on a file, Sweep will only modify the commented file
* Edit the original issue to get Sweep to recreate the PR from scratch

*This is an automated message generated by [Sweep
AI](https://sweep.dev).*

---------

Co-authored-by: sweep-nightly[bot] <131841235+sweep-nightly[bot]@users.noreply.github.com>
Co-authored-by: William Zeng <[email protected]>
Co-authored-by: wwzeng1 <[email protected]>
  • Loading branch information
3 people authored May 27, 2024
1 parent 4b71abd commit c9ba053
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
3 changes: 0 additions & 3 deletions sweepai/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,6 @@ def handle_event(request_dict, event):
except Exception as e:
logger.exception(f"Failed to add config to top repos: {e}")
case "pull_request", "edited":
# apparently body is sometimes None
if not request_dict.get('body', ''):
request_dict['body'] = ''
request = PREdited(**request_dict)

if (
Expand Down
2 changes: 1 addition & 1 deletion sweepai/core/vector_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def openai_call_embedding(batch: list[str], input_type: str="document"):
raise e
except openai.BadRequestError as e:
# In the future we can better handle this by averaging the embeddings of the split batch
if "This model's maximum context length" in str(e):
if "maximum context length" in str(e):
logger.warning(f"Token count exceeded for batch: {max([tiktoken_client.count(text) for text in batch])} truncating down to 8192 tokens.")
batch = [tiktoken_client.truncate_string(text) for text in batch]
return openai_call_embedding(batch, input_type)
Expand Down
3 changes: 3 additions & 0 deletions sweepai/utils/github_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ def __post_init__(self):
self.commit_hash = self.repo.get_commits()[0].sha
self.git_repo = self.clone()
self.branch = self.branch or SweepConfig.get_branch(self.repo)
# branch may have been deleted or not exist
if self.branch not in self.git_repo.heads:
raise Exception(f"Branch '{self.branch}' does not exist.")
self.git_repo.git.checkout(self.branch)

def __del__(self):
Expand Down
2 changes: 1 addition & 1 deletion sweepai/web/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class User(BaseModel):

html_url: str
title: str
body: str
body: str | None
number: int

user: User
Expand Down

0 comments on commit c9ba053

Please sign in to comment.