From 7a209d3e8011912b5577733513a2ce0f42111566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Casta=C3=B1o=20Arteaga?= Date: Fri, 30 Aug 2024 11:02:44 +0200 Subject: [PATCH] Improve email validation (#7) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergio CastaƱo Arteaga --- dco2/src/dco.rs | 36 ++++++++++++++++++++++-------------- dco2/src/github.rs | 20 +++++++------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/dco2/src/dco.rs b/dco2/src/dco.rs index 860ccc5..92bbd9d 100644 --- a/dco2/src/dco.rs +++ b/dco2/src/dco.rs @@ -126,8 +126,8 @@ pub fn check(input: &CheckInput) -> Result { 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 @@ -155,22 +155,30 @@ pub fn check(input: &CheckInput) -> Result { } /// 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> { + 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. @@ -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()); } diff --git a/dco2/src/github.rs b/dco2/src/github.rs index 5c89324..44ec33c 100644 --- a/dco2/src/github.rs +++ b/dco2/src/github.rs @@ -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() { @@ -147,23 +148,16 @@ pub struct CheckRun { /// Commit information. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Commit { - pub author: Option, - pub committer: Option, + pub author: Option, + pub committer: Option, 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, } @@ -172,11 +166,11 @@ impl From 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, }),