Skip to content

Commit

Permalink
Sort generate output alphabetically (#180)
Browse files Browse the repository at this point in the history
Closes #178

Signed-off-by: Sergio Castaño Arteaga <[email protected]>
  • Loading branch information
tegioz authored Mar 20, 2024
1 parent 622b9eb commit f61dccc
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions clowarden-core/src/services/github/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
collections::{HashMap, HashSet},
collections::{BTreeMap, HashSet},
fmt::{self, Write},
};

Expand Down Expand Up @@ -158,6 +158,8 @@ impl State {
}
}
}
maintainers.sort();
members.sort();

// Setup team from info collected
Ok(Team {
Expand All @@ -177,6 +179,7 @@ impl State {
Err(err) => return Err(err.context("error getting team info")),
}
}
state.directory.teams.sort_by(|a, b| a.name.cmp(&b.name));

// Repositories
let org_admins: Vec<UserName> =
Expand All @@ -185,7 +188,7 @@ impl State {
.filter(|repo| future::ready(!repo.archived && !GHSA_TEMP_FORK.is_match(&repo.name)))
.map(|repo| async {
// Get collaborators (including pending invitations and excluding org admins)
let mut collaborators: HashMap<UserName, Role> = svc
let mut collaborators: BTreeMap<UserName, Role> = svc
.list_repository_collaborators(ctx, &repo.name)
.await
.context(format!("error listing repository {} collaborators", &repo.name))?
Expand All @@ -209,7 +212,7 @@ impl State {
};

// Get teams
let teams: HashMap<TeamName, Role> = svc
let teams: BTreeMap<TeamName, Role> = svc
.list_repository_teams(ctx, &repo.name)
.await
.context(format!("error listing repository {} teams", &repo.name))?
Expand All @@ -235,6 +238,7 @@ impl State {
Err(err) => return Err(err.context("error getting repository info")),
}
}
state.repositories.sort_by(|a, b| a.name.cmp(&b.name));

Ok(state)
}
Expand Down Expand Up @@ -355,19 +359,19 @@ impl State {
let mut changes = vec![];

// Repositories
let repos_old: HashMap<&RepositoryName, &Repository> = old.iter().map(|r| (&r.name, r)).collect();
let repos_new: HashMap<&RepositoryName, &Repository> = new.iter().map(|r| (&r.name, r)).collect();
let repos_old: BTreeMap<&RepositoryName, &Repository> = old.iter().map(|r| (&r.name, r)).collect();
let repos_new: BTreeMap<&RepositoryName, &Repository> = new.iter().map(|r| (&r.name, r)).collect();

// Helper closures to get the team's/collaborator's role
let team_role = |collection: &HashMap<&RepositoryName, &Repository>,
let team_role = |collection: &BTreeMap<&RepositoryName, &Repository>,
repo_name: &RepositoryName,
team_name: &TeamName| {
if let Some(teams) = collection[repo_name].teams.as_ref() {
return teams.get(&team_name.to_string()).cloned().unwrap_or_default();
}
Role::default()
};
let user_role = |collection: &HashMap<&RepositoryName, &Repository>,
let user_role = |collection: &BTreeMap<&RepositoryName, &Repository>,
repo_name: &RepositoryName,
user_name: &UserName| {
if let Some(collaborators) = collection[repo_name].collaborators.as_ref() {
Expand Down Expand Up @@ -488,10 +492,10 @@ pub struct Repository {
pub name: String,

#[serde(alias = "external_collaborators", skip_serializing_if = "Option::is_none")]
pub collaborators: Option<HashMap<UserName, Role>>,
pub collaborators: Option<BTreeMap<UserName, Role>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub teams: Option<HashMap<TeamName, Role>>,
pub teams: Option<BTreeMap<TeamName, Role>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub visibility: Option<Visibility>,
Expand Down Expand Up @@ -848,7 +852,7 @@ mod tests {
..Default::default()
};
let repo1_adding_team = Repository {
teams: Some(HashMap::from([("team1".to_string(), Role::Write)])),
teams: Some(BTreeMap::from([("team1".to_string(), Role::Write)])),
..repo1.clone()
};
let state1 = State {
Expand Down Expand Up @@ -876,7 +880,7 @@ mod tests {
fn diff_repository_team_removed() {
let repo1 = Repository {
name: "repo1".to_string(),
teams: Some(HashMap::from([("team1".to_string(), Role::Write)])),
teams: Some(BTreeMap::from([("team1".to_string(), Role::Write)])),
..Default::default()
};
let repo1_removing_team = Repository {
Expand Down Expand Up @@ -907,11 +911,11 @@ mod tests {
fn diff_repository_team_role_updated() {
let repo1 = Repository {
name: "repo1".to_string(),
teams: Some(HashMap::from([("team1".to_string(), Role::Write)])),
teams: Some(BTreeMap::from([("team1".to_string(), Role::Write)])),
..Default::default()
};
let repo1_updating_team_role = Repository {
teams: Some(HashMap::from([("team1".to_string(), Role::Read)])),
teams: Some(BTreeMap::from([("team1".to_string(), Role::Read)])),
..repo1.clone()
};
let state1 = State {
Expand Down Expand Up @@ -942,7 +946,7 @@ mod tests {
..Default::default()
};
let repo1_adding_collaborator = Repository {
collaborators: Some(HashMap::from([("user1".to_string(), Role::Write)])),
collaborators: Some(BTreeMap::from([("user1".to_string(), Role::Write)])),
..repo1.clone()
};
let state1 = State {
Expand Down Expand Up @@ -970,7 +974,7 @@ mod tests {
fn diff_repository_collaborator_removed() {
let repo1 = Repository {
name: "repo1".to_string(),
collaborators: Some(HashMap::from([("user1".to_string(), Role::Write)])),
collaborators: Some(BTreeMap::from([("user1".to_string(), Role::Write)])),
..Default::default()
};
let repo1_removing_collaborator = Repository {
Expand Down Expand Up @@ -1001,11 +1005,11 @@ mod tests {
fn diff_repository_collaborator_role_updated() {
let repo1 = Repository {
name: "repo1".to_string(),
collaborators: Some(HashMap::from([("user1".to_string(), Role::Write)])),
collaborators: Some(BTreeMap::from([("user1".to_string(), Role::Write)])),
..Default::default()
};
let repo1_updating_collaborator_role = Repository {
collaborators: Some(HashMap::from([("user1".to_string(), Role::Read)])),
collaborators: Some(BTreeMap::from([("user1".to_string(), Role::Read)])),
..repo1.clone()
};
let state1 = State {
Expand Down Expand Up @@ -1064,15 +1068,15 @@ mod tests {
fn diff_multiple_changes() {
let repo1 = Repository {
name: "repo1".to_string(),
teams: Some(HashMap::from([
teams: Some(BTreeMap::from([
("team1".to_string(), Role::Write),
("team2".to_string(), Role::Write),
])),
visibility: Some(Visibility::Private),
..Default::default()
};
let repo1_updated = Repository {
teams: Some(HashMap::from([
teams: Some(BTreeMap::from([
("team1".to_string(), Role::Write),
("team3".to_string(), Role::Write),
])),
Expand Down

0 comments on commit f61dccc

Please sign in to comment.