Skip to content

Commit

Permalink
update commitonly to create two branches
Browse files Browse the repository at this point in the history
  • Loading branch information
aglassoforange committed Dec 16, 2024
1 parent 944e8b6 commit bd55f17
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 131 deletions.
18 changes: 6 additions & 12 deletions .github/workflows/committest_1216.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,12 @@ jobs:
# Create commits for both models (if successful) and store their commit hashes in Firebase

- name: Commit changes for model 1
if: env.MODEL1_RESOLUTION_SUCCESS == 'true'
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
LLM_MODELS: ${{ secrets.LLM_MODELS }}
LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
FIREBASE_CONFIG: ${{ secrets.FIREBASE_CONFIG }}
GITHUB_USERNAME: ${{ secrets.PAT_USERNAME }}
FIREBASE_CONFIG: ${{ secrets.FIREBASE_CONFIG }}
run: |
cd /tmp && python commit_only.py \
python commit_only.py \
--issue-number ${{ env.ISSUE_NUMBER }} \
--model-number 1 \
--repo ${{ github.repository }} \
Expand All @@ -125,16 +121,12 @@ jobs:
--firebase-config "${{ secrets.FIREBASE_CONFIG }}"
- name: Commit changes for model 2
if: env.MODEL2_RESOLUTION_SUCCESS == 'true'
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
LLM_MODELS: ${{ secrets.LLM_MODELS }}
LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
FIREBASE_CONFIG: ${{ secrets.FIREBASE_CONFIG }}
GITHUB_USERNAME: ${{ secrets.PAT_USERNAME }}
FIREBASE_CONFIG: ${{ secrets.FIREBASE_CONFIG }}
run: |
cd /tmp && python commit_only.py \
python commit_only.py \
--issue-number ${{ env.ISSUE_NUMBER }} \
--model-number 2 \
--repo ${{ github.repository }} \
Expand All @@ -144,6 +136,8 @@ jobs:
--firebase-config "${{ secrets.FIREBASE_CONFIG }}"

# Now that both commits are created and their hashes stored in Firebase, post the webpage link
- name: Post webpage link to GitHub issue comment
uses: actions/github-script@v7
Expand Down
188 changes: 69 additions & 119 deletions openhands_resolver/commit_only.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import argparse
import os
import shutil
import json
import subprocess
from openhands_resolver.resolver_output import ResolverOutput
from openhands_resolver.io_utils import load_single_resolver_output
from openhands_resolver.github_issue import GithubIssue
from openhands_resolver.patching import parse_patch, apply_diff
import firebase_admin
from firebase_admin import credentials, firestore


def apply_patch(repo_dir: str, patch: str) -> None:
diffs = parse_patch(patch)
Expand All @@ -22,100 +23,57 @@ def apply_patch(repo_dir: str, patch: str) -> None:
)
new_path = os.path.join(repo_dir, diff.header.new_path.removeprefix("b/"))

# Check if the file is being deleted
if diff.header.new_path == "/dev/null":
if old_path and os.path.exists(old_path):
os.remove(old_path)
print(f"Deleted file: {old_path}")
continue

if old_path and os.path.exists(old_path):
# Detect line endings
with open(old_path, "rb") as f:
original_content = f.read()

if b"\r\n" in original_content:
newline = "\r\n"
elif b"\n" in original_content:
newline = "\n"
else:
newline = None

with open(old_path, "r", newline=newline) as f:
split_content = [x.strip(newline) for x in f.readlines()]
with open(old_path, "r") as f:
original_content = f.readlines()
else:
newline = "\n"
split_content = []

if diff.changes is None:
print(f"Warning: No changes to apply for {old_path}")
continue
original_content = []

new_content = apply_diff(diff, split_content)
new_content = apply_diff(diff, original_content)

# Ensure the directory exists before writing the file
os.makedirs(os.path.dirname(new_path), exist_ok=True)

# Write the new content using the detected line endings
with open(new_path, "w", newline=newline) as f:
for line in new_content:
print(line, file=f)
with open(new_path, "w") as f:
f.writelines(new_content)

print("Patch applied successfully")

def initialize_repo(output_dir: str, issue_number: int, issue_type: str, base_commit: str | None = None) -> str:
src_dir = os.path.join(output_dir, "repo")
dest_dir = os.path.join(output_dir, "patches", f"{issue_type}_{issue_number}")

if not os.path.exists(src_dir):
raise ValueError(f"Source directory {src_dir} does not exist.")

if os.path.exists(dest_dir):
shutil.rmtree(dest_dir)
def initialize_repo(output_dir: str, base_commit: str) -> str:
repo_dir = os.path.join(output_dir, "repo")
if not os.path.exists(repo_dir):
raise ValueError(f"Source directory {repo_dir} does not exist.")

shutil.copytree(src_dir, dest_dir)
print(f"Copied repository to {dest_dir}")

if base_commit:
result = subprocess.run(
["git", "-C", dest_dir, "checkout", base_commit],
capture_output=True,
text=True,
)
if result.returncode != 0:
print(f"Error checking out commit: {result.stderr}")
raise RuntimeError("Failed to check out commit")

return dest_dir

def configure_git(repo_dir: str):
result = subprocess.run(
["git", "-C", repo_dir, "config", "user.name"],
["git", "-C", repo_dir, "checkout", base_commit],
capture_output=True,
text=True,
)
if result.returncode != 0:
print(f"Error checking out base commit: {result.stderr}")
raise RuntimeError("Failed to check out base commit")

