| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::path::PathBuf; |
| 7 | |
| 8 | use forge_core::authz::{Action, authorize}; |
| 9 | use forge_core::{Repo, Role, Session, User, Visibility, WatchLevel}; |
| 10 | use forge_git::repo::repo_path; |
| 11 | use http::{HeaderValue, header::LOCATION}; |
| 12 | use topcoat::Result; |
| 13 | use topcoat::context::{Cx, app_context}; |
| 14 | use topcoat::router::error::not_found; |
| 15 | use topcoat::router::request::uri; |
| 16 | use topcoat::router::response::Response; |
| 17 | use topcoat::router::{Body, Layer, LayerFuture, Next, Path, StatusCode}; |
| 18 | |
| 19 | use crate::auth::current_user; |
| 20 | use crate::i18n::{self, Lang}; |
| 21 | use crate::pages::repo_mirrors::{redacted_url, stamp_in_tz}; |
| 22 | use crate::state::AppState; |
| 23 | |
| 24 | pub struct RepoCtx { |
| 25 | pub repo: Repo, |
| 26 | |
| 27 | pub path: PathBuf, |
| 28 | pub viewer: Option<(User, Session)>, |
| 29 | |
| 30 | pub role: Option<Role>, |
| 31 | |
| 32 | |
| 33 | pub org_role: Option<Role>, |
| 34 | |
| 35 | pub lang: Lang, |
| 36 | } |
| 37 | |
| 38 | |
| 39 | #[derive(Clone)] |
| 40 | pub struct StarButton { |
| 41 | pub starred: bool, |
| 42 | |
| 43 | pub csrf: String, |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | #[derive(Clone)] |
| 49 | pub struct RepoHead { |
| 50 | |
| 51 | pub lang: Lang, |
| 52 | pub owner: String, |
| 53 | pub name: String, |
| 54 | pub default_branch: String, |
| 55 | pub visibility: Visibility, |
| 56 | pub description: String, |
| 57 | pub archived: bool, |
| 58 | |
| 59 | |
| 60 | pub active: &'static str, |
| 61 | |
| 62 | pub can_admin: bool, |
| 63 | |
| 64 | pub open_issues: u64, |
| 65 | |
| 66 | pub open_pulls: u64, |
| 67 | |
| 68 | |
| 69 | pub forked_from: Option<(String, String)>, |
| 70 | |
| 71 | pub can_fork: bool, |
| 72 | pub stars: u64, |
| 73 | |
| 74 | pub star: Option<StarButton>, |
| 75 | |
| 76 | pub watchers: u64, |
| 77 | |
| 78 | pub watch: Option<WatchButton>, |
| 79 | |
| 80 | pub mirror: Option<MirrorSource>, |
| 81 | |
| 82 | pub discussions: bool, |
| 83 | |
| 84 | pub wiki: bool, |
| 85 | |
| 86 | pub security: bool, |
| 87 | } |
| 88 | |
| 89 | |
| 90 | |
| 91 | #[derive(Clone)] |
| 92 | pub struct MirrorSource { |
| 93 | pub url: String, |
| 94 | pub last_sync: Option<String>, |
| 95 | pub last_ok: Option<bool>, |
| 96 | } |
| 97 | |
| 98 | |
| 99 | #[derive(Clone)] |
| 100 | pub struct WatchButton { |
| 101 | pub level: WatchLevel, |
| 102 | pub csrf: String, |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
| 107 | #[derive(Debug)] |
| 108 | pub struct RepoMoved { |
| 109 | pub location: String, |
| 110 | } |
| 111 | |
| 112 | impl std::fmt::Display for RepoMoved { |
| 113 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 114 | f.write_str("repository moved") |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl std::error::Error for RepoMoved {} |
| 119 | |
| 120 | impl RepoCtx { |
| 121 | |
| 122 | pub fn can_delete(&self) -> bool { |
| 123 | let actor = self.viewer.as_ref().map(|(user, _)| user); |
| 124 | authorize(actor, &self.repo, None, self.org_role, Action::Delete).is_ok() |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | pub fn can_admin(&self) -> bool { |
| 130 | let actor = self.viewer.as_ref().map(|(user, _)| user); |
| 131 | authorize(actor, &self.repo, self.role, self.org_role, Action::Admin).is_ok() |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | pub fn can_write(&self) -> bool { |
| 137 | let actor = self.viewer.as_ref().map(|(user, _)| user); |
| 138 | !self.repo.archived |
| 139 | && authorize(actor, &self.repo, self.role, self.org_role, Action::Write).is_ok() |
| 140 | } |
| 141 | |
| 142 | |
| 143 | pub async fn open_issues(&self, state: &AppState) -> Result<u64> { |
| 144 | Ok(state.store.issue_counts(&self.repo.id).await?.0) |
| 145 | } |
| 146 | |
| 147 | |
| 148 | async fn forked_from(&self, state: &AppState) -> Result<Option<(String, String)>> { |
| 149 | let Some(source_id) = &self.repo.fork_of else { |
| 150 | return Ok(None); |
| 151 | }; |
| 152 | let Some(source) = state.store.repo_by_id(source_id).await? else { |
| 153 | return Ok(None); |
| 154 | }; |
| 155 | let (role, org_role) = match &self.viewer { |
| 156 | Some((user, _)) => ( |
| 157 | state.store.collaborator_role(&source.id, &user.id).await?, |
| 158 | state |
| 159 | .store |
| 160 | .org_repo_role(&source.owner_id, &user.id, &source.id) |
| 161 | .await?, |
| 162 | ), |
| 163 | None => (None, None), |
| 164 | }; |
| 165 | let actor = self.viewer.as_ref().map(|(user, _)| user); |
| 166 | if authorize(actor, &source, role, org_role, Action::Read).is_err() { |
| 167 | return Ok(None); |
| 168 | } |
| 169 | Ok(Some((source.owner_name, source.name))) |
| 170 | } |
| 171 | |
| 172 | |
| 173 | |
| 174 | pub async fn head(&self, state: &AppState, active: &'static str) -> Result<RepoHead> { |
| 175 | let stars = state.store.star_count(&self.repo.id).await?; |
| 176 | let open_pulls = state.store.pull_counts(&self.repo.id).await?.0; |
| 177 | let forked_from = self.forked_from(state).await?; |
| 178 | let can_fork = self |
| 179 | .viewer |
| 180 | .as_ref() |
| 181 | .is_some_and(|(user, _)| user.id != self.repo.owner_id); |
| 182 | let star = match &self.viewer { |
| 183 | Some((user, session)) => Some(StarButton { |
| 184 | starred: state.store.is_starred(&user.id, &self.repo.id).await?, |
| 185 | csrf: session.csrf_token.clone(), |
| 186 | }), |
| 187 | None => None, |
| 188 | }; |
| 189 | let watchers = |
| 190 | u64::try_from(state.store.watchers(&self.repo.id).await?.len()).unwrap_or(u64::MAX); |
| 191 | let watch = match &self.viewer { |
| 192 | Some((user, session)) => Some(WatchButton { |
| 193 | level: state.store.watch_level(&user.id, &self.repo.id).await?, |
| 194 | csrf: session.csrf_token.clone(), |
| 195 | }), |
| 196 | None => None, |
| 197 | }; |
| 198 | let discussions = state.store.discussions_enabled(&self.repo.id).await?; |
| 199 | let wiki = state.store.wiki_enabled(&self.repo.id).await?; |
| 200 | let security = |
| 201 | state.config.advisories.enabled && crate::pages::repo_security::can_write(self); |
| 202 | let mirror = state |
| 203 | .store |
| 204 | .pull_mirror(&self.repo.id) |
| 205 | .await? |
| 206 | .map(|mirror| MirrorSource { |
| 207 | url: redacted_url(&mirror.url), |
| 208 | last_sync: mirror |
| 209 | .last_sync_at |
| 210 | .as_deref() |
| 211 | .map(|stamp| stamp_in_tz(stamp, self.viewer_tz())), |
| 212 | last_ok: mirror.last_ok, |
| 213 | }); |
| 214 | Ok(RepoHead { |
| 215 | lang: self.lang, |
| 216 | owner: self.repo.owner_name.clone(), |
| 217 | name: self.repo.name.clone(), |
| 218 | default_branch: self.repo.default_branch.clone(), |
| 219 | visibility: self.repo.visibility, |
| 220 | description: self.repo.description.clone(), |
| 221 | archived: self.repo.archived, |
| 222 | active, |
| 223 | can_admin: self.can_admin(), |
| 224 | open_issues: self.open_issues(state).await?, |
| 225 | open_pulls, |
| 226 | forked_from, |
| 227 | can_fork, |
| 228 | stars, |
| 229 | star, |
| 230 | watchers, |
| 231 | watch, |
| 232 | mirror, |
| 233 | discussions, |
| 234 | wiki, |
| 235 | security, |
| 236 | }) |
| 237 | } |
| 238 | |
| 239 | |
| 240 | pub fn viewer_tz(&self) -> &str { |
| 241 | self.viewer |
| 242 | .as_ref() |
| 243 | .map_or("UTC", |(user, _)| user.timezone.as_str()) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | |
| 248 | pub async fn load_repo(cx: &Cx, owner: &str, name: &str) -> Result<RepoCtx> { |
| 249 | let state = app_context::<AppState>(cx); |
| 250 | let viewer = current_user(cx).await?; |
| 251 | |
| 252 | let Some(repo) = state.store.repo_by_owner_and_name(owner, name).await? else { |
| 253 | if let Some(location) = moved_location(cx, state, viewer.as_ref(), owner, name).await? { |
| 254 | return Err(RepoMoved { location }.into()); |
| 255 | } |
| 256 | return Err(not_found().into()); |
| 257 | }; |
| 258 | let (role, org_role) = match &viewer { |
| 259 | Some((user, _)) => ( |
| 260 | state.store.collaborator_role(&repo.id, &user.id).await?, |
| 261 | state |
| 262 | .store |
| 263 | .org_repo_role(&repo.owner_id, &user.id, &repo.id) |
| 264 | .await?, |
| 265 | ), |
| 266 | None => (None, None), |
| 267 | }; |
| 268 | let actor = viewer.as_ref().map(|(user, _)| user); |
| 269 | if authorize(actor, &repo, role, org_role, Action::Read).is_err() { |
| 270 | return Err(not_found().into()); |
| 271 | } |
| 272 | let path = repo_path(&state.config.data_dir, &repo.id)?; |
| 273 | let lang = i18n::lang(cx, viewer.as_ref().map(|(user, _)| user)); |
| 274 | Ok(RepoCtx { |
| 275 | repo, |
| 276 | path, |
| 277 | viewer, |
| 278 | role, |
| 279 | org_role, |
| 280 | lang, |
| 281 | }) |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | async fn moved_location( |
| 288 | cx: &Cx, |
| 289 | state: &AppState, |
| 290 | viewer: Option<&(User, Session)>, |
| 291 | owner: &str, |
| 292 | name: &str, |
| 293 | ) -> Result<Option<String>> { |
| 294 | let Some(target) = state.store.repo_by_redirect(owner, name).await? else { |
| 295 | return Ok(None); |
| 296 | }; |
| 297 | let (role, org_role) = match viewer { |
| 298 | Some((user, _)) => ( |
| 299 | state.store.collaborator_role(&target.id, &user.id).await?, |
| 300 | state |
| 301 | .store |
| 302 | .org_repo_role(&target.owner_id, &user.id, &target.id) |
| 303 | .await?, |
| 304 | ), |
| 305 | None => (None, None), |
| 306 | }; |
| 307 | let actor = viewer.map(|(user, _)| user); |
| 308 | if authorize(actor, &target, role, org_role, Action::Read).is_err() { |
| 309 | return Ok(None); |
| 310 | } |
| 311 | |
| 312 | let request = uri(cx); |
| 313 | let rest = request |
| 314 | .path() |
| 315 | .splitn(4, '/') |
| 316 | .nth(3) |
| 317 | .map(|rest| format!("/{rest}")) |
| 318 | .unwrap_or_default(); |
| 319 | let query = request |
| 320 | .query() |
| 321 | .map(|query| format!("?{query}")) |
| 322 | .unwrap_or_default(); |
| 323 | Ok(Some(format!( |
| 324 | "/{}/{}{rest}{query}", |
| 325 | target.owner_name, target.name |
| 326 | ))) |
| 327 | } |
| 328 | |
| 329 | |
| 330 | |
| 331 | pub struct RepoMovedLayer; |
| 332 | |
| 333 | impl Layer for RepoMovedLayer { |
| 334 | fn path(&self) -> Option<&Path> { |
| 335 | None |
| 336 | } |
| 337 | |
| 338 | fn handle<'a>(&'a self, cx: &'a Cx, body: Body, next: Next<'a>) -> LayerFuture<'a> { |
| 339 | Box::pin(repo_moved(cx, body, next)) |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | |
| 344 | |
| 345 | async fn repo_moved(cx: &Cx, body: Body, next: Next<'_>) -> Result<Response> { |
| 346 | match next.run(cx, body).await { |
| 347 | Ok(response) => Ok(response), |
| 348 | Err(error) => match error.downcast_ref::<RepoMoved>() { |
| 349 | Some(moved) => { |
| 350 | let mut response = Response::new(Body::empty()); |
| 351 | *response.status_mut() = StatusCode::MOVED_PERMANENTLY; |
| 352 | |
| 353 | |
| 354 | let Ok(location) = HeaderValue::from_str(&moved.location) else { |
| 355 | return Err(not_found().into()); |
| 356 | }; |
| 357 | response.headers_mut().insert(LOCATION, location); |
| 358 | Ok(response) |
| 359 | } |
| 360 | None => Err(error), |
| 361 | }, |
| 362 | } |
| 363 | } |