Skip to content
Snippets Groups Projects
Verified Commit 0f6b61ab authored by Geoffrey Arthaud's avatar Geoffrey Arthaud
Browse files

Create Package API (absent from crate gitlab)

parent 28578904
No related branches found
No related tags found
No related merge requests found
...@@ -406,6 +406,7 @@ version = "0.1.0" ...@@ -406,6 +406,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"console", "console",
"derive_builder",
"dialoguer", "dialoguer",
"git2", "git2",
"gitlab", "gitlab",
......
...@@ -8,6 +8,7 @@ edition = "2021" ...@@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
chrono = "0.4.20" chrono = "0.4.20"
console = "0.15.1" console = "0.15.1"
derive_builder = "0.11.2"
dialoguer = "0.10.2" dialoguer = "0.10.2"
git2 = "0.15.0" git2 = "0.15.0"
gitlab = "0.1502.0" gitlab = "0.1502.0"
......
pub mod packages;
\ No newline at end of file
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use derive_builder::Builder;
use gitlab::api::common::{NameOrId, SortOrder};
use gitlab::api::endpoint_prelude::*;
use gitlab::api::ParamValue;
/// Keys packages results may be ordered by.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackageOrderBy {
/// When the pipeline was last updated.
CreatedAt,
/// Order by name of the package.
Name,
/// Order by version of the package.
Version,
/// Order by package type.
Type,
}
impl PackageOrderBy {
/// The ordering as a query parameter.
fn as_str(self) -> &'static str {
match self {
PackageOrderBy::CreatedAt => "created_at",
PackageOrderBy::Name => "name",
PackageOrderBy::Version => "version",
PackageOrderBy::Type => "type",
}
}
}
impl ParamValue<'static> for PackageOrderBy {
fn as_value(&self) -> Cow<'static, str> {
self.as_str().into()
}
}
/// The type of a package.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackageType {
/// Conan package type.
Conan,
/// Maven package type.
Maven,
/// Npm package type.
Npm,
/// Pypi package type.
Pypi,
/// Composer package type.
Composer,
/// Nuget package type.
Nuget,
/// Helm package type.
Helm,
/// Terraform module package type.
TerraformModule,
/// Golang package type.
Golang,
}
impl PackageType {
/// The package type as a query parameter.
fn as_str(self) -> &'static str {
match self {
PackageType::Conan => "conan",
PackageType::Maven => "maven",
PackageType::Npm => "npm",
PackageType::Pypi => "pypi",
PackageType::Composer => "composer",
PackageType::Nuget => "nuget",
PackageType::Helm => "helm",
PackageType::TerraformModule => "terraform_module",
PackageType::Golang => "golang"
}
}
}
impl ParamValue<'static> for PackageType {
fn as_value(&self) -> Cow<'static, str> {
self.as_str().into()
}
}
/// The status of a package.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackageStatus {
/// The default package status
Default,
/// The status of a hidden package
Hidden,
/// The processiog package status
Processing,
/// The status of a package in error
Error,
/// The status of a pending package for destruction
PendingDestruction
}
impl PackageStatus {
/// The package type as a query parameter.
fn as_str(self) -> &'static str {
match self {
PackageStatus::Default => "default",
PackageStatus::Hidden => "hidden",
PackageStatus::Processing => "processing",
PackageStatus::Error => "error",
PackageStatus::PendingDestruction => "pending_destruction"
}
}
}
impl ParamValue<'static> for PackageStatus {
fn as_value(&self) -> Cow<'static, str> {
self.as_str().into()
}
}
/// Query for pipelines within a project.
#[derive(Debug, Builder)]
#[builder(setter(strip_option))]
pub struct Packages<'a> {
/// The project to query for packages.
#[builder(setter(into))]
project: NameOrId<'a>,
/// Order results by a given key.
#[builder(default)]
order_by: Option<PackageOrderBy>,
/// Sort order for resulting packages.
#[builder(default)]
sort: Option<SortOrder>,
/// Filter packages by its type.
#[builder(default)]
package_type: Option<PackageType>,
/// Filter packages by its name.
#[builder(setter(into), default)]
package_name: Option<Cow<'a, str>>,
/// Filter packages with or without versionless packages.
#[builder(default)]
include_versionless: Option<bool>,
/// Filter packages by its status.
#[builder(default)]
status: Option<PackageStatus>,
}
impl<'a> Packages<'a> {
/// Create a builder for the endpoint.
pub fn builder() -> PackagesBuilder<'a> {
PackagesBuilder::default()
}
}
impl<'a> Endpoint for Packages<'a> {
fn method(&self) -> Method {
Method::GET
}
fn endpoint(&self) -> Cow<'static, str> {
format!("projects/{}/packages", self.project).into()
}
fn parameters(&self) -> QueryParams {
let mut params = QueryParams::default();
params
.push_opt("order_by", self.order_by)
.push_opt("sort", self.sort)
.push_opt("package_type", self.package_type)
.push_opt("package_name", self.package_name.as_ref())
.push_opt("include_versionless", self.include_versionless)
.push_opt("status", self.status);
params
}
}
impl<'a> Pageable for Packages<'a> {}
...@@ -4,6 +4,7 @@ use std::thread::JoinHandle; ...@@ -4,6 +4,7 @@ use std::thread::JoinHandle;
pub mod gitlab_connection; pub mod gitlab_connection;
pub mod pipeline_analysis; pub mod pipeline_analysis;
pub mod pipeline_clean; pub mod pipeline_clean;
pub mod package_analysis;
pub const STORAGE_LIMIT: u64 = 2_000_000_000; pub const STORAGE_LIMIT: u64 = 2_000_000_000;
pub const REPO_LIMIT: u64 = 100_000_000; pub const REPO_LIMIT: u64 = 100_000_000;
......
...@@ -4,12 +4,14 @@ use cli::Args; ...@@ -4,12 +4,14 @@ use cli::Args;
use crate::diagnosis::{RemedyJob, Reportable, ReportJob, ReportPending}; use crate::diagnosis::{RemedyJob, Reportable, ReportJob, ReportPending};
use crate::diagnosis::gitlab_connection::ConnectionJob; use crate::diagnosis::gitlab_connection::ConnectionJob;
use crate::diagnosis::package_analysis::PackageAnalysisJob;
use crate::diagnosis::pipeline_analysis::PipelineAnalysisJob; use crate::diagnosis::pipeline_analysis::PipelineAnalysisJob;
use crate::diagnosis::pipeline_clean::PipelineCleanJob; use crate::diagnosis::pipeline_clean::PipelineCleanJob;
use crate::diagnosis::ReportStatus; use crate::diagnosis::ReportStatus;
pub mod diagnosis; pub mod diagnosis;
pub mod cli; pub mod cli;
pub mod api;
fn main() { fn main() {
let args = Args::from_args(); let args = Args::from_args();
...@@ -37,6 +39,7 @@ fn main() { ...@@ -37,6 +39,7 @@ fn main() {
if let Some(days) = cli::input_clean_artifacts() { if let Some(days) = cli::input_clean_artifacts() {
let report_pending = PipelineCleanJob::from(pipeline_report, days).remedy(); let report_pending = PipelineCleanJob::from(pipeline_report, days).remedy();
let _ = cli::display_report_pending(report_pending); let _ = cli::display_report_pending(report_pending);
} else { } else {
cli::console_report_statuses( cli::console_report_statuses(
&[ReportStatus::WARNING("Jobs deletion cancelled".to_string())], &[ReportStatus::WARNING("Jobs deletion cancelled".to_string())],
...@@ -44,5 +47,12 @@ fn main() { ...@@ -44,5 +47,12 @@ fn main() {
} }
} }
// Analysis of packages
let report_pending = PackageAnalysisJob::from(&connection_data).diagnose();
let package_report = cli::display_report_pending(report_pending);
eprintln!("{:?}", package_report.packages);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment