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

modularize github codeowners parsing code #102

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions cli/src/codeowners.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};

use codeowners::Owners;
use codeowners::{FromPath, GitHubOwners};
use serde::{Deserialize, Serialize};

use crate::constants::CODEOWNERS_LOCATIONS;
Expand All @@ -9,7 +9,7 @@ use crate::constants::CODEOWNERS_LOCATIONS;
pub struct CodeOwners {
pub path: PathBuf,
#[serde(skip_serializing, skip_deserializing)]
pub owners: Option<Owners>,
pub owners: Option<GitHubOwners>,
}

impl CodeOwners {
Expand All @@ -30,7 +30,7 @@ impl CodeOwners {
all_locations.find_map(|location| locate_codeowners(&repo_root, location));

codeowners_path.map(|path| {
let owners_result = codeowners::from_path(&path);
let owners_result = GitHubOwners::from_path(&path);
if let Err(ref err) = owners_result {
log::error!(
"Found CODEOWNERS file `{}`, but couldn't parse it: {}",
Expand Down
1 change: 1 addition & 0 deletions cli/src/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::format;
use std::time::SystemTime;

use codeowners::OwnersOfPath;
use regex::Regex;
use serde::{Deserialize, Serialize};

Expand Down
354 changes: 354 additions & 0 deletions codeowners/src/github.rs
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty much exactly the same code from lib.rs. Moved here to better split up code for each CODEOWNERS provider e.g. GitHub vs GitLab vs Bitbucket

Original file line number Diff line number Diff line change
@@ -0,0 +1,354 @@
use glob::Pattern;
use lazy_static::lazy_static;
use regex::Regex;
use std::{
fmt,
fs::File,
io::{BufRead, BufReader, Read},
path::Path,
str::FromStr,
};

use crate::{FromPath, FromReader, OwnersOfPath};

/// Various types of owners
///
/// GitHubOwner supports parsing from strings as well as displaying as strings
///
/// # Examples
///
/// ```rust
/// let raw = "@org/team";
/// assert_eq!(
/// raw.parse::<codeowners::GitHubOwner>().unwrap().to_string(),
/// raw
/// );
/// ```
#[derive(Debug, PartialEq, Clone)]
pub enum GitHubOwner {
/// Owner in the form @username
Username(String),
/// Owner in the form @org/Team
Team(String),
/// Owner in the form user@domain.com
Email(String),
}

impl fmt::Display for GitHubOwner {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let inner = match *self {
GitHubOwner::Username(ref u) => u,
GitHubOwner::Team(ref t) => t,
GitHubOwner::Email(ref e) => e,
};
f.write_str(inner.as_str())
}
}

impl FromStr for GitHubOwner {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
lazy_static! {
static ref TEAM: Regex = Regex::new(r"^@\S+/\S+").unwrap();
static ref USERNAME: Regex = Regex::new(r"^@\S+").unwrap();
static ref EMAIL: Regex = Regex::new(r"^\S+@\S+").unwrap();
}
if TEAM.is_match(s) {
Ok(GitHubOwner::Team(s.into()))
} else if USERNAME.is_match(s) {
Ok(GitHubOwner::Username(s.into()))
} else if EMAIL.is_match(s) {
Ok(GitHubOwner::Email(s.into()))
} else {
Err(String::from("not an owner"))
}
}
}

/// Mappings of GitHub owners to path patterns
#[derive(Debug, PartialEq, Clone)]
pub struct GitHubOwners {
paths: Vec<(Pattern, Vec<GitHubOwner>)>,
}

impl OwnersOfPath for GitHubOwners {
type Owner = GitHubOwner;

fn of<P>(&self, path: P) -> Option<Vec<GitHubOwner>>
where
P: AsRef<Path>,
{
self.paths
.iter()
.filter_map(|mapping| {
let (pattern, owners) = mapping;
let opts = glob::MatchOptions {
case_sensitive: false,
require_literal_separator: pattern.as_str().contains('/'),
require_literal_leading_dot: false,
};
if pattern.matches_path_with(path.as_ref(), opts) {
Some(owners)
} else {
// this pattern is only meant to match
// direct children
if pattern.as_str().ends_with("/*") {
return None;
}
// case of implied owned children
// foo/bar @owner should indicate that foo/bar/baz.rs is
// owned by @owner
let mut p = path.as_ref();
while let Some(parent) = p.parent() {
if pattern.matches_path_with(parent, opts) {
return Some(owners);
} else {
p = parent;
}
}
None
}
})
.next()
.cloned()
}
}

impl FromPath for GitHubOwners {
fn from_path<P>(path: P) -> anyhow::Result<GitHubOwners>
where
P: AsRef<Path>,
{
Self::from_reader(File::open(path)?)
}
}

impl FromReader for GitHubOwners {
/// Parse a CODEOWNERS file from some readable source
/// This format is defined in
/// [Github's documentation](https://help.github.com/articles/about-codeowners/)
/// The syntax is uses gitgnore
/// [patterns](https://www.kernel.org/pub/software/scm/git/docs/gitignore.html#_pattern_format)
/// followed by an identifier for an owner. More information can be found
/// [here](https://help.github.com/articles/about-codeowners/#codeowners-syntax)
fn from_reader<R>(read: R) -> anyhow::Result<GitHubOwners>
where
R: Read,
{
let mut paths = BufReader::new(read)
.lines()
/* trunk-ignore(clippy/lines_filter_map_ok) */
.filter_map(Result::ok)
.filter(|line| !line.is_empty() && !line.starts_with('#'))
.try_fold(Vec::new(), |mut paths, line| -> anyhow::Result<_> {
let mut elements = line.split_whitespace();
if let Some(path) = elements.next() {
let owners = elements.fold(Vec::new(), |mut result, owner| {
if let Ok(owner) = owner.parse() {
result.push(owner)
}
result
});
paths.push((pattern(path)?, owners))
}
Ok(paths)
})?;
// last match takes precedence
paths.reverse();
Ok(GitHubOwners { paths })
}
}

fn pattern(path: &str) -> anyhow::Result<Pattern> {
// if pattern starts with anchor or explicit wild card, it should
// match any prefix
let prefixed = if path.starts_with('*') || path.starts_with('/') {
path.to_owned()
} else {
format!("**/{}", path)
};
// if pattern starts with anchor it should only match paths
// relative to root
let mut normalized = prefixed.trim_start_matches('/').to_string();
// if pattern ends with /, it should match children of that directory
if normalized.ends_with('/') {
normalized.push_str("**");
}
Pattern::new(&normalized).map_err(anyhow::Error::msg)
}

#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &[u8] = include_bytes!("../test_fixtures/github/codeowners_example");

#[test]
fn owner_parses() {
assert!("@user".parse() == Ok(GitHubOwner::Username("@user".into())));
assert!("@org/team".parse() == Ok(GitHubOwner::Team("@org/team".into())));
assert!("user@domain.com".parse() == Ok(GitHubOwner::Email("user@domain.com".into())));
assert!("bogus".parse::<GitHubOwner>() == Err("not an owner".into()));
}

#[test]
fn owner_displays() {
assert!(GitHubOwner::Username("@user".into()).to_string() == "@user");
assert!(GitHubOwner::Team("@org/team".into()).to_string() == "@org/team");
assert!(GitHubOwner::Email("user@domain.com".into()).to_string() == "user@domain.com");
}

#[test]
fn from_reader_parses() {
let owners = GitHubOwners::from_reader(EXAMPLE).unwrap();
assert_eq!(
owners,
GitHubOwners {
paths: vec![
(
Pattern::new("docs/**").unwrap(),
vec![GitHubOwner::Username("@doctocat".into())]
),
(
Pattern::new("**/apps/**").unwrap(),
vec![GitHubOwner::Username("@octocat".into())]
),
(
Pattern::new("**/docs/*").unwrap(),
vec![GitHubOwner::Email("docs@example.com".into())]
),
(
Pattern::new("build/logs/**").unwrap(),
vec![GitHubOwner::Username("@doctocat".into())]
),
(
Pattern::new("*.go").unwrap(),
vec![GitHubOwner::Email("docs@example.com".into())]
),
(
Pattern::new("*.js").unwrap(),
vec![GitHubOwner::Username("@js-owner".into())]
),
(
Pattern::new("*").unwrap(),
vec![
GitHubOwner::Username("@global-owner1".into()),
GitHubOwner::Username("@global-owner2".into()),
]
),
],
}
)
}

#[test]
fn owners_owns_wildcard() {
let owners = GitHubOwners::from_reader(EXAMPLE).unwrap();
assert_eq!(
owners.of("foo.txt"),
Some(vec![
GitHubOwner::Username("@global-owner1".into()),
GitHubOwner::Username("@global-owner2".into()),
])
);
assert_eq!(
owners.of("foo/bar.txt"),
Some(vec![
GitHubOwner::Username("@global-owner1".into()),
GitHubOwner::Username("@global-owner2".into()),
])
)
}

#[test]
fn owners_owns_js_extention() {
let owners = GitHubOwners::from_reader(EXAMPLE).unwrap();
assert_eq!(
owners.of("foo.js"),
Some(vec![GitHubOwner::Username("@js-owner".into())])
);
assert_eq!(
owners.of("foo/bar.js"),
Some(vec![GitHubOwner::Username("@js-owner".into())])
)
}

#[test]
fn owners_owns_go_extention() {
let owners = GitHubOwners::from_reader(EXAMPLE).unwrap();
assert_eq!(
owners.of("foo.go"),
Some(vec![GitHubOwner::Email("docs@example.com".into())])
);
assert_eq!(
owners.of("foo/bar.go"),
Some(vec![GitHubOwner::Email("docs@example.com".into())])
)
}

#[test]
fn owners_owns_anchored_build_logs() {
let owners = GitHubOwners::from_reader(EXAMPLE).unwrap();
// relative to root
assert_eq!(
owners.of("build/logs/foo.go"),
Some(vec![GitHubOwner::Username("@doctocat".into())])
);
assert_eq!(
owners.of("build/logs/foo/bar.go"),
Some(vec![GitHubOwner::Username("@doctocat".into())])
);
// not relative to root
assert_eq!(
owners.of("foo/build/logs/foo.go"),
Some(vec![GitHubOwner::Email("docs@example.com".into())])
)
}

#[test]
fn owners_owns_unanchored_docs() {
let owners = GitHubOwners::from_reader(EXAMPLE).unwrap();
// docs anywhere
assert_eq!(
owners.of("foo/docs/foo.js"),
Some(vec![GitHubOwner::Email("docs@example.com".into())])
);
assert_eq!(
owners.of("foo/bar/docs/foo.js"),
Some(vec![GitHubOwner::Email("docs@example.com".into())])
);
// but not nested
assert_eq!(
owners.of("foo/bar/docs/foo/foo.js"),
Some(vec![GitHubOwner::Username("@js-owner".into())])
)
}

#[test]
fn owners_owns_unanchored_apps() {
let owners = GitHubOwners::from_reader(EXAMPLE).unwrap();
assert_eq!(
owners.of("foo/apps/foo.js"),
Some(vec![GitHubOwner::Username("@octocat".into())])
)
}

#[test]
fn owners_owns_anchored_docs() {
let owners = GitHubOwners::from_reader(EXAMPLE).unwrap();
// relative to root
assert_eq!(
owners.of("docs/foo.js"),
Some(vec![GitHubOwner::Username("@doctocat".into())])
)
}

#[test]
fn implied_children_owners() {
let owners = GitHubOwners::from_reader("foo/bar @doug".as_bytes()).unwrap();
assert_eq!(
owners.of("foo/bar/baz.rs"),
Some(vec![GitHubOwner::Username("@doug".into())])
)
}
}
Loading