mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 23:36:11 +00:00
feat: owner-scoped import listing, ssh protocol, robust import
- providers: list_owner_repos (org/user endpoints), ssh_url, Send+Sync - import: parallel sides, case-insensitive/origin-based presence check, continue after failed clones and report them - config: per-provider protocol (https|ssh); GUI settings, onboarding - gui: import scan on its own thread, per-group cache, Rescan, failures Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5aeb7b2077
commit
28e07aab56
14 files changed
+580
-119
No files matched your search
@@ -30,6 +30,7 @@ fn repo(v: &Value) -> RemoteRepo {
|
||||
name: s("name"),
|
||||
private: v["private"].as_bool().unwrap_or(false),
|
||||
clone_url: s("clone_url"),
|
||||
ssh_url: s("ssh_url"),
|
||||
default_branch: v["default_branch"].as_str().unwrap_or("main").to_string(),
|
||||
}
|
||||
}
|
||||
@@ -67,19 +68,33 @@ impl Provider for Gitea {
|
||||
}
|
||||
|
||||
fn list_repos(&self) -> Result<Vec<RemoteRepo>> {
|
||||
let mut out = Vec::new();
|
||||
for page in 1..=200 {
|
||||
let v = self
|
||||
Ok(self
|
||||
.http
|
||||
.pages("/user/repos", 50, "limit")?
|
||||
.iter()
|
||||
.map(repo)
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn list_owner_repos(&self, owner: &str, login: &str) -> Result<Vec<RemoteRepo>> {
|
||||
let path = if owner.eq_ignore_ascii_case(login) {
|
||||
"/user/repos".to_string()
|
||||
} else {
|
||||
format!("/orgs/{owner}/repos")
|
||||
};
|
||||
let items = match self.http.pages(&path, 50, "limit") {
|
||||
Ok(v) => v,
|
||||
// Not an organisation: another user's public repos.
|
||||
Err(Error::Api { status: 404, .. }) if !owner.eq_ignore_ascii_case(login) => self
|
||||
.http
|
||||
.ok("GET", &format!("/user/repos?limit=50&page={page}"), None)?;
|
||||
let items = v.as_array().cloned().unwrap_or_default();
|
||||
let n = items.len();
|
||||
out.extend(items.iter().map(repo));
|
||||
if n < 50 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
.pages(&format!("/users/{owner}/repos"), 50, "limit")?,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
Ok(items
|
||||
.iter()
|
||||
.map(repo)
|
||||
.filter(|r| r.owner.eq_ignore_ascii_case(owner))
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn create_repo(&self, new: &NewRepo) -> Result<RemoteRepo> {
|
||||
|
||||
@@ -29,6 +29,7 @@ fn repo(v: &Value) -> RemoteRepo {
|
||||
name: v["name"].as_str().unwrap_or_default().to_string(),
|
||||
private: v["private"].as_bool().unwrap_or(false),
|
||||
clone_url: v["clone_url"].as_str().unwrap_or_default().to_string(),
|
||||
ssh_url: v["ssh_url"].as_str().unwrap_or_default().to_string(),
|
||||
default_branch: v["default_branch"].as_str().unwrap_or("main").to_string(),
|
||||
}
|
||||
}
|
||||
@@ -74,21 +75,33 @@ impl Provider for GitHub {
|
||||
}
|
||||
|
||||
fn list_repos(&self) -> Result<Vec<RemoteRepo>> {
|
||||
let mut out = Vec::new();
|
||||
for page in 1..=200 {
|
||||
let v = self.http.ok(
|
||||
"GET",
|
||||
&format!("/user/repos?per_page=100&page={page}"),
|
||||
None,
|
||||
)?;
|
||||
let items = v.as_array().cloned().unwrap_or_default();
|
||||
let n = items.len();
|
||||
out.extend(items.iter().map(repo));
|
||||
if n < 100 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
Ok(self
|
||||
.http
|
||||
.pages("/user/repos", 100, "per_page")?
|
||||
.iter()
|
||||
.map(repo)
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn list_owner_repos(&self, owner: &str, login: &str) -> Result<Vec<RemoteRepo>> {
|
||||
let path = if owner.eq_ignore_ascii_case(login) {
|
||||
"/user/repos?affiliation=owner".to_string()
|
||||
} else {
|
||||
format!("/orgs/{owner}/repos?type=all")
|
||||
};
|
||||
let items = match self.http.pages(&path, 100, "per_page") {
|
||||
Ok(v) => v,
|
||||
// Not an organisation: another user's public repos.
|
||||
Err(Error::Api { status: 404, .. }) if !owner.eq_ignore_ascii_case(login) => self
|
||||
.http
|
||||
.pages(&format!("/users/{owner}/repos"), 100, "per_page")?,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
Ok(items
|
||||
.iter()
|
||||
.map(repo)
|
||||
.filter(|r| r.owner.eq_ignore_ascii_case(owner))
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn create_repo(&self, new: &NewRepo) -> Result<RemoteRepo> {
|
||||
|
||||
@@ -60,4 +60,24 @@ impl Http {
|
||||
.to_string();
|
||||
Err(Error::Api { status, message })
|
||||
}
|
||||
|
||||
/// Every item of a paginated list endpoint (`path` may already carry a query string).
|
||||
pub fn pages(&self, path: &str, per_page: usize, size_param: &str) -> Result<Vec<Value>> {
|
||||
let sep = if path.contains('?') { '&' } else { '?' };
|
||||
let mut out = Vec::new();
|
||||
for page in 1..=200 {
|
||||
let v = self.ok(
|
||||
"GET",
|
||||
&format!("{path}{sep}{size_param}={per_page}&page={page}"),
|
||||
None,
|
||||
)?;
|
||||
let items = v.as_array().cloned().unwrap_or_default();
|
||||
let n = items.len();
|
||||
out.extend(items);
|
||||
if n < per_page {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
@@ -53,9 +53,26 @@ pub struct RemoteRepo {
|
||||
pub name: String,
|
||||
pub private: bool,
|
||||
pub clone_url: String,
|
||||
/// SSH clone URL (empty when the forge did not report one).
|
||||
pub ssh_url: String,
|
||||
pub default_branch: String,
|
||||
}
|
||||
|
||||
impl RemoteRepo {
|
||||
/// The URL local git should use for `protocol`.
|
||||
pub fn git_url(&self, protocol: grove_core::config::Protocol) -> Result<&str> {
|
||||
use grove_core::config::Protocol;
|
||||
match protocol {
|
||||
Protocol::Https => Ok(&self.clone_url),
|
||||
Protocol::Ssh if self.ssh_url.is_empty() => Err(Error::Other(format!(
|
||||
"the forge reported no SSH URL for {}/{}",
|
||||
self.owner, self.name
|
||||
))),
|
||||
Protocol::Ssh => Ok(&self.ssh_url),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NewRepo {
|
||||
pub name: String,
|
||||
@@ -99,7 +116,7 @@ pub struct PullMirrorSpec {
|
||||
pub interval: String,
|
||||
}
|
||||
|
||||
pub trait Provider {
|
||||
pub trait Provider: Send + Sync {
|
||||
fn kind(&self) -> ProviderKind;
|
||||
fn capabilities(&self) -> Capabilities;
|
||||
/// Verifies the token and returns the authenticated login.
|
||||
@@ -107,6 +124,9 @@ pub trait Provider {
|
||||
/// One repo's metadata (notably its visibility): `GET /repos/{owner}/{repo}`.
|
||||
fn get_repo(&self, owner: &str, name: &str) -> Result<RemoteRepo>;
|
||||
fn list_repos(&self) -> Result<Vec<RemoteRepo>>;
|
||||
/// The repos owned by `owner` (an organisation, or the authenticated user `login`), without
|
||||
/// paging through everything else the token can see.
|
||||
fn list_owner_repos(&self, owner: &str, login: &str) -> Result<Vec<RemoteRepo>>;
|
||||
fn create_repo(&self, new: &NewRepo) -> Result<RemoteRepo>;
|
||||
fn configure_mirror(&self, owner: &str, repo: &str, spec: &MirrorSpec) -> Result<()>;
|
||||
/// Creates a pull mirror (see [`Capabilities::pull_mirror`]).
|
||||
|
||||
@@ -161,6 +161,7 @@ mod tests {
|
||||
let h = |kind, url: &str| {
|
||||
host(&ProviderCfg {
|
||||
kind,
|
||||
protocol: Default::default(),
|
||||
url: Some(url.into()),
|
||||
})
|
||||
};
|
||||
|
||||
@@ -301,10 +301,12 @@ fn falls_back_to_tea_and_gh_logins() {
|
||||
let off = EnvGuard::set("GROVE_NO_CLI_TOKENS", "0");
|
||||
let gitea = |url: &str| ProviderCfg {
|
||||
kind: ProviderKind::Gitea,
|
||||
protocol: Default::default(),
|
||||
url: Some(url.into()),
|
||||
};
|
||||
let github = ProviderCfg {
|
||||
kind: ProviderKind::Github,
|
||||
protocol: Default::default(),
|
||||
url: None,
|
||||
};
|
||||
let id = format!("clitok-{}", std::process::id());
|
||||
@@ -368,3 +370,75 @@ fn get_repo_reports_visibility_on_both_forges() {
|
||||
.private
|
||||
);
|
||||
}
|
||||
|
||||
const REPO_A: &str = r#"{"name":"a","private":false,"clone_url":"https://h/o/a.git","ssh_url":"git@h:o/a.git","default_branch":"main","owner":{"login":"Org"}}"#;
|
||||
const REPO_B: &str = r#"{"name":"b","private":true,"clone_url":"https://h/me/b.git","ssh_url":"ssh://gitea@h:2222/me/b.git","default_branch":"main","owner":{"login":"me"}}"#;
|
||||
const REPO_C: &str = r#"{"name":"c","private":true,"clone_url":"https://h/x/c.git","ssh_url":"","default_branch":"main","owner":{"login":"x"}}"#;
|
||||
|
||||
fn lines(req: &str) -> &str {
|
||||
req.lines().next().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gitea_owner_listing_uses_the_org_endpoint_or_the_users_own() {
|
||||
let org = format!("[{REPO_A}]");
|
||||
let user = format!("[{REPO_B},{REPO_C}]");
|
||||
let (url, rx) = mock(vec![
|
||||
(200, Box::leak(org.into_boxed_str())),
|
||||
(200, Box::leak(user.into_boxed_str())),
|
||||
]);
|
||||
let g = Gitea::new(ProviderKind::Gitea, &url, "t".into());
|
||||
let r = g.list_owner_repos("Org", "me").unwrap();
|
||||
assert_eq!(r.len(), 1);
|
||||
assert_eq!(r[0].ssh_url, "git@h:o/a.git");
|
||||
assert!(lines(&rx.recv().unwrap()).starts_with("GET /api/v1/orgs/Org/repos?limit=50&page=1 "));
|
||||
// The login itself: `/user/repos`, then only its own repos (org repos it can see are dropped).
|
||||
let r = g.list_owner_repos("ME", "me").unwrap();
|
||||
assert_eq!(r.iter().map(|r| r.name.as_str()).collect::<Vec<_>>(), ["b"]);
|
||||
assert!(lines(&rx.recv().unwrap()).starts_with("GET /api/v1/user/repos?limit=50&page=1 "));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn github_owner_listing_scopes_the_query_and_falls_back_for_users() {
|
||||
let org = format!("[{REPO_A}]");
|
||||
let mine = format!("[{REPO_B}]");
|
||||
let (url, rx) = mock(vec![
|
||||
(200, Box::leak(org.into_boxed_str())),
|
||||
(200, Box::leak(mine.into_boxed_str())),
|
||||
(404, r#"{"message":"Not Found"}"#),
|
||||
(200, "[]"),
|
||||
]);
|
||||
let g = GitHub::new(&url, "t".into());
|
||||
assert_eq!(g.list_owner_repos("Org", "me").unwrap().len(), 1);
|
||||
assert!(
|
||||
lines(&rx.recv().unwrap()).starts_with("GET /orgs/Org/repos?type=all&per_page=100&page=1 ")
|
||||
);
|
||||
assert_eq!(g.list_owner_repos("me", "me").unwrap().len(), 1);
|
||||
assert!(
|
||||
lines(&rx.recv().unwrap())
|
||||
.starts_with("GET /user/repos?affiliation=owner&per_page=100&page=1 ")
|
||||
);
|
||||
// Not an organisation (404): the other user's public repos.
|
||||
assert!(g.list_owner_repos("someone", "me").unwrap().is_empty());
|
||||
rx.recv().unwrap();
|
||||
assert!(
|
||||
lines(&rx.recv().unwrap()).starts_with("GET /users/someone/repos?per_page=100&page=1 ")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn git_url_follows_the_protocol_and_needs_an_ssh_url_for_ssh() {
|
||||
use grove_core::config::Protocol;
|
||||
let (url, _rx) = mock(vec![(
|
||||
200,
|
||||
Box::leak(format!("[{REPO_B},{REPO_C}]").into_boxed_str()),
|
||||
)]);
|
||||
let g = Gitea::new(ProviderKind::Gitea, &url, "t".into());
|
||||
let r = g.list_repos().unwrap();
|
||||
assert_eq!(r[0].git_url(Protocol::Https).unwrap(), "https://h/me/b.git");
|
||||
assert_eq!(
|
||||
r[0].git_url(Protocol::Ssh).unwrap(),
|
||||
"ssh://gitea@h:2222/me/b.git"
|
||||
);
|
||||
assert!(r[1].git_url(Protocol::Ssh).is_err());
|
||||
}
|
||||
Reference in new issue
Block a user