mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-10-03 02:13:28 +00:00
fix: use GitHub App installation token for PR creation instead of user token (#1149)
* fix: use GitHub App installation token for PR creation instead of user token * fix: move installation token fetch after no-changes check to avoid unnecessary API call
This commit is contained in:
parent
7d1004ad66
commit
86307affe4
2 changed files with 34 additions and 26 deletions
|
|
@ -36,6 +36,7 @@ from ..utils.github import (
|
||||||
git_has_unpushed_commits,
|
git_has_unpushed_commits,
|
||||||
git_push,
|
git_push,
|
||||||
)
|
)
|
||||||
|
from ..utils.github_app import get_github_app_installation_token
|
||||||
from ..utils.github_token import get_github_token
|
from ..utils.github_token import get_github_token
|
||||||
from ..utils.sandbox_paths import aresolve_repo_dir
|
from ..utils.sandbox_paths import aresolve_repo_dir
|
||||||
from ..utils.sandbox_state import get_sandbox_backend
|
from ..utils.sandbox_state import get_sandbox_backend
|
||||||
|
|
@ -98,6 +99,11 @@ async def open_pr_if_needed(
|
||||||
pr_body = add_pr_collaboration_note(pr_body, user_identity)
|
pr_body = add_pr_collaboration_note(pr_body, user_identity)
|
||||||
commit_message = add_user_coauthor_trailer(commit_message, user_identity)
|
commit_message = add_user_coauthor_trailer(commit_message, user_identity)
|
||||||
|
|
||||||
|
installation_token = await get_github_app_installation_token()
|
||||||
|
if not installation_token:
|
||||||
|
logger.error("Failed to get GitHub App installation token for thread %s", thread_id)
|
||||||
|
return None
|
||||||
|
|
||||||
if not thread_id:
|
if not thread_id:
|
||||||
raise ValueError("No thread_id found in config")
|
raise ValueError("No thread_id found in config")
|
||||||
|
|
||||||
|
|
@ -154,23 +160,22 @@ async def open_pr_if_needed(
|
||||||
await asyncio.to_thread(git_add_all, sandbox_backend, repo_dir)
|
await asyncio.to_thread(git_add_all, sandbox_backend, repo_dir)
|
||||||
await asyncio.to_thread(git_commit, sandbox_backend, repo_dir, commit_message)
|
await asyncio.to_thread(git_commit, sandbox_backend, repo_dir, commit_message)
|
||||||
|
|
||||||
if github_token:
|
await asyncio.to_thread(
|
||||||
await asyncio.to_thread(
|
git_push, sandbox_backend, repo_dir, target_branch, installation_token
|
||||||
git_push, sandbox_backend, repo_dir, target_branch, github_token
|
)
|
||||||
)
|
|
||||||
|
|
||||||
base_branch = await get_github_default_branch(repo_owner, repo_name, github_token)
|
base_branch = await get_github_default_branch(repo_owner, repo_name, installation_token)
|
||||||
logger.info("Using base branch: %s", base_branch)
|
logger.info("Using base branch: %s", base_branch)
|
||||||
|
|
||||||
await create_github_pr(
|
await create_github_pr(
|
||||||
repo_owner=repo_owner,
|
repo_owner=repo_owner,
|
||||||
repo_name=repo_name,
|
repo_name=repo_name,
|
||||||
github_token=github_token,
|
github_token=installation_token,
|
||||||
title=pr_title,
|
title=pr_title,
|
||||||
head_branch=target_branch,
|
head_branch=target_branch,
|
||||||
base_branch=base_branch,
|
base_branch=base_branch,
|
||||||
body=pr_body,
|
body=pr_body,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info("After-agent middleware completed successfully")
|
logger.info("After-agent middleware completed successfully")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ from ..utils.github import (
|
||||||
git_has_unpushed_commits,
|
git_has_unpushed_commits,
|
||||||
git_push,
|
git_push,
|
||||||
)
|
)
|
||||||
|
from ..utils.github_app import get_github_app_installation_token
|
||||||
from ..utils.github_token import get_github_token
|
from ..utils.github_token import get_github_token
|
||||||
from ..utils.sandbox_paths import resolve_repo_dir
|
from ..utils.sandbox_paths import resolve_repo_dir
|
||||||
from ..utils.sandbox_state import get_sandbox_backend_sync
|
from ..utils.sandbox_state import get_sandbox_backend_sync
|
||||||
|
|
@ -149,6 +150,14 @@ def commit_and_open_pr(
|
||||||
if not (has_uncommitted_changes or has_unpushed_commits):
|
if not (has_uncommitted_changes or has_unpushed_commits):
|
||||||
return {"success": False, "error": "No changes detected", "pr_url": None}
|
return {"success": False, "error": "No changes detected", "pr_url": None}
|
||||||
|
|
||||||
|
installation_token = asyncio.run(get_github_app_installation_token())
|
||||||
|
if not installation_token:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"error": "Failed to get GitHub App installation token",
|
||||||
|
"pr_url": None,
|
||||||
|
}
|
||||||
|
|
||||||
metadata = config.get("metadata", {})
|
metadata = config.get("metadata", {})
|
||||||
branch_name = metadata.get("branch_name")
|
branch_name = metadata.get("branch_name")
|
||||||
current_branch = git_current_branch(sandbox_backend, repo_dir)
|
current_branch = git_current_branch(sandbox_backend, repo_dir)
|
||||||
|
|
@ -188,15 +197,7 @@ def commit_and_open_pr(
|
||||||
"pr_url": None,
|
"pr_url": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
if not github_token:
|
push_result = git_push(sandbox_backend, repo_dir, target_branch, installation_token)
|
||||||
logger.error("commit_and_open_pr missing GitHub token for thread %s", thread_id)
|
|
||||||
return {
|
|
||||||
"success": False,
|
|
||||||
"error": "Missing GitHub token",
|
|
||||||
"pr_url": None,
|
|
||||||
}
|
|
||||||
|
|
||||||
push_result = git_push(sandbox_backend, repo_dir, target_branch, github_token)
|
|
||||||
if push_result.exit_code != 0:
|
if push_result.exit_code != 0:
|
||||||
return {
|
return {
|
||||||
"success": False,
|
"success": False,
|
||||||
|
|
@ -204,12 +205,14 @@ def commit_and_open_pr(
|
||||||
"pr_url": None,
|
"pr_url": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
base_branch = asyncio.run(get_github_default_branch(repo_owner, repo_name, github_token))
|
base_branch = asyncio.run(
|
||||||
|
get_github_default_branch(repo_owner, repo_name, installation_token)
|
||||||
|
)
|
||||||
pr_url, _pr_number, pr_existing = asyncio.run(
|
pr_url, _pr_number, pr_existing = asyncio.run(
|
||||||
create_github_pr(
|
create_github_pr(
|
||||||
repo_owner=repo_owner,
|
repo_owner=repo_owner,
|
||||||
repo_name=repo_name,
|
repo_name=repo_name,
|
||||||
github_token=github_token,
|
github_token=installation_token,
|
||||||
title=title,
|
title=title,
|
||||||
head_branch=target_branch,
|
head_branch=target_branch,
|
||||||
base_branch=base_branch,
|
base_branch=base_branch,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue