| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use crate::common::{ |
| 9 | Http, Resp, Server, bin, input_value, launch, login, pick_ports, tool_on_path, write_config, |
| 10 | }; |
| 11 | use std::fs; |
| 12 | use std::path::{Path, PathBuf}; |
| 13 | use std::process::{Command, Output}; |
| 14 | use std::time::{Duration, Instant}; |
| 15 | |
| 16 | use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, COOKIE}; |
| 17 | |
| 18 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 19 | const BOB_PW: &str = "bob-password-4567"; |
| 20 | const BOB_EMAIL: &str = "bob@users.noreply.127.0.0.1"; |
| 21 | const REPO_PATH: &str = "/tahinli/r"; |
| 22 | |
| 23 | |
| 24 | fn state_badge(body: &str) -> String { |
| 25 | let doc = scraper::Html::parse_document(body); |
| 26 | let selector = |
| 27 | scraper::Selector::parse(".head-sub > .badge, .head-sub > .issue-badge").expect("selector"); |
| 28 | doc.select(&selector) |
| 29 | .next() |
| 30 | .map(|el| el.text().collect::<String>().trim().to_owned()) |
| 31 | .unwrap_or_default() |
| 32 | } |
| 33 | |
| 34 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { |
| 35 | let out = Command::new(bin()) |
| 36 | .arg("--config") |
| 37 | .arg(config) |
| 38 | .args(["admin", "create-user", username]) |
| 39 | .args(extra) |
| 40 | .env("FORGE_PASSWORD", password) |
| 41 | .output() |
| 42 | .expect("run admin create-user"); |
| 43 | assert!( |
| 44 | out.status.success(), |
| 45 | "create-user {username} failed:\n{}", |
| 46 | String::from_utf8_lossy(&out.stderr) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | fn git_env(cwd: &Path, args: &[&str], env: &[(&str, &str)]) -> String { |
| 53 | let mut cmd = Command::new("git"); |
| 54 | cmd.current_dir(cwd) |
| 55 | .args(["-c", "credential.helper="]) |
| 56 | .args(args) |
| 57 | .env("GIT_TERMINAL_PROMPT", "0"); |
| 58 | for (key, value) in env { |
| 59 | cmd.env(key, value); |
| 60 | } |
| 61 | let out: Output = cmd.output().expect("run git"); |
| 62 | assert!( |
| 63 | out.status.success(), |
| 64 | "git {args:?} failed:\n{}", |
| 65 | String::from_utf8_lossy(&out.stderr) |
| 66 | ); |
| 67 | String::from_utf8_lossy(&out.stdout).trim().to_owned() |
| 68 | } |
| 69 | |
| 70 | fn git(cwd: &Path, args: &[&str]) -> String { |
| 71 | git_env(cwd, args, &[]) |
| 72 | } |
| 73 | |
| 74 | |
| 75 | fn commit( |
| 76 | repo: &Path, |
| 77 | file: &str, |
| 78 | contents: &str, |
| 79 | message: &str, |
| 80 | who: &str, |
| 81 | email: &str, |
| 82 | ) -> String { |
| 83 | fs::write(repo.join(file), contents).expect("write file"); |
| 84 | git(repo, &["add", file]); |
| 85 | git_env( |
| 86 | repo, |
| 87 | &["-c", "commit.gpgsign=false", "commit", "-q", "-m", message], |
| 88 | &[ |
| 89 | ("GIT_AUTHOR_NAME", who), |
| 90 | ("GIT_AUTHOR_EMAIL", email), |
| 91 | ("GIT_COMMITTER_NAME", who), |
| 92 | ("GIT_COMMITTER_EMAIL", email), |
| 93 | ], |
| 94 | ); |
| 95 | git(repo, &["rev-parse", "HEAD"]) |
| 96 | } |
| 97 | |
| 98 | |
| 99 | fn clone(url: &str, into: &Path) -> PathBuf { |
| 100 | let parent = into.parent().expect("clone target has a parent"); |
| 101 | git( |
| 102 | parent, |
| 103 | &["clone", "-q", url, into.to_str().expect("utf-8 path")], |
| 104 | ); |
| 105 | into.to_path_buf() |
| 106 | } |
| 107 | |
| 108 | |
| 109 | fn remote(port: u16, user: &str, token: &str, owner: &str, repo: &str) -> String { |
| 110 | format!("http://{user}:{token}@127.0.0.1:{port}/{owner}/{repo}.git") |
| 111 | } |
| 112 | |
| 113 | |
| 114 | fn remote_sha(dir: &Path, url: &str, branch: &str) -> String { |
| 115 | let out = git(dir, &["ls-remote", url, &format!("refs/heads/{branch}")]); |
| 116 | out.split_whitespace() |
| 117 | .next() |
| 118 | .unwrap_or_else(|| panic!("branch {branch} missing on remote")) |
| 119 | .to_owned() |
| 120 | } |
| 121 | |
| 122 | |
| 123 | fn parent_count(dir: &Path, commit: &str) -> usize { |
| 124 | let line = git(dir, &["rev-list", "--parents", "-n1", commit]); |
| 125 | line.split_whitespace().count() - 1 |
| 126 | } |
| 127 | |
| 128 | |
| 129 | async fn csrf_token(http: &Http, session: &str) -> String { |
| 130 | let page = http.get_cookie("/settings/keys", session).await; |
| 131 | assert_eq!(page.status, 200, "GET /settings/keys"); |
| 132 | input_value(&page.body, "csrf") |
| 133 | } |
| 134 | |
| 135 | |
| 136 | async fn create_token(http: &Http, session: &str, name: &str) -> String { |
| 137 | let csrf = csrf_token(http, session).await; |
| 138 | let res = http |
| 139 | .post_form( |
| 140 | "/settings/tokens", |
| 141 | &[("csrf", &csrf), ("name", name), ("expires", "30")], |
| 142 | Some(session), |
| 143 | ) |
| 144 | .await; |
| 145 | assert_eq!(res.status, 200, "create token: {}", res.body); |
| 146 | let doc = scraper::Html::parse_document(&res.body); |
| 147 | let selector = scraper::Selector::parse("pre.code code").expect("selector"); |
| 148 | let token: String = doc |
| 149 | .select(&selector) |
| 150 | .next() |
| 151 | .expect("token shown after creation") |
| 152 | .text() |
| 153 | .collect(); |
| 154 | let token = token.trim().to_owned(); |
| 155 | assert!(token.starts_with("forge_"), "unexpected token shape"); |
| 156 | token |
| 157 | } |
| 158 | |
| 159 | |
| 160 | async fn create_repo(http: &Http, session: &str, name: &str) { |
| 161 | let page = http.get_cookie("/new", session).await; |
| 162 | assert_eq!(page.status, 200, "GET /new"); |
| 163 | let csrf = input_value(&page.body, "csrf"); |
| 164 | let res = http |
| 165 | .post_form( |
| 166 | "/new", |
| 167 | &[ |
| 168 | ("csrf", &csrf), |
| 169 | ("name", name), |
| 170 | ("description", ""), |
| 171 | ("visibility", "public"), |
| 172 | ("default_branch", "main"), |
| 173 | ], |
| 174 | Some(session), |
| 175 | ) |
| 176 | .await; |
| 177 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 178 | assert_eq!( |
| 179 | res.location().as_deref(), |
| 180 | Some(format!("/tahinli/{name}").as_str()) |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | struct Booted { |
| 187 | _server: Server, |
| 188 | _root: tempfile::TempDir, |
| 189 | dir: PathBuf, |
| 190 | http: Http, |
| 191 | http_port: u16, |
| 192 | } |
| 193 | |
| 194 | |
| 195 | fn server_log(b: &Booted) -> String { |
| 196 | fs::read_to_string(b.dir.join("serve.log")).unwrap_or_default() |
| 197 | } |
| 198 | |
| 199 | |
| 200 | async fn boot() -> Booted { |
| 201 | if !tool_on_path("git") { |
| 202 | panic!("`git` was not found on PATH"); |
| 203 | } |
| 204 | |
| 205 | let root = tempfile::Builder::new() |
| 206 | .prefix("pr2e") |
| 207 | .tempdir_in("/tmp") |
| 208 | .expect("temp dir"); |
| 209 | let dir = root.path().to_path_buf(); |
| 210 | let ports = pick_ports(); |
| 211 | let config = write_config(&dir, ports); |
| 212 | |
| 213 | run_admin(&config, "tahinli", &["--admin"], TAHINLI_PW); |
| 214 | run_admin(&config, "bob", &[], BOB_PW); |
| 215 | let (server, ports) = launch(&dir, ports).await; |
| 216 | Booted { |
| 217 | _server: server, |
| 218 | _root: root, |
| 219 | dir, |
| 220 | http: Http::new(ports.http), |
| 221 | http_port: ports.http, |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | |
| 226 | async fn tahinli_repo_with_main(b: &Booted) -> (String, String, PathBuf) { |
| 227 | let tahinli = login(&b.http, "tahinli", TAHINLI_PW).await; |
| 228 | let token = create_token(&b.http, &tahinli, "pulls-token").await; |
| 229 | create_repo(&b.http, &tahinli, "r").await; |
| 230 | let work = clone( |
| 231 | &remote(b.http_port, "tahinli", &token, "tahinli", "r"), |
| 232 | &b.dir.join("tahinli-work"), |
| 233 | ); |
| 234 | commit( |
| 235 | &work, |
| 236 | "base.txt", |
| 237 | "base\n", |
| 238 | "base commit", |
| 239 | "tahinli", |
| 240 | "tahinli@example.test", |
| 241 | ); |
| 242 | git(&work, &["push", "-q", "origin", "HEAD:refs/heads/main"]); |
| 243 | (tahinli, token, work) |
| 244 | } |
| 245 | |
| 246 | |
| 247 | async fn open_pull(http: &Http, session: &str, base: &str, head: &str, title: &str) -> u32 { |
| 248 | let compare = http |
| 249 | .get_cookie(&format!("{REPO_PATH}/compare/{base}...{head}"), session) |
| 250 | .await; |
| 251 | assert_eq!(compare.status, 200, "GET compare {base}...{head}"); |
| 252 | let csrf = csrf_token(http, session).await; |
| 253 | let res = http |
| 254 | .post_form( |
| 255 | &format!("{REPO_PATH}/pulls"), |
| 256 | &[ |
| 257 | ("csrf", &csrf), |
| 258 | ("title", title), |
| 259 | ("body", ""), |
| 260 | ("base", base), |
| 261 | ("head", head), |
| 262 | ], |
| 263 | Some(session), |
| 264 | ) |
| 265 | .await; |
| 266 | assert_eq!(res.status, 303, "open pull {head}: {}", res.body); |
| 267 | let location = res.location().expect("redirect to the pull"); |
| 268 | location |
| 269 | .rsplit('/') |
| 270 | .next() |
| 271 | .and_then(|n| n.parse().ok()) |
| 272 | .unwrap_or_else(|| panic!("no pull number in {location}")) |
| 273 | } |
| 274 | |
| 275 | |
| 276 | async fn open_issue(http: &Http, session: &str, title: &str) -> u32 { |
| 277 | let page = http |
| 278 | .get_cookie(&format!("{REPO_PATH}/issues/new"), session) |
| 279 | .await; |
| 280 | assert_eq!(page.status, 200, "GET issues/new"); |
| 281 | let csrf = input_value(&page.body, "csrf"); |
| 282 | let res = http |
| 283 | .post_form( |
| 284 | &format!("{REPO_PATH}/issues/new"), |
| 285 | &[("csrf", &csrf), ("title", title), ("body", "details")], |
| 286 | Some(session), |
| 287 | ) |
| 288 | .await; |
| 289 | assert_eq!(res.status, 303, "create issue: {}", res.body); |
| 290 | let location = res.location().expect("redirect to the issue"); |
| 291 | location |
| 292 | .rsplit('/') |
| 293 | .next() |
| 294 | .and_then(|n| n.parse().ok()) |
| 295 | .unwrap_or_else(|| panic!("no issue number in {location}")) |
| 296 | } |
| 297 | |
| 298 | |
| 299 | async fn wait_page( |
| 300 | http: &Http, |
| 301 | session: &str, |
| 302 | path: &str, |
| 303 | what: &str, |
| 304 | done: impl Fn(&str) -> bool, |
| 305 | ) -> String { |
| 306 | let deadline = Instant::now() + Duration::from_secs(10); |
| 307 | loop { |
| 308 | let res = http.get_cookie(path, session).await; |
| 309 | assert_eq!(res.status, 200, "GET {path}"); |
| 310 | if done(&res.body) { |
| 311 | return res.body; |
| 312 | } |
| 313 | assert!( |
| 314 | Instant::now() < deadline, |
| 315 | "timed out after 10s waiting for {what} on {path}" |
| 316 | ); |
| 317 | tokio::time::sleep(Duration::from_millis(200)).await; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | |
| 322 | async fn merge(http: &Http, session: &str, number: u32, style: &str) -> Resp { |
| 323 | let csrf = csrf_token(http, session).await; |
| 324 | http.post_form( |
| 325 | &format!("{REPO_PATH}/pulls/{number}/merge"), |
| 326 | &[("csrf", &csrf), ("style", style), ("message", "")], |
| 327 | Some(session), |
| 328 | ) |
| 329 | .await |
| 330 | } |
| 331 | |
| 332 | #[tokio::test] |
| 333 | async fn same_repo_pull_merges_and_closes_issue() { |
| 334 | let b = boot().await; |
| 335 | let (tahinli, token, work) = tahinli_repo_with_main(&b).await; |
| 336 | |
| 337 | let issue = open_issue(&b.http, &tahinli, "add x").await; |
| 338 | assert_eq!(issue, 1); |
| 339 | |
| 340 | git(&work, &["checkout", "-q", "-b", "feature"]); |
| 341 | let feature_head = commit( |
| 342 | &work, |
| 343 | "x.txt", |
| 344 | "x\n", |
| 345 | "Add x\n\nFixes #1", |
| 346 | "tahinli", |
| 347 | "tahinli@example.test", |
| 348 | ); |
| 349 | git(&work, &["push", "-q", "origin", "HEAD:refs/heads/feature"]); |
| 350 | |
| 351 | let compare = b |
| 352 | .http |
| 353 | .get_cookie(&format!("{REPO_PATH}/compare/main...feature"), &tahinli) |
| 354 | .await; |
| 355 | assert_eq!(compare.status, 200, "GET compare"); |
| 356 | assert!(compare.body.contains("Add x"), "compare lists the commit"); |
| 357 | |
| 358 | let number = open_pull(&b.http, &tahinli, "main", "feature", "Add x").await; |
| 359 | assert_eq!(number, 2, "the pull shares the issue number sequence"); |
| 360 | |
| 361 | let files = b |
| 362 | .http |
| 363 | .get_cookie(&format!("{REPO_PATH}/pulls/2/files"), &tahinli) |
| 364 | .await; |
| 365 | assert_eq!(files.status, 200, "GET pull files"); |
| 366 | assert!(files.body.contains("x.txt"), "files tab shows x.txt"); |
| 367 | |
| 368 | let res = merge(&b.http, &tahinli, 2, "merge").await; |
| 369 | assert_eq!(res.status, 303, "merge: {}", res.body); |
| 370 | assert_eq!(res.location().as_deref(), Some("/tahinli/r/pulls/2")); |
| 371 | |
| 372 | let url = remote(b.http_port, "tahinli", &token, "tahinli", "r"); |
| 373 | let main_sha = remote_sha(&b.dir, &url, "main"); |
| 374 | assert_ne!(main_sha, feature_head, "main is a new merge commit"); |
| 375 | let check = clone(&url, &b.dir.join("check")); |
| 376 | assert_eq!( |
| 377 | parent_count(&check, "HEAD"), |
| 378 | 2, |
| 379 | "main tip is a merge commit with two parents" |
| 380 | ); |
| 381 | assert!( |
| 382 | git(&check, &["log", "--format=%s", "-n", "5"]).contains("Add x"), |
| 383 | "feature commit reachable from main" |
| 384 | ); |
| 385 | |
| 386 | let pr = b |
| 387 | .http |
| 388 | .get_cookie(&format!("{REPO_PATH}/pulls/2"), &tahinli) |
| 389 | .await; |
| 390 | assert_eq!(state_badge(&pr.body), "Merged"); |
| 391 | |
| 392 | wait_page( |
| 393 | &b.http, |
| 394 | &tahinli, |
| 395 | &format!("{REPO_PATH}/issues/1"), |
| 396 | "issue closed", |
| 397 | |body| state_badge(body) == "Closed", |
| 398 | ) |
| 399 | .await; |
| 400 | } |
| 401 | |
| 402 | #[tokio::test] |
| 403 | async fn push_to_head_branch_updates_open_pull() { |
| 404 | let b = boot().await; |
| 405 | let (tahinli, _token, work) = tahinli_repo_with_main(&b).await; |
| 406 | |
| 407 | git(&work, &["checkout", "-q", "-b", "feature2"]); |
| 408 | commit( |
| 409 | &work, |
| 410 | "one.txt", |
| 411 | "1\n", |
| 412 | "first on feature2", |
| 413 | "tahinli", |
| 414 | "tahinli@example.test", |
| 415 | ); |
| 416 | git(&work, &["push", "-q", "origin", "HEAD:refs/heads/feature2"]); |
| 417 | let number = open_pull(&b.http, &tahinli, "main", "feature2", "Feature two").await; |
| 418 | |
| 419 | let second = commit( |
| 420 | &work, |
| 421 | "two.txt", |
| 422 | "2\n", |
| 423 | "second on feature2", |
| 424 | "tahinli", |
| 425 | "tahinli@example.test", |
| 426 | ); |
| 427 | git(&work, &["push", "-q", "origin", "HEAD:refs/heads/feature2"]); |
| 428 | |
| 429 | let path = format!("{REPO_PATH}/pulls/{number}"); |
| 430 | let page = wait_page(&b.http, &tahinli, &path, "head_updated event", |body| { |
| 431 | body.contains("pushed new commits") |
| 432 | }) |
| 433 | .await; |
| 434 | assert!(page.contains("pushed new commits")); |
| 435 | |
| 436 | let commits = b |
| 437 | .http |
| 438 | .get_cookie(&format!("{path}/commits"), &tahinli) |
| 439 | .await; |
| 440 | assert_eq!(commits.status, 200, "GET pull commits"); |
| 441 | assert!( |
| 442 | commits.body.contains("second on feature2"), |
| 443 | "new commit listed" |
| 444 | ); |
| 445 | assert!( |
| 446 | commits.body.contains("first on feature2"), |
| 447 | "old commit listed" |
| 448 | ); |
| 449 | assert!(commits.body.contains(&second[..7]), "new commit sha shown"); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | async fn open_stream(http: &Http, path: &str, session: Option<&str>) -> reqwest::Response { |
| 454 | let mut req = http.client.get(format!("{}{path}", http.base)); |
| 455 | if let Some(session) = session { |
| 456 | req = req.header(COOKIE, session); |
| 457 | } |
| 458 | let mut res = req.send().await.expect("stream request"); |
| 459 | |
| 460 | if res.status().is_success() { |
| 461 | let retry = tokio::time::timeout(Duration::from_secs(5), res.chunk()) |
| 462 | .await |
| 463 | .expect("the retry field") |
| 464 | .expect("stream chunk") |
| 465 | .expect("stream still open"); |
| 466 | assert!(String::from_utf8_lossy(&retry).contains("retry: 1000")); |
| 467 | } |
| 468 | res |
| 469 | } |
| 470 | |
| 471 | |
| 472 | async fn next_changed(res: &mut reqwest::Response, what: &str) { |
| 473 | let chunk = tokio::time::timeout(Duration::from_secs(5), res.chunk()) |
| 474 | .await |
| 475 | .unwrap_or_else(|_| panic!("no event after {what}")) |
| 476 | .expect("stream chunk") |
| 477 | .expect("stream still open"); |
| 478 | let text = String::from_utf8_lossy(&chunk); |
| 479 | assert!(text.contains("event: changed"), "{what}: {text}"); |
| 480 | } |
| 481 | |
| 482 | |
| 483 | async fn assert_quiet(res: &mut reqwest::Response, what: &str) { |
| 484 | let got = tokio::time::timeout(Duration::from_secs(2), res.chunk()).await; |
| 485 | assert!(got.is_err(), "{what}: the stream sent an event"); |
| 486 | } |
| 487 | |
| 488 | #[tokio::test] |
| 489 | async fn pull_pages_follow_writes_live() { |
| 490 | let b = boot().await; |
| 491 | let (tahinli, token, work) = tahinli_repo_with_main(&b).await; |
| 492 | |
| 493 | git(&work, &["checkout", "-q", "-b", "live"]); |
| 494 | let first = commit( |
| 495 | &work, |
| 496 | "live.txt", |
| 497 | "live\n", |
| 498 | "first on live", |
| 499 | "tahinli", |
| 500 | "tahinli@example.test", |
| 501 | ); |
| 502 | git(&work, &["push", "-q", "origin", "HEAD:refs/heads/live"]); |
| 503 | let number = open_pull(&b.http, &tahinli, "main", "live", "Live pull").await; |
| 504 | let pull = format!("{REPO_PATH}/pulls/{number}"); |
| 505 | let list = format!("{REPO_PATH}/pulls"); |
| 506 | |
| 507 | |
| 508 | let page = b.http.get_cookie(&pull, &tahinli).await; |
| 509 | assert_eq!(page.status, 200, "GET pull"); |
| 510 | assert!( |
| 511 | page.body.contains(&format!("data-live=\"{pull}/events\"")), |
| 512 | "pull page follows its stream" |
| 513 | ); |
| 514 | let listing = b.http.get_cookie(&list, &tahinli).await; |
| 515 | assert!( |
| 516 | listing |
| 517 | .body |
| 518 | .contains(&format!("data-live=\"{list}/events\"")), |
| 519 | "pull list follows its stream" |
| 520 | ); |
| 521 | |
| 522 | let mut pull_events = open_stream(&b.http, &format!("{pull}/events"), Some(&tahinli)).await; |
| 523 | assert_eq!(pull_events.status().as_u16(), 200, "pull stream"); |
| 524 | let mut list_events = open_stream(&b.http, &format!("{list}/events"), Some(&tahinli)).await; |
| 525 | assert_eq!(list_events.status().as_u16(), 200, "list stream"); |
| 526 | assert_quiet(&mut pull_events, "connecting to the pull stream").await; |
| 527 | |
| 528 | |
| 529 | let csrf = csrf_token(&b.http, &tahinli).await; |
| 530 | let res = b |
| 531 | .http |
| 532 | .post_form( |
| 533 | &format!("{pull}/comments"), |
| 534 | &[ |
| 535 | ("csrf", &csrf), |
| 536 | ("body", "a comment"), |
| 537 | ("action", "comment"), |
| 538 | ], |
| 539 | Some(&tahinli), |
| 540 | ) |
| 541 | .await; |
| 542 | assert_eq!(res.status, 303, "comment: {}", res.body); |
| 543 | next_changed(&mut pull_events, "a comment").await; |
| 544 | next_changed(&mut list_events, "a comment").await; |
| 545 | |
| 546 | |
| 547 | let csrf = csrf_token(&b.http, &tahinli).await; |
| 548 | let res = b |
| 549 | .http |
| 550 | .post_form( |
| 551 | &format!("{pull}/reviews"), |
| 552 | &[ |
| 553 | ("csrf", &csrf), |
| 554 | ("state", "comment"), |
| 555 | ("body", "a review"), |
| 556 | ("head_oid", &first), |
| 557 | ], |
| 558 | Some(&tahinli), |
| 559 | ) |
| 560 | .await; |
| 561 | assert_eq!(res.status, 303, "review: {}", res.body); |
| 562 | next_changed(&mut pull_events, "a review").await; |
| 563 | |
| 564 | |
| 565 | let second = commit( |
| 566 | &work, |
| 567 | "second.txt", |
| 568 | "2\n", |
| 569 | "second on live", |
| 570 | "tahinli", |
| 571 | "tahinli@example.test", |
| 572 | ); |
| 573 | git(&work, &["push", "-q", "origin", "HEAD:refs/heads/live"]); |
| 574 | next_changed(&mut pull_events, "a push to the head").await; |
| 575 | next_changed(&mut list_events, "a push to the head").await; |
| 576 | |
| 577 | |
| 578 | let status = b |
| 579 | .http |
| 580 | .client |
| 581 | .post(format!( |
| 582 | "{}/api/v1/repos/tahinli/r/statuses/{second}", |
| 583 | b.http.base |
| 584 | )) |
| 585 | .header(AUTHORIZATION, format!("Bearer {token}")) |
| 586 | .header(CONTENT_TYPE, "application/json") |
| 587 | .body(r#"{"state":"success","context":"ci/test"}"#) |
| 588 | .send() |
| 589 | .await |
| 590 | .expect("status request"); |
| 591 | assert_eq!(status.status().as_u16(), 201, "status posted"); |
| 592 | next_changed(&mut pull_events, "a commit status on the head").await; |
| 593 | |
| 594 | |
| 595 | let res = merge(&b.http, &tahinli, number, "merge").await; |
| 596 | assert_eq!(res.status, 303, "merge: {}", res.body); |
| 597 | next_changed(&mut pull_events, "the merge").await; |
| 598 | next_changed(&mut list_events, "the merge").await; |
| 599 | |
| 600 | |
| 601 | let csrf = csrf_token(&b.http, &tahinli).await; |
| 602 | let res = b |
| 603 | .http |
| 604 | .post_form( |
| 605 | "/new", |
| 606 | &[ |
| 607 | ("csrf", &csrf), |
| 608 | ("name", "priv"), |
| 609 | ("description", ""), |
| 610 | ("visibility", "private"), |
| 611 | ("default_branch", "main"), |
| 612 | ], |
| 613 | Some(&tahinli), |
| 614 | ) |
| 615 | .await; |
| 616 | assert_eq!(res.status, 303, "create private repo: {}", res.body); |
| 617 | let anonymous = open_stream(&b.http, "/tahinli/priv/pulls/events", None).await; |
| 618 | assert_eq!(anonymous.status().as_u16(), 404, "private list, anonymous"); |
| 619 | let anonymous = open_stream(&b.http, "/tahinli/priv/pulls/1/events", None).await; |
| 620 | assert_eq!(anonymous.status().as_u16(), 404, "private pull, anonymous"); |
| 621 | let member = open_stream(&b.http, "/tahinli/priv/pulls/events", Some(&tahinli)).await; |
| 622 | assert_eq!(member.status().as_u16(), 200, "private list, owner"); |
| 623 | } |
| 624 | |
| 625 | #[tokio::test] |
| 626 | async fn fork_pull_squash_merge_credits_contributor() { |
| 627 | let b = boot().await; |
| 628 | let (tahinli, tahinli_token, _work) = tahinli_repo_with_main(&b).await; |
| 629 | |
| 630 | let bob = login(&b.http, "bob", BOB_PW).await; |
| 631 | let bob_token = create_token(&b.http, &bob, "bob-token").await; |
| 632 | |
| 633 | |
| 634 | let csrf = csrf_token(&b.http, &bob).await; |
| 635 | let fork = b |
| 636 | .http |
| 637 | .post_form(&format!("{REPO_PATH}/fork"), &[("csrf", &csrf)], Some(&bob)) |
| 638 | .await; |
| 639 | assert_eq!(fork.status, 303, "fork: {}\n{}", fork.body, server_log(&b)); |
| 640 | assert_eq!(fork.location().as_deref(), Some("/bob/r")); |
| 641 | |
| 642 | let bob_url = remote(b.http_port, "bob", &bob_token, "bob", "r"); |
| 643 | let bob_work = clone(&bob_url, &b.dir.join("bob-work")); |
| 644 | git(&bob_work, &["checkout", "-q", "-b", "bob-feature"]); |
| 645 | let bob_head = commit( |
| 646 | &bob_work, |
| 647 | "bob.txt", |
| 648 | "bob\n", |
| 649 | "Bob change", |
| 650 | "bob", |
| 651 | BOB_EMAIL, |
| 652 | ); |
| 653 | git( |
| 654 | &bob_work, |
| 655 | &["push", "-q", "origin", "HEAD:refs/heads/bob-feature"], |
| 656 | ); |
| 657 | |
| 658 | let number = open_pull(&b.http, &bob, "main", "bob:bob-feature", "Bob change").await; |
| 659 | |
| 660 | |
| 661 | let denied = merge(&b.http, &bob, number, "merge").await; |
| 662 | assert_eq!(denied.status, 403, "bob must not merge"); |
| 663 | |
| 664 | let res = merge(&b.http, &tahinli, number, "squash").await; |
| 665 | assert_eq!(res.status, 303, "tahinli squash merge: {}", res.body); |
| 666 | |
| 667 | let tahinli_url = remote(b.http_port, "tahinli", &tahinli_token, "tahinli", "r"); |
| 668 | let check = clone(&tahinli_url, &b.dir.join("check")); |
| 669 | assert_eq!(parent_count(&check, "HEAD"), 1, "squash makes one parent"); |
| 670 | assert_ne!( |
| 671 | git(&check, &["rev-parse", "HEAD"]), |
| 672 | bob_head, |
| 673 | "main has a new squash commit, not bob's commit" |
| 674 | ); |
| 675 | assert_eq!(git(&check, &["log", "-1", "--format=%an"]), "bob"); |
| 676 | assert_eq!(git(&check, &["log", "-1", "--format=%ae"]), BOB_EMAIL); |
| 677 | } |
| 678 | |
| 679 | #[tokio::test] |
| 680 | async fn conflicting_pull_cannot_merge() { |
| 681 | let b = boot().await; |
| 682 | let (tahinli, token, work) = tahinli_repo_with_main(&b).await; |
| 683 | |
| 684 | |
| 685 | git(&work, &["checkout", "-q", "-b", "conflict"]); |
| 686 | commit( |
| 687 | &work, |
| 688 | "shared.txt", |
| 689 | "branch side\n", |
| 690 | "branch edit", |
| 691 | "tahinli", |
| 692 | "tahinli@example.test", |
| 693 | ); |
| 694 | git(&work, &["push", "-q", "origin", "HEAD:refs/heads/conflict"]); |
| 695 | let number = open_pull(&b.http, &tahinli, "main", "conflict", "Conflicting").await; |
| 696 | |
| 697 | git(&work, &["checkout", "-q", "main"]); |
| 698 | let main_head = commit( |
| 699 | &work, |
| 700 | "shared.txt", |
| 701 | "main side\n", |
| 702 | "main edit", |
| 703 | "tahinli", |
| 704 | "tahinli@example.test", |
| 705 | ); |
| 706 | git(&work, &["push", "-q", "origin", "HEAD:refs/heads/main"]); |
| 707 | |
| 708 | let path = format!("{REPO_PATH}/pulls/{number}"); |
| 709 | let page = wait_page(&b.http, &tahinli, &path, "conflict detection", |body| { |
| 710 | body.contains("This branch has conflicts that must be resolved") |
| 711 | }) |
| 712 | .await; |
| 713 | assert!(page.contains("shared.txt"), "conflict lists the file"); |
| 714 | assert!( |
| 715 | !page.contains("merge-form"), |
| 716 | "no merge form on a conflicting pull" |
| 717 | ); |
| 718 | |
| 719 | let res = merge(&b.http, &tahinli, number, "merge").await; |
| 720 | assert_eq!( |
| 721 | res.status, |
| 722 | 303, |
| 723 | "conflicting merge: {}\n{}", |
| 724 | res.body, |
| 725 | server_log(&b) |
| 726 | ); |
| 727 | assert_eq!( |
| 728 | res.location().as_deref(), |
| 729 | Some(format!("{REPO_PATH}/pulls/{number}?merge_error=conflict").as_str()), |
| 730 | "refusal redirects back to the pull with the conflict code" |
| 731 | ); |
| 732 | |
| 733 | let url = remote(b.http_port, "tahinli", &token, "tahinli", "r"); |
| 734 | assert_eq!( |
| 735 | remote_sha(&b.dir, &url, "main"), |
| 736 | main_head, |
| 737 | "main is unchanged by the refused merge" |
| 738 | ); |
| 739 | } |