Skip to content

Commit c9dd46e

Browse files
committed
Extract repository authorization checking into utils
1 parent 614aef7 commit c9dd46e

File tree

4 files changed

+45
-77
lines changed

4 files changed

+45
-77
lines changed

src/gh_changes_since.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ use axum::{
77
};
88
use hyper::StatusCode;
99

10-
use crate::{github, handlers::Context, utils::AppError};
10+
use crate::{
11+
github,
12+
handlers::Context,
13+
utils::{AppError, is_repo_autorized},
14+
};
1115

1216
/// Redirects to either `/gh-range-diff` (when the base changed) or to GitHub's compare
1317
/// page (when the base is the same).
@@ -25,26 +29,10 @@ pub async fn gh_changes_since(
2529
.into_response());
2630
};
2731

28-
let repos = ctx
29-
.team
30-
.repos()
31-
.await
32-
.context("unable to retrieve team repos")?;
33-
34-
// Verify that the request org is part of the Rust project
35-
let Some(repos) = repos.repos.get(&owner) else {
32+
if !is_repo_autorized(&ctx, &owner, &repo).await? {
3633
return Ok((
37-
StatusCode::BAD_REQUEST,
38-
format!("organization `{owner}` is not part of the Rust Project team repos"),
39-
)
40-
.into_response());
41-
};
42-
43-
// Verify that the request repo is part of the Rust project
44-
if !repos.iter().any(|r| r.name == repo) {
45-
return Ok((
46-
StatusCode::BAD_REQUEST,
47-
format!("repository `{owner}` is not part of the Rust Project team repos"),
34+
StatusCode::UNAUTHORIZED,
35+
format!("repository `{owner}/{repo}` is not part of the Rust Project team repos"),
4836
)
4937
.into_response());
5038
}

src/gh_range_diff.rs

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use pulldown_cmark_escape::FmtWriter;
2121
use regex::Regex;
2222

2323
use crate::github::GithubCompare;
24+
use crate::utils::is_repo_autorized;
2425
use crate::{github, handlers::Context, utils::AppError};
2526

2627
static MARKER_RE: LazyLock<Regex> =
@@ -42,27 +43,11 @@ pub async fn gh_range_diff(
4243
));
4344
};
4445

45-
let repos = ctx
46-
.team
47-
.repos()
48-
.await
49-
.context("unable to retrieve team repos")?;
50-
51-
// Verify that the request org is part of the Rust project
52-
let Some(repos) = repos.repos.get(&owner) else {
53-
return Ok((
54-
StatusCode::BAD_REQUEST,
55-
HeaderMap::new(),
56-
format!("organization `{owner}` is not part of the Rust Project team repos"),
57-
));
58-
};
59-
60-
// Verify that the request repo is part of the Rust project
61-
if !repos.iter().any(|r| r.name == repo) {
46+
if !is_repo_autorized(&ctx, &owner, &repo).await? {
6247
return Ok((
63-
StatusCode::BAD_REQUEST,
48+
StatusCode::UNAUTHORIZED,
6449
HeaderMap::new(),
65-
format!("repository `{owner}` is not part of the Rust Project team repos"),
50+
format!("repository `{owner}/{repo}` is not part of the Rust Project team repos"),
6651
));
6752
}
6853

@@ -166,27 +151,11 @@ pub async fn gh_ranges_diff(
166151
));
167152
};
168153

169-
let repos = ctx
170-
.team
171-
.repos()
172-
.await
173-
.context("unable to retrieve team repos")?;
174-
175-
// Verify that the request org is part of the Rust project
176-
let Some(repos) = repos.repos.get(&owner) else {
177-
return Ok((
178-
StatusCode::BAD_REQUEST,
179-
HeaderMap::new(),
180-
format!("organization `{owner}` is not part of the Rust Project team repos"),
181-
));
182-
};
183-
184-
// Verify that the request repo is part of the Rust project
185-
if !repos.iter().any(|r| r.name == repo) {
154+
if !is_repo_autorized(&ctx, &owner, &repo).await? {
186155
return Ok((
187-
StatusCode::BAD_REQUEST,
156+
StatusCode::UNAUTHORIZED,
188157
HeaderMap::new(),
189-
format!("repository `{owner}` is not part of the Rust Project team repos"),
158+
format!("repository `{owner}/{repo}` is not part of the Rust Project team repos"),
190159
));
191160
}
192161

src/gha_logs.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::github::{self, WorkflowRunJob};
22
use crate::handlers::Context;
33
use crate::interactions::REPORT_TO;
4-
use crate::utils::AppError;
4+
use crate::utils::{AppError, is_repo_autorized};
55
use anyhow::Context as _;
66
use axum::extract::{Path, State};
77
use axum::http::HeaderValue;
@@ -71,25 +71,11 @@ pub async fn gha_logs(
7171
Path((owner, repo, log_id)): Path<(String, String, u128)>,
7272
State(ctx): State<Arc<Context>>,
7373
) -> axum::response::Result<impl IntoResponse, AppError> {
74-
let repos = ctx
75-
.team
76-
.repos()
77-
.await
78-
.context("unable to retrieve team repos")?;
79-
80-
let Some(repos) = repos.repos.get(&owner) else {
81-
return Ok((
82-
StatusCode::BAD_REQUEST,
83-
HeaderMap::new(),
84-
format!("organization `{owner}` is not part of the Rust Project team repos"),
85-
));
86-
};
87-
88-
if !repos.iter().any(|r| r.name == repo) {
74+
if !is_repo_autorized(&ctx, &owner, &repo).await? {
8975
return Ok((
90-
StatusCode::BAD_REQUEST,
76+
StatusCode::UNAUTHORIZED,
9177
HeaderMap::new(),
92-
format!("repository `{owner}` is not part of the Rust Project team repos"),
78+
format!("repository `{owner}/{repo}` is not part of the Rust Project team repos"),
9379
));
9480
}
9581

src/utils.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::interactions::REPORT_TO;
1+
use crate::{handlers::Context, interactions::REPORT_TO};
22

3+
use anyhow::Context as _;
34
use axum::{
45
http::StatusCode,
56
response::{IntoResponse, Response},
@@ -36,3 +37,27 @@ where
3637
AppError(err.into())
3738
}
3839
}
40+
41+
pub(crate) async fn is_repo_autorized(
42+
ctx: &Context,
43+
owner: &str,
44+
repo: &str,
45+
) -> anyhow::Result<bool> {
46+
let repos = ctx
47+
.team
48+
.repos()
49+
.await
50+
.context("unable to retrieve team repos")?;
51+
52+
// Verify that the request org is part of the Rust project
53+
let Some(repos) = repos.repos.get(owner) else {
54+
return Ok(false);
55+
};
56+
57+
// Verify that the request repo is part of the Rust project
58+
if !repos.iter().any(|r| r.name == repo) {
59+
return Ok(false);
60+
}
61+
62+
Ok(true)
63+
}

0 commit comments

Comments
 (0)