Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Rework jobs and projects response types. #48

Draft
wants to merge 1 commit into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 63 additions & 35 deletions src/types/job.rs
Original file line number Diff line number Diff line change
@@ -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<Utc>,
pub updated_at: DateTime<Utc>,
pub label: Option<String>,
pub ecosystem: PackageType,
pub project: JobProject,
pub score: JobScore,
pub num_incomplete: u32,
}

/// Response type for the API /jobs/<job id> endpoint.
#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct JobResponse {
#[serde(flatten)]
pub essentials: JobEssentials,
pub status: Status,
pub package_statuses: Vec<PackageStatusExtended>,
pub action: Action,
pub thresholds: ProjectThresholds,
}
Comment on lines +25 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A list of issues would be a good addition here


/// Metadata about a job.
#[derive(PartialEq, PartialOrd, Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct JobDescriptor {
#[serde(flatten)]
pub essentials: JobEssentials,
pub package_descriptors: Vec<PackageDescriptor>,
}

#[derive(PartialEq, PartialOrd, Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct JobProject {
pub id: ProjectId,
pub name: String,
}
Comment on lines +47 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support group-owned projects, this probably also needs an optional group name.


#[derive(Debug, PartialEq, PartialOrd, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct JobScore {
pub value: f64,
Comment on lines +54 to +55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be really helpful to have the per-axis scores for the job included along side the overall job score.

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?
Expand All @@ -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<PackageDescriptor>,
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,
Expand Down Expand Up @@ -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<JobDescriptor>,
/// 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<PackageStatusExtended>),
Basic(JobStatusResponse<PackageStatus>),
/// 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<T> {
Expand Down Expand Up @@ -126,11 +162,3 @@ pub struct JobStatusResponse<T> {
/// The packages that are a part of this job
pub packages: Vec<T>,
}

/// Response from canceling a job
#[derive(
PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, Serialize, Deserialize, JsonSchema,
)]
pub struct CancelJobResponse {
pub msg: String,
}
19 changes: 3 additions & 16 deletions src/types/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub id: Option<String>,
pub ignored: IgnoredReason,
pub ignored: Option<IgnoredReason>,
}

#[derive(
Expand Down Expand Up @@ -335,7 +322,7 @@ pub struct Package {
pub risk_scores: RiskScores,
pub total_risk_score_dynamics: Option<Vec<ScoreDynamicsPoint>>,
pub issues_details: Vec<Issue>,
pub issues: Vec<IssuesListItem>,
pub issues: Vec<Issue>,
pub authors: Vec<Author>,
pub developer_responsiveness: Option<DeveloperResponsiveness>,
pub issue_impacts: IssueImpacts,
Expand Down Expand Up @@ -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,
Expand Down
95 changes: 78 additions & 17 deletions src/types/project.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
//! 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/<project id> endpoint.
#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ProjectResponse {
pub id: Uuid,
pub name: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub thresholds: ProjectThresholds,
pub stats: ProjectStats,
pub latest_job: Option<JobResponse>,
}

/// Summary response for a project
#[derive(
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<Utc>,
/// When the project was created
Expand All @@ -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<PackageType>,
/// The configured risk cutoff thresholds for the project
pub thresholds: ProjectThresholds,
/// Most recent analysis job runs
pub jobs: Vec<JobDescriptor>,
}

/// Rquest to create a project
#[derive(Serialize, Deserialize, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct LicensesStats {
pub counts: HashMap<String, u32>,
}

#[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,
}
Comment on lines +97 to +103
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem to be related to a job and not a project. Should this be JobStats?


/// 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,
)]
Expand Down