wip: pull request view & md rendering

This commit is contained in:
2026-05-11 00:32:12 +08:00
parent 9f1e051073
commit c29a923e0e
36 changed files with 2716 additions and 99 deletions

View File

@@ -0,0 +1,11 @@
query PullRequestQuery($id: ID!) {
node(id: $id) {
__typename
... on PullRequest {
title
body
state
isDraft
}
}
}

View File

@@ -0,0 +1,267 @@
query PullRequestTimelineQuery($id: ID!, $first: Int!, $after: String) {
node(id: $id) {
__typename
... on PullRequest {
timelineItems(first: $first, after: $after) {
pageInfo {
endCursor
hasNextPage
}
nodes {
__typename
... on AssignedEvent {
createdAt
actor {
...actorFields
}
assignee {
...assigneeFields
}
}
... on UnassignedEvent {
createdAt
actor {
...actorFields
}
assignee {
...assigneeFields
}
}
... on IssueComment {
createdAt
author {
...actorFields
}
body
}
... on PullRequestCommit {
commit {
committedDate
abbreviatedOid
messageHeadline
}
}
... on PullRequestReview {
createdAt
author {
...actorFields
}
state
body
}
... on ReviewRequestedEvent {
createdAt
actor {
...actorFields
}
requestedReviewer {
...requestedReviewerFields
}
}
... on ReviewRequestRemovedEvent {
createdAt
actor {
...actorFields
}
requestedReviewer {
...requestedReviewerFields
}
}
... on ReviewDismissedEvent {
createdAt
actor {
...actorFields
}
}
... on MergedEvent {
createdAt
actor {
...actorFields
}
}
... on ClosedEvent {
createdAt
actor {
...actorFields
}
}
... on ReopenedEvent {
createdAt
actor {
...actorFields
}
}
... on ConvertToDraftEvent {
createdAt
actor {
...actorFields
}
}
... on ReadyForReviewEvent {
createdAt
actor {
...actorFields
}
}
... on HeadRefForcePushedEvent {
createdAt
actor {
...actorFields
}
beforeCommit {
abbreviatedOid
}
afterCommit {
abbreviatedOid
}
}
... on BaseRefChangedEvent {
createdAt
actor {
...actorFields
}
}
... on LabeledEvent {
createdAt
actor {
...actorFields
}
label {
name
}
}
... on UnlabeledEvent {
createdAt
actor {
...actorFields
}
label {
name
}
}
... on MilestonedEvent {
createdAt
actor {
...actorFields
}
milestoneTitle
}
... on DemilestonedEvent {
createdAt
actor {
...actorFields
}
milestoneTitle
}
... on ReferencedEvent {
createdAt
actor {
...actorFields
}
}
... on CrossReferencedEvent {
createdAt
actor {
...actorFields
}
}
... on AutoMergeEnabledEvent {
createdAt
actor {
...actorFields
}
}
... on AutoMergeDisabledEvent {
createdAt
actor {
...actorFields
}
reason
}
... on AddedToMergeQueueEvent {
createdAt
actor {
...actorFields
}
}
... on RemovedFromMergeQueueEvent {
createdAt
actor {
...actorFields
}
}
}
}
}
}
}
fragment actorFields on Actor {
__typename
login
avatarUrl(size: 100)
}
fragment assigneeFields on Assignee {
__typename
... on Bot {
login
avatarUrl(size: 100)
}
... on Mannequin {
login
avatarUrl(size: 100)
}
... on Organization {
login
avatarUrl(size: 100)
}
... on User {
login
avatarUrl(size: 100)
}
}
fragment requestedReviewerFields on RequestedReviewer {
__typename
... on Bot {
login
avatarUrl(size: 100)
}
... on Mannequin {
login
avatarUrl(size: 100)
}
... on Team {
name
}
... on User {
login
avatarUrl(size: 100)
}
}

View File

