Файли
openidea/src-tauri/src/commands/github.rs
T
Deng Liuтаdengliu a5e0c41663 feat(github): add project-scoped GitHub CLI account selection (#6277)
A project can pin a GitHub CLI account in vcs.github.account (.openidea/settings.toml, a login, never a token), chosen in Settings > Version Control > GitHub or from the Pull Requests tool window header. With an account chosen, its token is resolved through gh and set as GH_TOKEN in the project environment for the project root and its agent worktrees; a .envrc GH_TOKEN still wins, and the token is cleared when the project closes or loses trust. Copilot keeps billing to its own account through COPILOT_GITHUB_TOKEN.

Automatic (the default) sets no GH_TOKEN and makes no GitHub calls on project open. When the settings page or the Pull Requests window asks for the status with several gh accounts signed in, the project's GitHub remote is probed to suggest a non-active account with access ("Use <login> for this project").

New commands: gh_project_account_status, gh_set_project_account. The gh account listing is shared with the Copilot account selector.

Co-authored-by: dengliu <[email protected]>
2026-11-27 08:16:10 +08:00

246 рядки
7.3 KiB
Rust
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
//! GitHub pull requests (`ide-github`, through the gh CLI) for the project of the window.
use crate::state::AppState;
use ide_command::desktop_command;
use ide_core::{IdeError, IdeResult, RequestId};
use ide_github::{
CreatePullRequest, GhAccountRepos, GhApiRequest, GhAvailability, PrDiffRevisions, PrListQuery, PrReviewComment,
PullRequestDetails, PullRequestSummary, ReviewEvent,
};
use std::path::PathBuf;
use tauri::ipc::Channel;
use tauri::{State, Window};
async fn blocking<T: Send + 'static>(f: impl FnOnce() -> IdeResult<T> + Send + 'static) -> IdeResult<T> {
tauri::async_runtime::spawn_blocking(f).await.map_err(|e| IdeError::internal(e.to_string()))?
}
/// The working directory of gh: the project repository `repo` names, else the project's
/// primary repository (a folder holding several repositories is not one itself).
fn root(window: &Window, state: &AppState, repo: Option<PathBuf>) -> IdeResult<PathBuf> {
let project = state.project(window.label())?;
let repos = project.repos();
match repo {
Some(at) => match repos.at_root(&at) {
Some(r) => Ok(r.root().to_path_buf()),
None => Err(IdeError::unavailable(format!("{} is not a git repository of the project", at.display()))),
},
None => Ok(repos.primary().map_or_else(|| project.root.clone(), |r| r.root().to_path_buf())),
}
}
/// Emits `VcsChanged` after operations that move HEAD or refs (checkout, push).
fn vcs_changed(state: &AppState, window: &Window, root: &std::path::Path) {
if let Ok(p) = state.project(window.label()) {
p.sink.emit(ide_core::IdeEvent::VcsChanged { root: ide_core::IpcPath(root.to_path_buf()) });
}
}
/// The repositories of the account gh is signed in to ("Get from Version Control" › GitHub); needs no project.
#[desktop_command(trust = trusted)]
#[tauri::command]
#[specta::specta]
pub async fn gh_account_repos() -> IdeResult<GhAccountRepos> {
blocking(|| Ok(ide_github::account_repositories())).await
}
#[desktop_command(trust = trusted)]
#[tauri::command]
#[specta::specta]
pub async fn gh_availability(
window: Window,
state: State<'_, AppState>,
repo: Option<PathBuf>,
) -> IdeResult<GhAvailability> {
let project = state.project(window.label())?;
let root = root(&window, &state, repo)?;
blocking(move || {
ide_host::github_auth::apply_to_project(&project);
Ok(ide_github::availability(&root))
})
.await
}
#[desktop_command(trust = trusted)]
#[tauri::command]
#[specta::specta]
pub async fn gh_pr_list(
window: Window,
state: State<'_, AppState>,
repo: Option<PathBuf>,
query: PrListQuery,
) -> IdeResult<Vec<PullRequestSummary>> {
let root = root(&window, &state, repo)?;
blocking(move || ide_github::list(&root, &query)).await
}
#[desktop_command(trust = trusted)]
#[tauri::command]
#[specta::specta]
pub async fn gh_pr_view(
window: Window,
state: State<'_, AppState>,
repo: Option<PathBuf>,
number: u32,
) -> IdeResult<PullRequestDetails> {
let root = root(&window, &state, repo)?;
blocking(move || ide_github::view(&root, number)).await
}
#[desktop_command(trust = trusted)]
#[tauri::command]
#[specta::specta]
pub async fn gh_pr_review_comments(
window: Window,
state: State<'_, AppState>,
repo: Option<PathBuf>,
number: u32,
) -> IdeResult<Vec<PrReviewComment>> {
let root = root(&window, &state, repo)?;
blocking(move || ide_github::review_comments(&root, number)).await
}
#[desktop_command(trust = trusted)]
#[tauri::command]
#[specta::specta]
pub async fn gh_pr_checkout(
window: Window,
state: State<'_, AppState>,
repo: Option<PathBuf>,
request: RequestId,
number: u32,
on_output: Channel<String>,
) -> IdeResult<()> {
let root = root(&window, &state, repo)?;
let cancel = state.begin_request(request);
let dir = root.clone();
let result = blocking(move || {
ide_github::checkout(&dir, number, &cancel, &|line| {
let _ = on_output.send(line);
})
})
.await;
state.end_request(request);
vcs_changed(&state, &window, &root);
result
}
/// Creates a pull request from the current branch (pushing it first when asked); returns its URL.
#[desktop_command(trust = trusted)]
#[tauri::command]
#[specta::specta]
pub async fn gh_pr_create(
window: Window,
state: State<'_, AppState>,
repo: Option<PathBuf>,
request: RequestId,
pr: CreatePullRequest,
on_output: Channel<String>,
) -> IdeResult<String> {
let root = root(&window, &state, repo)?;
let git = state.project(window.label())?.git_executable();
let cancel = state.begin_request(request);
let dir = root.clone();
let result = blocking(move || {
ide_vcs::with_git_executable(git.as_deref(), || {
ide_github::create(&dir, &pr, &cancel, &|line| {
let _ = on_output.send(line);
})
})
})
.await;
state.end_request(request);
vcs_changed(&state, &window, &root);
result
}
#[desktop_command(trust = trusted)]
#[tauri::command]
#[specta::specta]
pub async fn gh_pr_review(
window: Window,
state: State<'_, AppState>,
repo: Option<PathBuf>,
number: u32,
event: ReviewEvent,
body: String,
) -> IdeResult<()> {
let root = root(&window, &state, repo)?;
blocking(move || ide_github::submit_review(&root, number, event, &body)).await
}
#[desktop_command(trust = trusted)]
#[tauri::command]
#[specta::specta]
pub async fn gh_pr_comment(
window: Window,
state: State<'_, AppState>,
repo: Option<PathBuf>,
number: u32,
body: String,
) -> IdeResult<()> {
let root = root(&window, &state, repo)?;
blocking(move || ide_github::add_comment(&root, number, &body)).await
}
/// A GitHub GraphQL / REST call through `gh api`; returns the response body.
#[desktop_command(trust = trusted)]
#[tauri::command]
#[specta::specta]
pub async fn gh_api(
window: Window,
state: State<'_, AppState>,
repo: Option<PathBuf>,
request: GhApiRequest,
) -> IdeResult<String> {
let root = root(&window, &state, repo)?;
blocking(move || ide_github::api(&root, &request)).await
}
/// Fetches what is missing to diff a pull request locally; returns the revisions to compare.
#[desktop_command(trust = trusted)]
#[tauri::command]
#[specta::specta]
#[allow(clippy::too_many_arguments)]
pub async fn gh_pr_prepare_diff(
window: Window,
state: State<'_, AppState>,
repo: Option<PathBuf>,
request: RequestId,
number: u32,
head_oid: String,
base_oid: String,
base_ref: String,
name_with_owner: String,
on_output: Channel<String>,
) -> IdeResult<PrDiffRevisions> {
let root = root(&window, &state, repo)?;
let git = state.project(window.label())?.git_executable();
let cancel = state.begin_request(request);
let result = blocking(move || {
let output = |line: String| {
let _ = on_output.send(line);
};
ide_vcs::with_git_executable(git.as_deref(), || {
ide_github::prepare_diff(&root, number, &head_oid, &base_oid, &base_ref, &name_with_owner, &cancel, &output)
})
})
.await;
state.end_request(request);
result
}
ide_command::desktop_domain!(
gh_account_repos,
gh_api,
gh_availability,
gh_pr_checkout,
gh_pr_comment,
gh_pr_create,
gh_pr_list,
gh_pr_prepare_diff,
gh_pr_review,
gh_pr_review_comments,
gh_pr_view,
);