about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-03-09 00:11:10 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-03-09 13:44:46 +0100
commit90a1b14f4f310a2e518bc9bc19253d42b4cb6bed (patch)
treead29ce16fbeff06bab50a1012bdbbb59b2a791fc
parent{modules,tests}/back: Update to deal with newest back (diff)
downloadnixos-server-90a1b14f4f310a2e518bc9bc19253d42b4cb6bed.zip
pkgs/back: Do not store repositories in config
Otherwise, back will need to be restarted every time a new repository is
added or removed.
-rw-r--r--pkgs/by-name/ba/back/src/config/mod.rs72
-rw-r--r--pkgs/by-name/ba/back/src/web/generate/mod.rs82
2 files changed, 62 insertions, 92 deletions
diff --git a/pkgs/by-name/ba/back/src/config/mod.rs b/pkgs/by-name/ba/back/src/config/mod.rs
index 1161ce3..789c0b0 100644
--- a/pkgs/by-name/ba/back/src/config/mod.rs
+++ b/pkgs/by-name/ba/back/src/config/mod.rs
@@ -23,26 +23,25 @@ use crate::{
     git_bug::dag::is_git_bug,
 };
 
+#[derive(Deserialize)]
 pub struct BackConfig {
     /// The url to the source code of back. This is needed, because back is licensed under the
     /// AGPL.
     pub source_code_repository_url: Url,
 
-    /// A list of the repositories known to back.
-    /// This list is constructed from the `scan_path` and the `project_list` file.
-    pub repositories: BackRepositories,
-
     /// The root url this instance of back is hosted on.
     /// For example:
     ///     `issues.foss-syndicate.org`
-    pub root: Url,
+    pub root_url: Url,
+
+    project_list: PathBuf,
+
+    /// The path that is the common parent of all the repositories.
+    pub scan_path: PathBuf,
 }
 
 pub struct BackRepositories {
     repositories: Vec<BackRepository>,
-
-    /// The path that is the common parent of all the repositories.
-    scan_path: PathBuf,
 }
 
 impl BackRepositories {
@@ -72,10 +71,6 @@ impl BackRepositories {
                 repository_path: path.to_owned(),
             })
     }
-
-    pub fn scan_path(&self) -> &Path {
-        &self.scan_path
-    }
 }
 
 pub struct BackRepository {
@@ -103,39 +98,12 @@ impl BackRepository {
     }
 }
 
