From dfb5714045e99a09bf3f67890ae3cdeab47058b3 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Thu, 26 Dec 2024 17:50:54 +0100 Subject: feat(pkgs/back): Rewrite the `git-bug` interface code The previous code was more or less reverse engineered, whilst this code is based on the actually git-bug source code. This improves the whole issue and operation handling immensely and also makes the code better maintainable. Furthermore, it also adds support for the operations that had not already used in `vhack.eu/nixos-server.git`. --- pkgs/by-name/ba/back/src/error/mod.rs | 44 ++- pkgs/by-name/ba/back/src/error/responder.rs | 23 ++ pkgs/by-name/ba/back/src/git_bug/dag/mod.rs | 143 +++++++++ pkgs/by-name/ba/back/src/git_bug/format/mod.rs | 123 ++++++++ .../ba/back/src/git_bug/issue/entity/mod.rs | 78 +++++ .../ba/back/src/git_bug/issue/identity/mod.rs | 71 +++++ .../by-name/ba/back/src/git_bug/issue/label/mod.rs | 85 ++++++ pkgs/by-name/ba/back/src/git_bug/issue/mod.rs | 185 +++++++++++ .../ba/back/src/git_bug/issue/operation/mod.rs | 124 ++++++++ .../src/git_bug/issue/operation/operation_type.rs | 51 ++++ pkgs/by-name/ba/back/src/git_bug/mod.rs | 28 ++ pkgs/by-name/ba/back/src/main.rs | 1 + pkgs/by-name/ba/back/src/web/format/mod.rs | 88 ------ pkgs/by-name/ba/back/src/web/issue/mod.rs | 337 --------------------- pkgs/by-name/ba/back/src/web/issue/raw.rs | 145 --------- pkgs/by-name/ba/back/src/web/issue_html.rs | 166 ++++++++++ pkgs/by-name/ba/back/src/web/issue_show.rs | 34 --- pkgs/by-name/ba/back/src/web/mod.rs | 148 +++++---- pkgs/by-name/ba/back/src/web/prefix.rs | 35 +++ 19 files changed, 1226 insertions(+), 683 deletions(-) create mode 100644 pkgs/by-name/ba/back/src/error/responder.rs create mode 100644 pkgs/by-name/ba/back/src/git_bug/dag/mod.rs create mode 100644 pkgs/by-name/ba/back/src/git_bug/format/mod.rs create mode 100644 pkgs/by-name/ba/back/src/git_bug/issue/entity/mod.rs create mode 100644 pkgs/by-name/ba/back/src/git_bug/issue/identity/mod.rs create mode 100644 pkgs/by-name/ba/back/src/git_bug/issue/label/mod.rs create mode 100644 pkgs/by-name/ba/back/src/git_bug/issue/mod.rs create mode 100644 pkgs/by-name/ba/back/src/git_bug/issue/operation/mod.rs create mode 100644 pkgs/by-name/ba/back/src/git_bug/issue/operation/operation_type.rs create mode 100644 pkgs/by-name/ba/back/src/git_bug/mod.rs delete mode 100644 pkgs/by-name/ba/back/src/web/format/mod.rs delete mode 100644 pkgs/by-name/ba/back/src/web/issue/mod.rs delete mode 100644 pkgs/by-name/ba/back/src/web/issue/raw.rs create mode 100644 pkgs/by-name/ba/back/src/web/issue_html.rs delete mode 100644 pkgs/by-name/ba/back/src/web/issue_show.rs create mode 100644 pkgs/by-name/ba/back/src/web/prefix.rs (limited to 'pkgs/by-name/ba/back/src') diff --git a/pkgs/by-name/ba/back/src/error/mod.rs b/pkgs/by-name/ba/back/src/error/mod.rs index 7e1c9cf..8b71700 100644 --- a/pkgs/by-name/ba/back/src/error/mod.rs +++ b/pkgs/by-name/ba/back/src/error/mod.rs @@ -1,9 +1,24 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This file is part of Back. +// +// You should have received a copy of the License along with this program. +// If not, see . + use std::{fmt::Display, io, path::PathBuf}; use thiserror::Error; +use crate::web::prefix::BackPrefix; + pub type Result = std::result::Result; +pub mod responder; + #[derive(Error, Debug)] pub enum Error { ConfigParse { @@ -14,11 +29,19 @@ pub enum Error { file: PathBuf, error: io::Error, }, + RocketLaunch(#[from] rocket::Error), + RepoOpen { repository_path: PathBuf, error: Box, }, - RocketLaunch(#[from] rocket::Error), + RepoRefsIter(#[from] gix::refs::packed::buffer::open::Error), + RepoRefsPrefixed(#[from] std::io::Error), + + IssuesPrefixMissing { + prefix: BackPrefix, + }, + IssuesPrefixParse(#[from] gix::hash::prefix::from_hex::Error), } impl Display for Error { @@ -38,6 +61,9 @@ impl Display for Error { file.display() ) } + Error::RocketLaunch(error) => { + write!(f, "while trying to start back: {error}") + } Error::RepoOpen { repository_path, error, @@ -48,8 +74,20 @@ impl Display for Error { repository_path.display() ) } - Error::RocketLaunch(error) => { - write!(f, "while trying to start back: {error}") + Error::RepoRefsIter(error) => { + write!(f, "while iteration over the refs in a repository: {error}",) + } + Error::RepoRefsPrefixed(error) => { + write!(f, "while prefixing the refs with a path: {error}") + } + Error::IssuesPrefixMissing { prefix } => { + write!( + f, + "There is no 'issue' associated with the prefix: {prefix}" + ) + } + Error::IssuesPrefixParse(error) => { + write!(f, "The given prefix can not be parsed as prefix: {error}") } } } diff --git a/pkgs/by-name/ba/back/src/error/responder.rs b/pkgs/by-name/ba/back/src/error/responder.rs new file mode 100644 index 0000000..7bea961 --- /dev/null +++ b/pkgs/by-name/ba/back/src/error/responder.rs @@ -0,0 +1,23 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This file is part of Back. +// +// You should have received a copy of the License along with this program. +// If not, see . + +use rocket::{ + response::{self, Responder, Response}, + Request, +}; + +use super::Error; + +impl<'r> Responder<'r, 'static> for Error { + fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> { + Response::build_from(self.to_string().respond_to(req)?).ok() + } +} diff --git a/pkgs/by-name/ba/back/src/git_bug/dag/mod.rs b/pkgs/by-name/ba/back/src/git_bug/dag/mod.rs new file mode 100644 index 0000000..9c158a7 --- /dev/null +++ b/pkgs/by-name/ba/back/src/git_bug/dag/mod.rs @@ -0,0 +1,143 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This file is part of Back. +// +// You should have received a copy of the License along with this program. +// If not, see . + +use std::path::Path; + +use gix::{bstr::ByteSlice, refs::Target, Commit, Id, ObjectId, Repository}; + +use crate::error; + +use super::issue::{ + entity::{Entity, RawEntity}, + CollapsedIssue, RawCollapsedIssue, +}; + +#[derive(Debug)] +pub struct Dag { + entities: Vec, +} + +impl Dag { + pub fn collapse(self) -> CollapsedIssue { + let raw_collapsed_issue = self.entities.into_iter().rev().fold( + RawCollapsedIssue::default(), + |mut collapsed_issue, entity| { + collapsed_issue.append_entity(entity); + collapsed_issue + }, + ); + + CollapsedIssue::from(raw_collapsed_issue) + } + + /// Construct a DAG from the root child upwards. + pub fn construct(repo: &Repository, id: ObjectId) -> Self { + let mut entities = vec![]; + + let base_commit = repo + .find_object(id) + .expect("The object with this id should exist.") + .try_into_commit() + .expect("The git-bug's data model enforces this."); + + entities.push(Self::commit_to_operations(repo, &base_commit)); + + let mut current_commit = base_commit; + while let Some(parent_id) = Self::try_get_parent(repo, ¤t_commit) { + entities.push(Self::commit_to_operations(repo, &parent_id)); + current_commit = parent_id; + } + + Self { + entities: { + entities + .into_iter() + .map(|(raw_entity, id)| Entity::from_raw(repo, raw_entity, id)) + .collect() + }, + } + } + + fn commit_to_operations<'b>(repo: &Repository, id: &Commit<'b>) -> (RawEntity, Id<'b>) { + let tree_obj = repo + .find_object( + id.tree_id() + .expect("All of git-bug's commits should have trees attached to them'"), + ) + .expect("The object with this id should exist.") + .try_into_tree() + .expect("git-bug's data model enforces this."); + + let ops_ref = tree_obj + .find_entry("ops") + .expect("All of git-bug's trees should contain a 'ops' json file"); + + let issue_data = repo + .find_object(ops_ref.object_id()) + .expect("The object with this id should exist.") + .try_into_blob() + .expect("The git-bug's data model enforces this.") + .data + .clone(); + + let operations = serde_json::from_str( + issue_data + .to_str() + .expect("git-bug's ensures, that this is valid json."), + ) + .expect("The returned json should be valid"); + + (operations, id.id()) + } + + fn try_get_parent<'a>(repo: &'a Repository, base_commit: &Commit<'a>) -> Option> { + let count = base_commit.parent_ids().count(); + + match count { + 0 => None, + 1 => { + let parent = base_commit.parent_ids().last().expect("One does exist"); + + let parent_id = parent.object().expect("The object exists").id; + Some( + repo.find_object(parent_id) + .expect("This is a valid id") + .try_into_commit() + .expect("This should be a commit"), + ) + } + other => { + unreachable!( + "Each commit, used by git-bug should only have one parent, but found: {other}" + ); + } + } + } +} + +pub fn issues_from_repository(repo: &Repository) -> error::Result> { + let dags = repo + .refs + .iter()? + .prefixed(Path::new("refs/bugs/"))? + .map(|val| { + let reference = val.expect("All `git-bug` references in 'refs/bugs' should be objects"); + + if let Target::Object(id) = reference.target { + Dag::construct(repo, id) + } else { + unreachable!("All 'refs/bugs/' should contain a clear target."); + } + }) + .collect::>(); + + Ok(dags) +} diff --git a/pkgs/by-name/ba/back/src/git_bug/format/mod.rs b/pkgs/by-name/ba/back/src/git_bug/format/mod.rs new file mode 100644 index 0000000..4ebf6d4 --- /dev/null +++ b/pkgs/by-name/ba/back/src/git_bug/format/mod.rs @@ -0,0 +1,123 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This file is part of Back. +// +// You should have received a copy of the License along with this program. +// If not, see . + +use std::fmt::Display; + +use chrono::DateTime; +use markdown::to_html; +use serde::Deserialize; +use serde_json::Value; + +#[derive(Debug, Default, Clone)] +/// Markdown content. +pub struct MarkDown { + value: String, +} + +impl Display for MarkDown { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(to_html(&self.value).as_str()) + } +} +impl From<&Value> for MarkDown { + fn from(value: &Value) -> Self { + Self { + value: value.as_str().expect("This will exist").to_owned(), + } + } +} + +/// An UNIX time stamp. +/// +/// These should only ever be used for human-display, because timestamps are unreliably in a +/// distributed system. +/// Because of this reason, there is no `value()` function. +#[derive(Debug, Default, Clone, Copy)] +pub struct TimeStamp { + value: u64, +} +impl From<&Value> for TimeStamp { + fn from(value: &Value) -> Self { + Self { + value: value.as_u64().expect("This must exist"), + } + } +} +impl Display for TimeStamp { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let date = + DateTime::from_timestamp(self.value as i64, 0).expect("This timestamp should be vaild"); + + let newdate = date.format("%Y-%m-%d %H:%M:%S"); + f.write_str(newdate.to_string().as_str()) + } +} + +#[derive(Debug, Default, Deserialize, Clone, PartialEq, Eq)] +/// A string that should be escaped when injected into html content. +pub struct HtmlString { + value: String, +} + +impl From for HtmlString { + fn from(value: MarkDown) -> Self { + Self { value: value.value } + } +} + +impl From<&Value> for HtmlString { + fn from(value: &Value) -> Self { + Self { + value: value.as_str().expect("This will exist").to_owned(), + } + } +} +impl Display for HtmlString { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(escape_html(&self.value).as_str()) + } +} + +// From `tera::escape_html` +/// Escape HTML following [OWASP](https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet) +/// +/// Escape the following characters with HTML entity encoding to prevent switching +/// into any execution context, such as script, style, or event handlers. Using +/// hex entities is recommended in the spec. In addition to the 5 characters +/// significant in XML (&, <, >, ", '), the forward slash is included as it helps +/// to end an HTML entity. +/// +/// ```text +/// & --> & +/// < --> < +/// > --> > +/// " --> " +/// ' --> ' ' is not recommended +/// / --> / forward slash is included as it helps end an HTML entity +/// ``` +#[inline] +pub fn escape_html(input: &str) -> String { + let mut output = String::with_capacity(input.len() * 2); + for c in input.chars() { + match c { + '&' => output.push_str("&"), + '<' => output.push_str("<"), + '>' => output.push_str(">"), + '"' => output.push_str("""), + '\'' => output.push_str("'"), + '/' => output.push_str("/"), + _ => output.push(c), + } + } + + // Not using shrink_to_fit() on purpose + output +} diff --git a/pkgs/by-name/ba/back/src/git_bug/issue/entity/mod.rs b/pkgs/by-name/ba/back/src/git_bug/issue/entity/mod.rs new file mode 100644 index 0000000..f2e9af0 --- /dev/null +++ b/pkgs/by-name/ba/back/src/git_bug/issue/entity/mod.rs @@ -0,0 +1,78 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This file is part of Back. +// +// You should have received a copy of the License along with this program. +// If not, see . + +use std::fmt::Display; + +use gix::Repository; +use serde::Deserialize; +use serde_json::Value; + +use super::{ + identity::{Author, RawAuthor}, + operation::Operation, +}; + +#[derive(Deserialize, Debug, PartialEq, Eq, Clone)] +#[serde(from = "Value")] +pub struct Id { + value: String, +} +impl From for Id { + fn from(value: Value) -> Self { + Self::from(&value) + } +} +impl From<&Value> for Id { + fn from(value: &Value) -> Self { + Self { + value: value.as_str().expect("This should be a string").to_owned(), + } + } +} +impl From> for Id { + fn from(value: gix::Id<'_>) -> Self { + Self { + value: value.shorten().expect("This should work?").to_string(), + } + } +} +impl Display for Id { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.value.fmt(f) + // let shortend = self.value.shorten().expect("This should work."); + // f.write_str(shortend.to_string().as_str()) + } +} + +#[derive(Debug)] +pub struct Entity { + pub id: Id, + pub author: Author, + pub operations: Vec, +} + +impl Entity { + pub fn from_raw<'a>(repo: &'a Repository, raw: RawEntity, id: gix::Id<'a>) -> Self { + Self { + id: Id::from(id), + author: Author::construct(repo, raw.author), + operations: raw.operations, + } + } +} + +#[derive(Deserialize)] +pub struct RawEntity { + pub author: RawAuthor, + + #[serde(alias = "ops")] + pub operations: Vec, +} diff --git a/pkgs/by-name/ba/back/src/git_bug/issue/identity/mod.rs b/pkgs/by-name/ba/back/src/git_bug/issue/identity/mod.rs new file mode 100644 index 0000000..0c2f426 --- /dev/null +++ b/pkgs/by-name/ba/back/src/git_bug/issue/identity/mod.rs @@ -0,0 +1,71 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This file is part of Back. +// +// You should have received a copy of the License along with this program. +// If not, see . + +use gix::{bstr::ByteSlice, Repository}; +use serde::Deserialize; +use serde_json::Value; + +use crate::{get, git_bug::format::HtmlString}; + +use super::entity::Id; + +#[derive(Debug, Clone)] +pub struct Author { + pub name: HtmlString, + pub email: HtmlString, + pub id: Id, +} + +impl Author { + pub fn construct(repo: &Repository, raw: RawAuthor) -> Self { + let commit_obj = repo + .find_reference(&format!("refs/identities/{}", raw.id)) + .expect("All authors should also have identities") + .peel_to_commit() + .expect("All identities should be commits"); + + let tree_obj = repo + .find_tree( + commit_obj + .tree() + .expect("The commit should have an tree associated with it") + .id, + ) + .expect("This should be a tree"); + + let data = repo + .find_blob( + tree_obj + .find_entry("version") + .expect("This entry should exist") + .object() + .expect("This should point to a blob entry") + .id, + ) + .expect("This blob should exist") + .data + .clone(); + + let json: Value = serde_json::from_str(data.to_str().expect("This is encoded json")) + .expect("This is valid json"); + + Author { + name: get! {json, "name"}, + email: get! {json, "email"}, + id: raw.id, + } + } +} + +#[derive(Deserialize)] +pub struct RawAuthor { + id: Id, +} diff --git a/pkgs/by-name/ba/back/src/git_bug/issue/label/mod.rs b/pkgs/by-name/ba/back/src/git_bug/issue/label/mod.rs new file mode 100644 index 0000000..a971234 --- /dev/null +++ b/pkgs/by-name/ba/back/src/git_bug/issue/label/mod.rs @@ -0,0 +1,85 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This file is part of Back. +// +// You should have received a copy of the License along with this program. +// If not, see . + +use std::fmt::Display; + +use serde::Deserialize; +use sha2::{Digest, Sha256}; + +use crate::git_bug::format::HtmlString; + +#[derive(Debug, Deserialize, PartialEq, Eq, Clone)] +pub struct Label { + value: HtmlString, +} + +impl Display for Label { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.value.fmt(f) + } +} + +impl Label { + /// RGBA from a Label computed in a deterministic way + /// This is taken completely from `git_bug` + pub fn associate_color(&self) -> Color { + // colors from: https://material-ui.com/style/color/ + let colors = vec![ + Color::from_rgba(244, 67, 54, 255), // red + Color::from_rgba(233, 30, 99, 255), // pink + Color::from_rgba(156, 39, 176, 255), // purple + Color::from_rgba(103, 58, 183, 255), // deepPurple + Color::from_rgba(63, 81, 181, 255), // indigo + Color::from_rgba(33, 150, 243, 255), // blue + Color::from_rgba(3, 169, 244, 255), // lightBlue + Color::from_rgba(0, 188, 212, 255), // cyan + Color::from_rgba(0, 150, 136, 255), // teal + Color::from_rgba(76, 175, 80, 255), // green + Color::from_rgba(139, 195, 74, 255), // lightGreen + Color::from_rgba(205, 220, 57, 255), // lime + Color::from_rgba(255, 235, 59, 255), // yellow + Color::from_rgba(255, 193, 7, 255), // amber + Color::from_rgba(255, 152, 0, 255), // orange + Color::from_rgba(255, 87, 34, 255), // deepOrange + Color::from_rgba(121, 85, 72, 255), // brown + Color::from_rgba(158, 158, 158, 255), // grey + Color::from_rgba(96, 125, 139, 255), // blueGrey + ]; + + let hash = Sha256::digest(self.to_string().as_bytes()); + + let id: usize = hash + .into_iter() + .map(|val| val as usize) + .fold(0, |acc, val| (acc + val) % colors.len()); + + colors[id] + } +} + +#[derive(Default, Clone, Copy, Debug)] +pub struct Color { + pub red: u32, + pub green: u32, + pub blue: u32, + pub alpha: u32, +} + +impl Color { + pub fn from_rgba(red: u32, green: u32, blue: u32, alpha: u32) -> Self { + Self { + red, + green, + blue, + alpha, + } + } +} diff --git a/pkgs/by-name/ba/back/src/git_bug/issue/mod.rs b/pkgs/by-name/ba/back/src/git_bug/issue/mod.rs new file mode 100644 index 0000000..f27bfec --- /dev/null +++ b/pkgs/by-name/ba/back/src/git_bug/issue/mod.rs @@ -0,0 +1,185 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This file is part of Back. +// +// You should have received a copy of the License along with this program. +// If not, see . + +use std::fmt::Display; + +use entity::{Entity, Id}; +use identity::Author; +use label::Label; +use operation::Operation; +use serde_json::Value; + +use super::format::{MarkDown, TimeStamp}; + +pub mod entity; +pub mod identity; +pub mod label; +pub mod operation; + +#[derive(Debug, Eq, PartialEq, Copy, Clone)] +pub enum Status { + Open, + Closed, +} +impl From<&Value> for Status { + fn from(value: &Value) -> Self { + match value.as_u64().expect("This should be a integer") { + 1 => Self::Open, + 2 => Self::Closed, + other => unimplemented!("Invalid status string: '{other}'"), + } + } +} +impl Display for Status { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Status::Open => f.write_str("Open"), + Status::Closed => f.write_str("Closed"), + } + } +} + +#[derive(Debug)] +pub struct CollapsedIssue { + pub id: Id, + pub author: Author, + pub timestamp: TimeStamp, + pub title: MarkDown, + pub message: MarkDown, + pub comments: Vec, + pub status: Status, + pub last_status_change: TimeStamp, + pub labels: Vec