forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.09.3 MiB2e29cbcTahinli8 hours ago333 commits

pulls_e2e.rs470 lines, 16.1 KiBHistoryRawDownload
1use crate::common::{
2 BOB_CMD, Http, PW, Resp, Server, TAHINLI_CMD, assert_quiet, boot_forge, clone, commit,
3 create_repo, create_token, csrf_for, git_text, login, next_changed, temp_dir, text_or_empty,
4 tool_on_path,
5};
6use std::fs;
7use std::path::{Path, PathBuf};
8use std::time::{Duration, Instant};
9
10use reqwest::header::{AUTHORIZATION, CONTENT_TYPE};
11
12const BOB_EMAIL: &str = "bob@users.noreply.127.0.0.1";
13const REPO_PATH: &str = "/tahinli/r";
14const TAHINLI: (&str, &str) = ("tahinli", "tahinli@example.test");
15const BOB: (&str, &str) = ("bob", BOB_EMAIL);
16
17fn remote(port: u16, user: &str, token: &str, owner: &str, repo: &str) -> String {
18 format!("http://{user}:{token}@127.0.0.1:{port}/{owner}/{repo}.git")
19}
20
21fn remote_sha(dir: &Path, url: &str, branch: &str) -> String {
22 let out = git_text(dir, &["ls-remote", url, &format!("refs/heads/{branch}")]);
23 out.split_whitespace()
24 .next()
25 .unwrap_or_else(|| panic!("branch {branch} missing on remote"))
26 .to_owned()
27}
28
29fn parent_count(dir: &Path, commit: &str) -> usize {
30 let line = git_text(dir, &["rev-list", "--parents", "-n1", commit]);
31 line.split_whitespace().count() - 1
32}
33
34struct Booted {
35 _server: Server,
36 _root: tempfile::TempDir,
37 dir: PathBuf,
38 http: Http,
39 http_port: u16,
40}
41
42fn server_log(b: &Booted) -> String {
43 fs::read_to_string(b.dir.join("serve.log")).unwrap_or_default()
44}
45
46async fn boot() -> Booted {
47 if !tool_on_path("git") {
48 panic!("`git` was not found on PATH");
49 }
50 let root = temp_dir("pr2e");
51 let dir = root.path().to_path_buf();
52 let (server, ports) = boot_forge(&dir, "", &[TAHINLI_CMD, BOB_CMD]).await;
53 Booted {
54 _server: server,
55 _root: root,
56 dir,
57 http: Http::new(ports.http),
58 http_port: ports.http,
59 }
60}
61
62async fn tahinli_repo_with_main(b: &Booted) -> (String, String, PathBuf) {
63 let tahinli = login(&b.http, "tahinli", PW).await;
64 let token = create_token(&b.http, &tahinli, "pulls-token").await;
65 create_repo(&b.http, &tahinli, "r", "public").await;
66 let work = clone(
67 &remote(b.http_port, "tahinli", &token, "tahinli", "r"),
68 &b.dir.join("tahinli-work"),
69 );
70 commit(&work, "base.txt", "base\n", "base commit", TAHINLI);
71 git_text(&work, &["push", "-q", "origin", "HEAD:refs/heads/main"]);
72 (tahinli, token, work)
73}
74
75async fn open_pull(http: &Http, session: &str, base: &str, head: &str, title: &str) -> u32 {
76 let compare = http
77 .get_cookie(&format!("{REPO_PATH}/compare/{base}...{head}"), session)
78 .await;
79 assert_eq!(compare.status, 200, "GET compare {base}...{head}");
80 let csrf = csrf_for(http, "/settings/keys", session).await;
81 let res = http
82 .post_form(
83 &format!("{REPO_PATH}/pulls"),
84 &[
85 ("csrf", &csrf),
86 ("title", title),
87 ("body", ""),
88 ("base", base),
89 ("head", head),
90 ],
91 Some(session),
92 )
93 .await;
94 assert_eq!(res.status, 303, "open pull {head}: {}", res.body);
95 let location = res.location().expect("redirect to the pull");
96 location
97 .rsplit('/')
98 .next()
99 .and_then(|n| n.parse().ok())
100 .unwrap_or_else(|| panic!("no pull number in {location}"))
101}
102
103async fn open_issue(http: &Http, session: &str, title: &str) -> u32 {
104 let csrf = csrf_for(http, &format!("{REPO_PATH}/issues/new"), session).await;
105 let res = http
106 .post_form(
107 &format!("{REPO_PATH}/issues/new"),
108 &[("csrf", &csrf), ("title", title), ("body", "details")],
109 Some(session),
110 )
111 .await;
112 assert_eq!(res.status, 303, "create issue: {}", res.body);
113 let location = res.location().expect("redirect to the issue");
114 location
115 .rsplit('/')
116 .next()
117 .and_then(|n| n.parse().ok())
118 .unwrap_or_else(|| panic!("no issue number in {location}"))
119}
120
121async fn wait_page(
122 http: &Http,
123 session: &str,
124 path: &str,
125 what: &str,
126 done: impl Fn(&str) -> bool,
127) -> String {
128 let deadline = Instant::now() + Duration::from_secs(10);
129 loop {
130 let res = http.get_cookie(path, session).await;
131 assert_eq!(res.status, 200, "GET {path}");
132 if done(&res.body) {
133 return res.body;
134 }
135 assert!(
136 Instant::now() < deadline,
137 "timed out after 10s waiting for {what} on {path}"
138 );
139 tokio::time::sleep(Duration::from_millis(200)).await;
140 }
141}
142
143async fn merge(http: &Http, session: &str, number: u32, style: &str) -> Resp {
144 let csrf = csrf_for(http, "/settings/keys", session).await;
145 http.post_form(
146 &format!("{REPO_PATH}/pulls/{number}/merge"),
147 &[("csrf", &csrf), ("style", style), ("message", "")],
148 Some(session),
149 )
150 .await
151}
152
153#[tokio::test]
154async fn same_repo_pull_merges_and_closes_issue() {
155 let b = boot().await;
156 let (tahinli, token, work) = tahinli_repo_with_main(&b).await;
157
158 let issue = open_issue(&b.http, &tahinli, "add x").await;
159 assert_eq!(issue, 1);
160
161 git_text(&work, &["checkout", "-q", "-b", "feature"]);
162 let feature_head = commit(&work, "x.txt", "x\n", "Add x\n\nFixes #1", TAHINLI);
163 git_text(&work, &["push", "-q", "origin", "HEAD:refs/heads/feature"]);
164
165 let compare = b
166 .http
167 .get_cookie(&format!("{REPO_PATH}/compare/main...feature"), &tahinli)
168 .await;
169 assert_eq!(compare.status, 200, "GET compare");
170 assert!(compare.body.contains("Add x"), "compare lists the commit");
171
172 let number = open_pull(&b.http, &tahinli, "main", "feature", "Add x").await;
173 assert_eq!(number, 2, "the pull shares the issue number sequence");
174
175 let files = b
176 .http
177 .get_cookie(&format!("{REPO_PATH}/pulls/2/files"), &tahinli)
178 .await;
179 assert_eq!(files.status, 200, "GET pull files");
180 assert!(files.body.contains("x.txt"), "files tab shows x.txt");
181
182 let res = merge(&b.http, &tahinli, 2, "merge").await;
183 assert_eq!(res.status, 303, "merge: {}", res.body);
184 assert_eq!(res.location().as_deref(), Some("/tahinli/r/pulls/2"));
185
186 let url = remote(b.http_port, "tahinli", &token, "tahinli", "r");
187 let main_sha = remote_sha(&b.dir, &url, "main");
188 assert_ne!(main_sha, feature_head, "main is a new merge commit");
189 let check = clone(&url, &b.dir.join("check"));
190 assert_eq!(
191 parent_count(&check, "HEAD"),
192 2,
193 "main tip is a merge commit with two parents"
194 );
195 assert!(
196 git_text(&check, &["log", "--format=%s", "-n", "5"]).contains("Add x"),
197 "feature commit reachable from main"
198 );
199
200 let pr = b
201 .http
202 .get_cookie(&format!("{REPO_PATH}/pulls/2"), &tahinli)
203 .await;
204 assert_eq!(
205 text_or_empty(&pr.body, ".head-sub > .badge, .head-sub > .issue-badge"),
206 "Merged"
207 );
208
209 wait_page(
210 &b.http,
211 &tahinli,
212 &format!("{REPO_PATH}/issues/1"),
213 "issue closed",
214 |body| text_or_empty(body, ".head-sub > .badge, .head-sub > .issue-badge") == "Closed",
215 )
216 .await;
217}
218
219#[tokio::test]
220async fn push_to_head_branch_updates_open_pull() {
221 let b = boot().await;
222 let (tahinli, _token, work) = tahinli_repo_with_main(&b).await;
223
224 git_text(&work, &["checkout", "-q", "-b", "feature2"]);
225 commit(&work, "one.txt", "1\n", "first on feature2", TAHINLI);
226 git_text(&work, &["push", "-q", "origin", "HEAD:refs/heads/feature2"]);
227 let number = open_pull(&b.http, &tahinli, "main", "feature2", "Feature two").await;
228
229 let second = commit(&work, "two.txt", "2\n", "second on feature2", TAHINLI);
230 git_text(&work, &["push", "-q", "origin", "HEAD:refs/heads/feature2"]);
231
232 let path = format!("{REPO_PATH}/pulls/{number}");
233 let page = wait_page(&b.http, &tahinli, &path, "head_updated event", |body| {
234 body.contains("pushed new commits")
235 })
236 .await;
237 assert!(page.contains("pushed new commits"));
238
239 let commits = b
240 .http
241 .get_cookie(&format!("{path}/commits"), &tahinli)
242 .await;
243 assert_eq!(commits.status, 200, "GET pull commits");
244 assert!(
245 commits.body.contains("second on feature2"),
246 "new commit listed"
247 );
248 assert!(
249 commits.body.contains("first on feature2"),
250 "old commit listed"
251 );
252 assert!(commits.body.contains(&second[..7]), "new commit sha shown");
253}
254
255#[tokio::test]
256async fn pull_pages_follow_writes_live() {
257 let b = boot().await;
258 let (tahinli, token, work) = tahinli_repo_with_main(&b).await;
259
260 git_text(&work, &["checkout", "-q", "-b", "live"]);
261 let first = commit(&work, "live.txt", "live\n", "first on live", TAHINLI);
262 git_text(&work, &["push", "-q", "origin", "HEAD:refs/heads/live"]);
263 let number = open_pull(&b.http, &tahinli, "main", "live", "Live pull").await;
264 let pull = format!("{REPO_PATH}/pulls/{number}");
265 let list = format!("{REPO_PATH}/pulls");
266
267 let page = b.http.get_cookie(&pull, &tahinli).await;
268 assert_eq!(page.status, 200, "GET pull");
269 assert!(
270 page.body.contains(&format!("data-live=\"{pull}/events\"")),
271 "pull page follows its stream"
272 );
273 let listing = b.http.get_cookie(&list, &tahinli).await;
274 assert!(
275 listing
276 .body
277 .contains(&format!("data-live=\"{list}/events\"")),
278 "pull list follows its stream"
279 );
280
281 let mut pull_events = b
282 .http
283 .stream(&format!("{pull}/events"), Some(&tahinli))
284 .await;
285 assert_eq!(pull_events.status().as_u16(), 200, "pull stream");
286 let mut list_events = b
287 .http
288 .stream(&format!("{list}/events"), Some(&tahinli))
289 .await;
290 assert_eq!(list_events.status().as_u16(), 200, "list stream");
291 assert_quiet(&mut pull_events, "connecting to the pull stream").await;
292
293 let csrf = csrf_for(&b.http, "/settings/keys", &tahinli).await;
294 let res = b
295 .http
296 .post_form(
297 &format!("{pull}/comments"),
298 &[
299 ("csrf", &csrf),
300 ("body", "a comment"),
301 ("action", "comment"),
302 ],
303 Some(&tahinli),
304 )
305 .await;
306 assert_eq!(res.status, 303, "comment: {}", res.body);
307 next_changed(&mut pull_events, "a comment").await;
308 next_changed(&mut list_events, "a comment").await;
309
310 let csrf = csrf_for(&b.http, "/settings/keys", &tahinli).await;
311 let res = b
312 .http
313 .post_form(
314 &format!("{pull}/reviews"),
315 &[
316 ("csrf", &csrf),
317 ("state", "comment"),
318 ("body", "a review"),
319 ("head_oid", &first),
320 ],
321 Some(&tahinli),
322 )
323 .await;
324 assert_eq!(res.status, 303, "review: {}", res.body);
325 next_changed(&mut pull_events, "a review").await;
326
327 let second = commit(&work, "second.txt", "2\n", "second on live", TAHINLI);
328 git_text(&work, &["push", "-q", "origin", "HEAD:refs/heads/live"]);
329 next_changed(&mut pull_events, "a push to the head").await;
330 next_changed(&mut list_events, "a push to the head").await;
331
332 let status = b
333 .http
334 .client
335 .post(format!(
336 "{}/api/v1/repos/tahinli/r/statuses/{second}",
337 b.http.base
338 ))
339 .header(AUTHORIZATION, format!("Bearer {token}"))
340 .header(CONTENT_TYPE, "application/json")
341 .body(r#"{"state":"success","context":"ci/test"}"#)
342 .send()
343 .await
344 .expect("status request");
345 assert_eq!(status.status().as_u16(), 201, "status posted");
346 next_changed(&mut pull_events, "a commit status on the head").await;
347
348 let res = merge(&b.http, &tahinli, number, "merge").await;
349 assert_eq!(res.status, 303, "merge: {}", res.body);
350 next_changed(&mut pull_events, "the merge").await;
351 next_changed(&mut list_events, "the merge").await;
352
353 let csrf = csrf_for(&b.http, "/settings/keys", &tahinli).await;
354 let res = b
355 .http
356 .post_form(
357 "/new",
358 &[
359 ("csrf", &csrf),
360 ("name", "priv"),
361 ("description", ""),
362 ("visibility", "private"),
363 ("default_branch", "main"),
364 ],
365 Some(&tahinli),
366 )
367 .await;
368 assert_eq!(res.status, 303, "create private repo: {}", res.body);
369 let anonymous = b.http.stream("/tahinli/priv/pulls/events", None).await;
370 assert_eq!(anonymous.status().as_u16(), 404, "private list, anonymous");
371 let anonymous = b.http.stream("/tahinli/priv/pulls/1/events", None).await;
372 assert_eq!(anonymous.status().as_u16(), 404, "private pull, anonymous");
373 let member = b
374 .http
375 .stream("/tahinli/priv/pulls/events", Some(&tahinli))
376 .await;
377 assert_eq!(member.status().as_u16(), 200, "private list, owner");
378}
379
380#[tokio::test]
381async fn fork_pull_squash_merge_credits_contributor() {
382 let b = boot().await;
383 let (tahinli, tahinli_token, _work) = tahinli_repo_with_main(&b).await;
384
385 let bob = login(&b.http, "bob", PW).await;
386 let bob_token = create_token(&b.http, &bob, "bob-token").await;
387
388 let csrf = csrf_for(&b.http, "/settings/keys", &bob).await;
389 let fork = b
390 .http
391 .post_form(&format!("{REPO_PATH}/fork"), &[("csrf", &csrf)], Some(&bob))
392 .await;
393 assert_eq!(fork.status, 303, "fork: {}\n{}", fork.body, server_log(&b));
394 assert_eq!(fork.location().as_deref(), Some("/bob/r"));
395
396 let bob_url = remote(b.http_port, "bob", &bob_token, "bob", "r");
397 let bob_work = clone(&bob_url, &b.dir.join("bob-work"));
398 git_text(&bob_work, &["checkout", "-q", "-b", "bob-feature"]);
399 let bob_head = commit(&bob_work, "bob.txt", "bob\n", "Bob change", BOB);
400 git_text(
401 &bob_work,
402 &["push", "-q", "origin", "HEAD:refs/heads/bob-feature"],
403 );
404
405 let number = open_pull(&b.http, &bob, "main", "bob:bob-feature", "Bob change").await;
406
407 let denied = merge(&b.http, &bob, number, "merge").await;
408 assert_eq!(denied.status, 403, "bob must not merge");
409
410 let res = merge(&b.http, &tahinli, number, "squash").await;
411 assert_eq!(res.status, 303, "tahinli squash merge: {}", res.body);
412
413 let tahinli_url = remote(b.http_port, "tahinli", &tahinli_token, "tahinli", "r");
414 let check = clone(&tahinli_url, &b.dir.join("check"));
415 assert_eq!(parent_count(&check, "HEAD"), 1, "squash makes one parent");
416 assert_ne!(
417 git_text(&check, &["rev-parse", "HEAD"]),
418 bob_head,
419 "main has a new squash commit, not bob's commit"
420 );
421 assert_eq!(git_text(&check, &["log", "-1", "--format=%an"]), "bob");
422 assert_eq!(git_text(&check, &["log", "-1", "--format=%ae"]), BOB_EMAIL);
423}
424
425#[tokio::test]
426async fn conflicting_pull_cannot_merge() {
427 let b = boot().await;
428 let (tahinli, token, work) = tahinli_repo_with_main(&b).await;
429
430 git_text(&work, &["checkout", "-q", "-b", "conflict"]);
431 commit(&work, "shared.txt", "branch side\n", "branch edit", TAHINLI);
432 git_text(&work, &["push", "-q", "origin", "HEAD:refs/heads/conflict"]);
433 let number = open_pull(&b.http, &tahinli, "main", "conflict", "Conflicting").await;
434
435 git_text(&work, &["checkout", "-q", "main"]);
436 let main_head = commit(&work, "shared.txt", "main side\n", "main edit", TAHINLI);
437 git_text(&work, &["push", "-q", "origin", "HEAD:refs/heads/main"]);
438
439 let path = format!("{REPO_PATH}/pulls/{number}");
440 let page = wait_page(&b.http, &tahinli, &path, "conflict detection", |body| {
441 body.contains("This branch has conflicts that must be resolved")
442 })
443 .await;
444 assert!(page.contains("shared.txt"), "conflict lists the file");
445 assert!(
446 !page.contains("merge-form"),
447 "no merge form on a conflicting pull"
448 );
449
450 let res = merge(&b.http, &tahinli, number, "merge").await;
451 assert_eq!(
452 res.status,
453 303,
454 "conflicting merge: {}\n{}",
455 res.body,
456 server_log(&b)
457 );
458 assert_eq!(
459 res.location().as_deref(),
460 Some(format!("{REPO_PATH}/pulls/{number}?merge_error=conflict").as_str()),
461 "refusal redirects back to the pull with the conflict code"
462 );
463
464 let url = remote(b.http_port, "tahinli", &token, "tahinli", "r");
465 assert_eq!(
466 remote_sha(&b.dir, &url, "main"),
467 main_head,
468 "main is unchanged by the refused merge"
469 );
470}