forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·9.3 MiB·ed35470·Tahinli·2 hours ago·362 commits
| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use forge_core::store::commit_status::StatusState; |
| 4 | use forge_core::{IssueState, Mergeable, PullRequest, Review, ReviewState}; |
| 5 | use forge_git::browse::CommitInfo; |
| 6 | use forge_git::history::CommitDiff; |
| 7 | use forge_git::pulls::{is_ancestor, range_diff_files, range_log}; |
| 8 | use topcoat::Result; |
| 9 | use topcoat::context::{Cx, context}; |
| 10 | use topcoat::router::content::Form; |
| 11 | use topcoat::router::error::see_other; |
| 12 | use topcoat::router::{page, path_param}; |
| 13 | use topcoat::view::{Child, View, component, view}; |
| 14 | |
| 15 | use crate::auth::require_user; |
| 16 | use crate::i18n::pulls_review::{self as g, K}; |
| 17 | use crate::i18n::{self, Lang}; |
| 18 | use crate::merge::{ |
| 19 | ERR_ALREADY_MERGED, ERR_AUTO_MERGE_BLOCKED, ERR_BASE_MISSING, ERR_BASE_MOVED, |
| 20 | ERR_CHANGES_REQUESTED, ERR_CONFLICT, ERR_DRAFT, ERR_FF_IMPOSSIBLE, ERR_HEAD_MISSING, |
| 21 | ERR_HEAD_MOVED, ERR_MESSAGE_INVALID, ERR_NEEDS_APPROVALS, ERR_NEEDS_CHECKS, ERR_NOT_OPEN, |
| 22 | ERR_NOT_UP_TO_DATE, ERR_QUEUE_ALREADY, ERR_QUEUE_REQUIRED, ERR_REBASE_CONFLICT, ERR_UP_TO_DATE, |
| 23 | merge_gate, |
| 24 | }; |
| 25 | use crate::pages::commits::CommitRow; |
| 26 | use crate::pages::issue_meta::item_href; |
| 27 | use crate::pages::issue_parts::{ |
| 28 | CommentForm, ItemKind, Posted, check_write, comment_form, csrf_of, discussion_timeline, |
| 29 | item_head_tools, item_sidebar, load_discussion, load_sidebar, lock_message, may_change, |
| 30 | may_converse, parse_number, post_comment, reachable_among, triage, |
| 31 | }; |
| 32 | use crate::pages::notifications::notify_box_for; |
| 33 | use crate::pages::pull_branch::{self, branch_box, branch_error_from}; |
| 34 | use crate::pages::pull_reviews::error_from; |
| 35 | use crate::pages::repo_ctx::{RepoCtx, load_pull, load_repo}; |
| 36 | use crate::pages::review_requests::{load_reviewers, reviewers_box}; |
| 37 | use crate::state::AppState; |
| 38 | use crate::views::When; |
| 39 | use crate::views::commit_status::{StatusLang, StatusSummary, checks_summary, commit_status}; |
| 40 | use crate::views::diff::LIMITS; |
| 41 | use crate::views::parts::{confirm_act, post_act, reveal}; |
| 42 | use crate::views::{alert, now_unix, query_value, repo_href, shell, short_oid, user_link}; |
| 43 | |
| 44 | path_param!(owner); |
| 45 | path_param!(repo); |
| 46 | path_param!(number); |
| 47 | |
| 48 | const MAX_COMMITS: usize = 250; |
| 49 | |
| 50 | pub(crate) fn is_merged(pr: &PullRequest) -> bool { |
| 51 | pr.merged_at.is_some() || pr.mergeable == Mergeable::Merged |
| 52 | } |
| 53 | |
| 54 | async fn load_repo_pull(cx: &Cx) -> Result<(RepoCtx, PullRequest)> { |
| 55 | let number = parse_number(number(cx))?; |
| 56 | let ctx = load_repo(cx, owner(cx), repo(cx)).await?; |
| 57 | let pr = load_pull(cx, &ctx, number).await?; |
| 58 | Ok((ctx, pr)) |
| 59 | } |
| 60 | |
| 61 | pub(crate) async fn commits_of( |
| 62 | state: &AppState, |
| 63 | ctx: &RepoCtx, |
| 64 | pr: &PullRequest, |
| 65 | ) -> Result<Vec<CommitInfo>> { |
| 66 | Ok(range_log( |
| 67 | &state.git, |
| 68 | &ctx.path, |
| 69 | &pr.base_oid, |
| 70 | &pr.head_oid, |
| 71 | MAX_COMMITS, |
| 72 | ) |
| 73 | .await?) |
| 74 | } |
| 75 | |
| 76 | pub(crate) async fn diff_of( |
| 77 | state: &AppState, |
| 78 | ctx: &RepoCtx, |
| 79 | pr: &PullRequest, |
| 80 | ) -> Result<CommitDiff> { |
| 81 | Ok(range_diff_files(&state.git, &ctx.path, &pr.base_oid, &pr.head_oid, LIMITS).await?) |
| 82 | } |
| 83 | |
| 84 | #[derive(Clone)] |
| 85 | pub(crate) struct Frame { |
| 86 | title: String, |
| 87 | number: i64, |
| 88 | pub(crate) page_title: String, |
| 89 | pub(crate) href: String, |
| 90 | author: String, |
| 91 | author_known: bool, |
| 92 | opened: When, |
| 93 | pub(crate) commit_count: usize, |
| 94 | pub(crate) file_count: usize, |
| 95 | base_branch: String, |
| 96 | head_label: String, |
| 97 | state_text: &'static str, |
| 98 | state_class: &'static str, |
| 99 | lang: Lang, |
| 100 | pub(crate) totals: Option<(u32, u32)>, |
| 101 | } |
| 102 | |
| 103 | pub(crate) async fn frame_in( |
| 104 | lang: Lang, |
| 105 | state: &AppState, |
| 106 | ctx: &RepoCtx, |
| 107 | pr: &PullRequest, |
| 108 | commit_count: usize, |
| 109 | file_count: usize, |
| 110 | ) -> Result<Frame> { |
| 111 | let owner = ctx.repo.owner_name.clone(); |
| 112 | let name = ctx.repo.name.clone(); |
| 113 | let number = pr.issue.number; |
| 114 | |
| 115 | let users = state |
| 116 | .store |
| 117 | .usernames_by_ids(std::slice::from_ref(&pr.issue.author_id)) |
| 118 | .await?; |
| 119 | let author_known = users.contains_key(&pr.issue.author_id); |
| 120 | let author = users |
| 121 | .get(&pr.issue.author_id) |
| 122 | .map(|(display, _)| display.clone()) |
| 123 | .unwrap_or_else(|| g::t(lang, K::Unknown).to_owned()); |
| 124 | |
| 125 | let head_label = match pr.head_repo_id.as_deref() { |
| 126 | None => g::branch_deleted(lang, &pr.head_branch), |
| 127 | Some(id) if id == ctx.repo.id => pr.head_branch.clone(), |
| 128 | Some(id) => match state.store.repo_by_id(id).await? { |
| 129 | Some(head) => format!("{}:{}", head.owner_name, pr.head_branch), |
| 130 | None => g::branch_deleted(lang, &pr.head_branch), |
| 131 | }, |
| 132 | }; |
| 133 | |
| 134 | let (state_text, state_class) = if is_merged(pr) { |
| 135 | ( |
| 136 | crate::i18n::t(lang, crate::i18n::Key::StateMerged), |
| 137 | "issue-badge issue-badge-merged", |
| 138 | ) |
| 139 | } else if matches!(pr.issue.state, IssueState::Open) && pr.draft { |
| 140 | ( |
| 141 | crate::i18n::t(lang, crate::i18n::Key::DraftBadge), |
| 142 | "issue-badge", |
| 143 | ) |
| 144 | } else if matches!(pr.issue.state, IssueState::Open) { |
| 145 | ( |
| 146 | crate::i18n::t(lang, crate::i18n::Key::StateOpen), |
| 147 | "issue-badge issue-badge-open", |
| 148 | ) |
| 149 | } else { |
| 150 | ( |
| 151 | crate::i18n::t(lang, crate::i18n::Key::StateClosed), |
| 152 | "issue-badge issue-badge-closed", |
| 153 | ) |
| 154 | }; |
| 155 | |
| 156 | Ok(Frame { |
| 157 | lang, |
| 158 | title: pr.issue.title.clone(), |
| 159 | number, |
| 160 | page_title: format!("{} #{number} \u{b7} {owner}/{name}", pr.issue.title), |
| 161 | href: repo_href(&owner, &name, "pulls", &number.to_string()), |
| 162 | author, |
| 163 | author_known, |
| 164 | opened: When::new(lang, &pr.issue.created_at, ctx.viewer_tz(), now_unix()), |
| 165 | commit_count, |
| 166 | file_count, |
| 167 | base_branch: pr.base_branch.clone(), |
| 168 | head_label, |
| 169 | state_text, |
| 170 | state_class, |
| 171 | totals: None, |
| 172 | }) |
| 173 | } |
| 174 | |
| 175 | #[component] |
| 176 | pub(crate) async fn pull_header( |
| 177 | cx: &Cx, |
| 178 | frame: Frame, |
| 179 | active: String, |
| 180 | #[default] child: Child<'_>, |
| 181 | ) -> Result<impl View> { |
| 182 | let Frame { |
| 183 | title, |
| 184 | number, |
| 185 | href, |
| 186 | state_text, |
| 187 | state_class, |
| 188 | author, |
| 189 | author_known, |
| 190 | opened, |
| 191 | commit_count, |
| 192 | file_count, |
| 193 | base_branch, |
| 194 | head_label, |
| 195 | lang, |
| 196 | totals, |
| 197 | .. |
| 198 | } = frame; |
| 199 | let number_label = format!("#{number}"); |
| 200 | let live_url = format!("{href}/events"); |
| 201 | let commits_href = format!("{href}/commits"); |
| 202 | let files_href = format!("{href}/files"); |
| 203 | let commits_text = g::commit_count_line(lang, commit_count); |
| 204 | let commits_label = g::commits_tab(lang, commit_count); |
| 205 | let files_label = g::files_tab(lang, file_count); |
| 206 | let conversation = g::t(lang, K::TabConversation); |
| 207 | let aria = g::t(lang, K::PullPages); |
| 208 | let wants = g::t(lang, K::ByWantsToMerge); |
| 209 | let into = g::t(lang, K::ByInto); |
| 210 | let from = g::t(lang, K::ByFrom); |
| 211 | let byline_end = g::t(lang, K::BylineEnd); |
| 212 | let turkish = matches!(lang, Lang::Tr); |
| 213 | let totals_text = totals.map(|(add, del)| { |
| 214 | format!( |
| 215 | "{} \u{b7} +{add} \u{2212}{del}", |
| 216 | g::files_tab(lang, file_count) |
| 217 | ) |
| 218 | }); |
| 219 | Ok(view! { |
| 220 | <section class="head" data-live=(live_url.as_str())> |
| 221 | <div class="head-title"> |
| 222 | <h1>(title.as_str()) " " <span class="num">(number_label.as_str())</span></h1> |
| 223 | (child) |
| 224 | </div> |
| 225 | <p class="head-sub"> |
| 226 | <span class=(state_class)>(state_text)</span> |
| 227 | " " |
| 228 | if author_known { |
| 229 | user_link(shown: author.as_str(), login: author.as_str()) |
| 230 | } else { |
| 231 | (author.as_str()) |
| 232 | } |
| 233 | " " |
| 234 | if turkish { |
| 235 | (wants) <code>(head_label.as_str())</code> |
| 236 | (from) <code>(base_branch.as_str())</code> |
| 237 | (into) (commits_text.as_str()) |
| 238 | } else { |
| 239 | (wants) <code>(head_label.as_str())</code> |
| 240 | (into) <code>(base_branch.as_str())</code> |
| 241 | } |
| 242 | (byline_end) |
| 243 | " " |
| 244 | <time data-rel="true" datetime=(opened.iso.as_str()) title=(opened.absolute.as_str())>(opened.relative.as_str())</time> |
| 245 | if let Some(text) = totals_text { |
| 246 | " · " |
| 247 | <span class="mono">(text.as_str())</span> |
| 248 | } |
| 249 | </p> |
| 250 | <nav class="tabs" aria-label=(aria)> |
| 251 | <a aria-current=(current_tab(&active, "conversation")) href=(href.as_str())>(conversation)</a> |
| 252 | <a aria-current=(current_tab(&active, "commits")) href=(commits_href.as_str())>(commits_label.as_str())</a> |
| 253 | <a aria-current=(current_tab(&active, "files")) href=(files_href.as_str())>(files_label.as_str())</a> |
| 254 | </nav> |
| 255 | </section> |
| 256 | }) |
| 257 | } |
| 258 | |
| 259 | fn current_tab(active: &str, tab: &str) -> Option<&'static str> { |
| 260 | (active == tab).then_some("page") |
| 261 | } |
| 262 | |
| 263 | struct MergeForm { |
| 264 | action: String, |
| 265 | csrf: String, |
| 266 | ff: bool, |
| 267 | head: String, |
| 268 | } |
| 269 | |
| 270 | struct CheckRow { |
| 271 | context: String, |
| 272 | label: &'static str, |
| 273 | class: &'static str, |
| 274 | } |
| 275 | |
| 276 | struct FlowView { |
| 277 | csrf: String, |
| 278 | draft_toggle: Option<(String, &'static str)>, |
| 279 | auto_on: Option<(String, String)>, |
| 280 | auto_enable: Option<String>, |
| 281 | queue_position: Option<(String, String)>, |
| 282 | queue_add: Option<String>, |
| 283 | } |
| 284 | |
| 285 | #[component] |
| 286 | async fn style_select(cx: &Cx, id: &str, lang: Lang) -> Result<impl View> { |
| 287 | let merge = g::t(lang, K::StyleMergeCommit).trim(); |
| 288 | let squash = g::t(lang, K::StyleSquash).trim(); |
| 289 | let rebase = g::t(lang, K::StyleRebase).trim(); |
| 290 | Ok(view! { |
| 291 | <select id=(id) name="style"> |
| 292 | <option value="merge">(merge)</option> |
| 293 | <option value="squash">(squash)</option> |
| 294 | <option value="rebase">(rebase)</option> |
| 295 | </select> |
| 296 | }) |
| 297 | } |
| 298 | |
| 299 | #[component] |
| 300 | async fn merge_box( |
| 301 | cx: &Cx, |
| 302 | headline: String, |
| 303 | tone: String, |
| 304 | conflicts: Vec<String>, |
| 305 | form: Option<MergeForm>, |
| 306 | merge_error: Option<String>, |
| 307 | approvals: Option<String>, |
| 308 | blocked: Option<String>, |
| 309 | checks: Option<StatusSummary>, |
| 310 | required: Vec<CheckRow>, |
| 311 | flow: FlowView, |
| 312 | lang: Lang, |
| 313 | ) -> Result<impl View> { |
| 314 | let class = format!("sec merge-{tone}"); |
| 315 | let required_heading = g::t(lang, K::RequiredChecksHeading); |
| 316 | let style_rebase = g::t(lang, K::StyleRebase); |
| 317 | let enable_auto = g::t(lang, K::EnableAutoMerge); |
| 318 | let disable_auto = g::t(lang, K::DisableAutoMerge); |
| 319 | let FlowView { |
| 320 | csrf: flow_csrf, |
| 321 | draft_toggle, |
| 322 | auto_on, |
| 323 | auto_enable, |
| 324 | queue_position, |
| 325 | queue_add, |
| 326 | } = flow; |
| 327 | let queue_add_label = |
| 328 | crate::i18n::merge_queue::t(lang, crate::i18n::merge_queue::K::AddToMergeQueue); |
| 329 | let queue_remove_label = |
| 330 | crate::i18n::merge_queue::t(lang, crate::i18n::merge_queue::K::RemoveFromQueue); |
| 331 | let style_legend = g::t(lang, K::MergeStyleLegend); |
| 332 | let style_merge = g::t(lang, K::StyleMergeCommit); |
| 333 | let style_squash = g::t(lang, K::StyleSquash); |
| 334 | let style_ff = g::t(lang, K::StyleFastForward); |
| 335 | let message_label = g::t(lang, K::MergeMessageLabel); |
| 336 | let merge_button = g::t(lang, K::MergeButton); |
| 337 | Ok(view! { |
| 338 | <section class=(class.as_str())> |
| 339 | if let Some(text) = merge_error { |
| 340 | <p class="alert merge-error" role="alert">(text.as_str())</p> |
| 341 | } |
| 342 | <div class="line merge-state"> |
| 343 | <span class="val merge-headline">(headline.as_str())</span> |
| 344 | if let Some(summary) = checks { |
| 345 | checks_summary(summary: summary, lang: StatusLang(lang)) |
| 346 | } |
| 347 | if let Some((action, label)) = draft_toggle { |
| 348 | post_act(label: label, action: action.as_str(), csrf: flow_csrf.as_str()) |
| 349 | } |
| 350 | </div> |
| 351 | if !required.is_empty() { |
| 352 | <div> |
| 353 | <p class="state">(required_heading)</p> |
| 354 | for row in required.iter() { |
| 355 | <div class=(format!("line {}", row.class))><span class="name"><code>(row.context.as_str())</code></span><span class="val">(row.label)</span></div> |
| 356 | } |
| 357 | </div> |
| 358 | } |
| 359 | if let Some(text) = approvals { |
| 360 | <p class="state">(text.as_str())</p> |
| 361 | } |
| 362 | if let Some(text) = blocked { |
| 363 | <p class="state merge-blocked">(text.as_str())</p> |
| 364 | } |
| 365 | if !conflicts.is_empty() { |
| 366 | <div> |
| 367 | for file in conflicts.iter() { |
| 368 | <div class="line"><span class="name"><code>(file.as_str())</code></span></div> |
| 369 | } |
| 370 | </div> |
| 371 | } |
| 372 | if let Some(f) = form { |
| 373 | <form method="post" action=(f.action.as_str()) class="fields merge-form"> |
| 374 | <input type="hidden" name="csrf" value=(f.csrf.as_str())> |
| 375 | <input type="hidden" name="head" value=(f.head.as_str())> |
| 376 | <fieldset> |
| 377 | <legend>(style_legend)</legend> |
| 378 | <div class="choice"> |
| 379 | <label><input type="radio" name="style" value="merge" checked=(true)><span>(style_merge)</span></label> |
| 380 | <label><input type="radio" name="style" value="squash"><span>(style_squash)</span></label> |
| 381 | <label><input type="radio" name="style" value="rebase"><span>(style_rebase)</span></label> |
| 382 | if f.ff { |
| 383 | <label><input type="radio" name="style" value="ff"><span>(style_ff)</span></label> |
| 384 | } |
| 385 | </div> |
| 386 | </fieldset> |
| 387 | <div class="field"> |
| 388 | <label for="merge-message">(message_label)</label> |
| 389 | <textarea id="merge-message" name="message" rows="3"></textarea> |
| 390 | </div> |
| 391 | <button type="submit" class="primary">(merge_button)</button> |
| 392 | </form> |
| 393 | } |
| 394 | if let Some((text, action)) = queue_position { |
| 395 | <div class="line"> |
| 396 | <span class="val">(text.as_str())</span> |
| 397 | confirm_act(lang: lang, label: queue_remove_label, action: action.as_str(), csrf: flow_csrf.as_str()) |
| 398 | </div> |
| 399 | } |
| 400 | if let Some(action) = queue_add { |
| 401 | <form method="post" action=(action.as_str()) class="fields"> |
| 402 | <input type="hidden" name="csrf" value=(flow_csrf.as_str())> |
| 403 | <div class="field"> |
| 404 | <label for="queue-style">(queue_add_label)</label> |
| 405 | style_select(id: "queue-style", lang: lang) |
| 406 | </div> |
| 407 | <button type="submit" class="primary">(queue_add_label)</button> |
| 408 | </form> |
| 409 | } |
| 410 | if let Some((text, action)) = auto_on { |
| 411 | <div class="line"> |
| 412 | <span class="val">(text.as_str())</span> |
| 413 | post_act(label: disable_auto, action: action.as_str(), csrf: flow_csrf.as_str()) |
| 414 | </div> |
| 415 | } |
| 416 | if let Some(action) = auto_enable { |
| 417 | <form method="post" action=(action.as_str()) class="fields"> |
| 418 | <input type="hidden" name="csrf" value=(flow_csrf.as_str())> |
| 419 | <div class="field"> |
| 420 | <label for="auto-style">(enable_auto)</label> |
| 421 | style_select(id: "auto-style", lang: lang) |
| 422 | </div> |
| 423 | <button type="submit" class="primary">(enable_auto)</button> |
| 424 | </form> |
| 425 | } |
| 426 | </section> |
| 427 | }) |
| 428 | } |
| 429 | |
| 430 | #[component] |
| 431 | async fn merged_notice( |
| 432 | cx: &Cx, |
| 433 | who: String, |
| 434 | when: When, |
| 435 | commit: String, |
| 436 | commit_href: Option<String>, |
| 437 | lang: Lang, |
| 438 | ) -> Result<impl View> { |
| 439 | let short = short_oid(&commit).to_owned(); |
| 440 | let prefix = g::t(lang, K::MergedByPrefix); |
| 441 | let middle = g::t(lang, K::MergedByMiddle); |
| 442 | let as_sep = g::t(lang, K::MergedAsSep); |
| 443 | let heading = crate::i18n::t(lang, crate::i18n::Key::StateMerged); |
| 444 | Ok(view! { |
| 445 | <section class="sec merge-merged"> |
| 446 | <h2>(heading)</h2> |
| 447 | <p class="state merge-headline"> |
| 448 | (prefix) <strong>user_link(shown: who.as_str(), login: who.as_str())</strong> (middle) |
| 449 | <time data-rel="true" datetime=(when.iso.as_str()) title=(when.absolute.as_str())>(when.relative.as_str())</time> |
| 450 | if !commit.is_empty() { |
| 451 | (as_sep) |
| 452 | if let Some(href) = commit_href { |
| 453 | <a class="mono" href=(href.as_str())>(short.as_str())</a> |
| 454 | } else { |
| 455 | <span class="mono">(short.as_str())</span> |
| 456 | } |
| 457 | } |
| 458 | </p> |
| 459 | </section> |
| 460 | }) |
| 461 | } |
| 462 | |
| 463 | struct DismissRow { |
| 464 | action: String, |
| 465 | label: String, |
| 466 | } |
| 467 | |
| 468 | #[component] |
| 469 | async fn review_box( |
| 470 | cx: &Cx, |
| 471 | approved: Option<String>, |
| 472 | approved_before: Option<String>, |
| 473 | changes: Option<String>, |
| 474 | dismiss: Vec<DismissRow>, |
| 475 | csrf: String, |
| 476 | lang: Lang, |
| 477 | ) -> Result<impl View> { |
| 478 | let dismiss_text = g::t(lang, K::DismissButton); |
| 479 | let heading = crate::i18n::s(lang, "Reviews", "İncelemeler"); |
| 480 | Ok(view! { |
| 481 | <section class="sec"> |
| 482 | <h2>(heading)</h2> |
| 483 | if let Some(text) = approved { |
| 484 | <p class="state">(text.as_str())</p> |
| 485 | } |
| 486 | if let Some(text) = approved_before { |
| 487 | <p class="state">(text.as_str())</p> |
| 488 | } |
| 489 | if let Some(text) = changes { |
| 490 | <p class="state">(text.as_str())</p> |
| 491 | } |
| 492 | for row in dismiss.iter() { |
| 493 | reveal(label: dismiss_text, |
| 494 | <form method="post" action=(row.action.as_str()) class="fields review-dismiss"> |
| 495 | <input type="hidden" name="csrf" value=(csrf.as_str())> |
| 496 | <div class="field"> |
| 497 | <label><span>(row.label.as_str())</span><input type="text" name="reason" required=(true)></label> |
| 498 | </div> |
| 499 | <button type="submit" class="primary">(dismiss_text)</button> |
| 500 | </form> |
| 501 | ) |
| 502 | } |
| 503 | </section> |
| 504 | }) |
| 505 | } |
| 506 | |
| 507 | async fn counted_reviews(state: &AppState, pr: &PullRequest) -> Result<Vec<Review>> { |
| 508 | let mut latest: HashMap<String, Review> = HashMap::new(); |
| 509 | for review in state.store.list_reviews(&pr.issue.id).await? { |
| 510 | if matches!( |
| 511 | review.state, |
| 512 | ReviewState::Approved | ReviewState::ChangesRequested |
| 513 | ) { |
| 514 | latest.insert(review.author_id.clone(), review); |
| 515 | } |
| 516 | } |
| 517 | let mut counted: Vec<Review> = latest.into_values().collect(); |
| 518 | counted.sort_by(|a, b| { |
| 519 | a.submitted_at |
| 520 | .cmp(&b.submitted_at) |
| 521 | .then_with(|| a.id.cmp(&b.id)) |
| 522 | }); |
| 523 | Ok(counted) |
| 524 | } |
| 525 | |
| 526 | async fn head_checks( |
| 527 | state: &AppState, |
| 528 | ctx: &RepoCtx, |
| 529 | pr: &PullRequest, |
| 530 | ) -> Result<Option<StatusSummary>> { |
| 531 | let rows = state.store.statuses_for(&ctx.repo.id, &pr.head_oid).await?; |
| 532 | Ok((!rows.is_empty()).then(|| StatusSummary::from_rows(&rows))) |
| 533 | } |
| 534 | |
| 535 | #[component] |
| 536 | async fn commit_list(cx: &Cx, rows: Vec<CommitRow>, lang: Lang) -> Result<impl View> { |
| 537 | let empty = rows.is_empty(); |
| 538 | let no_commits = g::t(lang, K::NoCommits); |
| 539 | let committed = crate::i18n::t(lang, crate::i18n::Key::Committed); |
| 540 | let browse = crate::i18n::t(lang, crate::i18n::Key::BrowseFiles); |
| 541 | Ok(view! { |
| 542 | if empty { |
| 543 | <p class="state">(no_commits)</p> |
| 544 | } else { |
| 545 | <div> |
| 546 | for row in rows.iter() { |
| 547 | <div class="line" data-kb-row=""> |
| 548 | <span class="name"><a href=(row.commit_href.as_str())>(row.summary.as_str())</a></span> |
| 549 | <span class="val"> |
| 550 | commit_status(summary: row.status.clone(), lang: StatusLang(lang)) |
| 551 | " " |
| 552 | <a class="mono" href=(row.commit_href.as_str())>(row.short.as_str())</a> |
| 553 | " · " (row.author.as_str()) " " (committed) " " |
| 554 | <time data-rel="true" datetime=(row.iso.as_str()) title=(row.absolute.as_str())>(row.relative.as_str())</time> |
| 555 | " · " |
| 556 | <a href=(row.tree_href.as_str())>(browse)</a> |
| 557 | </span> |
| 558 | </div> |
| 559 | } |
| 560 | </div> |
| 561 | } |
| 562 | }) |
| 563 | } |
| 564 | |
| 565 | async fn pull_conversation( |
| 566 | cx: &Cx, |
| 567 | ctx: RepoCtx, |
| 568 | pr: PullRequest, |
| 569 | draft: String, |
| 570 | error: Option<String>, |
| 571 | merge_error: Option<String>, |
| 572 | ) -> Result<impl View> { |
| 573 | let state = context::<AppState>(cx); |
| 574 | let lang = i18n::lang(cx, ctx.viewer_user()); |
| 575 | let commit_count = commits_of(state, &ctx, &pr).await?.len(); |
| 576 | let file_count = diff_of(state, &ctx, &pr).await?.files.len(); |
| 577 | let frame = frame_in(lang, state, &ctx, &pr, commit_count, file_count).await?; |
| 578 | let href = frame.href.clone(); |
| 579 | let owner = ctx.repo.owner_name.clone(); |
| 580 | let name = ctx.repo.name.clone(); |
| 581 | |
| 582 | let discussion = load_discussion( |
| 583 | lang, |
| 584 | state, |
| 585 | &ctx, |
| 586 | &pr.issue, |
| 587 | ItemKind::Pull, |
| 588 | &pr.base_branch, |
| 589 | ) |
| 590 | .await?; |
| 591 | let sidebar = load_sidebar(state, &ctx.repo.id, &pr.issue).await?; |
| 592 | let viewer_login = ctx.viewer.as_ref().map(|(u, _)| u.username.clone()); |
| 593 | let notify = notify_box_for(state, &ctx, &pr.issue.id, &href).await?; |
| 594 | let reviewers = load_reviewers(cx, state, &ctx, &pr.issue, &href).await?; |
| 595 | let csrf = csrf_of(&ctx); |
| 596 | let checks = head_checks(state, &ctx, &pr).await?; |
| 597 | let archived = ctx.repo.archived; |
| 598 | let signed_in = ctx.viewer.is_some(); |
| 599 | let merged = is_merged(&pr); |
| 600 | let open = matches!(pr.issue.state, IssueState::Open) && !merged; |
| 601 | let can_comment = signed_in && !archived && may_converse(&ctx, &pr.issue); |
| 602 | let locked_for_viewer = !may_converse(&ctx, &pr.issue); |
| 603 | let lock_notice = lock_message(lang, &pr.issue); |
| 604 | let can_close = may_change(&ctx, &pr.issue.author_id) && !merged; |
| 605 | let write_meta = !archived && triage(&ctx); |
| 606 | let comment_action = format!("{href}/comments"); |
| 607 | let state_action = format!("{href}/state"); |
| 608 | let close = (can_comment && can_close).then(|| (open, state_action.clone())); |
| 609 | let login_href = "/login".to_owned(); |
| 610 | let archived_text = g::t(lang, K::ArchivedReadOnly); |
| 611 | let login_before = g::t(lang, K::LoginBefore); |
| 612 | let login_link = crate::i18n::t(lang, crate::i18n::Key::LogInLink); |
| 613 | let login_after = g::t(lang, K::LoginAfter); |
| 614 | |
| 615 | let (headline, tone, conflicts) = match pr.mergeable { |
| 616 | Mergeable::Clean => (g::t(lang, K::MergeClean).to_owned(), "ok", Vec::new()), |
| 617 | Mergeable::Conflict => ( |
| 618 | g::t(lang, K::MergeConflict).to_owned(), |
| 619 | "bad", |
| 620 | pr.conflict_files.clone(), |
| 621 | ), |
| 622 | Mergeable::UpToDate => (g::t(lang, K::MergeUpToDate).to_owned(), "muted", Vec::new()), |
| 623 | Mergeable::Unknown | Mergeable::Merged => { |
| 624 | (g::t(lang, K::MergeChecking).to_owned(), "muted", Vec::new()) |
| 625 | } |
| 626 | }; |
| 627 | let can_write = ctx.can_write(); |
| 628 | let gate = if open { |
| 629 | Some(merge_gate(state, &ctx.repo, &pr).await?) |
| 630 | } else { |
| 631 | None |
| 632 | }; |
| 633 | let queue_required = |
| 634 | crate::merge_queue::requires_queue(state, &ctx.repo, &pr.base_branch).await?; |
| 635 | let blocked = gate |
| 636 | .as_ref() |
| 637 | .and_then(|gate| { |
| 638 | gate.blockers() |
| 639 | .into_iter() |
| 640 | .find(|code| !(queue_required && *code == ERR_NOT_UP_TO_DATE)) |
| 641 | }) |
| 642 | .and_then(|code| merge_error_text(lang, code)) |
| 643 | .map(str::to_owned); |
| 644 | let approvals = gate |
| 645 | .as_ref() |
| 646 | .filter(|gate| gate.rule.required_approvals > 0) |
| 647 | .map(|gate| { |
| 648 | g::approvals_of( |
| 649 | lang, |
| 650 | gate.approvals.min(gate.rule.required_approvals), |
| 651 | gate.rule.required_approvals, |
| 652 | ) |
| 653 | }); |
| 654 | let auto_merge_user = match pr.auto_merge.as_ref() { |
| 655 | Some(auto) => state |
| 656 | .store |
| 657 | .usernames_by_ids(std::slice::from_ref(&auto.user_id)) |
| 658 | .await? |
| 659 | .get(&auto.user_id) |
| 660 | .map(|(display, _)| display.clone()) |
| 661 | .unwrap_or_else(|| g::t(lang, K::Unknown).to_owned()), |
| 662 | None => String::new(), |
| 663 | }; |
| 664 | let required: Vec<CheckRow> = gate |
| 665 | .as_ref() |
| 666 | .map(|gate| { |
| 667 | gate.required |
| 668 | .iter() |
| 669 | .map(|check| { |
| 670 | let (class, label) = match check.state { |
| 671 | Some(StatusState::Success) => ("check-ok", g::t(lang, K::CheckPassing)), |
| 672 | Some(StatusState::Pending) => ("check-pending", g::t(lang, K::Pending)), |
| 673 | Some(StatusState::Failure) => ("check-bad", g::t(lang, K::CheckFailing)), |
| 674 | Some(StatusState::Error) => ("check-bad", g::t(lang, K::CheckErrored)), |
| 675 | None => ("check-pending", g::t(lang, K::CheckMissing)), |
| 676 | }; |
| 677 | CheckRow { |
| 678 | context: check.context.clone(), |
| 679 | label, |
| 680 | class, |
| 681 | } |
| 682 | }) |
| 683 | .collect() |
| 684 | }) |
| 685 | .unwrap_or_default(); |
| 686 | let auto_waiting = gate |
| 687 | .as_ref() |
| 688 | .is_some_and(|gate| !gate.blockers().is_empty() && gate.waiting_only()); |
| 689 | let queue_entry = match (open, queue_required) { |
| 690 | (true, true) => state.store.queue_entry_for_pull(&pr.issue.id).await?, |
| 691 | _ => None, |
| 692 | }; |
| 693 | let queue_position = match queue_entry.as_ref() { |
| 694 | Some(entry) => state |
| 695 | .store |
| 696 | .queue_for_branch(&ctx.repo.id, &entry.branch) |
| 697 | .await? |
| 698 | .iter() |
| 699 | .position(|waiting| waiting.id == entry.id) |
| 700 | .map(|index| index + 1), |
| 701 | None => None, |
| 702 | }; |
| 703 | let queue_can_add = open |
| 704 | && can_write |
| 705 | && queue_required |
| 706 | && queue_entry.is_none() |
| 707 | && pr.mergeable != Mergeable::Conflict |
| 708 | && pr.head_repo_id.is_some() |
| 709 | && blocked.is_none(); |
| 710 | let flow = FlowView { |
| 711 | queue_position: queue_position.filter(|_| can_write).map(|position| { |
| 712 | ( |
| 713 | crate::i18n::merge_queue::position_text(lang, position), |
| 714 | format!("{href}/merge-queue/remove"), |
| 715 | ) |
| 716 | }), |
| 717 | queue_add: queue_can_add.then(|| format!("{href}/merge-queue")), |
| 718 | csrf: csrf.clone(), |
| 719 | draft_toggle: (open |
| 720 | && !archived |
| 721 | && ctx |
| 722 | .viewer |
| 723 | .as_ref() |
| 724 | .is_some_and(|(user, _)| can_write || user.id == pr.issue.author_id)) |
| 725 | .then(|| { |
| 726 | if pr.draft { |
| 727 | (format!("{href}/ready"), g::t(lang, K::ReadyForReview)) |
| 728 | } else { |
| 729 | (format!("{href}/draft"), g::t(lang, K::ConvertToDraft)) |
| 730 | } |
| 731 | }), |
| 732 | auto_on: pr |
| 733 | .auto_merge |
| 734 | .as_ref() |
| 735 | .filter(|_| open && can_write) |
| 736 | .map(|auto| { |
| 737 | ( |
| 738 | g::auto_merge_on(lang, auto.style.as_str(), &auto_merge_user), |
| 739 | format!("{href}/auto-merge/disable"), |
| 740 | ) |
| 741 | }), |
| 742 | auto_enable: (open |
| 743 | && can_write |
| 744 | && !queue_required |
| 745 | && pr.auto_merge.is_none() |
| 746 | && auto_waiting |
| 747 | && pr.mergeable != Mergeable::Conflict |
| 748 | && pr.head_repo_id.is_some()) |
| 749 | .then(|| format!("{href}/auto-merge")), |
| 750 | }; |
| 751 | let merge_form = if open |
| 752 | && can_write |
| 753 | && !queue_required |
| 754 | && pr.mergeable == Mergeable::Clean |
| 755 | && pr.head_repo_id.is_some() |
| 756 | && blocked.is_none() |
| 757 | { |
| 758 | let ff = is_ancestor(&state.git, &ctx.path, &pr.base_oid, &pr.head_oid).await?; |
| 759 | Some(MergeForm { |
| 760 | action: format!("{href}/merge"), |
| 761 | csrf: csrf.clone(), |
| 762 | ff, |
| 763 | head: pr.head_oid.clone(), |
| 764 | }) |
| 765 | } else { |
| 766 | None |
| 767 | }; |
| 768 | |
| 769 | let merged_who = match pr.merged_by.clone() { |
| 770 | Some(id) => state |
| 771 | .store |
| 772 | .usernames_by_ids(std::slice::from_ref(&id)) |
| 773 | .await? |
| 774 | .get(&id) |
| 775 | .map(|(display, _)| display.clone()) |
| 776 | .unwrap_or_else(|| g::t(lang, K::Unknown).to_owned()), |
| 777 | None => g::t(lang, K::Unknown).to_owned(), |
| 778 | }; |
| 779 | let merged_when = pr |
| 780 | .merged_at |
| 781 | .as_deref() |
| 782 | .map(|stamp| When::new(lang, stamp, ctx.viewer_tz(), now_unix())); |
| 783 | let merge_commit = pr.merge_commit.clone().unwrap_or_default(); |
| 784 | let merge_commit_href = match merge_commit.as_str() { |
| 785 | "" => None, |
| 786 | oid => reachable_among(&ctx.path, vec![oid.to_owned()]) |
| 787 | .await |
| 788 | .contains(oid) |
| 789 | .then(|| format!("/{owner}/{name}/commit/{oid}")), |
| 790 | }; |
| 791 | |
| 792 | let review_error = error_from(lang, cx); |
| 793 | let summary = state.store.review_summary(&pr.issue.id).await?; |
| 794 | let mut outdated: Vec<String> = state |
| 795 | .store |
| 796 | .latest_review_states(&pr.issue.id) |
| 797 | .await? |
| 798 | .into_iter() |
| 799 | .filter(|(_, _, review, commit)| *review == ReviewState::Approved && *commit != pr.head_oid) |
| 800 | .map(|(_, name, _, _)| name) |
| 801 | .collect(); |
| 802 | outdated.sort_unstable(); |
| 803 | let current: Vec<&str> = summary |
| 804 | .approved_by |
| 805 | .iter() |
| 806 | .filter(|name| !outdated.contains(name)) |
| 807 | .map(String::as_str) |
| 808 | .collect(); |
| 809 | let approved = (!current.is_empty()).then(|| g::approved_by(lang, ¤t.join(", "))); |
| 810 | let approved_before = |
| 811 | (!outdated.is_empty()).then(|| g::approved_before_push(lang, &outdated.join(", "))); |
| 812 | let changes = (!summary.changes_requested_by.is_empty()) |
| 813 | .then(|| g::changes_requested_by(lang, &summary.changes_requested_by.join(", "))); |
| 814 | let review_box_shown = approved.is_some() || approved_before.is_some() || changes.is_some(); |
| 815 | let dismiss: Vec<DismissRow> = if open && can_write { |
| 816 | counted_reviews(state, &pr) |
| 817 | .await? |
| 818 | .into_iter() |
| 819 | .map(|review| DismissRow { |
| 820 | action: format!("{href}/reviews/{}/dismiss", review.id), |
| 821 | label: g::dismiss_reason(lang, &review.author_name), |
| 822 | }) |
| 823 | .collect() |
| 824 | } else { |
| 825 | Vec::new() |
| 826 | }; |
| 827 | |
| 828 | let branch = pull_branch::branch_action(state, &ctx, &pr, &csrf).await?; |
| 829 | let branch_error = branch_error_from(cx, lang); |
| 830 | let branch_shown = !open && (branch.is_some() || branch_error.is_some()); |
| 831 | |
| 832 | let page_title = frame.page_title.clone(); |
| 833 | let labels_href = repo_href(&owner, &name, "labels.css", ""); |
| 834 | let live = format!("{href}/events"); |
| 835 | Ok(view! { |
| 836 | shell(title: page_title.as_str(), viewer: ctx.viewer.as_ref(), extra_stylesheet: Some(labels_href.as_str()), |
| 837 | pull_header(frame: frame.clone(), active: "conversation".to_owned(), |
| 838 | item_head_tools(lang: lang, close: close, notify: notify) |
| 839 | ) |
| 840 | if let Some(message) = review_error { |
| 841 | alert(message: message) |
| 842 | } |
| 843 | <div class="sheet" data-live=(live.as_str())> |
| 844 | discussion_timeline(lang: lang, discussion: discussion) |
| 845 | if review_box_shown { |
| 846 | review_box(approved: approved, approved_before: approved_before, changes: changes, dismiss: dismiss, |
| 847 | csrf: csrf.clone(), lang: lang) |
| 848 | } |
| 849 | if open { |
| 850 | merge_box(headline: headline, tone: tone.to_owned(), conflicts: conflicts, |
| 851 | form: merge_form, merge_error: merge_error, |
| 852 | approvals: approvals, blocked: blocked, checks: checks, |
| 853 | required: required, flow: flow, lang: lang) |
| 854 | } |
| 855 | if let Some(when) = merged_when { |
| 856 | merged_notice(who: merged_who, when: when, commit: merge_commit, |
| 857 | commit_href: merge_commit_href, lang: lang) |
| 858 | } |
| 859 | if branch_shown { |
| 860 | branch_box(lang: lang, action: branch, error: branch_error) |
| 861 | } |
| 862 | if can_comment { |
| 863 | comment_form(lang: lang, action: comment_action, csrf: csrf.clone(), draft: draft, error: error) |
| 864 | } |
| 865 | reviewers_box(lang: lang, reviewers: reviewers) |
| 866 | item_sidebar(lang: lang, write: write_meta, base: href.clone(), csrf: csrf.clone(), viewer: viewer_login, sidebar: sidebar, pin: None) |
| 867 | if archived { |
| 868 | <p class="state">(archived_text)</p> |
| 869 | } |
| 870 | if locked_for_viewer { |
| 871 | <p class="state">(lock_notice.as_str())</p> |
| 872 | } else if !signed_in { |
| 873 | <p class="state">(login_before)<a href=(login_href.as_str())>(login_link)</a>(login_after)</p> |
| 874 | } |
| 875 | </div> |
| 876 | ) |
| 877 | }) |
| 878 | } |
| 879 | |
| 880 | fn merge_error_text(lang: Lang, code: &str) -> Option<&'static str> { |
| 881 | Some(match code { |
| 882 | ERR_CONFLICT => g::t(lang, K::ErrConflict), |
| 883 | ERR_UP_TO_DATE => g::t(lang, K::ErrUpToDate), |
| 884 | ERR_FF_IMPOSSIBLE => g::t(lang, K::ErrFfImpossible), |
| 885 | ERR_BASE_MOVED => g::t(lang, K::ErrBaseMoved), |
| 886 | ERR_HEAD_MOVED => g::t(lang, K::ErrHeadMoved), |
| 887 | ERR_ALREADY_MERGED => g::t(lang, K::ErrAlreadyMerged), |
| 888 | ERR_NOT_OPEN => g::t(lang, K::ErrNotOpen), |
| 889 | ERR_HEAD_MISSING => g::t(lang, K::ErrHeadMissing), |
| 890 | ERR_BASE_MISSING => g::t(lang, K::ErrBaseMissing), |
| 891 | ERR_NEEDS_APPROVALS => g::t(lang, K::ErrNeedsApprovals), |
| 892 | ERR_CHANGES_REQUESTED => g::t(lang, K::ErrChangesRequested), |
| 893 | ERR_NOT_UP_TO_DATE => g::t(lang, K::ErrNotUpToDate), |
| 894 | ERR_MESSAGE_INVALID => g::t(lang, K::ErrMessageInvalid), |
| 895 | ERR_DRAFT => g::t(lang, K::ErrDraft), |
| 896 | ERR_NEEDS_CHECKS => g::t(lang, K::ErrNeedsChecks), |
| 897 | ERR_REBASE_CONFLICT => g::t(lang, K::ErrRebaseConflict), |
| 898 | ERR_AUTO_MERGE_BLOCKED => g::t(lang, K::ErrAutoMergeBlocked), |
| 899 | ERR_QUEUE_REQUIRED => { |
| 900 | crate::i18n::merge_queue::t(lang, crate::i18n::merge_queue::K::ErrQueueRequired) |
| 901 | } |
| 902 | ERR_QUEUE_ALREADY => { |
| 903 | crate::i18n::merge_queue::t(lang, crate::i18n::merge_queue::K::ErrQueueAlready) |
| 904 | } |
| 905 | _ => return None, |
| 906 | }) |
| 907 | } |
| 908 | |
| 909 | fn merge_error_from(lang: Lang, cx: &Cx) -> Option<String> { |
| 910 | merge_error_text(lang, &query_value(cx, "merge_error")?).map(str::to_owned) |
| 911 | } |
| 912 | |
| 913 | #[page("/{owner}/{repo}/pulls/{number}")] |
| 914 | async fn pull_show(cx: &Cx) -> Result<impl View> { |
| 915 | let (ctx, pr) = load_repo_pull(cx).await?; |
| 916 | let lang = i18n::lang(cx, ctx.viewer_user()); |
| 917 | let merge_error = merge_error_from(lang, cx); |
| 918 | pull_conversation(cx, ctx, pr, String::new(), None, merge_error).await |
| 919 | } |
| 920 | |
| 921 | #[page("/{owner}/{repo}/pulls/{number}/commits")] |
| 922 | async fn pull_commits(cx: &Cx) -> Result<impl View> { |
| 923 | let (ctx, pr) = load_repo_pull(cx).await?; |
| 924 | let state = context::<AppState>(cx); |
| 925 | let lang = i18n::lang(cx, ctx.viewer_user()); |
| 926 | let commits = commits_of(state, &ctx, &pr).await?; |
| 927 | let file_count = diff_of(state, &ctx, &pr).await?.files.len(); |
| 928 | let frame = frame_in(lang, state, &ctx, &pr, commits.len(), file_count).await?; |
| 929 | |
| 930 | let owner = ctx.repo.owner_name.clone(); |
| 931 | let name = ctx.repo.name.clone(); |
| 932 | let tz = ctx.viewer_tz().to_owned(); |
| 933 | let now = now_unix(); |
| 934 | let oids: Vec<&str> = commits.iter().map(|info| info.oid.as_str()).collect(); |
| 935 | let statuses = state |
| 936 | .store |
| 937 | .statuses_for_many_rows(&ctx.repo.id, &oids) |
| 938 | .await?; |
| 939 | let rows: Vec<CommitRow> = commits |
| 940 | .iter() |
| 941 | .map(|info| { |
| 942 | CommitRow::new(&owner, &name, info, now, &tz, lang).with_statuses(&statuses, &info.oid) |
| 943 | }) |
| 944 | .collect(); |
| 945 | |
| 946 | let page_title = frame.page_title.clone(); |
| 947 | Ok(view! { |
| 948 | shell(title: page_title.as_str(), viewer: ctx.viewer.as_ref(), |
| 949 | pull_header(frame: frame.clone(), active: "commits".to_owned()) |
| 950 | <div class="sheet"> |
| 951 | commit_list(rows: rows, lang: lang) |
| 952 | </div> |
| 953 | ) |
| 954 | }) |
| 955 | } |
| 956 | |
| 957 | async fn pull_post(cx: &Cx, form: CommentForm) -> Result<impl View> { |
| 958 | let number = parse_number(number(cx))?; |
| 959 | let ctx = load_repo(cx, owner(cx), repo(cx)).await?; |
| 960 | let (user, session) = require_user(cx).await?; |
| 961 | check_write(&ctx, &session, &form.csrf)?; |
| 962 | let state = context::<AppState>(cx); |
| 963 | let pr = load_pull(cx, &ctx, number).await?; |
| 964 | let lang = i18n::lang(cx, Some(&user)); |
| 965 | |
| 966 | let changes_state = !matches!(form.action.as_str(), "" | "comment"); |
| 967 | let posted = if changes_state && is_merged(&pr) { |
| 968 | Posted::Refused { |
| 969 | message: g::t(lang, K::MergedCannotChange).to_owned(), |
| 970 | } |
| 971 | } else { |
| 972 | post_comment(lang, state, &ctx, &user, &pr.issue, true, &form).await? |
| 973 | }; |
| 974 | match posted { |
| 975 | Posted::Saved { comment_id } => { |
| 976 | let anchor = match comment_id { |
| 977 | Some(id) => format!("comment-{id}"), |
| 978 | None => format!("pull-{number}"), |
| 979 | }; |
| 980 | Err(see_other(format!( |
| 981 | "{}#{anchor}", |
| 982 | item_href(&ctx, ItemKind::Pull, number) |
| 983 | )) |
| 984 | .into()) |
| 985 | } |
| 986 | Posted::Refused { message } => { |
| 987 | pull_conversation(cx, ctx, pr, form.body, Some(message), None).await |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | #[page(POST "/{owner}/{repo}/pulls/{number}/comments")] |
| 993 | async fn pull_comment(cx: &Cx, Form(form): Form<CommentForm>) -> Result<impl View> { |
| 994 | pull_post(cx, form).await |
| 995 | } |
| 996 | |
| 997 | #[page(POST "/{owner}/{repo}/pulls/{number}/state")] |
| 998 | async fn pull_state(cx: &Cx, Form(form): Form<CommentForm>) -> Result<impl View> { |
| 999 | pull_post(cx, form).await |
| 1000 | } |
| 1001 | |
| 1002 | #[cfg(test)] |
| 1003 | mod tests { |
| 1004 | use forge_core::{ |
| 1005 | IssueState, Mergeable, NewPull, PullRequest, ReviewState, Side, User, Visibility, |
| 1006 | }; |
| 1007 | use forge_git::GitEnv; |
| 1008 | use topcoat::router::request::Request; |
| 1009 | use topcoat::router::{Body, Router, to_bytes}; |
| 1010 | |
| 1011 | use crate::auth::{SESSION_COOKIE, percent_encode}; |
| 1012 | use crate::test_support::fixture::{Fixture, OWNER, REPO, git}; |
| 1013 | |
| 1014 | fn cookie(token: &str) -> String { |
| 1015 | format!("__Host-{SESSION_COOKIE}={token}") |
| 1016 | } |
| 1017 | |
| 1018 | fn page_main(body: &str) -> &str { |
| 1019 | body.split_once("<main tabindex=\"-1\"") |
| 1020 | .and_then(|(_, rest)| rest.split_once('>')) |
| 1021 | .and_then(|(_, rest)| rest.split_once("</main>")) |
| 1022 | .map(|(main, _)| main) |
| 1023 | .expect("page has a <main> element") |
| 1024 | } |
| 1025 | |
| 1026 | async fn send( |
| 1027 | router: &Router, |
| 1028 | method: &str, |
| 1029 | uri: &str, |
| 1030 | token: Option<&str>, |
| 1031 | form: Option<String>, |
| 1032 | ) -> (u16, Option<String>, String) { |
| 1033 | let mut req = Request::builder().method(method).uri(uri); |
| 1034 | if let Some(token) = token { |
| 1035 | req = req.header("cookie", cookie(token)); |
| 1036 | } |
| 1037 | let body = match form { |
| 1038 | Some(form) => { |
| 1039 | req = req.header("content-type", "application/x-www-form-urlencoded"); |
| 1040 | Body::from(form) |
| 1041 | } |
| 1042 | None => Body::empty(), |
| 1043 | }; |
| 1044 | let res = router.handle(req.body(body).unwrap()).await; |
| 1045 | let status = res.status().as_u16(); |
| 1046 | let location = res |
| 1047 | .headers() |
| 1048 | .get("location") |
| 1049 | .map(|v| v.to_str().unwrap().to_owned()); |
| 1050 | let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 1051 | ( |
| 1052 | status, |
| 1053 | location, |
| 1054 | String::from_utf8_lossy(&bytes).into_owned(), |
| 1055 | ) |
| 1056 | } |
| 1057 | |
| 1058 | async fn sign_in(fx: &Fixture, name: &str, token: &str) -> User { |
| 1059 | let user = fx |
| 1060 | .store |
| 1061 | .create_user(name, None, "not-a-real-hash", false) |
| 1062 | .await |
| 1063 | .unwrap(); |
| 1064 | crate::test_support::create_session(&fx.store, &user.id, token, &format!("{token}-csrf")) |
| 1065 | .await; |
| 1066 | user |
| 1067 | } |
| 1068 | |
| 1069 | async fn owner_session(fx: &Fixture) { |
| 1070 | let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); |
| 1071 | crate::test_support::create_session(&fx.store, &owner.id, "tahinli", "tahinli-csrf").await; |
| 1072 | } |
| 1073 | |
| 1074 | fn csrf_form(token: &str, pairs: &[(&str, &str)]) -> String { |
| 1075 | let mut form = format!("csrf={}", percent_encode(&format!("{token}-csrf"))); |
| 1076 | for (key, value) in pairs { |
| 1077 | form.push_str(&format!("&{key}={}", percent_encode(value))); |
| 1078 | } |
| 1079 | form |
| 1080 | } |
| 1081 | |
| 1082 | async fn pull(fx: &Fixture, number: i64) -> PullRequest { |
| 1083 | let repo = fx |
| 1084 | .store |
| 1085 | .repo_by_owner_and_name(OWNER, REPO) |
| 1086 | .await |
| 1087 | .unwrap() |
| 1088 | .unwrap(); |
| 1089 | fx.store |
| 1090 | .pull_by_number(&repo.id, number) |
| 1091 | .await |
| 1092 | .unwrap() |
| 1093 | .unwrap() |
| 1094 | } |
| 1095 | |
| 1096 | async fn open_pull(fx: &Fixture) -> i64 { |
| 1097 | let base_oid = git(&fx.work, &["rev-parse", "HEAD"]); |
| 1098 | git(&fx.work, &["checkout", "-q", "-b", "feature", &fx.root]); |
| 1099 | std::fs::write(fx.work.join("feat.txt"), "feature\n").unwrap(); |
| 1100 | git(&fx.work, &["add", "-A"]); |
| 1101 | git(&fx.work, &["commit", "-q", "-m", "Add feature file"]); |
| 1102 | let head_oid = git(&fx.work, &["rev-parse", "HEAD"]); |
| 1103 | git( |
| 1104 | &fx.work, |
| 1105 | &[ |
| 1106 | "push", |
| 1107 | "-q", |
| 1108 | &fx.bare.to_string_lossy(), |
| 1109 | "HEAD:refs/heads/feature", |
| 1110 | ], |
| 1111 | ); |
| 1112 | |
| 1113 | let repo = fx |
| 1114 | .store |
| 1115 | .repo_by_owner_and_name(OWNER, REPO) |
| 1116 | .await |
| 1117 | .unwrap() |
| 1118 | .unwrap(); |
| 1119 | let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); |
| 1120 | let pr = fx |
| 1121 | .store |
| 1122 | .create_pull(&NewPull { |
| 1123 | repo_id: &repo.id, |
| 1124 | author_id: &owner.id, |
| 1125 | title: "Add feature", |
| 1126 | body: "Body text", |
| 1127 | base_branch: "main", |
| 1128 | head_repo_id: &repo.id, |
| 1129 | head_branch: "feature", |
| 1130 | head_oid: &head_oid, |
| 1131 | base_oid: &base_oid, |
| 1132 | }) |
| 1133 | .await |
| 1134 | .unwrap(); |
| 1135 | let env = GitEnv { |
| 1136 | git_bin: "git".into(), |
| 1137 | data_dir: fx.work.clone(), |
| 1138 | }; |
| 1139 | forge_git::pulls::fetch_into_pull_ref(&env, &fx.bare, &fx.bare, &head_oid, pr.issue.number) |
| 1140 | .await |
| 1141 | .unwrap(); |
| 1142 | pr.issue.number |
| 1143 | } |
| 1144 | |
| 1145 | #[tokio::test] |
| 1146 | async fn conversation_merge_box_and_error_codes_on_one_pull() { |
| 1147 | let fx = Fixture::new(Visibility::Public).await; |
| 1148 | owner_session(&fx).await; |
| 1149 | sign_in(&fx, "bob", "bob-token").await; |
| 1150 | let number = open_pull(&fx).await; |
| 1151 | let pr = pull(&fx, number).await; |
| 1152 | let uri = format!("/tahinli/demo/pulls/{number}"); |
| 1153 | |
| 1154 | let req = Request::builder() |
| 1155 | .method("GET") |
| 1156 | .uri(uri.as_str()) |
| 1157 | .header("accept-language", "tr") |
| 1158 | .body(Body::empty()) |
| 1159 | .unwrap(); |
| 1160 | let res = fx.router.handle(req).await; |
| 1161 | let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 1162 | let body = String::from_utf8_lossy(&bytes).into_owned(); |
| 1163 | assert!(body.contains("Tartışma</a>"), "turkish conversation tab"); |
| 1164 | assert!(body.contains("Commitler "), "turkish commits tab"); |
| 1165 | assert!(body.contains("Değişen dosyalar "), "turkish files tab"); |
| 1166 | assert!(!body.contains(">Conversation<"), "no English tab label"); |
| 1167 | |
| 1168 | fx.store |
| 1169 | .set_mergeability( |
| 1170 | &pr.issue.id, |
| 1171 | &pr.base_oid, |
| 1172 | Some(&fx.root), |
| 1173 | Mergeable::Conflict, |
| 1174 | &["feat.txt".to_owned()], |
| 1175 | ) |
| 1176 | .await |
| 1177 | .unwrap(); |
| 1178 | let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; |
| 1179 | assert_eq!(status, 200, "conflict"); |
| 1180 | assert!( |
| 1181 | body.contains("This branch has conflicts that must be resolved"), |
| 1182 | "conflict: {body}" |
| 1183 | ); |
| 1184 | assert!(body.contains("<code>feat.txt</code>"), "conflict: {body}"); |
| 1185 | assert!( |
| 1186 | !body.contains(&format!("action=\"{uri}/merge\"")), |
| 1187 | "conflict: {body}" |
| 1188 | ); |
| 1189 | assert!(!body.contains("name=\"style\""), "conflict: {body}"); |
| 1190 | |
| 1191 | fx.store |
| 1192 | .set_mergeability( |
| 1193 | &pr.issue.id, |
| 1194 | &pr.base_oid, |
| 1195 | Some(&fx.root), |
| 1196 | Mergeable::Clean, |
| 1197 | &[], |
| 1198 | ) |
| 1199 | .await |
| 1200 | .unwrap(); |
| 1201 | let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; |
| 1202 | assert_eq!(status, 200, "clean"); |
| 1203 | assert!(body.contains("Add feature"), "clean: {body}"); |
| 1204 | assert!(body.contains("issue-badge-open\">Open<"), "clean: {body}"); |
| 1205 | assert!(body.contains("Ready to merge"), "clean: {body}"); |
| 1206 | assert!( |
| 1207 | body.contains(&format!("action=\"{uri}/merge\"")), |
| 1208 | "clean: {body}" |
| 1209 | ); |
| 1210 | assert!(body.contains("name=\"style\""), "clean: {body}"); |
| 1211 | assert!( |
| 1212 | body.contains(&format!("action=\"{uri}/comments\"")), |
| 1213 | "clean: {body}" |
| 1214 | ); |
| 1215 | assert!(body.contains("Commits 1"), "clean: {body}"); |
| 1216 | assert!(body.contains("Files changed 1"), "clean: {body}"); |
| 1217 | |
| 1218 | let (status, _, body) = send(&fx.router, "GET", &uri, Some("bob-token"), None).await; |
| 1219 | assert_eq!(status, 200, "reader"); |
| 1220 | assert!(body.contains("Ready to merge"), "reader: {body}"); |
| 1221 | assert!(!body.contains("name=\"style\""), "reader: {body}"); |
| 1222 | assert!(!body.contains("Merge pull request"), "reader: {body}"); |
| 1223 | |
| 1224 | let (status, _, body) = send( |
| 1225 | &fx.router, |
| 1226 | "GET", |
| 1227 | &format!("{uri}?merge_error=conflict"), |
| 1228 | Some("tahinli"), |
| 1229 | None, |
| 1230 | ) |
| 1231 | .await; |
| 1232 | assert_eq!(status, 200, "merge error"); |
| 1233 | assert!( |
| 1234 | body.contains("class=\"alert merge-error\""), |
| 1235 | "merge error: {body}" |
| 1236 | ); |
| 1237 | assert!( |
| 1238 | body.contains("This branch has conflicts that must be resolved"), |
| 1239 | "merge error: {body}" |
| 1240 | ); |
| 1241 | let (status, _, body) = send( |
| 1242 | &fx.router, |
| 1243 | "GET", |
| 1244 | &format!("{uri}?merge_error=%3Cscript%3Ealert(1)%3C%2Fscript%3E"), |
| 1245 | Some("tahinli"), |
| 1246 | None, |
| 1247 | ) |
| 1248 | .await; |
| 1249 | assert_eq!(status, 200, "unknown merge error"); |
| 1250 | assert!(!body.contains("merge-error"), "unknown merge error: {body}"); |
| 1251 | assert!( |
| 1252 | !page_main(&body).contains("alert(1)"), |
| 1253 | "unknown merge error: {body}" |
| 1254 | ); |
| 1255 | |
| 1256 | let (status, _, body) = send( |
| 1257 | &fx.router, |
| 1258 | "GET", |
| 1259 | &format!("{uri}?review_error=own_pr"), |
| 1260 | Some("tahinli"), |
| 1261 | None, |
| 1262 | ) |
| 1263 | .await; |
| 1264 | assert_eq!(status, 200, "review error"); |
| 1265 | assert!( |
| 1266 | body.contains("You cannot approve or request changes on your own pull request."), |
| 1267 | "review error: {body}" |
| 1268 | ); |
| 1269 | let (_, _, body) = send( |
| 1270 | &fx.router, |
| 1271 | "GET", |
| 1272 | &format!("{uri}?review_error=%3Cscript%3E"), |
| 1273 | Some("tahinli"), |
| 1274 | None, |
| 1275 | ) |
| 1276 | .await; |
| 1277 | assert!(!body.contains("<script>"), "unknown review error: {body}"); |
| 1278 | assert!( |
| 1279 | !body.contains("You cannot approve"), |
| 1280 | "unknown review error: {body}" |
| 1281 | ); |
| 1282 | |
| 1283 | let (status, location, _) = send( |
| 1284 | &fx.router, |
| 1285 | "GET", |
| 1286 | &format!("/tahinli/demo/issues/{number}"), |
| 1287 | None, |
| 1288 | None, |
| 1289 | ) |
| 1290 | .await; |
| 1291 | assert_eq!(status, 303, "issue url"); |
| 1292 | assert_eq!(location.as_deref(), Some(uri.as_str()), "issue url"); |
| 1293 | |
| 1294 | let (status, _, body) = |
| 1295 | send(&fx.router, "GET", &format!("{uri}/commits"), None, None).await; |
| 1296 | assert_eq!(status, 200, "commits tab"); |
| 1297 | assert!(body.contains("Add feature file"), "commits tab: {body}"); |
| 1298 | let (status, _, body) = send(&fx.router, "GET", &format!("{uri}/files"), None, None).await; |
| 1299 | assert_eq!(status, 200, "files tab"); |
| 1300 | assert!(body.contains("feat.txt"), "files tab: {body}"); |
| 1301 | } |
| 1302 | |
| 1303 | #[tokio::test] |
| 1304 | async fn comments_state_and_title_edits_on_one_pull() { |
| 1305 | let fx = Fixture::new(Visibility::Public).await; |
| 1306 | owner_session(&fx).await; |
| 1307 | sign_in(&fx, "bob", "bob-token").await; |
| 1308 | let number = open_pull(&fx).await; |
| 1309 | let uri = format!("/tahinli/demo/pulls/{number}"); |
| 1310 | let pr = pull(&fx, number).await; |
| 1311 | let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); |
| 1312 | |
| 1313 | let comment = fx |
| 1314 | .store |
| 1315 | .add_comment(&pr.issue.id, &owner.id, "first draft") |
| 1316 | .await |
| 1317 | .unwrap(); |
| 1318 | let base = format!("{uri}/comments/{}", comment.id); |
| 1319 | let (status, _, _) = send( |
| 1320 | &fx.router, |
| 1321 | "GET", |
| 1322 | &format!("{base}/edit"), |
| 1323 | Some("tahinli"), |
| 1324 | None, |
| 1325 | ) |
| 1326 | .await; |
| 1327 | assert_eq!(status, 200, "comment edit form"); |
| 1328 | let form = csrf_form("tahinli", &[("body", "edited text")]); |
| 1329 | let (status, location, _) = send( |
| 1330 | &fx.router, |
| 1331 | "POST", |
| 1332 | &format!("{base}/edit"), |
| 1333 | Some("tahinli"), |
| 1334 | Some(form), |
| 1335 | ) |
| 1336 | .await; |
| 1337 | assert_eq!(status, 303, "comment edit"); |
| 1338 | assert_eq!( |
| 1339 | location, |
| 1340 | Some(format!( |
| 1341 | "/tahinli/demo/pulls/{number}#comment-{}", |
| 1342 | comment.id |
| 1343 | )), |
| 1344 | "comment edit" |
| 1345 | ); |
| 1346 | let saved = fx |
| 1347 | .store |
| 1348 | .comment_by_id(&pr.issue.id, &comment.id) |
| 1349 | .await |
| 1350 | .unwrap() |
| 1351 | .unwrap(); |
| 1352 | assert_eq!(saved.body, "edited text", "comment edit"); |
| 1353 | assert!(saved.edited, "comment edit"); |
| 1354 | let (status, location, _) = send( |
| 1355 | &fx.router, |
| 1356 | "POST", |
| 1357 | &format!("{base}/delete"), |
| 1358 | Some("tahinli"), |
| 1359 | Some(csrf_form("tahinli", &[])), |
| 1360 | ) |
| 1361 | .await; |
| 1362 | assert_eq!(status, 303, "comment delete"); |
| 1363 | assert_eq!( |
| 1364 | location, |
| 1365 | Some(format!("/tahinli/demo/pulls/{number}")), |
| 1366 | "comment delete" |
| 1367 | ); |
| 1368 | assert!( |
| 1369 | fx.store |
| 1370 | .comment_by_id(&pr.issue.id, &comment.id) |
| 1371 | .await |
| 1372 | .unwrap() |
| 1373 | .is_none(), |
| 1374 | "comment delete" |
| 1375 | ); |
| 1376 | assert_eq!( |
| 1377 | pull(&fx, number).await.issue.comment_count, |
| 1378 | 0, |
| 1379 | "comment delete" |
| 1380 | ); |
| 1381 | |
| 1382 | let form = csrf_form("tahinli", &[("body", "Looks good to me")]); |
| 1383 | let (status, location, _) = send( |
| 1384 | &fx.router, |
| 1385 | "POST", |
| 1386 | &format!("{uri}/comments"), |
| 1387 | Some("tahinli"), |
| 1388 | Some(form), |
| 1389 | ) |
| 1390 | .await; |
| 1391 | assert_eq!(status, 303, "post comment"); |
| 1392 | assert!( |
| 1393 | location |
| 1394 | .unwrap() |
| 1395 | .starts_with(&format!("/tahinli/demo/pulls/{number}#comment-")), |
| 1396 | "post comment" |
| 1397 | ); |
| 1398 | let comments = fx.store.list_comments(&pr.issue.id).await.unwrap(); |
| 1399 | assert_eq!(comments.len(), 1, "post comment"); |
| 1400 | assert_eq!(comments[0].body, "Looks good to me", "post comment"); |
| 1401 | |
| 1402 | let keep = fx |
| 1403 | .store |
| 1404 | .add_comment(&pr.issue.id, &owner.id, "keep me") |
| 1405 | .await |
| 1406 | .unwrap(); |
| 1407 | let (status, _, _) = send( |
| 1408 | &fx.router, |
| 1409 | "GET", |
| 1410 | &format!("/tahinli/demo/issues/{number}/edit"), |
| 1411 | Some("tahinli"), |
| 1412 | None, |
| 1413 | ) |
| 1414 | .await; |
| 1415 | assert_eq!(status, 404, "issue edit route"); |
| 1416 | let form = csrf_form("tahinli", &[("title", "Nope"), ("body", "x")]); |
| 1417 | let (status, _, _) = send( |
| 1418 | &fx.router, |
| 1419 | "POST", |
| 1420 | &format!("/tahinli/demo/issues/{number}/edit"), |
| 1421 | Some("tahinli"), |
| 1422 | Some(form), |
| 1423 | ) |
| 1424 | .await; |
| 1425 | assert_eq!(status, 404, "issue edit post"); |
| 1426 | let issue_base = format!("/tahinli/demo/issues/{number}/comments/{}", keep.id); |
| 1427 | let (status, _, _) = send( |
| 1428 | &fx.router, |
| 1429 | "GET", |
| 1430 | &format!("{issue_base}/edit"), |
| 1431 | Some("tahinli"), |
| 1432 | None, |
| 1433 | ) |
| 1434 | .await; |
| 1435 | assert_eq!(status, 404, "issue comment edit"); |
| 1436 | let (status, _, _) = send( |
| 1437 | &fx.router, |
| 1438 | "POST", |
| 1439 | &format!("{issue_base}/delete"), |
| 1440 | Some("tahinli"), |
| 1441 | Some(csrf_form("tahinli", &[])), |
| 1442 | ) |
| 1443 | .await; |
| 1444 | assert_eq!(status, 404, "issue comment delete"); |
| 1445 | let pr = pull(&fx, number).await; |
| 1446 | assert_eq!(pr.issue.title, "Add feature", "issue routes"); |
| 1447 | assert!( |
| 1448 | fx.store |
| 1449 | .comment_by_id(&pr.issue.id, &keep.id) |
| 1450 | .await |
| 1451 | .unwrap() |
| 1452 | .is_some(), |
| 1453 | "issue routes kept the comment" |
| 1454 | ); |
| 1455 | |
| 1456 | let uri_state = format!("{uri}/state"); |
| 1457 | let form = csrf_form("tahinli", &[("action", "close_not_planned")]); |
| 1458 | let (status, _, _) = |
| 1459 | send(&fx.router, "POST", &uri_state, Some("tahinli"), Some(form)).await; |
| 1460 | assert_eq!(status, 303, "close"); |
| 1461 | assert!( |
| 1462 | matches!(pull(&fx, number).await.issue.state, IssueState::Closed), |
| 1463 | "close" |
| 1464 | ); |
| 1465 | let form = csrf_form("tahinli", &[("action", "reopen")]); |
| 1466 | let (status, _, _) = |
| 1467 | send(&fx.router, "POST", &uri_state, Some("tahinli"), Some(form)).await; |
| 1468 | assert_eq!(status, 303, "reopen"); |
| 1469 | assert!( |
| 1470 | matches!(pull(&fx, number).await.issue.state, IssueState::Open), |
| 1471 | "reopen" |
| 1472 | ); |
| 1473 | |
| 1474 | let (_, _, body) = send(&fx.router, "GET", &uri, Some("bob-token"), None).await; |
| 1475 | assert!( |
| 1476 | !body.contains(&format!("href=\"{uri}/edit\"")), |
| 1477 | "reader edit link" |
| 1478 | ); |
| 1479 | let (status, _, _) = send( |
| 1480 | &fx.router, |
| 1481 | "GET", |
| 1482 | &format!("{uri}/edit"), |
| 1483 | Some("bob-token"), |
| 1484 | None, |
| 1485 | ) |
| 1486 | .await; |
| 1487 | assert_eq!(status, 403, "reader edit form"); |
| 1488 | let form = csrf_form("bob-token", &[("title", "Hijacked"), ("body", "x")]); |
| 1489 | let (status, _, _) = send( |
| 1490 | &fx.router, |
| 1491 | "POST", |
| 1492 | &format!("{uri}/edit"), |
| 1493 | Some("bob-token"), |
| 1494 | Some(form), |
| 1495 | ) |
| 1496 | .await; |
| 1497 | assert_eq!(status, 403, "reader edit post"); |
| 1498 | assert_eq!( |
| 1499 | pull(&fx, number).await.issue.title, |
| 1500 | "Add feature", |
| 1501 | "reader edit" |
| 1502 | ); |
| 1503 | |
| 1504 | let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; |
| 1505 | assert_eq!(status, 200, "author edit link"); |
| 1506 | assert!( |
| 1507 | body.contains(&format!("href=\"{uri}/edit\"")), |
| 1508 | "author edit link" |
| 1509 | ); |
| 1510 | let (status, _, body) = send( |
| 1511 | &fx.router, |
| 1512 | "GET", |
| 1513 | &format!("{uri}/edit"), |
| 1514 | Some("tahinli"), |
| 1515 | None, |
| 1516 | ) |
| 1517 | .await; |
| 1518 | assert_eq!(status, 200, "author edit form"); |
| 1519 | assert!(body.contains("Edit pull request"), "author edit form"); |
| 1520 | let form = csrf_form( |
| 1521 | "tahinli", |
| 1522 | &[("title", "Renamed pull"), ("body", "New text")], |
| 1523 | ); |
| 1524 | let (status, location, _) = send( |
| 1525 | &fx.router, |
| 1526 | "POST", |
| 1527 | &format!("{uri}/edit"), |
| 1528 | Some("tahinli"), |
| 1529 | Some(form), |
| 1530 | ) |
| 1531 | .await; |
| 1532 | assert_eq!(status, 303, "author edit"); |
| 1533 | assert_eq!(location.as_deref(), Some(uri.as_str()), "author edit"); |
| 1534 | let pr = pull(&fx, number).await; |
| 1535 | assert_eq!(pr.issue.title, "Renamed pull", "author edit"); |
| 1536 | assert_eq!(pr.issue.body, "New text", "author edit"); |
| 1537 | let events = fx.store.list_events(&pr.issue.id).await.unwrap(); |
| 1538 | assert!( |
| 1539 | events.iter().any(|e| e.kind == "renamed"), |
| 1540 | "author edit event" |
| 1541 | ); |
| 1542 | } |
| 1543 | |
| 1544 | async fn submit_thread( |
| 1545 | fx: &Fixture, |
| 1546 | number: i64, |
| 1547 | user: &User, |
| 1548 | state: ReviewState, |
| 1549 | body: &str, |
| 1550 | ) -> String { |
| 1551 | let pr = pull(fx, number).await; |
| 1552 | let root = fx |
| 1553 | .store |
| 1554 | .add_review_comment( |
| 1555 | &pr.issue.id, |
| 1556 | &user.id, |
| 1557 | true, |
| 1558 | "feat.txt", |
| 1559 | Side::New, |
| 1560 | 1, |
| 1561 | &pr.head_oid, |
| 1562 | "@@ -0,0 +1 @@\n+feature", |
| 1563 | "Please rename this", |
| 1564 | ) |
| 1565 | .await |
| 1566 | .unwrap(); |
| 1567 | fx.store |
| 1568 | .submit_review(&pr.issue.id, &user.id, state, body, &pr.head_oid) |
| 1569 | .await |
| 1570 | .unwrap(); |
| 1571 | root.id |
| 1572 | } |
| 1573 | |
| 1574 | async fn all_reviews(fx: &Fixture, number: i64) -> Vec<forge_core::Review> { |
| 1575 | let pr = pull(fx, number).await; |
| 1576 | fx.store.list_reviews(&pr.issue.id).await.unwrap() |
| 1577 | } |
| 1578 | |
| 1579 | #[tokio::test] |
| 1580 | async fn review_summary_outdated_thread_and_dismissal() { |
| 1581 | let fx = Fixture::new(Visibility::Public).await; |
| 1582 | owner_session(&fx).await; |
| 1583 | let bob = sign_in(&fx, "bob", "bob-token").await; |
| 1584 | let carol = sign_in(&fx, "carol", "carol-token").await; |
| 1585 | let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); |
| 1586 | let number = open_pull(&fx).await; |
| 1587 | let pr = pull(&fx, number).await; |
| 1588 | let root = submit_thread(&fx, number, &bob, ReviewState::Approved, "LGTM").await; |
| 1589 | fx.store |
| 1590 | .mark_outdated(&pr.issue.id, std::slice::from_ref(&root)) |
| 1591 | .await |
| 1592 | .unwrap(); |
| 1593 | |
| 1594 | let uri = format!("/tahinli/demo/pulls/{number}"); |
| 1595 | let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; |
| 1596 | assert_eq!(status, 200, "approval"); |
| 1597 | assert!(body.contains("approved these changes"), "approval: {body}"); |
| 1598 | assert!(body.contains("LGTM"), "approval: {body}"); |
| 1599 | assert!(body.contains("review-thread"), "approval: {body}"); |
| 1600 | assert!(body.contains("Please rename this"), "approval: {body}"); |
| 1601 | assert!(body.contains("feat.txt:1"), "approval: {body}"); |
| 1602 | assert!(body.contains("@@ -0,0 +1 @@"), "approval: {body}"); |
| 1603 | assert!( |
| 1604 | body.contains("<span class=\"badge\">Outdated</span>"), |
| 1605 | "approval: {body}" |
| 1606 | ); |
| 1607 | assert!(body.contains("Approved by bob"), "approval: {body}"); |
| 1608 | |
| 1609 | fx.store |
| 1610 | .submit_review( |
| 1611 | &pr.issue.id, |
| 1612 | &carol.id, |
| 1613 | ReviewState::ChangesRequested, |
| 1614 | "Needs tests", |
| 1615 | &pr.head_oid, |
| 1616 | ) |
| 1617 | .await |
| 1618 | .unwrap(); |
| 1619 | let approval = all_reviews(&fx, number).await.remove(0); |
| 1620 | let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; |
| 1621 | assert_eq!(status, 200, "summary"); |
| 1622 | assert!(body.contains("Approved by bob"), "summary: {body}"); |
| 1623 | assert!( |
| 1624 | body.contains("Changes requested by carol"), |
| 1625 | "summary: {body}" |
| 1626 | ); |
| 1627 | assert!( |
| 1628 | body.contains(&format!("action=\"{uri}/reviews/{}/dismiss\"", approval.id)), |
| 1629 | "summary: {body}" |
| 1630 | ); |
| 1631 | |
| 1632 | let (_, _, body) = send(&fx.router, "GET", &uri, Some("carol-token"), None).await; |
| 1633 | assert!(body.contains("Approved by bob"), "reader summary: {body}"); |
| 1634 | assert!(!body.contains("/dismiss\""), "reader summary: {body}"); |
| 1635 | |
| 1636 | fx.store |
| 1637 | .dismiss_review(&approval.id, &owner.id, "superseded") |
| 1638 | .await |
| 1639 | .unwrap(); |
| 1640 | let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; |
| 1641 | assert_eq!(status, 200, "dismissal"); |
| 1642 | assert!( |
| 1643 | body.contains("review-verb review-verb-dismissed"), |
| 1644 | "dismissal: {body}" |
| 1645 | ); |
| 1646 | assert!(body.contains("dismissed: superseded"), "dismissal: {body}"); |
| 1647 | assert!(body.contains("LGTM"), "dismissal: {body}"); |
| 1648 | assert!(!body.contains("Approved by bob"), "dismissal: {body}"); |
| 1649 | } |
| 1650 | |
| 1651 | #[tokio::test] |
| 1652 | async fn comment_only_review_and_hidden_pending_comments() { |
| 1653 | let fx = Fixture::new(Visibility::Public).await; |
| 1654 | owner_session(&fx).await; |
| 1655 | let bob = sign_in(&fx, "bob", "bob-token").await; |
| 1656 | let owner = fx.store.user_by_username(OWNER).await.unwrap().unwrap(); |
| 1657 | let number = open_pull(&fx).await; |
| 1658 | let pr = pull(&fx, number).await; |
| 1659 | let root = submit_thread(&fx, number, &bob, ReviewState::Commented, "").await; |
| 1660 | fx.store |
| 1661 | .reply_review_comment(&pr.issue.id, &owner.id, &root, "Done, thanks") |
| 1662 | .await |
| 1663 | .unwrap(); |
| 1664 | assert_eq!( |
| 1665 | all_reviews(&fx, number).await.len(), |
| 1666 | 2, |
| 1667 | "comment-only review" |
| 1668 | ); |
| 1669 | |
| 1670 | let uri = format!("/tahinli/demo/pulls/{number}"); |
| 1671 | let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; |
| 1672 | assert_eq!(status, 200, "comment-only review"); |
| 1673 | assert_eq!( |
| 1674 | body.matches("id=\"review-").count(), |
| 1675 | 1, |
| 1676 | "comment-only review: {body}" |
| 1677 | ); |
| 1678 | assert!(body.contains("Done, thanks"), "comment-only review: {body}"); |
| 1679 | assert!( |
| 1680 | body.contains("Please rename this"), |
| 1681 | "comment-only review: {body}" |
| 1682 | ); |
| 1683 | |
| 1684 | fx.store |
| 1685 | .add_review_comment( |
| 1686 | &pr.issue.id, |
| 1687 | &bob.id, |
| 1688 | true, |
| 1689 | "feat.txt", |
| 1690 | Side::New, |
| 1691 | 1, |
| 1692 | &pr.head_oid, |
| 1693 | "@@ -0,0 +1 @@\n+feature", |
| 1694 | "secret draft", |
| 1695 | ) |
| 1696 | .await |
| 1697 | .unwrap(); |
| 1698 | let (status, _, body) = send(&fx.router, "GET", &uri, Some("tahinli"), None).await; |
| 1699 | assert_eq!(status, 200, "hidden pending"); |
| 1700 | assert!(!body.contains("secret draft"), "hidden pending: {body}"); |
| 1701 | } |
| 1702 | } |