Skip to content
This repository was archived by the owner on Nov 6, 2025. It is now read-only.
Closed
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
18 changes: 9 additions & 9 deletions src/api/pull_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ pub(crate) struct PullRequestComment {
pub(crate) struct Review {
pub(crate) author: String,
pub(crate) state: PullRequestReviewState,
pub(crate) body: Option<String>,
pub(crate) body: String,
pub(crate) url: String,
pub(crate) created_at: DateTimeUtc,
pub(crate) published_at: Option<DateTimeUtc>,
pub(crate) submitted_at: DateTimeUtc,
pub(crate) submitted_at: Option<DateTimeUtc>,
pub(crate) is_minimized: bool,
pub(crate) comments: Vec<PullRequestComment>,
}
Expand All @@ -61,7 +61,7 @@ pub(crate) struct PullRequest {
pub(crate) repo: String,
pub(crate) number: i32,
pub(crate) title: String,
pub(crate) body: Option<String>,
pub(crate) body: String,
pub(crate) state: PullRequestState,
pub(crate) assignees: Vec<String>,
pub(crate) created_at: DateTimeUtc,
Expand Down Expand Up @@ -111,7 +111,7 @@ impl TryFromKeyValue for PullRequest {
url: r.url,
created_at: DateTimeUtc(r.created_at),
published_at: r.published_at.map(DateTimeUtc),
submitted_at: DateTimeUtc(r.submitted_at),
submitted_at: r.submitted_at.map(DateTimeUtc),
is_minimized: r.is_minimized,
comments: r
.comments
Expand Down Expand Up @@ -184,7 +184,7 @@ impl Default for GitHubPullRequestNode {
id: String::new(),
number: 0,
title: String::new(),
body: None,
body: String::new(),
state: PullRequestState::OPEN,
created_at: Timestamp::default(),
updated_at: Timestamp::default(),
Expand Down Expand Up @@ -281,7 +281,7 @@ mod tests {
id: "pr-1".to_string(),
number: 1,
title: "pull request 1".to_string(),
body: Some(String::new()),
body: String::new(),
state: PullRequestState::OPEN,
created_at: "2024-01-01T00:00:00Z".parse().unwrap(),
updated_at: "2024-01-01T00:00:00Z".parse().unwrap(),
Expand Down Expand Up @@ -316,7 +316,7 @@ mod tests {
id: "pr-2".to_string(),
number: 2,
title: "pull request 2".to_string(),
body: Some(String::new()),
body: String::new(),
state: PullRequestState::OPEN,
created_at: "2024-01-01T00:00:00Z".parse().unwrap(),
updated_at: "2024-01-01T00:00:00Z".parse().unwrap(),
Expand Down Expand Up @@ -398,7 +398,7 @@ mod tests {
id: "pr-1".to_string(),
number: 1,
title: "pull request 1".to_string(),
body: Some(String::new()),
body: String::new(),
state: PullRequestState::OPEN,
created_at: "2024-01-01T00:00:00Z".parse().unwrap(),
updated_at: "2024-01-01T00:00:00Z".parse().unwrap(),
Expand Down Expand Up @@ -433,7 +433,7 @@ mod tests {
id: "pr-2".to_string(),
number: 2,
title: "pull request 2".to_string(),
body: Some(String::new()),
body: String::new(),
state: PullRequestState::OPEN,
created_at: "2024-01-01T00:00:00Z".parse().unwrap(),
updated_at: "2024-01-01T00:00:00Z".parse().unwrap(),
Expand Down
240 changes: 236 additions & 4 deletions src/database/pull_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ use serde::{Deserialize, Serialize};

use super::{Database, Iter};
use crate::api::pull_request::PullRequest;
use crate::outbound::pull_requests::{PullRequestReviewState, PullRequestState};
use crate::outbound::pull_requests::{
PullRequestReviewDecision, PullRequestReviewState, PullRequestState,
PullRequestsRepositoryPullRequestsNodes, PullRequestsRepositoryPullRequestsNodesAssignees,
PullRequestsRepositoryPullRequestsNodesAuthor,
PullRequestsRepositoryPullRequestsNodesCommentsNodesAuthor,
PullRequestsRepositoryPullRequestsNodesCommits, PullRequestsRepositoryPullRequestsNodesLabels,
PullRequestsRepositoryPullRequestsNodesReviewRequests,
PullRequestsRepositoryPullRequestsNodesReviewRequestsNodesRequestedReviewer,
PullRequestsRepositoryPullRequestsNodesReviews,
PullRequestsRepositoryPullRequestsNodesReviewsNodesAuthor,
};

impl Database {
pub(crate) fn insert_pull_requests(
Expand Down Expand Up @@ -72,11 +82,11 @@ pub struct RepositoryNode {
pub struct ReviewNode {
pub(crate) author: String,
pub(crate) state: PullRequestReviewState,
pub(crate) body: Option<String>,
pub(crate) body: String,
pub(crate) url: String,
pub(crate) created_at: Timestamp,
pub(crate) published_at: Option<Timestamp>,
pub(crate) submitted_at: Timestamp,
pub(crate) submitted_at: Option<Timestamp>,
pub(crate) is_minimized: bool,
pub(crate) comments: GitHubPRCommentConnection,
}
Expand All @@ -98,7 +108,7 @@ pub struct GitHubPullRequestNode {
pub(crate) id: String,
pub(crate) number: i32,
pub(crate) title: String,
pub(crate) body: Option<String>,
pub(crate) body: String,
pub(crate) state: PullRequestState,
pub(crate) created_at: Timestamp,
pub(crate) updated_at: Timestamp,
Expand All @@ -117,3 +127,225 @@ pub struct GitHubPullRequestNode {
pub(crate) reviews: GitHubReviewConnection,
pub(crate) commits: GitHubCommitConnection,
}

impl From<PullRequestsRepositoryPullRequestsNodesAuthor> for String {
fn from(author: PullRequestsRepositoryPullRequestsNodesAuthor) -> Self {
match author {
PullRequestsRepositoryPullRequestsNodesAuthor::User(u) => u.login,
_ => String::new(),
}
}
}

impl From<PullRequestsRepositoryPullRequestsNodesLabels> for Vec<String> {
fn from(labels: PullRequestsRepositoryPullRequestsNodesLabels) -> Self {
labels
.nodes
.unwrap_or_default()
.into_iter()
.flatten()
.map(|n| n.name)
.collect()
}
}

impl From<PullRequestsRepositoryPullRequestsNodesAssignees> for Vec<String> {
fn from(assignees: PullRequestsRepositoryPullRequestsNodesAssignees) -> Self {
assignees
.nodes
.unwrap_or_default()
.into_iter()
.flatten()
.map(|u| u.login)
.collect()
}
}

impl From<PullRequestsRepositoryPullRequestsNodesReviewRequests> for Vec<String> {
fn from(reqs: PullRequestsRepositoryPullRequestsNodesReviewRequests) -> Self {
reqs
.nodes
.unwrap_or_default()
.into_iter()
.flatten()
.filter_map(|rr| match rr.requested_reviewer {
Some(
PullRequestsRepositoryPullRequestsNodesReviewRequestsNodesRequestedReviewer::User(u),
) => Some(u.login),
_ => None,
})
.collect()
}
}

impl TryFrom<PullRequestsRepositoryPullRequestsNodesReviews> for GitHubReviewConnection {
type Error = anyhow::Error;

fn try_from(reviews: PullRequestsRepositoryPullRequestsNodesReviews) -> Result<Self> {
let total_count: i32 = reviews.total_count.try_into()?;
let nodes = reviews
.nodes
.unwrap_or_default()
.into_iter()
.flatten()
.map(|node| ReviewNode {
author: node
.author
.and_then(|a| match a {
PullRequestsRepositoryPullRequestsNodesReviewsNodesAuthor::User(u) => {
Some(u.login)
}
_ => None,
})
.unwrap_or_default(),
state: node.state,
body: node.body,
url: node.url,
created_at: node.created_at,
published_at: node.published_at,
submitted_at: node.submitted_at,
is_minimized: node.is_minimized,
comments: GitHubPRCommentConnection {
total_count: node.comments.total_count.try_into().unwrap_or_default(),
// FIX: assign real data, it already exists
nodes: vec![],
},
})
.collect();

Ok(Self { total_count, nodes })
}
}

impl TryFrom<PullRequestsRepositoryPullRequestsNodesCommits> for GitHubCommitConnection {
type Error = anyhow::Error;

fn try_from(commits: PullRequestsRepositoryPullRequestsNodesCommits) -> Result<Self> {
let total_count: i32 = commits.total_count.try_into()?;
let nodes = commits
.nodes
.unwrap_or_default()
.into_iter()
.flatten()
.map(|node| {
let commit = node.commit;
Ok(CommitInner {
additions: commit.additions.try_into()?,
deletions: commit.deletions.try_into()?,
message: commit.message,
message_body: Some(commit.message_body),
author: commit
.author
.and_then(|a| a.user)
.map(|u| u.login)
.unwrap_or_default(),
changed_files_if_available: commit
.changed_files_if_available
.map(TryInto::try_into)
.transpose()?,
committed_date: commit.committed_date,
committer: commit
.committer
.and_then(|c| c.user)
.map(|u| u.login)
.unwrap_or_default(),
})
})
.collect::<Result<Vec<_>>>()?;

Ok(Self { total_count, nodes })
}
}

impl TryFrom<PullRequestsRepositoryPullRequestsNodes> for GitHubPullRequestNode {
type Error = anyhow::Error;

fn try_from(pr: PullRequestsRepositoryPullRequestsNodes) -> Result<Self> {
let number: i32 = pr.number.try_into()?;
let author = pr.author.map(String::from).unwrap_or_default();
let additions: i32 = pr.additions.try_into()?;
let deletions: i32 = pr.deletions.try_into()?;

let labels = pr.labels.map(Vec::<String>::from).unwrap_or_default();
let assignees = Vec::<String>::from(pr.assignees);
let review_requests = pr
.review_requests
.map(Vec::<String>::from)
.unwrap_or_default();

let comments_total: i32 = pr.comments.total_count.try_into()?;
let repo_owner = pr.repository.owner.login.clone();
let repo_name = pr.repository.name.clone();
let comments_nodes = pr
.comments
.nodes
.unwrap_or_default()
.into_iter()
.flatten()
.map(|node| GitHubPRComment {
author: match node.author {
Some(PullRequestsRepositoryPullRequestsNodesCommentsNodesAuthor::User(u)) => {
u.login
}
_ => String::new(),
},
body: node.body,
created_at: node.created_at,
updated_at: node.updated_at,
repository_name: repo_name.clone(),
// FIX: add url field to comments query and assign it here
url: String::new(),
})
.collect();

let reviews = pr
.reviews
.map(GitHubReviewConnection::try_from)
.transpose()?
.unwrap_or_else(|| GitHubReviewConnection {
total_count: 0,
nodes: vec![],
});

let commits = GitHubCommitConnection::try_from(pr.commits)?;

let review_decision = pr.review_decision.and_then(|d| match d {
PullRequestReviewDecision::APPROVED => Some(PullRequestReviewState::APPROVED),
PullRequestReviewDecision::CHANGES_REQUESTED => {
Some(PullRequestReviewState::CHANGES_REQUESTED)
}
PullRequestReviewDecision::REVIEW_REQUIRED => Some(PullRequestReviewState::PENDING),
PullRequestReviewDecision::Other(_) => None,
});

Ok(Self {
id: pr.id,
number,
title: pr.title,
body: pr.body,
state: pr.state,
created_at: pr.created_at,
updated_at: pr.updated_at,
closed_at: pr.closed_at,
merged_at: pr.merged_at,
author,
additions,
deletions,
url: pr.url,
repository: RepositoryNode {
owner: repo_owner,
name: repo_name,
},
labels,
comments: GitHubPRCommentConnection {
total_count: comments_total,
nodes: comments_nodes,
},
review_decision,
assignees,
review_requests,
reviews,
commits,
})
}
}
Loading
Loading