Skip to content
Merged
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
24 changes: 24 additions & 0 deletions crates/fctl-client/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,30 @@ impl Api {
self.get(&q).await
}

/// The sessions that list a pull request in `state` (`open`, `merged`,
/// `closed` or `any`), newest first: the pull requests page's, and
/// `fctl pulls`'.
pub async fn sessions_with_pulls(
&self,
workspace: Option<&str>,
project: Option<&str>,
state: &str,
mine: bool,
limit: u32,
) -> Result<SessionList> {
let mut q = format!("/sessions?limit={limit}&pulls={}", enc(state));
if let Some(w) = workspace {
q.push_str(&format!("&workspace={}", enc(w)));
}
if let Some(p) = project {
q.push_str(&format!("&project={}", enc(p)));
}
if mine {
q.push_str("&mine=1");
}
self.get(&q).await
}

/// A project's sessions with its review agents', its archived ones and
/// those its schedules hide among them, which the other lists leave
/// out: to find one by its number.
Expand Down
23 changes: 23 additions & 0 deletions crates/fctl-control/src/api/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,29 @@ pub async fn list(ctx: &Ctx, req: &Request) -> AppResult<Response> {
if http::query(req, "mine").is_some_and(|v| v == "1" || v == "true") {
sql.push_str(" AND s.created_by = ?1");
}
// The sessions that list a pull request, in a state, for the pull
// requests page and the sidebar: among those `pull_request_sessions`
// has, those whose own list (what their pages show, from their agents'
// lookups and GitHub's webhooks) has one in that state; sessions from
// before that list have their one. One nobody looked up counts as open.
if let Some(pulls) = http::query(req, "pulls") {
let states = match pulls.as_str() {
"any" | "1" | "true" => None,
"open" => Some("'open','draft','queued'"),
"merged" => Some("'merged'"),
"closed" => Some("'closed'"),
_ => {
return Err(validation("pulls", "Use open, merged, closed or any"));
}
};
sql.push_str(" AND s.id IN (SELECT session_id FROM pull_request_sessions)");
if let Some(states) = states {
sql.push_str(&format!(
" AND EXISTS (SELECT 1 FROM json_each(COALESCE(s.pull_requests, json_array(json_object('state', s.pr_state)))) j \
WHERE COALESCE(json_extract(j.value, '$.state'), 'open') IN ({states}))"
));
}
}
// Review agents' sessions have a list of their own (the project's
// reviews), and stay off the Agent Floor and the project's sessions.
if !http::query(req, "reviews").is_some_and(|v| v == "1" || v == "true") {
Expand Down
153 changes: 153 additions & 0 deletions crates/fctl-domain/src/pulls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use fctl_proto::Millis;
use fctl_proto::event::GitState;
use fctl_proto::session::{PullRequestChecks, PullRequestRef, RepoRef, Session};
use serde::Serialize;

/// Most pull requests a session lists: the first its agent found.
pub const MAX_PULL_REQUESTS: usize = 20;
Expand Down Expand Up @@ -379,6 +380,92 @@ pub fn backfill(s: &mut Session) {
}
}

/// Whether `url` is a pull request's on GitHub, and nothing more:
/// `https://github.com/<owner>/<name>/pull/<number>`.
pub fn is_github_pull(url: &str) -> bool {
let Some(rest) = url.strip_prefix("https://github.com/") else {
return false;
};
let parts: Vec<&str> = rest.split('/').collect();
let name_ok = |p: &str| {
!p.is_empty()
&& p.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-'))
};
matches!(parts.as_slice(), [owner, name, "pull", number]
if name_ok(owner) && name_ok(name) && !number.is_empty() && number.chars().all(|c| c.is_ascii_digit()))
}

/// A pull request as the pull requests page and `fctl pulls` list it: as
/// the session that last heard of it has it, with every session that
/// lists it, newest first.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Listed {
pub pull_request: PullRequestRef,
pub sessions: Vec<SessionRef>,
}

/// A session, as a listed pull request names it.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct SessionRef {
pub id: fctl_proto::SessionId,
pub project_slug: String,
pub number: u32,
pub title: String,
pub status: fctl_proto::session::SessionStatus,
pub created_by: fctl_proto::session::UserRef,
}

