use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;
pub const PR_FIELDS: &str =
"number,title,author,headRefName,headRefOid,baseRefName,files,statusCheckRollup,url";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrFile {
pub path: String,
pub additions: u64,
pub deletions: u64,
pub change: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum CheckState {
Fail,
Pending,
Pass,
Neutral,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Check {
pub name: String,
pub state: CheckState,
pub url: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrInfo {
pub number: u64,
pub title: String,
pub author: String,
pub url: String,
pub head_ref: String,
pub head_oid: String,
pub base_ref: String,
pub files: Vec<PrFile>,
pub checks: Vec<Check>,
}
pub fn view_args(selector: &str) -> Vec<String> {
vec![
String::from("pr"),
String::from("view"),
selector.to_string(),
String::from("--json"),
String::from(PR_FIELDS),
]
}
pub fn split_diff_by_file(patch: &str) -> BTreeMap<String, String> {
let mut out = BTreeMap::new();
let mut current: Option<(String, String, String)> = None; let flush = |cur: Option<(String, String, String)>, out: &mut BTreeMap<String, String>| {
if let Some((a, b, text)) = cur {
let deleted = text.lines().any(|l| l == "+++ /dev/null");
out.insert(if deleted { a } else { b }, text);
}
};
for line in patch.split_inclusive('\n') {
if let Some(rest) = line.trim_end_matches('\n').strip_prefix("diff --git a/") {
flush(current.take(), &mut out);
let (a, b) = rest.split_once(" b/").unwrap_or((rest, rest));
current = Some((a.to_string(), b.to_string(), String::new()));
}
if let Some((_, _, text)) = current.as_mut() {
text.push_str(line);
}
}
flush(current, &mut out);
out
}
pub fn log_args(run: u64, job: Option<u64>) -> Vec<String> {
let mut args = vec![String::from("run"), String::from("view"), run.to_string()];
if let Some(job) = job {
args.push(String::from("--job"));
args.push(job.to_string());
}
args.push(String::from("--log-failed"));
args
}
pub fn review_key(pr: &PrInfo) -> String {
let path = pr
.url
.split("github.com/")
.nth(1)
.and_then(|rest| rest.split("/pull/").next())
.unwrap_or("");
format!("{path}#{}", pr.number)
}
pub fn parse_pr_selector(input: &str) -> Option<String> {
let t = input.trim();
let number = |s: &str| s.parse::<u64>().ok().filter(|n| *n > 0);
match t.split_once("/pull/") {
Some((base, rest)) => {
let n = number(rest.split('/').next().unwrap_or(""))?;
Some(format!("{base}/pull/{n}"))
}
None => number(t.strip_prefix('#').unwrap_or(t)).map(|n| n.to_string()),
}
}
pub fn list_args() -> Vec<String> {
[
"pr",
"list",
"--limit",
"50",
"--json",
"number,title,author,url,isDraft",
]
.into_iter()
.map(String::from)
.collect()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrListItem {
pub number: u64,
pub title: String,
pub author: String,
pub url: String,
pub draft: bool,
}
pub fn parse_pr_list(json: &str) -> Result<Vec<PrListItem>, String> {
let v: serde_json::Value = serde_json::from_str(json).map_err(|e| e.to_string())?;
let items = v.as_array().ok_or("gh pr list did not return a list")?;
Ok(items
.iter()
.filter_map(|it| {
Some(PrListItem {
number: it.get("number")?.as_u64()?,
title: it
.get("title")
.and_then(|x| x.as_str())
.unwrap_or("")
.to_string(),
author: it
.pointer("/author/login")
.and_then(|x| x.as_str())
.unwrap_or("")
.to_string(),
url: it
.get("url")
.and_then(|x| x.as_str())
.unwrap_or("")
.to_string(),
draft: it.get("isDraft").and_then(|x| x.as_bool()).unwrap_or(false),
})
})
.collect())
}
pub const GH_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);
pub fn run_gh(
program: &str,
args: &[String],
cwd: &std::path::Path,
timeout: std::time::Duration,
) -> Result<String, String> {
use std::io::Read;
let mut cmd = std::process::Command::new(program);
cmd.args(args)
.current_dir(cwd)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped());
let mut child = crate::review_ops::spawn_retrying_busy(&mut cmd)
.map_err(|e| crate::gh::spawn_error(&e, Some(cwd)))?;
let drain = |pipe: Option<Box<dyn Read + Send>>| {
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let mut buf = Vec::new();
if let Some(mut p) = pipe {
let _ = p.read_to_end(&mut buf);
}
let _ = tx.send(buf);
});
rx
};
let stdout = drain(
child
.stdout
.take()
.map(|p| Box::new(p) as Box<dyn Read + Send>),
);
let stderr = drain(
child
.stderr
.take()
.map(|p| Box::new(p) as Box<dyn Read + Send>),
);
let deadline = std::time::Instant::now() + timeout;
let status = loop {
match child.try_wait() {
Ok(Some(status)) => break status,
Ok(None) if std::time::Instant::now() >= deadline => {
let _ = child.kill();
let _ = child.wait();
return Err(format!("{program} took longer than {}s", timeout.as_secs()));
}
Ok(None) => std::thread::sleep(std::time::Duration::from_millis(20)),
Err(e) => return Err(format!("could not wait on {program}: {e}")),
}
};
let text = |rx: std::sync::mpsc::Receiver<Vec<u8>>| {
String::from_utf8_lossy(&rx.recv().unwrap_or_default()).into_owned()
};
if status.success() {
Ok(text(stdout))
} else if crate::gh::signed_out(status) {
Err(String::from(crate::gh::SIGNED_OUT))
} else {
Err(text(stderr).trim().to_string())
}
}
static STARTUP_PR: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);
pub fn set_startup_pr(selector: String) {
if let Ok(mut slot) = STARTUP_PR.lock() {
*slot = Some(selector);
}
}
pub fn take_startup_pr() -> Option<String> {
STARTUP_PR.lock().ok().and_then(|mut slot| slot.take())
}
pub fn parse_pr(json: &str) -> Result<PrInfo, String> {
let v: serde_json::Value = serde_json::from_str(json).map_err(|e| e.to_string())?;
let str_at = |key: &str| {
v.get(key)
.and_then(|x| x.as_str())
.unwrap_or("")
.to_string()
};
let number = v
.get("number")
.and_then(|n| n.as_u64())
.ok_or_else(|| String::from("no pull request number in gh's answer"))?;
let files = v
.get("files")
.and_then(|f| f.as_array())
.map(|files| {
files
.iter()
.map(|f| PrFile {
path: f
.get("path")
.and_then(|p| p.as_str())
.unwrap_or("")
.to_string(),
additions: f.get("additions").and_then(|n| n.as_u64()).unwrap_or(0),
deletions: f.get("deletions").and_then(|n| n.as_u64()).unwrap_or(0),
change: f
.get("changeType")
.and_then(|c| c.as_str())
.unwrap_or("")
.to_string(),
})
.collect()
})
.unwrap_or_default();
let checks = v
.get("statusCheckRollup")
.and_then(|c| c.as_array())
.map(|checks| checks.iter().map(check_from).collect())
.unwrap_or_default();
Ok(PrInfo {
number,
title: str_at("title"),
author: v
.pointer("/author/login")
.and_then(|a| a.as_str())
.unwrap_or("")
.to_string(),
url: str_at("url"),
head_ref: str_at("headRefName"),
head_oid: str_at("headRefOid"),
base_ref: str_at("baseRefName"),
files,
checks,
})
}
fn check_from(c: &serde_json::Value) -> Check {
let s = |k: &str| c.get(k).and_then(|x| x.as_str()).unwrap_or("");
let name =
if s("__typename") == "StatusContext" || c.get("context").is_some_and(|x| x.is_string()) {
s("context")
} else {
s("name")
}
.to_string();
let verdict = if s("__typename") == "StatusContext" {
s("state")
} else if s("status") == "COMPLETED" {
s("conclusion")
} else {
"PENDING"
};
let state = match verdict {
"SUCCESS" => CheckState::Pass,
"FAILURE" | "ERROR" | "TIMED_OUT" | "ACTION_REQUIRED" | "STARTUP_FAILURE" => {
CheckState::Fail
}
"SKIPPED" | "NEUTRAL" | "CANCELLED" | "STALE" => CheckState::Neutral,
_ => CheckState::Pending,
};
let url = [s("detailsUrl"), s("targetUrl")]
.into_iter()
.find(|u| !u.is_empty())
.map(str::to_string);
Check { name, state, url }
}
pub fn run_and_job(url: &str) -> Option<(u64, Option<u64>)> {
let after = url.split("/actions/runs/").nth(1)?;
let mut parts = after.split('/');
let run = parts.next()?.parse().ok()?;
let job = match (parts.next(), parts.next()) {
(Some("job"), Some(j)) => j.parse().ok(),
_ => None,
};
Some((run, job))
}
pub fn summary(pr: &PrInfo, viewed: &BTreeSet<String>) -> String {
let seen = pr.files.iter().filter(|f| viewed.contains(&f.path)).count();
let mut parts = vec![format!("{seen} of {} viewed", pr.files.len())];
let count = |st: CheckState| pr.checks.iter().filter(|c| c.state == st).count();
for (st, word) in [
(CheckState::Pass, "passed"),
(CheckState::Fail, "failed"),
(CheckState::Pending, "pending"),
] {
let n = count(st);
if n > 0 {
parts.push(format!("{n} {word}"));
}
}
parts.join(" · ")
}
#[derive(Debug, Default, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ViewedStore {
#[serde(default)]
pub prs: BTreeMap<String, BTreeSet<String>>,
}
impl ViewedStore {
pub fn load(path: &Path) -> ViewedStore {
std::fs::read_to_string(path)
.ok()
.and_then(|t| serde_json::from_str(&t).ok())
.unwrap_or_default()
}
pub fn save(&self, path: &Path) -> std::io::Result<()> {
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir)?;
}
let text = serde_json::to_string_pretty(self).map_err(std::io::Error::other)?;
std::fs::write(path, text)
}
pub fn toggle(&mut self, pr: &str, file: &str) -> bool {
let set = self.prs.entry(pr.to_string()).or_default();
if set.remove(file) {
if set.is_empty() {
self.prs.remove(pr);
}
false
} else {
set.insert(file.to_string());
true
}
}
pub fn viewed(&self, pr: &str) -> BTreeSet<String> {
self.prs.get(pr).cloned().unwrap_or_default()
}
pub fn set(&mut self, pr: &str, file: &str, viewed: bool) {
if viewed {
self.prs
.entry(pr.to_string())
.or_default()
.insert(file.to_string());
} else if let Some(set) = self.prs.get_mut(pr) {
set.remove(file);
if set.is_empty() {
self.prs.remove(pr);
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ViewedState {
Viewed,
Unviewed,
Dismissed,
}
impl ViewedState {
pub fn is_viewed(self) -> bool {
self == ViewedState::Viewed
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ViewedPage {
pub pr_id: String,
pub states: BTreeMap<String, ViewedState>,
pub next: Option<String>,
}
const VIEWED_QUERY: &str = "query($owner: String!, $name: String!, $number: Int!, $after: String) { repository(owner: $owner, name: $name) { pullRequest(number: $number) { id files(first: 100, after: $after) { nodes { path viewerViewedState } pageInfo { hasNextPage endCursor } } } } }";
pub fn viewed_query_args(slug: &str, number: u64, after: Option<&str>) -> Option<Vec<String>> {
let (owner, name) = slug.split_once('/')?;
if owner.is_empty() || name.is_empty() || name.contains('/') {
return None;
}
let mut args: Vec<String> = [
"api",
"graphql",
"-f",
&format!("query={VIEWED_QUERY}"),
"-f",
&format!("owner={owner}"),
"-f",
&format!("name={name}"),
"-F",
&format!("number={number}"),
]
.into_iter()
.map(String::from)
.collect();
if let Some(cursor) = after {
args.push(String::from("-f"));
args.push(format!("after={cursor}"));
}
Some(args)
}
pub fn parse_viewed_page(json: &str) -> Result<ViewedPage, String> {
let v: serde_json::Value = serde_json::from_str(json).map_err(|e| e.to_string())?;
if let Some(msg) = v
.get("errors")
.and_then(|e| e.as_array())
.and_then(|e| e.first())
{
let text = msg.get("message").and_then(|m| m.as_str()).unwrap_or("");
return Err(format!("GitHub refused the query: {text}"));
}
let pr = v
.pointer("/data/repository/pullRequest")
.filter(|p| !p.is_null())
.ok_or("GitHub did not return the pull request")?;
let pr_id = pr
.get("id")
.and_then(|i| i.as_str())
.filter(|i| !i.is_empty())
.ok_or("GitHub did not return the pull request's id")?
.to_string();
let states = pr
.pointer("/files/nodes")
.and_then(|n| n.as_array())
.map(|nodes| {
nodes
.iter()
.filter_map(|n| {
let path = n.get("path")?.as_str()?.to_string();
let state = match n.get("viewerViewedState")?.as_str()? {
"VIEWED" => ViewedState::Viewed,
"DISMISSED" => ViewedState::Dismissed,
_ => ViewedState::Unviewed,
};
Some((path, state))
})
.collect()
})
.unwrap_or_default();
let next = pr
.pointer("/files/pageInfo")
.filter(|p| p.get("hasNextPage").and_then(|h| h.as_bool()) == Some(true))
.and_then(|p| p.get("endCursor"))
.and_then(|c| c.as_str())
.map(str::to_string);
Ok(ViewedPage {
pr_id,
states,
next,
})
}
const VIEWED_MAX_PAGES: usize = 30;
pub fn fetch_viewed(
program: &str,
slug: &str,
number: u64,
cwd: &Path,
timeout: std::time::Duration,
) -> Result<(String, BTreeMap<String, ViewedState>), String> {
let mut states = BTreeMap::new();
let mut after: Option<String> = None;
for _ in 0..VIEWED_MAX_PAGES {
let args = viewed_query_args(slug, number, after.as_deref())
.ok_or_else(|| format!("{slug:?} is not a GitHub repository"))?;
let page = parse_viewed_page(&run_gh(program, &args, cwd, timeout)?)?;
states.extend(page.states);
if page.next.is_none() {
return Ok((page.pr_id, states));
}
after = page.next;
}
Err(String::from("too many pages of changed files"))
}
pub fn mark_viewed_args(pr_id: &str, path: &str, viewed: bool) -> Vec<String> {
let mutation = if viewed {
"markFileAsViewed"
} else {
"unmarkFileAsViewed"
};
vec![
String::from("api"),
String::from("graphql"),
String::from("-f"),
format!(
"query=mutation($id: ID!, $path: String!) {{ {mutation}(input: {{pullRequestId: $id, path: $path}}) {{ clientMutationId }} }}"
),
String::from("-f"),
format!("id={pr_id}"),
String::from("-f"),
format!("path={path}"),
]
}
#[cfg(test)]
mod tests {
use super::*;
const PR: &str = r#"{
"number": 579, "title": "feat(sarif): model", "url": "https://github.com/o/r/pull/579",
"author": {"login": "vitali87"},
"headRefName": "feat/x", "headRefOid": "9f100db0000000000000000000000000000000aa", "baseRefName": "main",
"files": [
{"path": "src/sarif/view.rs", "additions": 1048, "deletions": 0, "changeType": "ADDED"},
{"path": "Cargo.toml", "additions": 1, "deletions": 1, "changeType": "MODIFIED"}
],
"statusCheckRollup": [
{"__typename": "CheckRun", "name": "clippy + tests", "status": "COMPLETED", "conclusion": "SUCCESS",
"detailsUrl": "https://github.com/o/r/actions/runs/36038030899/job/107762908591"},
{"__typename": "CheckRun", "name": "docs", "status": "COMPLETED", "conclusion": "FAILURE",
"detailsUrl": "https://github.com/o/r/actions/runs/36038262943/job/107763684465"},
{"__typename": "CheckRun", "name": "build", "status": "IN_PROGRESS", "conclusion": ""},
{"__typename": "CheckRun", "name": "optional", "status": "COMPLETED", "conclusion": "SKIPPED"},
{"__typename": "StatusContext", "context": "CodeRabbit", "state": "SUCCESS", "targetUrl": "https://coderabbit.ai"}
]
}"#;
#[test]
fn parses_the_pr_its_files_and_both_kinds_of_check() {
let pr = parse_pr(PR).unwrap();
assert_eq!(pr.number, 579);
assert_eq!(pr.author, "vitali87");
assert_eq!(
(pr.head_ref.as_str(), pr.base_ref.as_str()),
("feat/x", "main")
);
assert_eq!(pr.files.len(), 2);
assert_eq!(
pr.files[0],
PrFile {
path: "src/sarif/view.rs".into(),
additions: 1048,
deletions: 0,
change: "ADDED".into()
}
);
let states: Vec<(&str, CheckState)> = pr
.checks
.iter()
.map(|c| (c.name.as_str(), c.state))
.collect();
assert_eq!(
states,
vec![
("clippy + tests", CheckState::Pass),
("docs", CheckState::Fail),
("build", CheckState::Pending),
("optional", CheckState::Neutral),
("CodeRabbit", CheckState::Pass),
]
);
assert_eq!(pr.checks[4].url.as_deref(), Some("https://coderabbit.ai"));
}
#[test]
fn the_fetch_asks_gh_for_every_field_the_parser_reads() {
let args = view_args("579");
assert_eq!(&args[..3], ["pr", "view", "579"]);
assert_eq!(args[3], "--json");
for field in ["files", "statusCheckRollup", "headRefOid", "author", "url"] {
assert!(args[4].split(',').any(|f| f == field), "{field} requested");
}
}
#[test]
fn a_whole_pr_patch_splits_per_file() {
let patch = "diff --git a/src/a.rs b/src/a.rs\nindex 1..2 100644\n--- a/src/a.rs\n+++ b/src/a.rs\n@@ -1 +1 @@\n-x\n+y\ndiff --git a/old.txt b/old.txt\ndeleted file mode 100644\n--- a/old.txt\n+++ /dev/null\n@@ -1 +0,0 @@\n-gone\n";
let files = split_diff_by_file(patch);
assert_eq!(
files.keys().collect::<Vec<_>>(),
vec!["old.txt", "src/a.rs"]
);
assert!(files["src/a.rs"].starts_with("diff --git a/src/a.rs"));
assert!(files["src/a.rs"].contains("+y"));
assert!(!files["src/a.rs"].contains("gone"), "sections do not bleed");
assert!(files["old.txt"].contains("-gone"));
}
#[test]
fn failing_check_logs_come_from_gh_run_view() {
assert_eq!(
log_args(7, Some(8)),
vec!["run", "view", "7", "--job", "8", "--log-failed"]
);
assert_eq!(log_args(7, None), vec!["run", "view", "7", "--log-failed"]);
}
#[test]
fn pr_selectors_parse_as_typed_or_pasted() {
let sel = |s: &str| parse_pr_selector(s);
assert_eq!(sel("579").as_deref(), Some("579"));
assert_eq!(sel(" #579 ").as_deref(), Some("579"));
assert_eq!(
sel("https://github.com/x/y/pull/579").as_deref(),
Some("https://github.com/x/y/pull/579")
);
assert_eq!(
sel("https://github.com/o/r/pull/579/files").as_deref(),
Some("https://github.com/o/r/pull/579")
);
assert_eq!(sel("https://github.com/o/r/pull/x"), None);
assert_eq!(sel("abc"), None);
assert_eq!(sel(""), None);
assert_eq!(sel("#0"), None, "no PR zero");
}
#[test]
fn open_prs_parse_for_the_picker() {
let items = parse_pr_list(
r#"[{"number": 7, "title": "Fix it", "author": {"login": "ada"}, "url": "https://github.com/o/r/pull/7", "isDraft": true},
{"title": "no number"}]"#,
)
.unwrap();
assert_eq!(
items,
vec![PrListItem {
number: 7,
title: "Fix it".into(),
author: "ada".into(),
url: "https://github.com/o/r/pull/7".into(),
draft: true,
}]
);
assert!(parse_pr_list("{}").is_err());
assert!(list_args().contains(&String::from("number,title,author,url,isDraft")));
}
#[test]
fn the_startup_pr_is_taken_once() {
set_startup_pr(String::from("579"));
assert_eq!(take_startup_pr().as_deref(), Some("579"));
assert_eq!(take_startup_pr(), None);
}
#[test]
fn gh_runs_bounded_and_reports_stdout_or_stderr() {
let dir = std::env::temp_dir();
let sh = |script: &str| vec![String::from("-c"), String::from(script)];
let t = std::time::Duration::from_secs(5);
assert_eq!(
run_gh("/bin/sh", &sh("echo out"), &dir, t).as_deref(),
Ok("out\n")
);
assert_eq!(
run_gh("/bin/sh", &sh("echo why >&2; exit 1"), &dir, t),
Err(String::from("why"))
);
let started = std::time::Instant::now();
let slow = run_gh(
"/bin/sh",
&sh("sleep 5"),
&dir,
std::time::Duration::from_millis(200),
);
assert!(slow.unwrap_err().contains("took longer"));
assert!(
started.elapsed() < std::time::Duration::from_secs(3),
"killed at the bound"
);
assert_eq!(
run_gh("/nonexistent/gh", &[], &dir, t),
Err(String::from(crate::gh::MISSING)),
"#917: a missing gh says how to get it"
);
assert_eq!(
run_gh("/bin/sh", &sh("exit 4"), &dir, t),
Err(String::from(crate::gh::SIGNED_OUT))
);
}
#[test]
fn the_viewed_key_names_the_repository_and_the_pr() {
let pr = parse_pr(PR).unwrap();
assert_eq!(review_key(&pr), "o/r#579");
}
#[test]
fn malformed_json_is_an_error_not_a_panic() {
assert!(parse_pr("not json").is_err());
assert!(parse_pr("{}").is_err(), "a PR needs at least a number");
}
#[test]
fn run_and_job_ids_come_from_the_details_url() {
assert_eq!(
run_and_job("https://github.com/o/r/actions/runs/36038262943/job/107763684465"),
Some((36038262943, Some(107763684465)))
);
assert_eq!(
run_and_job("https://github.com/o/r/actions/runs/5"),
Some((5, None))
);
assert_eq!(run_and_job("https://coderabbit.ai"), None);
}
#[test]
fn summary_counts_viewed_files_and_check_states() {
let pr = parse_pr(PR).unwrap();
let mut viewed = BTreeSet::new();
viewed.insert("Cargo.toml".to_string());
assert_eq!(
summary(&pr, &viewed),
"1 of 2 viewed · 2 passed · 1 failed · 1 pending"
);
}
#[test]
fn viewed_marks_persist_per_pull_request() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("viewed.json");
let mut store = ViewedStore::load(&path);
assert!(store.toggle("o/r#579", "a.rs"), "first toggle marks viewed");
store.save(&path).unwrap();
let back = ViewedStore::load(&path);
assert!(back.viewed("o/r#579").contains("a.rs"));
assert!(back.viewed("o/r#580").is_empty(), "other PRs are separate");
let mut back = back;
assert!(!back.toggle("o/r#579", "a.rs"), "second toggle clears it");
assert!(!back.viewed("o/r#579").contains("a.rs"));
}
#[test]
fn setting_a_mark_outright_adds_or_clears_it() {
let mut store = ViewedStore::default();
store.set("o/r#1", "a.rs", true);
store.set("o/r#1", "a.rs", true);
assert!(store.viewed("o/r#1").contains("a.rs"));
store.set("o/r#1", "a.rs", false);
assert_eq!(store, ViewedStore::default(), "an emptied PR is dropped");
store.set("o/r#1", "b.rs", false);
assert_eq!(store, ViewedStore::default());
}
#[test]
fn the_viewed_query_names_the_repository_the_pr_and_the_page() {
let args = viewed_query_args("o/r", 579, None).unwrap();
assert_eq!(&args[..3], ["api", "graphql", "-f"]);
assert!(args[3].starts_with("query=query("), "{}", args[3]);
assert!(args[3].contains("viewerViewedState") && args[3].contains("endCursor"));
assert!(!args[3].contains('\n'), "one line per call in a log");
assert_eq!(
&args[4..],
["-f", "owner=o", "-f", "name=r", "-F", "number=579"]
);
let next = viewed_query_args("o/r", 579, Some("Y3Vy")).unwrap();
assert_eq!(&next[next.len() - 2..], ["-f", "after=Y3Vy"]);
assert_eq!(viewed_query_args("", 1, None), None, "not on GitHub");
assert_eq!(viewed_query_args("o/", 1, None), None);
assert_eq!(viewed_query_args("a/b/c", 1, None), None);
}
#[test]
fn a_viewed_page_parses_to_states_and_the_next_cursor() {
let page = parse_viewed_page(
r#"{"data":{"repository":{"pullRequest":{"id":"PR_1","files":{
"nodes":[{"path":"a.rs","viewerViewedState":"VIEWED"},
{"path":"b.rs","viewerViewedState":"DISMISSED"},
{"path":"c.rs","viewerViewedState":"UNVIEWED"}],
"pageInfo":{"hasNextPage":true,"endCursor":"Y3Vy"}}}}}}"#,
)
.unwrap();
assert_eq!(page.pr_id, "PR_1");
assert_eq!(page.states["a.rs"], ViewedState::Viewed);
assert_eq!(page.states["b.rs"], ViewedState::Dismissed);
assert_eq!(page.states["c.rs"], ViewedState::Unviewed);
assert!(!ViewedState::Dismissed.is_viewed(), "changed since viewed");
assert_eq!(page.next.as_deref(), Some("Y3Vy"));
let last = parse_viewed_page(
r#"{"data":{"repository":{"pullRequest":{"id":"PR_1","files":{"nodes":[],
"pageInfo":{"hasNextPage":false,"endCursor":"Zm9v"}}}}}}"#,
)
.unwrap();
assert_eq!(last.next, None, "no next page past the last");
}
#[test]
fn a_refused_or_empty_viewed_answer_is_an_error() {
let refused = parse_viewed_page(
r#"{"data":{"repository":null},"errors":[{"message":"Could not resolve to a Repository"}]}"#,
);
assert!(refused.unwrap_err().contains("Could not resolve"));
assert!(parse_viewed_page(r#"{"data":{"repository":{"pullRequest":null}}}"#).is_err());
assert!(parse_viewed_page("{}").is_err());
assert!(parse_viewed_page("").is_err());
}
#[test]
fn marking_viewed_sends_the_matching_mutation() {
let mark = mark_viewed_args("PR_1", "src/a b.rs", true);
assert_eq!(&mark[..3], ["api", "graphql", "-f"]);
assert!(mark[3].contains("markFileAsViewed(input: {pullRequestId: $id, path: $path})"));
assert!(!mark[3].contains("unmark"));
assert_eq!(&mark[4..], ["-f", "id=PR_1", "-f", "path=src/a b.rs"]);
let unmark = mark_viewed_args("PR_1", "a.rs", false);
assert!(unmark[3].contains("unmarkFileAsViewed("), "{}", unmark[3]);
}
#[test]
fn a_corrupt_store_loads_empty() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("viewed.json");
std::fs::write(&path, "{ not json").unwrap();
assert_eq!(ViewedStore::load(&path), ViewedStore::default());
}
}