Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reload config #55

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/bors/handlers/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub async fn refresh_repository<Client: RepositoryClient>(
) -> anyhow::Result<()> {
let res = cancel_timed_out_builds(repo, db).await;
reload_permission(repo).await;
reload_config(repo).await?;

res
}
Expand Down Expand Up @@ -57,6 +58,14 @@ async fn reload_permission<Client: RepositoryClient>(repo: &mut RepositoryState<
repo.permissions_resolver.reload().await
}

async fn reload_config<Client: RepositoryClient>(
repo: &mut RepositoryState<Client>,
) -> anyhow::Result<()> {
let config = repo.client.load_config().await?;
repo.config = config;
Ok(())
}

#[cfg(not(test))]
fn now() -> DateTime<Utc> {
Utc::now()
Expand Down
3 changes: 3 additions & 0 deletions src/bors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub use handlers::handle_bors_event;
pub trait RepositoryClient {
fn repository(&self) -> &GithubRepoName;

/// load repository config
async fn load_config(&mut self) -> anyhow::Result<RepositoryConfig>;

/// Return the current SHA of the given branch.
async fn get_branch_sha(&mut self, name: &str) -> anyhow::Result<CommitSha>;

Expand Down
36 changes: 36 additions & 0 deletions src/github/api/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use anyhow::Context;
use axum::async_trait;
use base64::Engine;
use octocrab::models::{Repository, RunId};
use octocrab::{Error, Octocrab};
use tracing::log;

use crate::bors::{CheckSuite, CheckSuiteStatus, RepositoryClient};
use crate::config::{RepositoryConfig, CONFIG_FILE_PATH};
use crate::github::api::base_github_url;
use crate::github::api::operations::{merge_branches, set_branch_to_commit, MergeError};
use crate::github::{Branch, CommitSha, GithubRepoName, PullRequest, PullRequestNumber};
Expand Down Expand Up @@ -40,6 +42,40 @@ impl RepositoryClient for GithubRepositoryClient {
self.name()
}

/// Loads repository configuration from a file located at `[CONFIG_FILE_PATH]` in the main
/// branch.
async fn load_config(&mut self) -> anyhow::Result<RepositoryConfig> {
let mut response = self
.client
.repos(&self.repo_name.owner, &self.repo_name.name)
.get_content()
.path(CONFIG_FILE_PATH)
.send()
.await
.map_err(|error| {
anyhow::anyhow!(
"Could not fetch {CONFIG_FILE_PATH} from {}: {error:?}",
self.repo_name
)
})?;

let engine = base64::engine::general_purpose::STANDARD;
response
.take_items()
.into_iter()
.next()
.and_then(|content| content.content)
.ok_or_else(|| anyhow::anyhow!("Configuration file not found"))
.and_then(|content| Ok(engine.decode(content.trim())?))
.and_then(|content| Ok(String::from_utf8(content)?))
.and_then(|content| {
let config: RepositoryConfig = toml::from_str(&content).map_err(|error| {
anyhow::anyhow!("Could not deserialize repository config: {error:?}")
})?;
Ok(config)
})
}

async fn get_branch_sha(&mut self, name: &str) -> anyhow::Result<CommitSha> {
// https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#get-a-branch
let branch: octocrab::models::repos::Branch = self
Expand Down
51 changes: 8 additions & 43 deletions src/github/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::future::Future;
use std::pin::Pin;

use anyhow::Context;
use base64::Engine;
use octocrab::models::{App, AppId, InstallationRepositories, Repository};
use octocrab::Octocrab;
use secrecy::{ExposeSecret, SecretVec};
Expand All @@ -12,8 +11,7 @@ use url::Url;
use client::GithubRepositoryClient;

use crate::bors::event::PullRequestComment;
use crate::bors::{BorsState, RepositoryState};
use crate::config::{RepositoryConfig, CONFIG_FILE_PATH};
use crate::bors::{BorsState, RepositoryClient, RepositoryState};
use crate::database::{DbClient, SeaORMClient};
use crate::github::GithubRepoName;
use crate::permissions::TeamApiPermissionResolver;
Expand Down Expand Up @@ -142,7 +140,13 @@ async fn create_repo_state(
let name = GithubRepoName::new(&owner.login, &repo.name);
tracing::info!("Found repository {name}");

let config = match load_repository_config(&repo_client, &name).await {
let mut client = GithubRepositoryClient {
client: repo_client,
repo_name: name.clone(),
repository: repo,
};

let config = match client.load_config().await {
Ok(config) => {
tracing::info!("Loaded repository config for {name}: {config:#?}");
config
Expand All @@ -158,12 +162,6 @@ async fn create_repo_state(
.await
.map_err(|error| anyhow::anyhow!("Could not load permissions for {name}: {error:?}"))?;

let client = GithubRepositoryClient {
client: repo_client,
repo_name: name.clone(),
repository: repo,
};

Ok(RepositoryState {
repository: name,
client,
Expand All @@ -172,39 +170,6 @@ async fn create_repo_state(
})
}

/// Loads repository configuration from a file located at `[CONFIG_FILE_PATH]` in the main
/// branch.
async fn load_repository_config(
gh_client: &Octocrab,
repo: &GithubRepoName,
) -> anyhow::Result<RepositoryConfig> {
let mut response = gh_client
.repos(&repo.owner, &repo.name)
.get_content()
.path(CONFIG_FILE_PATH)
.send()
.await
.map_err(|error| {
anyhow::anyhow!("Could not fetch {CONFIG_FILE_PATH} from {repo}: {error:?}")
})?;

let engine = base64::engine::general_purpose::STANDARD;
response
.take_items()
.into_iter()
.next()
.and_then(|content| content.content)
.ok_or_else(|| anyhow::anyhow!("Configuration file not found"))
.and_then(|content| Ok(engine.decode(content.trim())?))
.and_then(|content| Ok(String::from_utf8(content)?))
.and_then(|content| {
let config: RepositoryConfig = toml::from_str(&content).map_err(|error| {
anyhow::anyhow!("Could not deserialize repository config: {error:?}")
})?;
Ok(config)
})
}

impl BorsState<GithubRepositoryClient> for GithubAppState {
fn is_comment_internal(&self, comment: &PullRequestComment) -> bool {
comment.author.html_url == self.app.html_url
Expand Down
4 changes: 4 additions & 0 deletions src/tests/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,8 @@ impl RepositoryClient for TestRepositoryClient {
.extend(labels.to_vec());
Ok(())
}

async fn load_config(&mut self) -> anyhow::Result<RepositoryConfig> {
Ok(RepoConfigBuilder::default().create())
}
}
Loading