diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-12-26 17:50:54 +0100 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-12-26 17:58:27 +0100 |
commit | dfb5714045e99a09bf3f67890ae3cdeab47058b3 (patch) | |
tree | 5e76e310892ea7e36099312e25023aa154217a28 /pkgs/by-name/ba/back/src/git_bug/issue/operation | |
parent | fix(pkgs/back): Use rocket to manage the configuration values (diff) | |
download | nixos-server-dfb5714045e99a09bf3f67890ae3cdeab47058b3.zip |
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`.
Diffstat (limited to 'pkgs/by-name/ba/back/src/git_bug/issue/operation')
-rw-r--r-- | pkgs/by-name/ba/back/src/git_bug/issue/operation/mod.rs | 124 | ||||
-rw-r--r-- | pkgs/by-name/ba/back/src/git_bug/issue/operation/operation_type.rs | 51 |
2 files changed, 175 insertions, 0 deletions
diff --git a/pkgs/by-name/ba/back/src/git_bug/issue/operation/mod.rs b/pkgs/by-name/ba/back/src/git_bug/issue/operation/mod.rs new file mode 100644 index 0000000..7f861a7 --- /dev/null +++ b/pkgs/by-name/ba/back/src/git_bug/issue/operation/mod.rs @@ -0,0 +1,124 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de> +// 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 <https://www.gnu.org/licenses/agpl.txt>. + +use std::convert::Infallible; + +use operation_type::OperationType; +use serde::Deserialize; +use serde_json::Value; + +use crate::{ + get, + git_bug::format::{MarkDown, TimeStamp}, +}; + +use super::{entity, label::Label, Status}; + +pub mod operation_type; + +#[derive(Deserialize, Debug)] +#[serde(try_from = "Value")] +pub enum Operation { + AddComment { + timestamp: TimeStamp, + message: MarkDown, + }, + Create { + timestamp: TimeStamp, + title: MarkDown, + message: MarkDown, + }, + EditComment { + timestamp: TimeStamp, + target: entity::Id, + message: MarkDown, + }, + LabelChange { + timestamp: TimeStamp, + added: Vec<Label>, + removed: Vec<Label>, + }, + SetStatus { + timestamp: TimeStamp, + status: Status, + }, + SetTitle { + timestamp: TimeStamp, + title: MarkDown, + was: MarkDown, + }, + + // These seem to be just weird non-operation, operations. + // defined in: git-bug/entities/bug/operation.go + NoOp {}, + SetMetadata {}, +} + +impl TryFrom<Value> for Operation { + type Error = Infallible; + + fn try_from(value: Value) -> Result<Self, Self::Error> { + let operation_type = OperationType::from_json_int( + value + .get("type") + .expect("Should exist") + .as_u64() + .expect("This should work"), + ); + + let op = match operation_type { + OperationType::AddComment => Self::AddComment { + timestamp: get! {value, "timestamp" }, + message: get! {value, "message"}, + }, + OperationType::Create => Self::Create { + timestamp: get! {value, "timestamp"}, + title: get! {value, "title"}, + message: get! {value, "message"}, + }, + OperationType::EditComment => Self::EditComment { + timestamp: get! {value, "timestamp"}, + target: get! {value, "target"}, + message: get! {value, "message"}, + }, + OperationType::LabelChange => Self::LabelChange { + timestamp: get! {value, "timestamp"}, + added: serde_json::from_value( + value + .get("added") + .expect("This should be available") + .to_owned(), + ) + .expect("This should be parsable"), + removed: serde_json::from_value( + value + .get("removed") + .expect("This should be available") + .to_owned(), + ) + .expect("This should be parsable"), + }, + OperationType::SetStatus => Self::SetStatus { + timestamp: get! {value, "timestamp"}, + status: get! {value, "status"}, + }, + OperationType::SetTitle => Self::SetTitle { + timestamp: get! {value, "timestamp"}, + title: get! {value, "title"}, + was: get! {value, "was"}, + }, + OperationType::NoOp => Self::NoOp {}, + OperationType::SetMetadata => Self::SetMetadata {}, + }; + + Ok(op) + } +} diff --git a/pkgs/by-name/ba/back/src/git_bug/issue/operation/operation_type.rs b/pkgs/by-name/ba/back/src/git_bug/issue/operation/operation_type.rs new file mode 100644 index 0000000..69d272f --- /dev/null +++ b/pkgs/by-name/ba/back/src/git_bug/issue/operation/operation_type.rs @@ -0,0 +1,51 @@ +// Back - An extremely simple git issue tracking system. Inspired by tvix's +// panettone +// +// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de> +// 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 <https://www.gnu.org/licenses/agpl.txt>. + +pub enum OperationType { + AddComment, + Create, + EditComment, + LabelChange, + NoOp, + SetMetadata, + SetStatus, + SetTitle, +} + +impl OperationType { + // NOTE(@bpeetz): This mapping should always be the same as `git_bug`'s. + // The mapping is defined in `git-bug/entities/bug/operation.go`. <2024-12-26> + pub fn to_json_int(self) -> u64 { + match self { + OperationType::Create => 1, + OperationType::SetTitle => 2, + OperationType::AddComment => 3, + OperationType::SetStatus => 4, + OperationType::LabelChange => 5, + OperationType::EditComment => 6, + OperationType::NoOp => 7, + OperationType::SetMetadata => 8, + } + } + pub fn from_json_int(value: u64) -> Self { + match value { + 1 => OperationType::Create, + 2 => OperationType::SetTitle, + 3 => OperationType::AddComment, + 4 => OperationType::SetStatus, + 5 => OperationType::LabelChange, + 6 => OperationType::EditComment, + 7 => OperationType::NoOp, + 8 => OperationType::SetMetadata, + other => unimplemented!("The operation type {other} is not recognized."), + } + } +} |