From 52212de1fe3784084fd9de25fc4c2166383b897d Mon Sep 17 00:00:00 2001 From: Mathew Horner Date: Wed, 17 Aug 2022 11:00:14 -0500 Subject: [PATCH] [WIP] Rework jobs and projects response types. --- src/types/job.rs | 98 ++++++++++++++++++++++++++++---------------- src/types/package.rs | 19 ++------- src/types/project.rs | 95 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 144 insertions(+), 68 deletions(-) diff --git a/src/types/job.rs b/src/types/job.rs index 07d970a..71d9110 100644 --- a/src/types/job.rs +++ b/src/types/job.rs @@ -1,11 +1,61 @@ //! This module contains types involved with handling phylum processing jobs. +use chrono::{DateTime, Utc}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use super::common::*; use super::project::*; -use crate::types::package::{PackageDescriptor, PackageStatus, PackageStatusExtended, PackageType}; +use crate::types::package::{PackageDescriptor, PackageStatusExtended, PackageType}; + +/// Data shared between the full [`JobResponse`] type and the abbreviated [`JobDescriptor`] type. +#[derive(PartialEq, PartialOrd, Clone, Debug, Serialize, Deserialize, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct JobEssentials { + pub id: JobId, + pub created_at: DateTime, + pub updated_at: DateTime, + pub label: Option, + pub ecosystem: PackageType, + pub project: JobProject, + pub score: JobScore, + pub num_incomplete: u32, +} + +/// Response type for the API /jobs/ endpoint. +#[derive(Serialize, Deserialize, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct JobResponse { + #[serde(flatten)] + pub essentials: JobEssentials, + pub status: Status, + pub package_statuses: Vec, + pub action: Action, + pub thresholds: ProjectThresholds, +} + +/// Metadata about a job. +#[derive(PartialEq, PartialOrd, Clone, Debug, Serialize, Deserialize, JsonSchema)] +pub struct JobDescriptor { + #[serde(flatten)] + pub essentials: JobEssentials, + pub package_descriptors: Vec, +} + +#[derive(PartialEq, PartialOrd, Clone, Debug, Serialize, Deserialize, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct JobProject { + pub id: ProjectId, + pub name: String, +} + +#[derive(Debug, PartialEq, PartialOrd, Clone, Serialize, Deserialize, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct JobScore { + pub value: f64, + pub pass: bool, + pub message: String, +} /// When a job is completed, and some requirement is not met ( such as quality /// level ), what action should be taken? @@ -21,23 +71,6 @@ pub enum Action { Break, } -/// Metadata about a job -#[derive(PartialEq, PartialOrd, Clone, Debug, Serialize, Deserialize, JsonSchema)] -pub struct JobDescriptor { - pub job_id: JobId, - pub project: String, - pub label: String, - pub num_dependencies: u32, - pub score: f64, - pub packages: Vec, - pub pass: bool, - pub msg: String, - pub date: String, - pub ecosystem: String, - #[serde(default)] - pub num_incomplete: u32, -} - /// Submit Package for analysis #[derive( PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, Serialize, Deserialize, JsonSchema, @@ -71,21 +104,24 @@ pub struct SubmitPackageResponse { /// Represents a response that summarizes the output of all current jobs #[derive(PartialEq, PartialOrd, Clone, Debug, Serialize, Deserialize, JsonSchema)] pub struct AllJobsStatusResponse { - /// A description of the latest jobs + /// A description of the latest jobs. pub jobs: Vec, - /// Total jobs run + /// Total jobs run. pub total_jobs: u32, - pub count: u32, } -#[derive(PartialEq, Clone, Debug, Serialize, Deserialize, JsonSchema)] -#[serde(untagged)] -pub enum JobStatusResponseVariant { - // Serde returns the one that deserializes successfully first, so most complicated goes first - Extended(JobStatusResponse), - Basic(JobStatusResponse), +/// Response from canceling a job +#[derive( + PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, Serialize, Deserialize, JsonSchema, +)] +pub struct CancelJobResponse { + pub msg: String, } +// -------------------------------------------------------------- +// V V +// V -------- EVERYTHING BELOW THIS LINE IS DEPRECATED -------- V + /// Data returned when querying the job status endpoint #[derive(PartialEq, PartialOrd, Clone, Debug, Serialize, Deserialize, JsonSchema)] pub struct JobStatusResponse { @@ -126,11 +162,3 @@ pub struct JobStatusResponse { /// The packages that are a part of this job pub packages: Vec, } - -/// Response from canceling a job -#[derive( - PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, Serialize, Deserialize, JsonSchema, -)] -pub struct CancelJobResponse { - pub msg: String, -} diff --git a/src/types/package.rs b/src/types/package.rs index a29c2fd..6fc8a68 100644 --- a/src/types/package.rs +++ b/src/types/package.rs @@ -186,20 +186,7 @@ pub struct Issue { pub severity: RiskLevel, #[serde(alias = "risk_domain")] pub domain: RiskDomain, -} - -/// Issue description. -#[derive(PartialEq, PartialOrd, Clone, Debug, Serialize, Deserialize, JsonSchema)] -#[serde(rename_all = "camelCase")] -pub struct IssuesListItem { - pub risk_type: RiskType, - pub score: f32, - pub impact: RiskLevel, - pub description: String, - pub title: String, - pub tag: Option, - pub id: Option, - pub ignored: IgnoredReason, + pub ignored: Option, } #[derive( @@ -335,7 +322,7 @@ pub struct Package { pub risk_scores: RiskScores, pub total_risk_score_dynamics: Option>, pub issues_details: Vec, - pub issues: Vec, + pub issues: Vec, pub authors: Vec, pub developer_responsiveness: Option, pub issue_impacts: IssueImpacts, @@ -412,7 +399,7 @@ pub struct PackageStatus { /// Package metadata with extended info info // TODO Clearer name -#[derive(PartialEq, Clone, Debug, Serialize, Deserialize, JsonSchema)] +#[derive(Serialize, Deserialize, JsonSchema)] pub struct PackageStatusExtended { #[serde(flatten)] pub basic_status: PackageStatus, diff --git a/src/types/project.rs b/src/types/project.rs index c4d710d..43e89e3 100644 --- a/src/types/project.rs +++ b/src/types/project.rs @@ -1,21 +1,27 @@ -//! This module contains types for working with project data +//! This module contains types for working with project data. + +use std::collections::HashMap; + use chrono::{DateTime, Utc}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; +use uuid::Uuid; use super::common::ProjectId; -use super::job::*; +use super::job::{JobDescriptor, JobResponse}; use super::package::PackageType; -/// Rick cut off thresholds for a project -#[derive(PartialEq, PartialOrd, Copy, Clone, Debug, Serialize, Deserialize, JsonSchema)] -pub struct ProjectThresholds { - pub author: f32, - pub engineering: f32, - pub license: f32, - pub malicious: f32, - pub total: f32, - pub vulnerability: f32, +// Response type for the API /projects/ endpoint. +#[derive(Serialize, Deserialize, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct ProjectResponse { + pub id: Uuid, + pub name: String, + pub created_at: DateTime, + pub updated_at: DateTime, + pub thresholds: ProjectThresholds, + pub stats: ProjectStats, + pub latest_job: Option, } /// Summary response for a project @@ -23,10 +29,10 @@ pub struct ProjectThresholds { PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, Serialize, Deserialize, JsonSchema, )] pub struct ProjectSummaryResponse { - /// The project name - pub name: String, /// The project id pub id: ProjectId, + /// The project name + pub name: String, /// When the project was updated pub updated_at: DateTime, /// When the project was created @@ -40,19 +46,74 @@ pub struct ProjectSummaryResponse { /// A more detailed project response #[derive(PartialEq, PartialOrd, Clone, Debug, Serialize, Deserialize, JsonSchema)] pub struct ProjectDetailsResponse { + /// The project id + pub id: ProjectId, /// The project name pub name: String, - /// The project id - pub id: String, /// The project ecosystem / package type - pub ecosystem: String, + pub ecosystem: Option, /// The configured risk cutoff thresholds for the project pub thresholds: ProjectThresholds, /// Most recent analysis job runs pub jobs: Vec, } -/// Rquest to create a project +#[derive(Serialize, Deserialize, Default, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct LicensesStats { + pub counts: HashMap, +} + +#[derive(Serialize, Deserialize, Default, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct IssueStatusCounts { + pub untagged: u32, + pub will_fix: u32, + pub accept: u32, + pub not_relevant: u32, +} + +#[derive(Serialize, Deserialize, Default, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct IssueStatusStats { + pub counts: IssueStatusCounts, +} + +#[derive(Serialize, Deserialize, Default, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct DependenciesCounts { + pub total: u32, + pub num_incomplete: u32, + pub above_threshold: u32, + pub below_threshold: u32, +} + +#[derive(Serialize, Deserialize, Default, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct DependenciesStats { + pub counts: DependenciesCounts, +} + +#[derive(Serialize, Deserialize, Default, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct ProjectStats { + pub licenses: LicensesStats, + pub issue_status: IssueStatusStats, + pub dependencies: DependenciesStats, +} + +/// Risk cut off thresholds for a project. +#[derive(PartialEq, PartialOrd, Copy, Clone, Debug, Serialize, Deserialize, JsonSchema)] +pub struct ProjectThresholds { + pub author: f32, + pub engineering: f32, + pub license: f32, + pub malicious: f32, + pub total: f32, + pub vulnerability: f32, +} + +/// Request to create a project. #[derive( PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, Serialize, Deserialize, JsonSchema, )]