Skip to content

Commit

Permalink
fix: we don't need repository ownership (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolftousen authored Aug 16, 2024
1 parent 70c2b4d commit 4c93fb6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 80 deletions.
8 changes: 1 addition & 7 deletions src/helpers/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ pub struct ResponseRecord {
pub total_cycle_time: Option<f32>
}

#[derive(Serialize, Debug, Clone, Default)]
pub struct Team {
pub name: String,
pub repositories: Vec<String>
}

#[derive(Serialize, Debug, Default, Clone)]
pub struct TeamsResponse {
pub teams: Vec<Team>
pub teams: Vec<String>
}
96 changes: 23 additions & 73 deletions src/routes/teams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,21 @@ use dashmap::DashMap;
use anyhow::{Result, anyhow};
use reqwest::Error;

use crate::helpers::response::{Team, TeamsResponse};
use crate::helpers::response::TeamsResponse;

#[derive(Deserialize, Debug, Clone)]
pub struct GitHubTeam {
id: u64,
name: String,
}

#[derive(Deserialize, Debug, Clone)]
pub struct GitHubRepository {
name: String,
}

pub type TeamsCache = Arc<DashMap<String, TeamsResponse>>;

async fn get_teams(gh_org: &String, gh_token: &String) -> Result<Vec<GitHubTeam>> {
async fn get_teams(gh_org: &String, gh_token: &String, page: usize) -> Result<Vec<GitHubTeam>> {
let client = reqwest::Client::new();
let url = format!("https://api.github.com/orgs/{}/teams", gh_org);

let response_result = client.get(url)
.query(&[("page", page), ("per_page", 100)])
.header("User-Agent", "request")
.header("Authorization", format!("token {}", gh_token))
.header("Accept", "application/vnd.github+json")
Expand Down Expand Up @@ -60,44 +55,6 @@ async fn get_teams(gh_org: &String, gh_token: &String) -> Result<Vec<GitHubTeam>
}
}

async fn get_repositories(team: GitHubTeam, gh_org: &String, gh_token: &String) -> Result<Vec<GitHubRepository>> {
let client = reqwest::Client::new();
let url = format!("https://api.github.com/teams/{}/repos", team.id);

let response_result = client.get(url)
.header("User-Agent", "request")
.header("Authorization", format!("token {}", gh_token))
.header("Accept", "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28")
.send()
.await;

match response_result {
Ok(response) => {
let status = response.status();

if !status.is_success() {
tracing::error!("GitHub Repositories Request Responded with status: {:?}", status);
return Err(anyhow!(format!("GitHUb responsed with status: {:?}", status)));
}

let parse_result: Result<Vec<GitHubRepository>, Error> = response.json().await;

match parse_result {
Ok(value) => return Ok(value),
Err(e) => {
tracing::error!("GitHub Repositories Response Parsing Failed: {:?}", e);
return Err(e.into());
}
}
},
Err(e) => {
tracing::error!("GitHub Repositories Request Failed: {:?}", e);
return Err(e.into());
}
}
}


pub async fn handle_request(Extension(cache): Extension<TeamsCache>) -> Result<Json<TeamsResponse>, StatusCode> {
let request_key = format!("teams");
Expand Down Expand Up @@ -126,38 +83,31 @@ pub async fn handle_request(Extension(cache): Extension<TeamsCache>) -> Result<J
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
};

let teams_result = get_teams(&gh_org, &gh_token).await;

match teams_result {
Ok(teams) => {
for team in teams {
let repositories_result = get_repositories(team.clone(), &gh_org, &gh_token).await;

match repositories_result {
Ok(repositories) => {
let repository_names = repositories.into_iter().map(|e| e.name).collect();

let new_team: Team = Team {
name: team.name.clone(),
repositories: repository_names
};

response.teams.push(new_team);
},
Err(e) => {
tracing::error!("Failed to get repositories for team: {}", team.name);
}

let mut page = 1;
let mut all_teams: Vec<GitHubTeam> = [].to_vec();

loop {
let team_result = get_teams(&gh_org, &gh_token, page).await;

match team_result {
Ok(mut teams) => {
if teams.len() > 0 {
all_teams.append(&mut teams);
page = page + 1;
} else {
break;
}
}
},
Err(e) => {
tracing::error!("GitHub Request Failed");
return Err(StatusCode::INTERNAL_SERVER_ERROR);
Err(e) => {
tracing::error!("GitHub Request Failed");
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
}
}


response.teams = all_teams.iter().map(|team| team.name.clone()).collect();

cache.insert(request_key, response.clone());
Ok(Json(response))
}

0 comments on commit 4c93fb6

Please sign in to comment.