Skip to content

Commit 9b07bda

Browse files
authored
Add endpoint to list usernames of ITP starters (#67)
1 parent 451fa32 commit 9b07bda

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

src/bin/trainee-tracker.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ async fn main() {
110110
"/api/expected-attendance",
111111
get(trainee_tracker::endpoints::expected_attendance),
112112
)
113+
.route(
114+
"/api/started-itp",
115+
get(trainee_tracker::endpoints::started_itp),
116+
)
113117
.route(
114118
"/codility/verify-webhook",
115119
post(trainee_tracker::codility::verify_webhook),

src/endpoints.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{collections::BTreeMap, ops::AddAssign};
1+
use std::{
2+
collections::{BTreeMap, BTreeSet},
3+
ops::AddAssign,
4+
};
25

36
use ::octocrab::models::{Author, teams::RequestedTeam};
47
use anyhow::Context;
@@ -18,7 +21,7 @@ use crate::{
1821
Error, ServerState,
1922
github_accounts::get_trainees,
2023
newtypes::GithubLogin,
21-
octocrab::{all_pages, octocrab},
24+
octocrab::{all_pages, octocrab, octocrab_for_maybe_token},
2225
prs::{PrWithReviews, fill_in_reviewers, get_prs},
2326
register::{Attendance, get_register},
2427
sheets::sheets_client,
@@ -331,3 +334,30 @@ pub async fn expected_attendance(
331334
}
332335
Json(expected_attendance)
333336
}
337+
338+
pub async fn started_itp(
339+
session: Session,
340+
State(server_state): State<ServerState>,
341+
OriginalUri(original_uri): OriginalUri,
342+
) -> Result<Json<BTreeSet<GithubLogin>>, Error> {
343+
let octocrab = octocrab(&session, &server_state, original_uri).await;
344+
// Allow un-authenticated requests to this endpoint.
345+
let octocrab = if let Ok(octocrab) = octocrab {
346+
octocrab
347+
} else {
348+
octocrab_for_maybe_token(None)?
349+
};
350+
let prs = all_pages("pull requests", &octocrab, async || {
351+
octocrab
352+
.pulls(server_state.config.github_org, "Module-Onboarding")
353+
.list()
354+
.send()
355+
.await
356+
})
357+
.await?;
358+
let usernames: BTreeSet<_> = prs
359+
.into_iter()
360+
.filter_map(|pr| Some(GithubLogin::from(pr.user?.login)))
361+
.collect();
362+
Ok(Json(usernames))
363+
}

src/octocrab.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ pub(crate) async fn octocrab(
3939
}
4040

4141
pub fn octocrab_for_token(token: String) -> Result<Octocrab, Error> {
42+
octocrab_for_maybe_token(Some(token))
43+
}
44+
45+
pub fn octocrab_for_maybe_token(token: Option<String>) -> Result<Octocrab, Error> {
46+
let header_value = if let Some(token) = token {
47+
Some(
48+
HeaderValue::from_str(&format!("Bearer {token}"))
49+
.context("Token couldn't used as a header")?,
50+
)
51+
} else {
52+
None
53+
};
54+
4255
let connector = HttpsConnectorBuilder::new()
4356
.with_webpki_roots()
4457
.https_only()
@@ -67,10 +80,7 @@ pub fn octocrab_for_token(token: String) -> Result<Octocrab, Error> {
6780
HeaderValue::from_static("octocrab"),
6881
)])))
6982
.with_layer(&AuthHeaderLayer::new(
70-
Some(
71-
HeaderValue::from_str(&format!("Bearer {token}"))
72-
.context("Token couldn't used as a header")?,
73-
),
83+
header_value,
7484
Uri::from_static(GITHUB_BASE_URI),
7585
Uri::from_static(GITHUB_BASE_UPLOAD_URI),
7686
))

0 commit comments

Comments
 (0)