Fixed commit workflow
This commit is contained in:
+14
-22
@@ -2,6 +2,7 @@
|
|||||||
from .base_tool import BaseTool
|
from .base_tool import BaseTool
|
||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
|
import base64
|
||||||
|
|
||||||
class GitHubTool(BaseTool):
|
class GitHubTool(BaseTool):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -110,8 +111,6 @@ class GitHubTool(BaseTool):
|
|||||||
return self._create_branch(kwargs["branch_name"], kwargs.get("base_branch", "main"))
|
return self._create_branch(kwargs["branch_name"], kwargs.get("base_branch", "main"))
|
||||||
elif function_name == "commit_file":
|
elif function_name == "commit_file":
|
||||||
return self._commit_file(kwargs["branch_name"], kwargs["file_path"], kwargs["content"], kwargs["commit_message"])
|
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":
|
elif function_name == "create_pull_request":
|
||||||
return self._create_pull_request(kwargs["title"], kwargs["body"], kwargs["head"], kwargs.get("base", "main"))
|
return self._create_pull_request(kwargs["title"], kwargs["body"], kwargs["head"], kwargs.get("base", "main"))
|
||||||
else:
|
else:
|
||||||
@@ -149,16 +148,27 @@ class GitHubTool(BaseTool):
|
|||||||
return "Cannot commit directly to main branch"
|
return "Cannot commit directly to main branch"
|
||||||
|
|
||||||
url = f"{self.base_url}/repos/{self.repo}/contents/{file_path}"
|
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 = {
|
data = {
|
||||||
"message": commit_message,
|
"message": commit_message,
|
||||||
"content": content,
|
"content": base64.b64encode(content.encode()).decode(),
|
||||||
"branch": branch_name
|
"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)
|
response = requests.put(url, headers=self.headers, json=data)
|
||||||
|
|
||||||
if response.status_code in [200, 201]:
|
if response.status_code in [200, 201]:
|
||||||
return f"File committed successfully to branch '{branch_name}'"
|
return f"File committed successfully to branch '{branch_name}'"
|
||||||
else:
|
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):
|
def _create_pull_request(self, title, body, head, base):
|
||||||
url = f"{self.base_url}/repos/{self.repo}/pulls"
|
url = f"{self.base_url}/repos/{self.repo}/pulls"
|
||||||
@@ -174,21 +184,3 @@ class GitHubTool(BaseTool):
|
|||||||
else:
|
else:
|
||||||
return f"Error creating pull request: {response.status_code}\nResponse: {response.text}"
|
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