Fixed commit workflow
This commit is contained in:
+14
-22
@@ -2,6 +2,7 @@
|
||||
from .base_tool import BaseTool
|
||||
import requests
|
||||
import os
|
||||
import base64
|
||||
|
||||
class GitHubTool(BaseTool):
|
||||
def __init__(self):
|
||||
@@ -110,8 +111,6 @@ class GitHubTool(BaseTool):
|
||||
return self._create_branch(kwargs["branch_name"], kwargs.get("base_branch", "main"))
|
||||
elif function_name == "commit_file":
|
||||
return self._commit_file(kwargs["branch_name"], kwargs["file_path"], kwargs["content"], kwargs["commit_message"])
|
||||
elif function_name == "push_branch":
|
||||
return self._push_branch(kwargs["branch_name"])
|
||||
elif function_name == "create_pull_request":
|
||||
return self._create_pull_request(kwargs["title"], kwargs["body"], kwargs["head"], kwargs.get("base", "main"))
|
||||
else:
|
||||
@@ -149,16 +148,27 @@ class GitHubTool(BaseTool):
|
||||
return "Cannot commit directly to main branch"
|
||||
|
||||
url = f"{self.base_url}/repos/{self.repo}/contents/{file_path}"
|
||||
|
||||
# First, check if the file already exists
|
||||
response = requests.get(url, headers=self.headers, params={"ref": branch_name})
|
||||
|
||||
data = {
|
||||
"message": commit_message,
|
||||
"content": content,
|
||||
"content": base64.b64encode(content.encode()).decode(),
|
||||
"branch": branch_name
|
||||
}
|
||||
|
||||
if response.status_code == 200:
|
||||
# File exists, so we need to update it
|
||||
file_sha = response.json()["sha"]
|
||||
data["sha"] = file_sha
|
||||
|
||||
response = requests.put(url, headers=self.headers, json=data)
|
||||
|
||||
if response.status_code in [200, 201]:
|
||||
return f"File committed successfully to branch '{branch_name}'"
|
||||
else:
|
||||
return f"Error committing file: {response.status_code}"
|
||||
return f"Error committing file: {response.status_code}\nResponse: {response.text}"
|
||||
|
||||
def _create_pull_request(self, title, body, head, base):
|
||||
url = f"{self.base_url}/repos/{self.repo}/pulls"
|
||||
@@ -174,21 +184,3 @@ class GitHubTool(BaseTool):
|
||||
else:
|
||||
return f"Error creating pull request: {response.status_code}\nResponse: {response.text}"
|
||||
|
||||
def _push_branch(self, branch_name):
|
||||
url = f"{self.base_url}/repos/{self.repo}/git/refs/heads/{branch_name}"
|
||||
response = requests.get(url, headers=self.headers)
|
||||
if response.status_code != 200:
|
||||
return f"Error getting branch information: {response.status_code}"
|
||||
|
||||
sha = response.json()["object"]["sha"]
|
||||
|
||||
push_url = f"{self.base_url}/repos/{self.repo}/git/refs/heads/{branch_name}"
|
||||
data = {
|
||||
"sha": sha,
|
||||
"force": True
|
||||
}
|
||||
response = requests.patch(push_url, headers=self.headers, json=data)
|
||||
if response.status_code == 200:
|
||||
return f"Branch '{branch_name}' pushed successfully"
|
||||
else:
|
||||
return f"Error pushing branch: {response.status_code}\nResponse: {response.text}"
|
||||
|
||||
Reference in New Issue
Block a user