/// The pull requests `sessions` list (newest session first), each once:
/// the freshest of what the sessions say of it, those whose state is in
/// `states` (empty: any; a pull request nobody looked up counts as open),
/// the ones that changed last first.
pub fn listed(sessions: &[fctl_proto::session::Session], states: &[&str]) -> Vec<Listed> {
Comment thread
fctl-review[bot] marked this conversation as resolved.
Resolved
let mut out: Vec<Listed> = Vec::new();
for s in sessions {
let prs: Vec<&PullRequestRef> = if s.pull_requests.is_empty() {
s.pull_request.iter().collect()
} else {
s.pull_requests.iter().collect()
};
// Only links to GitHub's pull requests, as the web app's list has
// them: what runs in a sandbox can pose as its agent.
for pr in prs.into_iter().filter(|pr| is_github_pull(&pr.url)) {
let me = SessionRef {
id: s.id.clone(),
project_slug: s.project_slug.clone(),
number: s.number,
title: s.title.clone(),
status: s.status,
created_by: s.created_by.clone(),
};
match out.iter_mut().find(|l| l.pull_request.url == pr.url) {
Some(l) => {
if pr.updated_at.unwrap_or(0) > l.pull_request.updated_at.unwrap_or(0) {
l.pull_request = pr.clone();
}
l.sessions.push(me);
}
None => out.push(Listed {
pull_request: pr.clone(),
sessions: vec![me],
}),
}
}
}
out.retain(|l| {
states.is_empty() || states.contains(&l.pull_request.state.as_deref().unwrap_or("open"))
});
let moved = |l: &Listed| {
l.pull_request
.updated_at
.or(l.pull_request.state_changed_at)
.unwrap_or(0)
};
out.sort_by_key(|l| std::cmp::Reverse(moved(l)));
out
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -973,4 +1060,70 @@ mod tests {
assert_eq!(github_time(bad), None, "{bad}");
}
}

/// A session that lists `prs`, as the API sends one.
fn listing(n: u32, prs: Vec<PullRequestRef>) -> Session {
let mut s: Session = serde_json::from_value(serde_json::json!({
"id": format!("ses_{n}"),
"project_id": "prj_1",
"project_slug": "shop",
"number": n,
"title": format!("Session {n}"),
"status": "stopped",
"created_by": { "id": "usr_1", "name": "Noor Haddad" },
"created_at": i64::from(n),
"updated_at": i64::from(n),
"stats": {},
"archived": false
}))
.unwrap_or_else(|e| panic!("a session: {e}"));
s.pull_requests = prs;
s
}

#[test]
fn pull_requests_are_listed_once_with_every_session_that_lists_them() {
let mut fresh = pr("acme/shop", 12);
fresh.updated_at = Some(30);
fresh.state = Some("merged".into());
let mut stale = pr("acme/shop", 12);
stale.updated_at = Some(10);
let mut other = pr("acme/platform", 7);
other.updated_at = Some(20);
// Newest session first, as the API lists them.
let sessions = [
listing(3, vec![stale]),
listing(2, vec![other]),
listing(1, vec![fresh]),
];
let all = listed(&sessions, &[]);
assert_eq!(all.len(), 2);
// The freshest of what the sessions say, the one that changed last first.
assert_eq!(all[0].pull_request.number, 12);
assert_eq!(all[0].pull_request.state.as_deref(), Some("merged"));
assert_eq!(
all[0].sessions.iter().map(|s| s.number).collect::<Vec<_>>(),
[3, 1]
);
assert_eq!(all[1].pull_request.number, 7);
// Only those in a state; one nobody looked up counts as open.
let open = listed(&sessions, &["open", "draft", "queued"]);
assert_eq!(
open.iter()
.map(|l| l.pull_request.number)
.collect::<Vec<_>>(),
[7]
);
let mut unknown = pr("acme/web", 1);
unknown.state = None;
assert_eq!(listed(&[listing(4, vec![unknown])], &["open"]).len(), 1);
// Only GitHub's pull requests: a sandbox can pose as its agent.
let mut elsewhere = pr("acme/web", 2);
elsewhere.url = "https://evil.example/acme/web/pull/2".into();
let mut longer = pr("acme/web", 3);
longer.url.push_str("/files");
assert!(listed(&[listing(5, vec![elsewhere, longer])], &[]).is_empty());
assert!(is_github_pull("https://github.com/acme/web.site/pull/12"));
assert!(!is_github_pull("https://github.com/acme/web/issues/12"));
}
}
1 change: 1 addition & 0 deletions crates/fctl/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod project_members;
pub mod project_secrets;
pub mod projects;
pub mod publish;
pub mod pulls;
pub mod resolve;
pub mod retro;
pub mod review;
Expand Down
Loading
Loading