summary refs log tree commit diff stats
path: root/pkgs/by-name/ba/back/src/git_bug/issue/identity/mod.rs
blob: bbf483cd2f415c1580e483d843828ef390aa293a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 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::fmt::Display;

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 Display for Author {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "({}, {})", self.name, self.email)
    }
}

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,
}