Skip to content

Commit

Permalink
Check teams names are valid (#186)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Castaño Arteaga <[email protected]>
  • Loading branch information
tegioz authored Apr 11, 2024
1 parent f61dccc commit 3cfb81b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions clowarden-core/src/directory/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ use crate::{
multierror::MultiError,
};
use anyhow::Result;
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};

lazy_static! {
pub(crate) static ref VALID_TEAM_NAME: Regex =
Regex::new(r"^[a-z0-9\-]+$").expect("expr in VALID_TEAM_NAME to be valid");
}

/// Legacy configuration.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub(crate) struct Cfg {
Expand Down Expand Up @@ -51,6 +58,7 @@ impl Cfg {
}

pub mod sheriff {
use super::VALID_TEAM_NAME;
use crate::{
directory::{TeamName, UserName},
github::{DynGH, Source},
Expand Down Expand Up @@ -149,6 +157,13 @@ pub mod sheriff {
merr.push(format_err!("team[{id}: name must be provided"));
}

// Name must be valid
if !VALID_TEAM_NAME.is_match(&team.name) {
merr.push(format_err!(
"team[{id}]: name must be lowercase alphanumeric with dashes (team slug)"
));
}

// No duplicate config per team
if !team.name.is_empty() {
if teams_seen.contains(&&team.name) {
Expand Down
12 changes: 12 additions & 0 deletions clowarden-core/src/services/github/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pub(crate) mod sheriff {
use crate::{
directory::legacy::VALID_TEAM_NAME,
github::{DynGH, Source},
multierror::MultiError,
services::github::state::Repository,
Expand Down Expand Up @@ -61,6 +62,17 @@ pub(crate) mod sheriff {
}
repos_seen.push(&repo.name);
}

// Teams names must be valid
if let Some(teams) = &repo.teams {
for team_name in teams.keys() {
if !VALID_TEAM_NAME.is_match(team_name) {
merr.push(format_err!(
"repo[{id}]: team[{team_name}] name must be lowercase alphanumeric with dashes (team slug)",
));
}
}
}
}

if merr.contains_errors() {
Expand Down

0 comments on commit 3cfb81b

Please sign in to comment.