Skip to content

Commit

Permalink
Improve email validation (#7)
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 Aug 30, 2024
1 parent 25ad738 commit 7a209d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
36 changes: 22 additions & 14 deletions dco2/src/dco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ pub fn check(input: &CheckInput) -> Result<CheckOutput> {
let mut commit_output = CommitCheckOutput::new(commit.clone());

// Validate author and committer emails
if let Err(err) = validate_emails(commit) {
commit_output.errors.push(err);
if let Err(errs) = validate_emails(commit) {
commit_output.errors.extend(errs);
}

// Check if sign-off is present
Expand Down Expand Up @@ -155,22 +155,30 @@ pub fn check(input: &CheckInput) -> Result<CheckOutput> {
}

/// Validate author and committer emails.
fn validate_emails(commit: &Commit) -> Result<(), CommitError> {
// Author
if let Some(author) = &commit.author {
if !EmailAddress::is_valid(&author.email) {
return Err(CommitError::InvalidAuthorEmail);
fn validate_emails(commit: &Commit) -> Result<(), Vec<CommitError>> {
let mut errors = Vec::new();

// Committer
let committer_email = commit.committer.as_ref().map(|c| &c.email);
if let Some(committer_email) = committer_email {
if !EmailAddress::is_valid(committer_email) {
errors.push(CommitError::InvalidCommitterEmail);
}
}

// Committer
if let Some(committer) = &commit.committer {
if !EmailAddress::is_valid(&committer.email) {
return Err(CommitError::InvalidCommitterEmail);
// Author
let author_email = commit.author.as_ref().map(|a| &a.email);
if let Some(author_email) = author_email {
if Some(author_email) != committer_email && !EmailAddress::is_valid(author_email) {
errors.push(CommitError::InvalidAuthorEmail);
}
}

Ok(())
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}

/// Get sign-offs found in the commit message.
Expand Down Expand Up @@ -201,11 +209,11 @@ fn signoffs_match(signoffs: &[SignOff], commit: &Commit) -> bool {
/// Display some details about a processed commit.
fn debug_processed_commit(commit_output: &CommitCheckOutput, signoffs: &[SignOff]) {
debug!("commit processed: {}", commit_output.commit.sha);
debug!("errors found: {:?}", commit_output.errors);
debug!("author: {:?}", commit_output.commit.author);
debug!("committer: {:?}", commit_output.commit.committer);
debug!("sign-offs");
debug!("sign-offs:");
for signoff in signoffs {
debug!("sign-off: {:?}", signoff);
}
debug!("commit has errors: {}", !commit_output.errors.is_empty());
}
20 changes: 7 additions & 13 deletions dco2/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl GHClient for GHClientOctorust {
let client = self.setup_client(ctx.inst_id)?;
let conclusion = match check_run.conclusion.as_str() {
"success" => octorust::types::ChecksCreateRequestConclusion::Success,
"failure" => octorust::types::ChecksCreateRequestConclusion::Failure,
_ => bail!("invalid conclusion: {}", check_run.conclusion),
};
let status = match check_run.status.as_str() {
Expand Down Expand Up @@ -147,23 +148,16 @@ pub struct CheckRun {
/// Commit information.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Commit {
pub author: Option<CommitAuthor>,
pub committer: Option<CommitCommitter>,
pub author: Option<GitUser>,
pub committer: Option<GitUser>,
pub html_url: String,
pub message: String,
pub sha: String,
}

/// Commit author information.
/// Git user information.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CommitAuthor {
pub name: String,
pub email: String,
}

/// Commit committer information.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CommitCommitter {
pub struct GitUser {
pub name: String,
pub email: String,
}
Expand All @@ -172,11 +166,11 @@ impl From<octorust::types::CommitDataType> for Commit {
/// Convert octorust commit data to Commit.
fn from(c: octorust::types::CommitDataType) -> Self {
Self {
author: c.commit.author.map(|author| CommitAuthor {
author: c.commit.author.map(|author| GitUser {
name: author.name,
email: author.email,
}),
committer: c.commit.committer.map(|committer| CommitCommitter {
committer: c.commit.committer.map(|committer| GitUser {
name: committer.name,
email: committer.email,
}),
Expand Down

0 comments on commit 7a209d3

Please sign in to comment.