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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::collections::HashSet;

use flatpak_rs::build_system::FlatpakBuildSystem;
use flatpak_rs::module::FlatpakModule;
use flatpak_rs::source::{FlatpakSource, FlatpakSourceItem};

use serde::{Deserialize, Serialize};

#[derive(Clone)]
#[derive(Deserialize)]
#[derive(Serialize)]
#[derive(Default)]
pub struct SoftwareProject {
    // Project ids are based on the reverse DNS notation, and
    // are either derived from build manifests found in the project
    // using the same reverse DNS notation, or from the urls
    // of version-control systems (VCS) repositories associated
    // with the project.
    pub id: String,

    // This is the main URL of the project, and also the one
    // that was used to generate the project id.
    pub vcs_url: String,

    // Common name of the software project.
    pub name: String,

    // Description of the software project.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    pub web_urls: HashSet<String>,

    pub vcs_urls: HashSet<String>,

    // A set of all the known siblings. Sibling have the same
    // root hashes fingerprint, but could be forks.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub siblings: Option<HashSet<String>>,

    // A list of the paths of known flatpak app manifests found
    // in the project's repository.
    pub flatpak_app_manifests: HashSet<String>,

    // A list of the paths of known flatpak module definition manifests found
    // in the project's repository.
    pub flatpak_module_manifests: HashSet<String>,

    // A list of the paths of known flatpak sources definition manifests found
    // in the project's repository.
    pub flatpak_sources_manifests: HashSet<String>,

    // A list of tags associated with the project.
    // Those include the sources from which a project was discovered.
    pub tags: HashSet<String>,

    // All the build systems that are known to be supported by the project.
    pub build_systems: HashSet<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub main_branch: Option<String>,

    // Hash of the latest commit on the main branch seen during the
    // latest update of the project.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_known_commit: Option<String>,

    // When the project's main branch was last updated
    // in the local git checkout of the project.
    // Stored as an ISO datetime string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_updated: Option<String>,

    // The root git commit hashes associated with the project. This is used
    // for project de-duplication, in the case a project has multiple remote
    // git repositories. I used a vector instead of a set, because
    // I believe it's possible to have two ancestors with the same hash.
    pub root_hashes: Vec<String>,
}
impl SoftwareProject {
    pub fn merge(&mut self, other_project: &SoftwareProject) {
        if self.id != other_project.id {
            panic!(
                "Cannot merge projects with different IDs! {} != {}",
                self.id, other_project.id
            );
        }
        if self.vcs_url != other_project.vcs_url {
            panic!(
                "Cannot merge projects with different VCS URLs! {} != {}",
                self.vcs_url, other_project.vcs_url
            );
        }
        for web_url in &other_project.web_urls {
            self.web_urls.insert(web_url.to_string());
        }
        for vcs_url in &other_project.vcs_urls {
            self.vcs_urls.insert(vcs_url.to_string());
        }
        for build_system in &other_project.build_systems {
            self.build_systems.insert(build_system.to_string());
        }
        for app_manifest in &other_project.flatpak_app_manifests {
            self.flatpak_app_manifests.insert(app_manifest.to_string());
        }
        for module_manifest in &other_project.flatpak_module_manifests {
            self.flatpak_module_manifests.insert(module_manifest.to_string());
        }
        for source_manifest in &other_project.flatpak_sources_manifests {
            self.flatpak_sources_manifests.insert(source_manifest.to_string());
        }
        for tag in &other_project.tags {
            self.tags.insert(tag.to_string());
        }
        if self.root_hashes.len() == 0 {
            for hash in &other_project.root_hashes {
                self.root_hashes.push(hash.clone());
            }
        }
        if let Some(siblings) = &other_project.siblings {
            if self.siblings.is_none() {
                self.siblings = other_project.siblings.clone();
            }
        }
        if let Some(description) = &other_project.description {
            self.description = Some(description.clone());
        }
        if let Some(last_known_commit) = &other_project.last_known_commit {
            self.last_known_commit = Some(last_known_commit.clone());
        }
        if let Some(main_branch) = &other_project.main_branch {
            self.main_branch = Some(main_branch.clone());
        }
        // TODO maybe we should preserve the most recent one instead.
        if let Some(last_updated) = &other_project.last_updated {
            self.last_updated = Some(last_updated.clone());
        }
    }

    pub fn supports_flatpak(&self) -> bool {
        if !self.flatpak_app_manifests.is_empty() {
            return true;
        }
        if !self.flatpak_module_manifests.is_empty() {
            return true;
        }
        if !self.flatpak_sources_manifests.is_empty() {
            return true;
        }
        return false;
    }

    pub fn get_main_vcs_url(&self) -> String {
        return self.vcs_url.to_string();
    }

    pub fn get_root_signature(&self) -> String {
        let mut root_signature: String = "".to_string();
        for root_hash in &self.root_hashes {
            root_signature += root_hash;
        }
        root_signature
    }

    pub fn get_default_modules(&self) -> Vec<FlatpakModule> {
        let mut response = vec![];
        let mut git_source = FlatpakSource::default();
        git_source.url = Some(self.vcs_url.clone());
        if let Some(branch) = &self.main_branch {
            git_source.branch = Some(branch.clone());
        }
        // TODO also set the mirror urls if available.

        for build_system in &self.build_systems {
            let buildsystem = match FlatpakBuildSystem::from_string(build_system) {
                Err(e) => {
                    // This is not a build system supported by Flatpak. It's not a big deal.
                    continue;
                }
                Ok(b) => b,
            };
            let mut derived_module = FlatpakModule::default();
            derived_module.name = self.id.clone();
            derived_module.buildsystem = Some(buildsystem);
            derived_module
                .sources
                .push(FlatpakSourceItem::Description(git_source.clone()));
            response.push(derived_module);
        }
        response
    }
}