Skip to content

Commit

Permalink
fixed a runtime error if the coordinate environment variables are not…
Browse files Browse the repository at this point in the history
… configured correctly
  • Loading branch information
CommanderStorm committed Apr 30, 2024
1 parent 2b964b6 commit 9ae8ed5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
18 changes: 12 additions & 6 deletions server/main-api/src/calendar/fetch/connectum.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::env;
use std::{env, io};

use cached::instant::Instant;
use chrono::{DateTime, Utc};
use log::{debug, error, info, warn};
use oauth2::{AuthUrl, ClientId, ClientSecret, Scope, TokenResponse, TokenUrl};
use oauth2::basic::{BasicClient, BasicTokenResponse};
use oauth2::reqwest::async_http_client;
use oauth2::url::Url;
use oauth2::{AuthUrl, ClientId, ClientSecret, Scope, TokenResponse, TokenUrl};
use sqlx::PgPool;

use crate::calendar::fetch::CalendarEntryFetcher;
Expand Down Expand Up @@ -109,10 +109,16 @@ impl APIRequestor {
Ok(())
}
async fn fetch_oauth_token(&self) -> Result<BasicTokenResponse, crate::BoxedError> {
let client_id = env::var("TUMONLINE_OAUTH_CLIENT_ID").expect(
"please configure the environment variable TUMONLINE_OAUTH_CLIENT_ID to use this endpoint",
);
let client_secret = env::var("TUMONLINE_OAUTH_CLIENT_SECRET").expect("please configure the environment variable TUMONLINE_OAUTH_CLIENT_SECRET to use this endpoint");
let client_id = env::var("TUMONLINE_OAUTH_CLIENT_ID")
.map_err(|e| {
error!("TUMONLINE_OAUTH_CLIENT_ID needs to be set: {e:?}");
io::Error::other("please configure the environment variable TUMONLINE_OAUTH_CLIENT_ID to use this endpoint")
})?;
let client_secret = env::var("TUMONLINE_OAUTH_CLIENT_SECRET")
.map_err(|e| {
error!("TUMONLINE_OAUTH_CLIENT_SECRET needs to be set: {e:?}");
io::Error::other("please configure the environment variable TUMONLINE_OAUTH_CLIENT_SECRET to use this endpoint")
})?;

// for urls see https://review.campus.tum.de/RSYSTEM/co/public/sec/auth/realms/CAMPUSonline/.well-known/openid-configuration
let auth_url = Url::parse("https://review.campus.tum.de/RSYSTEM/co/public/sec/auth/realms/CAMPUSonline/protocol/openid-connect/auth")?;
Expand Down
31 changes: 21 additions & 10 deletions server/main-api/src/feedback/github.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use actix_web::HttpResponse;

use log::error;
use octocrab::Octocrab;
use regex::Regex;

fn github_token() -> String {
std::env::var("GITHUB_TOKEN")
.expect("GITHUB_TOKEN to be set")
.trim()
.to_string()
fn github_token() -> Result<String,()> {
match std::env::var("GITHUB_TOKEN"){
Ok(token)=> Ok(token.trim().to_string()),
Err(e)=>{
format!("GITHUB_TOKEN has to be set for feedback: {e:?}");
Err(())
},
}

}

pub async fn open_issue(title: &str, description: &str, labels: Vec<String>) -> HttpResponse {
Expand All @@ -20,8 +23,10 @@ pub async fn open_issue(title: &str, description: &str, labels: Vec<String>) ->
.content_type("text/plain")
.body("Subject or body missing or too short");
}

let octocrab = match Octocrab::builder().personal_token(github_token()).build() {
let Ok(personal_token)=github_token() else {
return HttpResponse::InternalServerError().content_type("text/plain").body("Failed to create issue");
};
let octocrab = match Octocrab::builder().personal_token(personal_token).build() {
Err(e) => {
error!("Could not create Octocrab instance: {e:?}");
return HttpResponse::InternalServerError().body("Failed to create issue");
Expand Down Expand Up @@ -56,7 +61,12 @@ pub async fn open_pr(
description: &str,
labels: Vec<String>,
) -> HttpResponse {
let octocrab = match Octocrab::builder().personal_token(github_token()).build() {
let Ok(personal_token)=github_token() else {
return HttpResponse::InternalServerError()
.content_type("text/plain")
.body("Failed to create a pull request");
};
let octocrab = match Octocrab::builder().personal_token(personal_token).build() {
Err(e) => {
error!("Could not create Octocrab instance: {e:?}");
return HttpResponse::InternalServerError().body("Failed to create a pull request");
Expand Down Expand Up @@ -121,9 +131,10 @@ fn clean_feedback_data(s: &str, len: usize) -> String {

#[cfg(test)]
mod description_tests {
use super::*;
use pretty_assertions::assert_eq;

use super::*;

#[test]
fn newlines_whitespace() {
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion server/main-api/src/feedback/proposed_edits/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Coordinate {
let Range { start, end } = f.matches();
end - start
})
.expect("No matching file found");
.expect("No matching file found, which is impossible");
best_match.path
}
}
Expand Down

0 comments on commit 9ae8ed5

Please sign in to comment.