-#[derive(Deserialize)]
-struct RawBackConfig {
-    source_code_repository_url: Url,
-    root_url: Url,
-    project_list: PathBuf,
-    scan_path: PathBuf,
-}
-
 impl BackConfig {
-    pub fn from_config_file(path: &Path) -> error::Result<Self> {
-        let value = fs::read_to_string(path).map_err(|err| Error::ConfigRead {
-            file: path.to_owned(),
-            error: err,
-        })?;
-
-        let raw: RawBackConfig =
-            serde_json::from_str(&value).map_err(|err| Error::ConfigParse {
-                file: path.to_owned(),
-                error: err,
-            })?;
-
-        Self::try_from(raw)
-    }
-}
-
-impl TryFrom<RawBackConfig> for BackConfig {
-    type Error = error::Error;
-
-    fn try_from(value: RawBackConfig) -> Result<Self, Self::Error> {
-        let repositories = fs::read_to_string(&value.project_list)
+    pub fn repositories(&self) -> error::Result<BackRepositories> {
+        let repositories = fs::read_to_string(&self.project_list)
             .map_err(|err| error::Error::ProjectListRead {
                 error: err,
-                file: value.project_list,
+                file: self.project_list.to_owned(),
             })?
             .lines()
             .try_fold(vec![], |mut acc, path| {
@@ -143,16 +111,20 @@ impl TryFrom<RawBackConfig> for BackConfig {
                     repo_path: PathBuf::from(path),
                 });
 
-                Ok::<_, Self::Error>(acc)
+                Ok::<_, error::Error>(acc)
             })?;
+        Ok(BackRepositories { repositories })
+    }
 
-        Ok(Self {
-            source_code_repository_url: value.source_code_repository_url,
-            repositories: BackRepositories {
-                repositories,
-                scan_path: value.scan_path,
-            },
-            root: value.root_url,
+    pub fn from_config_file(path: &Path) -> error::Result<Self> {
+        let value = fs::read_to_string(path).map_err(|err| Error::ConfigRead {
+            file: path.to_owned(),
+            error: err,
+        })?;
+
+        serde_json::from_str(&value).map_err(|err| Error::ConfigParse {
+            file: path.to_owned(),
+            error: err,
         })
     }
 }
diff --git a/pkgs/by-name/ba/back/src/web/generate/mod.rs b/pkgs/by-name/ba/back/src/web/generate/mod.rs
index 10146bb..ae783a3 100644
--- a/pkgs/by-name/ba/back/src/web/generate/mod.rs
+++ b/pkgs/by-name/ba/back/src/web/generate/mod.rs
@@ -34,9 +34,9 @@ pub fn issues(
     repo_path: &Path,
 ) -> error::Result<String> {
     let repository = config
-        .repositories
+        .repositories()?
         .get(repo_path)?
-        .open(config.repositories.scan_path())?;
+        .open(&config.scan_path)?;
 
     let mut issue_list = issues_from_repository(&repository.to_thread_local())?
         .into_iter()
@@ -76,9 +76,9 @@ struct IssueTemplate {
 }
 pub fn issue(config: &BackConfig, repo_path: &Path, prefix: Prefix) -> error::Result<String> {
     let repository = config
-        .repositories
+        .repositories()?
         .get(repo_path)?
-        .open(config.repositories.scan_path())?
+        .open(&config.scan_path)?
         .to_thread_local();
 
     let maybe_issue = issues_from_repository(&repository)?
@@ -113,39 +113,37 @@ struct RepoValue {
 }
 pub fn repos(config: &BackConfig) -> error::Result<String> {
     let repos: Vec<RepoValue> = config
-        .repositories
+        .repositories()?
         .iter()
-        .filter_map(
-            |raw_repo| match raw_repo.open(config.repositories.scan_path()) {
-                Ok(repo) => {
-                    let repo = repo.to_thread_local();
-                    let git_config = repo.config_snapshot();
-
-                    let path = raw_repo.path().to_string_lossy().to_string();
-
-                    let owner = git_config
-                        .string("cgit.owner")
-                        .map(|v| v.to_string())
-                        .unwrap_or("<No owner>".to_owned());
-
-                    let description = fs::read_to_string(repo.git_dir().join("description"))
-                        .unwrap_or("<No description>".to_owned());
-
-                    Some(RepoValue {
-                        description,
-                        owner,
-                        path,
-                    })
-                }
-                Err(err) => {
-                    info!(
-                        "Repo '{}' could not be opened: '{err}'",
-                        raw_repo.path().display()
-                    );
-                    None
-                }
-            },
-        )
+        .filter_map(|raw_repo| match raw_repo.open(&config.scan_path) {
+            Ok(repo) => {
+                let repo = repo.to_thread_local();
+                let git_config = repo.config_snapshot();
+
+                let path = raw_repo.path().to_string_lossy().to_string();
+
+                let owner = git_config
+                    .string("cgit.owner")
+                    .map(|v| v.to_string())
+                    .unwrap_or("<No owner>".to_owned());
+
+                let description = fs::read_to_string(repo.git_dir().join("description"))
+                    .unwrap_or("<No description>".to_owned());
+
+                Some(RepoValue {
+                    description,
+                    owner,
+                    path,
+                })
+            }
+            Err(err) => {
+                info!(
+                    "Repo '{}' could not be opened: '{err}'",
+                    raw_repo.path().display()
+                );
+                None
+            }
+        })
         .collect();
 
     Ok(ReposTemplate {
@@ -160,9 +158,9 @@ pub fn feed(config: &BackConfig, repo_path: &Path) -> error::Result<String> {
     use rss::{ChannelBuilder, Item, ItemBuilder};
 
     let repository = config
-        .repositories
+        .repositories()?
         .get(repo_path)?
-        .open(config.repositories.scan_path())?
+        .open(&config.scan_path)?
         .to_thread_local();
 
     let issues: Vec<CollapsedIssue> = issues_from_repository(&repository)?
@@ -182,7 +180,7 @@ pub fn feed(config: &BackConfig, repo_path: &Path) -> error::Result<String> {
                 .link(format!(
                     "/{}/{}/issue/{}",
                     repo_path.display(),
-                    &config.root,
+                    &config.root_url,
                     issue.id
                 ))
                 .build()
@@ -207,7 +205,7 @@ pub fn feed(config: &BackConfig, repo_path: &Path) -> error::Result<String> {
                             .link(format!(
                                 "/{}/{}/issue/{}",
                                 repo_path.display(),
-                                &config.root,
+                                &config.root_url,
                                 issue.id
                             ))
                             .build()
@@ -219,8 +217,8 @@ pub fn feed(config: &BackConfig, repo_path: &Path) -> error::Result<String> {
 
     let channel = ChannelBuilder::default()
         .title("Issues")
-        .link(config.root.to_string())
-        .description(format!("The rss feed for issues on {}.", &config.root))
+        .link(config.root_url.to_string())
+        .description(format!("The rss feed for issues on {}.", &config.root_url))
         .items(items)
         .build();
     Ok(channel.to_string())