Skip to content

Commit 98934fb

Browse files
authored
clone: filter out repos that don't belong to the current owner (#206)
This should now filter out any repos that previously were owned by the current owner (but were transferred) as well as repos that user account has access to but doesn't own.
1 parent 9f8b706 commit 98934fb

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

src/github/graphql.rs

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ fn get_org_members_rec(
152152
Ok(list_member)
153153
}
154154

155+
fn filter_by_owner<'a>(name_with_owner: &'a str, expected_owner: &str) -> Option<&'a str> {
156+
let repo_owner = name_with_owner.split('/').next()?;
157+
repo_owner
158+
.eq_ignore_ascii_case(expected_owner)
159+
.then_some(repo_owner)
160+
}
161+
155162
fn list_owner_repos_rec(
156163
token: &str,
157164
owner: &str,
@@ -185,17 +192,15 @@ fn list_owner_repos_rec(
185192
.ok_or(NoReposFound)?
186193
.iter()
187194
.filter_map(|repo| repo.as_ref())
188-
.map(|x| RemoteRepo {
189-
name: x.name.to_string(),
190-
ssh_url: x.ssh_url.to_string(),
191-
owner: x
192-
.name_with_owner
193-
.split('/')
194-
.next()
195-
.unwrap_or(owner)
196-
.to_string(),
197-
https_url: x.url.to_string(),
198-
default_branch: x.default_branch_ref.as_ref().map(|b| b.name.to_string()),
195+
.filter_map(|x| {
196+
let repo_owner = filter_by_owner(&x.name_with_owner, owner)?;
197+
Some(RemoteRepo {
198+
name: x.name.to_string(),
199+
ssh_url: x.ssh_url.to_string(),
200+
owner: repo_owner.to_string(),
201+
https_url: x.url.to_string(),
202+
default_branch: x.default_branch_ref.as_ref().map(|b| b.name.to_string()),
203+
})
199204
})
200205
.collect();
201206

@@ -249,28 +254,26 @@ fn list_owner_repos_with_topics_rec(
249254
.ok_or(NoReposFound)?
250255
.iter()
251256
.filter_map(|repo| repo.as_ref())
252-
.map(|x| RemoteRepoWithTopics {
253-
repo: RemoteRepo {
254-
name: x.name.to_string(),
255-
ssh_url: x.ssh_url.to_string(),
256-
owner: x
257-
.name_with_owner
258-
.split('/')
259-
.next()
260-
.unwrap_or(owner)
261-
.to_string(),
262-
https_url: x.url.to_string(),
263-
default_branch: None,
264-
},
265-
topics: x
266-
.repository_topics
267-
.nodes
268-
.as_ref()
269-
.unwrap_or(&temp)
270-
.iter()
271-
.filter_map(|t| t.as_ref())
272-
.map(|x| x.topic.name.to_string())
273-
.collect(),
257+
.filter_map(|x| {
258+
let repo_owner = filter_by_owner(&x.name_with_owner, owner)?;
259+
Some(RemoteRepoWithTopics {
260+
repo: RemoteRepo {
261+
name: x.name.to_string(),
262+
ssh_url: x.ssh_url.to_string(),
263+
owner: repo_owner.to_string(),
264+
https_url: x.url.to_string(),
265+
default_branch: None,
266+
},
267+
topics: x
268+
.repository_topics
269+
.nodes
270+
.as_ref()
271+
.unwrap_or(&temp)
272+
.iter()
273+
.filter_map(|t| t.as_ref())
274+
.map(|x| x.topic.name.to_string())
275+
.collect(),
276+
})
274277
})
275278
.collect();
276279

0 commit comments

Comments
 (0)