Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nest: Refactor codecommit.rs to improve repository filtering #5

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions src/codecommit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub async fn initialize_client(region: &str, profile: &str) -> Client {
.profile_name(profile)
.build()
.await;

let config = Config::builder()
.credentials_provider(credentials_provider)
.region(codecommit_region)
Expand All @@ -19,49 +20,59 @@ pub async fn initialize_client(region: &str, profile: &str) -> Client {
Client::from_conf(config)
}

pub async fn list_filtered_repositories<F>(
async fn list_filtered_repositories_internal<F>(
client: &Client,
filter: F,
) -> Result<Vec<String>, Box<dyn std::error::Error>>
) -> Result<Vec<String>, aws_sdk_codecommit::Error>
where
F: Fn(&String) -> bool + Send + Sync,
F: Fn(&str) -> bool + Send + Sync,
{
let mut repos = Vec::new();

let mut repos_stream = client.list_repositories().into_paginator().send();

while let Some(output) = repos_stream.next().await {
for repo in output?.repositories.unwrap() {
let repo_name = repo.repository_name.unwrap();
if filter(&repo_name) {
repos.push(repo_name);
for repo in output?.repositories.unwrap_or_default() {
if let Some(repo_name) = repo.repository_name {
if filter(&repo_name) {
repos.push(repo_name);
}
}
}
}

debug!("Repositories: {:?}", repos);
Ok(repos)
}

pub async fn list_repositories(
client: &Client,
in_: &[String],
out: &[String],
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let in_: Vec<_> = in_.iter().map(|x| x.as_str()).collect();
let out: Vec<_> = out.iter().map(|x| x.as_str()).collect();
let filter = |repo_name: &String| {
!out.iter().any(|x| repo_name.contains(x)) && in_.iter().any(|x| repo_name.contains(x))
include: &[String],
exclude: &[String],
) -> Result<Vec<String>, aws_sdk_codecommit::Error> {
let include: Vec<_> = include.iter().map(|x| x.as_str()).collect();
let exclude: Vec<_> = exclude.iter().map(|x| x.as_str()).collect();

let filter = |repo_name: &str| {
include.iter().any(|&x| repo_name.contains(x))
&& exclude.iter().all(|&x| !repo_name.contains(x))
};
list_filtered_repositories(client, filter).await

list_filtered_repositories_internal(client, filter).await
}

pub async fn list_exact_repositories(
client: &Client,
in_: &[String],
out: &[String],
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let in_: Vec<_> = in_.iter().map(|x| x.as_str()).collect();
let out: Vec<_> = out.iter().map(|x| x.as_str()).collect();
let filter = |repo_name: &String| {
!out.iter().any(|x| repo_name.contains(x)) && in_.iter().any(|x| repo_name.eq(x))
include: &[String],
exclude: &[String],
) -> Result<Vec<String>, aws_sdk_codecommit::Error> {
let include: Vec<_> = include.iter().map(|x| x.as_str()).collect();
let exclude: Vec<_> = exclude.iter().map(|x| x.as_str()).collect();

let filter = |repo_name: &str| {
include.iter().any(|&x| x == repo_name)
&& exclude.iter().all(|&x| x != repo_name)
};
list_filtered_repositories(client, filter).await

list_filtered_repositories_internal(client, filter).await
}