if not result.stdout.strip():
subprocess.run(
["git", "-C", repo_dir, "config", "user.name", "openhands"],
check=True
)
subprocess.run(
["git", "-C", repo_dir, "config", "user.email", "openhands@all-hands.dev"],
check=True
)
print("Git user configured as openhands")
return repo_dir

def make_commit(repo_dir: str, issue: GithubIssue, issue_type: str) -> None:
configure_git(repo_dir)
result = subprocess.run(

def create_branch(repo_dir: str, branch_name: str):
subprocess.run(
["git", "-C", repo_dir, "checkout", "-b", branch_name],
check=True
)
print(f"Created and switched to branch: {branch_name}")


def make_commit(repo_dir: str, issue: GithubIssue, issue_type: str) -> str:
subprocess.run(
["git", "-C", repo_dir, "add", "."],
capture_output=True,
text=True
check=True
)
if result.returncode != 0:
print(f"Error adding files: {result.stderr}")
raise RuntimeError("Failed to add files to git")

commit_message = f"Fix {issue_type} #{issue.number}: {issue.title}"
result = subprocess.run(
Expand All @@ -126,83 +84,75 @@ def make_commit(repo_dir: str, issue: GithubIssue, issue_type: str) -> None:
if result.returncode != 0:
raise RuntimeError(f"Failed to commit changes: {result.stderr}")

# Return commit hash
commit_hash = subprocess.check_output(
["git", "-C", repo_dir, "rev-parse", "HEAD"]
).decode("utf-8").strip()
return commit_hash

def update_firebase_commit_id(firebase_config: dict, owner: str, repo: str, issue_number: int, model_number: int, commit_hash: str):
# Initialize Firebase if not already initialized
import firebase_admin
from firebase_admin import credentials, firestore

# Only initialize app if not already initialized
def push_branch(repo_dir: str, branch_name: str, github_token: str, github_username: str):
result = subprocess.run(
["git", "-C", repo_dir, "push", "origin", branch_name],
capture_output=True,
text=True,
env={"GIT_ASKPASS": "/bin/echo", "GIT_USERNAME": github_username, "GIT_PASSWORD": github_token}
)
if result.returncode != 0:
raise RuntimeError(f"Failed to push branch: {result.stderr}")
print(f"Pushed branch {branch_name} to remote")


def update_firebase_commit_id(firebase_config: str, owner: str, repo: str, issue_number: int, model_number: int, commit_hash: str):
if not firebase_admin._apps:
cred = credentials.Certificate(firebase_config)
firebase_admin.initialize_app(cred)

db = firestore.client()
doc_ref = db.collection("issues").document(f"{owner}-{repo}-{issue_number}")

# Determine which field to update based on model_number
field_name = f"json{model_number}.commit_id"

# Perform the update only for the commit_id field
doc_ref.update({field_name: commit_hash})
print(f"Updated {field_name} in Firebase with commit hash {commit_hash}")


def main():
parser = argparse.ArgumentParser(description="Apply patch and commit only (no PR).")
parser = argparse.ArgumentParser(description="Apply patch, commit, and update Firebase.")
parser.add_argument("--issue-number", type=int, required=True)
parser.add_argument("--model-number", type=int, required=True)
parser.add_argument("--repo", type=str, required=True, help="owner/repo")
parser.add_argument("--github-token", type=str, required=True)
parser.add_argument("--github-username", type=str, required=True)
parser.add_argument("--output-dir", type=str, default="output")
parser.add_argument("--firebase-config", type=str, default=None, help="Firebase config JSON")
parser.add_argument("--firebase-config", type=str, required=True, help="Path to Firebase config JSON")
args = parser.parse_args()

owner, repo = args.repo.split("/")

# Load the resolver output for this model and issue
# Load resolver output
output_path = os.path.join(args.output_dir, f"output{args.model_number}.jsonl")
resolver_output = load_single_resolver_output(output_path, args.issue_number)
issue_type = resolver_output.issue_type

# Initialize repo at base commit or head_branch (for PR)
if issue_type == "issue":
patched_repo_dir = initialize_repo(
args.output_dir,
resolver_output.issue.number,
issue_type,
resolver_output.base_commit
)
elif issue_type == "pr":
patched_repo_dir = initialize_repo(
args.output_dir,
resolver_output.issue.number,
issue_type,
resolver_output.issue.head_branch
)
else:
raise ValueError(f"Invalid issue type: {issue_type}")

apply_patch(patched_repo_dir, resolver_output.git_patch)
commit_hash = make_commit(patched_repo_dir, resolver_output.issue, issue_type)

print(f"New commit created: {commit_hash}")

if args.firebase_config:
update_firebase_with_commit(
args.firebase_config,
owner,
repo,
resolver_output.issue.number,
args.model_number,
commit_hash
)

# Initialize repo
repo_dir = initialize_repo(args.output_dir, resolver_output.base_commit)

# Create branch
branch_name = f"model{args.model_number}-branch"
create_branch(repo_dir, branch_name)

# Apply patch and commit
apply_patch(repo_dir, resolver_output.git_patch)
commit_hash = make_commit(repo_dir, resolver_output.issue, resolver_output.issue_type)

# Push branch
push_branch(repo_dir, branch_name, args.github_token, args.github_username)

# Update Firebase
update_firebase_commit_id(
args.firebase_config, owner, repo, resolver_output.issue.number, args.model_number, commit_hash
)

print(f"Commit hash for model {args.model_number}: {commit_hash}")


if __name__ == "__main__":
main()

0 comments on commit bd55f17

Please sign in to comment.