@@ -1,10 +1,11 @@
query PullRequestQuery($query: String!) {
query PullRequestPaginationQuery($query: String!) {
search(query: $query, first: 10, type: ISSUE) {
issueCount
edges {
node {
__typename
... on PullRequest {
id
isDraft
title
state

View File

@@ -1,17 +1,22 @@
use std::ops::Deref;
use graphql_client::{GraphQLQuery, Response};
use reqwest::Method;
use graphql_client::GraphQLQuery;
use serde::Deserialize;
use crate::{
api::{
self,
issues::pull_request_query::{PullRequestQuerySearchEdgesNode, PullRequestState},
issues::{
pull_request_pagination_query::PullRequestPaginationQuerySearchEdgesNode,
pull_request_query::PullRequestQueryNode,
},
},
query,
};
type DateTime = String;
type URI = String;
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Deserialize)]
#[serde(transparent)]
#[repr(transparent)]
@@ -25,6 +30,24 @@ impl Deref for Id {
}
}
impl From<&str> for Id {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl From<String> for Id {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<Id> for String {
fn from(value: Id) -> Self {
value.0
}
}
#[derive(Debug, Deserialize)]
pub(crate) struct PullRequestPaginatedResponse {
pub(crate) items: Vec<PullRequest>,
@@ -34,18 +57,155 @@ pub(crate) struct PullRequestPaginatedResponse {
#[derive(Debug, Deserialize)]
pub(crate) struct PullRequest {
pub(crate) id: Id,
pub(crate) title: String,
pub(crate) state: IssueState,
pub(crate) state: PullRequestState,
pub(crate) is_draft: bool,
pub(crate) repo_slug: String,
}
#[derive(Debug, Clone, Copy, Deserialize)]
pub(crate) enum IssueState {
Open,
Closed,
Merged,
Unknown,
#[derive(Debug, Deserialize)]
pub(crate) struct DetailedPullRequest {
pub(crate) title: String,
pub(crate) state: PullRequestState,
pub(crate) is_draft: bool,
pub(crate) body: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct PullRequestTimeline {
pub(crate) items: Vec<PullRequestTimelineItem>,
pub(crate) end_cursor: Option<String>,
pub(crate) has_next_page: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum PullRequestTimelineItem {
Assigned {
created_at: String,
actor: Option<TimelineActor>,
assignee: Option<TimelineActor>,
},
Unassigned {
created_at: String,
actor: Option<TimelineActor>,
assignee: Option<TimelineActor>,
},
Comment {
created_at: String,
author: Option<TimelineActor>,
body: String,
},
Commit {
committed_at: String,
abbreviated_oid: String,
message_headline: String,
},
Review {
created_at: String,
author: Option<TimelineActor>,
state: String,
body: String,
},
ReviewRequested {
created_at: String,
actor: Option<TimelineActor>,
reviewer: Option<TimelineActor>,
},
ReviewRequestRemoved {
created_at: String,
actor: Option<TimelineActor>,
reviewer: Option<TimelineActor>,
},
ReviewDismissed {
created_at: String,
actor: Option<TimelineActor>,
},
Merged {
created_at: String,
actor: Option<TimelineActor>,
},
Closed {
created_at: String,
actor: Option<TimelineActor>,
},
Reopened {
created_at: String,
actor: Option<TimelineActor>,
},
ConvertToDraft {
created_at: String,
actor: Option<TimelineActor>,
},
ReadyForReview {
created_at: String,
actor: Option<TimelineActor>,
},
HeadRefForcePushed {
created_at: String,
actor: Option<TimelineActor>,
before_commit_oid: Option<String>,
after_commit_oid: Option<String>,
},
BaseRefChanged {
created_at: String,
actor: Option<TimelineActor>,
},
Labeled {
created_at: String,
actor: Option<TimelineActor>,
label: String,
},
Unlabeled {
created_at: String,
actor: Option<TimelineActor>,
label: String,
},
Milestoned {
created_at: String,
actor: Option<TimelineActor>,
milestone_title: String,
},
Demilestoned {
created_at: String,
actor: Option<TimelineActor>,
milestone_title: String,
},
Referenced {
created_at: String,
actor: Option<TimelineActor>,
},
CrossReferenced {
created_at: String,
actor: Option<TimelineActor>,
},
AutoMergeEnabled {
created_at: String,
actor: Option<TimelineActor>,
},
AutoMergeDisabled {
created_at: String,
actor: Option<TimelineActor>,
reason: String,
},
AddedToMergeQueue {
created_at: String,
actor: Option<TimelineActor>,
},
RemovedFromMergeQueue {
created_at: String,
actor: Option<TimelineActor>,
},
Other {
typename: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TimelineActor {
pub(crate) kind: String,
pub(crate) name: String,
pub(crate) avatar_url: Option<String>,
}
impl std::fmt::Display for Id {
@@ -54,24 +214,50 @@ impl std::fmt::Display for Id {
}
}
impl From<PullRequestState> for IssueState {
fn from(state: PullRequestState) -> Self {
match state {
PullRequestState::OPEN => Self::Open,
PullRequestState::CLOSED => Self::Closed,
PullRequestState::MERGED => Self::Merged,
_ => Self::Unknown,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub(crate) enum PullRequestState {
Open,
Closed,
Merged,
}
#[derive(graphql_client::GraphQLQuery)]
#[graphql(
schema_path = "src/api/graphql/schema.json",
query_path = "src/api/graphql/list_pull_requests.graphql"
extern_enums("PullRequestState")
)]
struct PullRequestPaginationQuery;
#[derive(graphql_client::GraphQLQuery)]
#[graphql(
schema_path = "src/api/graphql/schema.json",
query_path = "src/api/graphql/fetch_pull_request.graphql"
extern_enums("PullRequestState")
)]
struct PullRequestQuery;
#[derive(graphql_client::GraphQLQuery)]
#[graphql(
schema_path = "src/api/graphql/schema.json",
query_path = "src/api/graphql/fetch_pull_request_timeline.graphql"
)]
struct PullRequestTimelineQuery;
pub(super) type PullRequestTimelineResponse = pull_request_timeline_query::ResponseData;
#[cfg(test)]
pub(super) type PullRequestTimelineResponseNode = PullRequestTimelineQueryNode;
#[cfg(test)]
pub(super) type PullRequestTimelineNode =
pull_request_timeline_query::PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes;
use self::pull_request_timeline_query::{
PullRequestReviewState, PullRequestTimelineQueryNode,
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes, actorFields, actorFieldsOn,
assigneeFields, requestedReviewerFields,
};
#[derive(Clone)]
pub(crate) struct ListPullRequests {
pub filter: Option<&'static str>,
@@ -103,20 +289,15 @@ impl query::QueryFn for ListPullRequests {
None => "is:pr archived:false sort:updated-desc".into(),
};
let gql = PullRequestQuery::build_query(pull_request_query::Variables {
query: query_string,
});
let gql =
PullRequestPaginationQuery::build_query(pull_request_pagination_query::Variables {
query: query_string,
});
let res = c
.github_request(Method::POST, "/graphql")?
.json(&gql)
.send()
.await?;
let res = c.github_graphql_request(&gql)?.send().await?;
let data = api::parse_graphql_response::<pull_request_query::ResponseData>(res)
.await?
.data
.unwrap();
let (_, data) =
api::parse_graphql_response::<pull_request_pagination_query::ResponseData>(res).await?;
Ok(PullRequestPaginatedResponse {
items: data
@@ -127,10 +308,11 @@ impl query::QueryFn for ListPullRequests {
.flatten()
.filter_map(|edge| {
edge.node.and_then(|n| match n {
PullRequestQuerySearchEdgesNode::PullRequest(p) => {
PullRequestPaginationQuerySearchEdgesNode::PullRequest(p) => {
Some(PullRequest {
id: p.id.into(),
title: p.title,
state: p.state.into(),
state: p.state,
is_draft: p.is_draft,
repo_slug: format!(
"{}/{}",
@@ -149,3 +331,399 @@ impl query::QueryFn for ListPullRequests {
})
}
}
#[derive(Clone)]
pub(crate) struct FetchPullRequest {
pub(crate) id: Id,
}
impl query::QueryFn for FetchPullRequest {
type Data = DetailedPullRequest;
type Error = api::Error;
type Context = api::QueryContext;
fn key(&self) -> query::Key {
format!("issues/{}", self.id).into()
}
async fn run(&self, c: &Self::Context) -> Result<Self::Data, Self::Error> {
#[cfg(debug_assertions)]
if c.should_use_fixtures {
return super::mock::fetch_pull_request(&self.id);
}
let gql = PullRequestQuery::build_query(pull_request_query::Variables {
id: self.id.clone().into(),
});
let res = c.github_graphql_request(&gql)?.send().await?;
let (_, data) =
api::parse_graphql_response::<pull_request_query::ResponseData>(res).await?;
data.node
.ok_or(api::Error::MalformedResponse(
"missing 'node' field on PullRequestQuery response".into(),
))
.and_then(|n| match n {
PullRequestQueryNode::PullRequest(p) => Ok(DetailedPullRequest {
title: p.title,
state: p.state,
is_draft: p.is_draft,
body: p.body,
}),
_ => Err(api::Error::MalformedResponse(
"unexpected node type on PullRequestQuery".into(),
)),
})
}
}
#[derive(Clone)]
pub(crate) struct FetchPullRequestTimeline {
pub(crate) id: Id,
pub(crate) first: i64,
pub(crate) after: Option<String>,
}
impl FetchPullRequestTimeline {
pub(crate) fn new(id: Id, first: i64, after: Option<String>) -> Self {
Self { id, first, after }
}
}
impl query::QueryFn for FetchPullRequestTimeline {
type Data = PullRequestTimeline;
type Error = api::Error;
type Context = api::QueryContext;
fn key(&self) -> query::Key {
format!(
"issues/{}/timeline?first={}&after={}",
self.id,
self.first,
self.after.as_deref().unwrap_or_default()
)
.into()
}
async fn run(&self, c: &Self::Context) -> Result<Self::Data, Self::Error> {
fn normalize_actor(actor: actorFields) -> TimelineActor {
let actorFields {
login,
avatar_url,
on,
} = actor;
TimelineActor {
kind: match on {
actorFieldsOn::Bot => "Bot",
actorFieldsOn::EnterpriseUserAccount => "EnterpriseUserAccount",
actorFieldsOn::Mannequin => "Mannequin",
actorFieldsOn::Organization => "Organization",
actorFieldsOn::User => "User",
}
.into(),
name: login,
avatar_url: Some(avatar_url),
}
}
fn normalize_assignee(actor: assigneeFields) -> TimelineActor {
match actor {
assigneeFields::Bot(actor) => TimelineActor {
kind: "Bot".into(),
name: actor.login,
avatar_url: Some(actor.avatar_url),
},
assigneeFields::Mannequin(actor) => TimelineActor {
kind: "Mannequin".into(),
name: actor.login,
avatar_url: Some(actor.avatar_url),
},
assigneeFields::Organization(actor) => TimelineActor {
kind: "Organization".into(),
name: actor.login,
avatar_url: Some(actor.avatar_url),
},
assigneeFields::User(actor) => TimelineActor {
kind: "User".into(),
name: actor.login,
avatar_url: Some(actor.avatar_url),
},
}
}
fn normalize_requested_reviewer(actor: requestedReviewerFields) -> TimelineActor {
match actor {
requestedReviewerFields::Bot(actor) => TimelineActor {
kind: "Bot".into(),
name: actor.login,
avatar_url: Some(actor.avatar_url),
},
requestedReviewerFields::Mannequin(actor) => TimelineActor {
kind: "Mannequin".into(),
name: actor.login,
avatar_url: Some(actor.avatar_url),
},
requestedReviewerFields::Team(actor) => TimelineActor {
kind: "Team".into(),
name: actor.name,
avatar_url: None,
},
requestedReviewerFields::User(actor) => TimelineActor {
kind: "User".into(),
name: actor.login,
avatar_url: Some(actor.avatar_url),
},
}
}
fn normalize_review_state(state: PullRequestReviewState) -> String {
match state {
PullRequestReviewState::PENDING => "PENDING",
PullRequestReviewState::COMMENTED => "COMMENTED",
PullRequestReviewState::APPROVED => "APPROVED",
PullRequestReviewState::CHANGES_REQUESTED => "CHANGES_REQUESTED",
PullRequestReviewState::DISMISSED => "DISMISSED",
_ => "OTHER",
}
.into()
}
fn normalize_item(
value: PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes,
) -> PullRequestTimelineItem {
match value {
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::AssignedEvent(
event,
) => PullRequestTimelineItem::Assigned {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
assignee: event.assignee.map(normalize_assignee),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::UnassignedEvent(
event,
) => PullRequestTimelineItem::Unassigned {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
assignee: event.assignee.map(normalize_assignee),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::IssueComment(
event,
) => PullRequestTimelineItem::Comment {
created_at: event.created_at,
author: event.author.map(normalize_actor),
body: event.body,
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::PullRequestCommit(
event,
) => PullRequestTimelineItem::Commit {
committed_at: event.commit.committed_date,
abbreviated_oid: event.commit.abbreviated_oid,
message_headline: event.commit.message_headline,
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::PullRequestReview(
event,
) => PullRequestTimelineItem::Review {
created_at: event.created_at,
author: event.author.map(normalize_actor),
state: normalize_review_state(event.state),
body: event.body,
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::ReviewRequestedEvent(
event,
) => PullRequestTimelineItem::ReviewRequested {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
reviewer: event.requested_reviewer.map(normalize_requested_reviewer),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::ReviewRequestRemovedEvent(
event,
) => PullRequestTimelineItem::ReviewRequestRemoved {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
reviewer: event.requested_reviewer.map(normalize_requested_reviewer),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::ReviewDismissedEvent(
event,
) => PullRequestTimelineItem::ReviewDismissed {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::MergedEvent(
event,
) => PullRequestTimelineItem::Merged {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::ClosedEvent(
event,
) => PullRequestTimelineItem::Closed {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::ReopenedEvent(
event,
) => PullRequestTimelineItem::Reopened {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::ConvertToDraftEvent(
event,
) => PullRequestTimelineItem::ConvertToDraft {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::ReadyForReviewEvent(
event,
) => PullRequestTimelineItem::ReadyForReview {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::HeadRefForcePushedEvent(
event,
) => PullRequestTimelineItem::HeadRefForcePushed {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
before_commit_oid: event.before_commit.map(|commit| commit.abbreviated_oid),
after_commit_oid: event.after_commit.map(|commit| commit.abbreviated_oid),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::BaseRefChangedEvent(
event,
) => PullRequestTimelineItem::BaseRefChanged {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::LabeledEvent(
event,
) => PullRequestTimelineItem::Labeled {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
label: event.label.name,
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::UnlabeledEvent(
event,
) => PullRequestTimelineItem::Unlabeled {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
label: event.label.name,
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::MilestonedEvent(
event,
) => PullRequestTimelineItem::Milestoned {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
milestone_title: event.milestone_title,
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::DemilestonedEvent(
event,
) => PullRequestTimelineItem::Demilestoned {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
milestone_title: event.milestone_title,
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::ReferencedEvent(
event,
) => PullRequestTimelineItem::Referenced {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::CrossReferencedEvent(
event,
) => PullRequestTimelineItem::CrossReferenced {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::AutoMergeEnabledEvent(
event,
) => PullRequestTimelineItem::AutoMergeEnabled {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::AutoMergeDisabledEvent(
event,
) => PullRequestTimelineItem::AutoMergeDisabled {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
reason: event.reason.unwrap_or_default(),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::AddedToMergeQueueEvent(
event,
) => PullRequestTimelineItem::AddedToMergeQueue {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
PullRequestTimelineQueryNodeOnPullRequestTimelineItemsNodes::RemovedFromMergeQueueEvent(
event,
) => PullRequestTimelineItem::RemovedFromMergeQueue {
created_at: event.created_at,
actor: event.actor.map(normalize_actor),
},
_ => PullRequestTimelineItem::Other {
typename: "Other".into(),
},
}
}
#[cfg(debug_assertions)]
let data = if c.should_use_fixtures {
super::mock::fetch_pull_request_timeline(&self.id, self.after.as_deref())?
} else {
let gql =
PullRequestTimelineQuery::build_query(pull_request_timeline_query::Variables {
id: self.id.clone().into(),
first: self.first,
after: self.after.clone(),
});
let res = c.github_graphql_request(&gql)?.send().await?;
api::parse_graphql_response::<PullRequestTimelineResponse>(res)
.await?
.1
};
#[cfg(not(debug_assertions))]
let data = {
let gql =
PullRequestTimelineQuery::build_query(pull_request_timeline_query::Variables {
id: self.id.clone().into(),
first: self.first,
after: self.after.clone(),
});
let res = c.github_graphql_request(&gql)?.send().await?;
api::parse_graphql_response::<PullRequestTimelineResponse>(res)
.await?
.1
};
let pull_request = data
.node
.ok_or(api::Error::MalformedResponse(
"missing 'node' field on PullRequestTimelineQuery response".into(),
))
.and_then(|node| match node {
PullRequestTimelineQueryNode::PullRequest(pull_request) => Ok(pull_request),
_ => Err(api::Error::MalformedResponse(
"unexpected node type on PullRequestTimelineQuery".into(),
)),
})?;
let timeline = pull_request.timeline_items;
let items = timeline
.nodes
.unwrap_or_default()
.into_iter()
.flatten()
.map(normalize_item)
.collect();
Ok(PullRequestTimeline {
items,
end_cursor: timeline.page_info.end_cursor,
has_next_page: timeline.page_info.has_next_page,
})
}
}

View File

@@ -29,11 +29,56 @@ pub(crate) fn list_pull_requests(
page: u32,
) -> Result<issues::PullRequestPaginatedResponse, api::Error> {
let filter = filter.unwrap_or_default();
let json = issues_pull_requests(filter, page).ok_or_else(|| {
api::Error::MissingMockFixture(format!("issues.pull_requests filter={filter} page={page}"))
let fixture_filter = issue_filter_fixture_key(filter);
let json = issues_pull_requests(fixture_filter, page).ok_or_else(|| {
api::Error::MissingMockFixture(format!(
"issues.pull_requests filter={filter} fixture_filter={fixture_filter} page={page}"
))
})?;
parse_fixture(&format!("issues.pull_requests.{filter}.page{page}"), json)
parse_fixture(
&format!("issues.pull_requests.{fixture_filter}.page{page}"),
json,
)
}
pub(crate) fn fetch_pull_request(
id: &issues::Id,
) -> Result<issues::DetailedPullRequest, api::Error> {
let id = id.to_string();
let json = issues_pull_request(&id)
.ok_or_else(|| api::Error::MissingMockFixture(format!("issues.pull_request id={id}")))?;
parse_fixture(&format!("issues.pull_request.{id}"), json)
}
pub(crate) fn fetch_pull_request_timeline(
id: &issues::Id,
after: Option<&str>,
) -> Result<issues::PullRequestTimelineResponse, api::Error> {
let id = id.to_string();
let json = issues_pull_request_timeline(&id, after).ok_or_else(|| {
api::Error::MissingMockFixture(format!(
"issues.pull_request_timeline id={id} after={}",
after.unwrap_or_default()
))
})?;
parse_fixture(
&format!(
"issues.pull_request_timeline.{id}.after.{}",
after.unwrap_or("start")
),
json,
)
.map_err(|err| {
println!(
"[mock fixture] failed to parse issues.pull_request_timeline.{id}.after.{}: {:?}",
after.unwrap_or("start"),
err
);
err
})
}
fn parse_fixture<T>(name: &str, json: &'static str) -> Result<T, api::Error>
@@ -42,6 +87,184 @@ where
{
serde_json::from_str(json).map_err(|err| {
println!("[mock fixture] failed to parse {name}: {err}");
api::Error::MalformedResponse(err)
api::Error::MalformedResponse(err.to_string())
})
}
fn issue_filter_fixture_key(filter: &str) -> &str {
let filter = filter.trim();
if filter.is_empty() {
return "all";
}
if filter.contains("author:@me") {
return "created";
}
if filter.contains("review-requested:@me") || filter.contains("assignee:@me") {
return "assigned";
}
filter
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn list_pull_request_fixtures_parse_with_current_filter_strings() {
let authored = list_pull_requests(Some("author:@me state:open"), 1)
.expect("authored fixture should parse");
let assigned = list_pull_requests(Some("review-requested:@me"), 1)
.expect("assigned fixture should parse");
let all = list_pull_requests(None, 1).expect("all fixture should parse");
assert_eq!(authored.items[0].id, issues::Id::from("PR_kwDONovem85"));
assert_eq!(assigned.items[0].state, issues::PullRequestState::Merged);
assert_eq!(all.items[0].state, issues::PullRequestState::Open);
}
#[test]
fn pull_request_detail_fixtures_parse() {
let merged = fetch_pull_request(&issues::Id::from("PR_kwDOSprint62"))
.expect("merged pull request fixture should parse");
let documented_failover = fetch_pull_request(&issues::Id::from("PR_kwDOInfra19"))
.expect("closed pull request fixture should parse");
let dashboard_markdown = fetch_pull_request(&issues::Id::from("PR_kwDONovem84"))
.expect("dashboard pull request fixture should parse");
assert_eq!(merged.state, issues::PullRequestState::Merged);
assert!(merged.body.contains("| Stage | Owner | Status |"));
assert!(
documented_failover
.body
.contains("./scripts/failover promote-standby")
);
assert!(dashboard_markdown.body.contains("```rust"));
}
#[test]
fn pull_request_timeline_fixtures_parse() {
let first_page = fetch_pull_request_timeline(&issues::Id::from("PR_kwDOSprint62"), None)
.expect("first timeline fixture page should parse");
let first_page_json: serde_json::Value =
serde_json::from_str(issues_pull_request_timeline("PR_kwDOSprint62", None).unwrap())
.expect("first timeline fixture json should parse");
let after = first_page_json
.get("node")
.and_then(|node| node.get("timelineItems"))
.and_then(|timeline| timeline.get("pageInfo"))
.and_then(|page_info| page_info.get("endCursor"))
.and_then(serde_json::Value::as_str)
.map(str::to_owned)
.expect("first timeline fixture page should provide an end cursor");
let second_page =
fetch_pull_request_timeline(&issues::Id::from("PR_kwDOSprint62"), Some(&after))
.expect("second timeline fixture page should parse");
let second_page_json: serde_json::Value = serde_json::from_str(
issues_pull_request_timeline("PR_kwDOSprint62", Some(&after)).unwrap(),
)
.expect("second timeline fixture json should parse");
let first_nodes = first_page_json
.get("node")
.and_then(|node| node.get("timelineItems"))
.and_then(|timeline| timeline.get("nodes"))
.and_then(serde_json::Value::as_array)
.map(Vec::len)
.expect("first timeline fixture page should contain nodes");
let second_has_next_page = second_page_json
.get("node")
.and_then(|node| node.get("timelineItems"))
.and_then(|timeline| timeline.get("pageInfo"))
.and_then(|page_info| page_info.get("hasNextPage"))
.and_then(serde_json::Value::as_bool)
.expect("second timeline fixture page should contain pagination data");
let third_cursor = second_page_json
.get("node")
.and_then(|node| node.get("timelineItems"))
.and_then(|timeline| timeline.get("pageInfo"))
.and_then(|page_info| page_info.get("endCursor"))
.and_then(serde_json::Value::as_str)
.map(str::to_owned)
.expect("second timeline fixture page should provide an end cursor");
let third_page =
fetch_pull_request_timeline(&issues::Id::from("PR_kwDOSprint62"), Some(&third_cursor))
.expect("third timeline fixture page should parse");
let third_page_json: serde_json::Value = serde_json::from_str(
issues_pull_request_timeline("PR_kwDOSprint62", Some(&third_cursor)).unwrap(),
)
.expect("third timeline fixture json should parse");
let first_page_nodes = match first_page.node.as_ref() {
Some(issues::PullRequestTimelineResponseNode::PullRequest(pull_request)) => {
pull_request
.timeline_items
.nodes
.as_ref()
.expect("first timeline fixture page should contain timeline nodes")
}
_ => panic!("first timeline fixture page should resolve to a pull request node"),
};
let second_page_nodes = match second_page.node.as_ref() {
Some(issues::PullRequestTimelineResponseNode::PullRequest(pull_request)) => {
pull_request
.timeline_items
.nodes
.as_ref()
.expect("second timeline fixture page should contain timeline nodes")
}
_ => panic!("second timeline fixture page should resolve to a pull request node"),
};
let third_page_nodes = match third_page.node.as_ref() {
Some(issues::PullRequestTimelineResponseNode::PullRequest(pull_request)) => {
pull_request
.timeline_items
.nodes
.as_ref()
.expect("third timeline fixture page should contain timeline nodes")
}
_ => panic!("third timeline fixture page should resolve to a pull request node"),
};
assert_eq!(
first_page_json["node"]["timelineItems"]["pageInfo"]["endCursor"],
"timeline:PR_kwDOSprint62:page1"
);
assert!(first_nodes >= 5);
assert!(second_has_next_page);
assert_eq!(
second_page_json["node"]["timelineItems"]["pageInfo"]["hasNextPage"],
true
);
assert_eq!(
third_page_json["node"]["timelineItems"]["pageInfo"]["hasNextPage"],
false
);
assert!(matches!(
first_page_nodes.first(),
Some(Some(issues::PullRequestTimelineNode::AssignedEvent(_)))
));
assert!(second_page_nodes.iter().flatten().any(|item| matches!(
item,
issues::PullRequestTimelineNode::AutoMergeDisabledEvent(_)
)));
assert!(
third_page_nodes
.iter()
.flatten()
.any(|item| matches!(item, issues::PullRequestTimelineNode::MergedEvent(_)))
);
}
}