Skip to content
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
21 changes: 19 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ pub async fn run(cli: Cli, token: Option<SecretString>) -> anyhow::Result<Verdic
}
Command::Run(args) => {
let api = connect(Http::new(origin)?, token, cli.org).await?;
let name = repository_name(&args.repository).await?;
let name = pull_request_repository(&api, &args.repository).await?;
tools::json(
&api.tool_get(&[
"reviews",
Expand Down Expand Up @@ -482,7 +482,7 @@ pub async fn run(cli: Cli, token: Option<SecretString>) -> anyhow::Result<Verdic
}
Command::History(args) => {
let api = connect(Http::new(origin)?, token, cli.org).await?;
let name = repository_name(&args.repository).await?;
let name = pull_request_repository(&api, &args.repository).await?;
tools::json(&api.history(&name, args.pr).await?)
}
Command::Guidance(args) => {
Expand Down Expand Up @@ -727,6 +727,23 @@ async fn reviewed_repository(api: &Api, args: &RepositoryArgs) -> anyhow::Result
Ok((name, id))
}

/// The repository segment of a pull-request review address. The server names
/// the repository without its owner, so `owner/name` is first checked against
/// the repositories with reviews, and only `name` is sent. A bare name is sent
/// as given.
async fn pull_request_repository(api: &Api, args: &RepositoryArgs) -> anyhow::Result<String> {
if let Some(name) = &args.repo
&& !name.contains('/')
{
return Ok(name.clone());
}
let (name, _) = reviewed_repository(api, args).await?;
Ok(name
.rsplit_once('/')
.map_or(name.as_str(), |(_, repository)| repository)
.to_owned())
}

/// The newest review in `repository` that `matches`, searching with `search`.
async fn latest(
api: &Api,
Expand Down
75 changes: 75 additions & 0 deletions tests/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,15 @@ async fn native_tools_keep_tenant_paths_and_reruns_make_one_attempt() {
)
.into_response();
}
if path.ends_with("/reviews") {
return Json(json!({
"items": [],
"truncated": false,
"next_cursor": null,
"filters": {"repositories": [{"id": REPOSITORY, "name": "owner/repo"}]}
}))
.into_response();
}
if path.ends_with("repository-names") {
return Json(
json!({"repositories": [{"id": REPOSITORY, "full_name":"owner/repo"}]}),
Expand Down Expand Up @@ -1976,6 +1985,72 @@ async fn native_tools_keep_tenant_paths_and_reruns_make_one_attempt() {
server.abort();
}

#[tokio::test]
async fn pull_request_runs_name_the_repository_without_its_owner() {
let requests = Arc::new(Mutex::new(Vec::new()));
let seen = Arc::clone(&requests);
let app = Router::new().fallback(move |request: axum::extract::Request| {
let seen = Arc::clone(&seen);
async move {
let path = request.uri().path().to_owned();
seen.lock().unwrap().push(path.clone());
if path.ends_with("/reviews") {
return Json(json!({
"items": [],
"truncated": false,
"next_cursor": null,
"filters": {"repositories": [{"id": REPOSITORY, "name": "Acme/Web"}]}
}))
.into_response();
}
Json(json!({"items": []})).into_response()
}
});
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let origin = format!("http://{}", listener.local_addr().unwrap());
let server = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
let prefix = format!("/api/v1/organizations/{ORG}/reviews/pr");
for (args, expected) in [
(
vec!["history", "--repo", "acme/web", "--pr", "7"],
format!("{prefix}/web/7"),
),
(
vec!["run", "--repo", "acme/web", "--pr", "7", "--run", "2"],
format!("{prefix}/web/7/runs/2"),
),
// A bare name is the address segment itself and needs no lookup.
(
vec!["history", "--repo", "web", "--pr", "7"],
format!("{prefix}/web/7"),
),
] {
requests.lock().unwrap().clear();
let output = cli(&origin, &args).await;
assert!(
output.status.success(),
"{args:?}: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(requests.lock().unwrap().last(), Some(&expected), "{args:?}");
}
// Another owner's repository of the same name is refused, not read.
requests.lock().unwrap().clear();
let output = cli(&origin, &["history", "--repo", "other/web", "--pr", "7"]).await;
assert_eq!(output.status.code(), Some(1));
assert!(String::from_utf8_lossy(&output.stderr).contains("other/web has no reviews"));
assert!(
requests
.lock()
.unwrap()
.iter()
.all(|path| !path.starts_with(&prefix))
);
server.abort();
}

#[tokio::test]
async fn native_session_and_collection_keep_scope_cursor_and_single_start() {
let starts = Arc::new(AtomicUsize::new(0));
Expand Down