pub mod api_code;
pub mod model;
#[cfg(feature = "github")]
pub(crate) mod github;
#[cfg(feature = "gitlab")]
pub(crate) mod gitlab;
#[cfg(all(test, feature = "github", feature = "gitlab"))]
mod api_code_tests;
#[cfg(all(test, feature = "github", feature = "gitlab"))]
mod tests;
pub use api_code::{ApiCommit, ApiFileDiff, NOT_SHOWN_BY_THE_HOST};
pub use model::{
DiffSide, FileStatus, MergeState, PatchSet, PendingComment, PullRequestDetail,
ReviewCapabilities, ReviewComment, ReviewEvent, ReviewFile, ReviewOutcome, ReviewState,
ReviewSubmission, ReviewThread, ReviewVote, SubmittedReview, VoteLabel, VoteValue,
};
#[cfg(any(
feature = "github",
feature = "gitlab",
feature = "gitea",
feature = "gerrit"
))]
use crate::Error;
pub fn change_ref(change: u64, patch_set: u32) -> String {
format!("refs/changes/{:02}/{change}/{patch_set}", change % 100)
}
pub fn patch_set_of_ref(text: &str) -> Option<u32> {
let rest = text.strip_prefix("refs/changes/")?;
let mut parts = rest.split('/');
parts.next()?;
parts.next()?;
parts.next()?.parse().ok()
}
pub fn review_head_ref(number: u64) -> String {
format!("refs/gitpoulpi/pr/{number}")
}
pub fn review_base_ref(number: u64) -> String {
format!("refs/gitpoulpi/pr/{number}-base")
}
pub fn review_head_refspec(kind: crate::IntegrationKind, number: u64) -> Option<String> {
let (remote_ref, _) = crate::pull_request_head_ref(kind, number)?;
Some(format!("+{remote_ref}:{}", review_head_ref(number)))
}
pub fn review_head_refspec_at(
kind: crate::IntegrationKind,
number: u64,
patch_set: u32,
) -> Option<String> {
(kind.family() == gitpoulpi_config::ApiFamily::Gerrit).then(|| {
format!(
"+{}:{}",
change_ref(number, patch_set),
review_head_ref(number)
)
})
}
pub fn review_compare_ref(number: u64, patch_set: u32) -> String {
format!("refs/gitpoulpi/pr/{number}-ps{patch_set}")
}
pub fn review_compare_refspec(
kind: crate::IntegrationKind,
number: u64,
patch_set: u32,
) -> Option<String> {
(kind.family() == gitpoulpi_config::ApiFamily::Gerrit).then(|| {
format!(
"+{}:{}",
change_ref(number, patch_set),
review_compare_ref(number, patch_set)
)
})
}
pub fn review_base_refspec(branch: &str, number: u64) -> String {
format!("+refs/heads/{branch}:{}", review_base_ref(number))
}
#[cfg(any(
feature = "github",
feature = "gitlab",
feature = "gitea",
feature = "gerrit"
))]
pub(crate) fn invalid(what: &str, detail: impl Into<String>) -> Error {
Error::Api {
what: what.to_string(),
status: 422,
detail: detail.into(),
}
}
#[cfg(any(
feature = "github",
feature = "gitlab",
feature = "gitea",
feature = "gerrit"
))]
pub(crate) fn validate_submission(
caps: ReviewCapabilities,
submission: &ReviewSubmission,
) -> crate::Result<()> {
const WHAT: &str = "submitting a review";
if !caps.allows(submission.event) {
return Err(Error::Unsupported(format!(
"review event `{}`",
submission.event.as_str()
)));
}
let has_body = !submission.body.trim().is_empty();
match submission.event {
ReviewEvent::RequestChanges if !has_body && submission.comments.is_empty() => {
return Err(invalid(
WHAT,
"a review that requests changes needs a body or at least one comment",
));
}
ReviewEvent::Comment if !has_body && submission.comments.is_empty() => {
return Err(invalid(
WHAT,
"a comment review needs a body or at least one comment",
));
}
_ => {}
}
for c in &submission.comments {
if c.body.trim().is_empty() {
return Err(invalid(
WHAT,
format!("the comment on {}:{} has no text", c.path, c.line),
));
}
if c.path.trim().is_empty() || c.line == 0 {
return Err(invalid(
WHAT,
"a line comment needs a path and a line (1-based)",
));
}
if let Some(start) = c.start_line {
if start == 0 || start > c.line {
return Err(invalid(
WHAT,
format!(
"the range of the comment on {} is backwards ({start}..{})",
c.path, c.line
),
));
}
}
}
Ok(())
}
#[cfg(feature = "gitlab")]
pub(crate) fn count_changes(diff: &str) -> (u32, u32) {
let (mut added, mut removed) = (0u32, 0u32);
let mut in_hunk = false;
for line in diff.lines() {
if line.starts_with("@@") {
in_hunk = true;
} else if in_hunk {
match line.as_bytes().first() {
Some(b'+') => added += 1,
Some(b'-') => removed += 1,
_ => {}
}
}
}
(added, removed)
}