/
mod.rs
175 lines (159 loc) · 5.51 KB
/
mod.rs
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
use self::{
check::*,
checks::util::helpers::{find_exemption, should_skip_check},
};
use anyhow::Result;
use async_trait::async_trait;
use clap::ValueEnum;
#[cfg(feature = "mocks")]
use mockall::automock;
use postgres_types::ToSql;
use serde::{Deserialize, Serialize};
use std::{fmt, path::PathBuf, sync::Arc};
use time::Date;
mod check;
mod checks;
mod metadata;
mod report;
pub use self::{
check::{CheckId, CheckOutput},
report::*,
};
pub use checks::datasource::github::setup_http_client as setup_github_http_client;
pub(crate) use checks::*;
/// Type alias to represent a Linter trait object.
pub type DynLinter = Arc<dyn Linter + Send + Sync>;
/// Trait that defines some operations a Linter implementation must support.
#[async_trait]
#[cfg_attr(feature = "mocks", automock)]
pub trait Linter {
/// Lint the repository provided returning a report with the results.
async fn lint(&self, input: &LinterInput) -> Result<Report>;
}
/// Input used by the linter to perform its operations.
#[derive(Debug, Clone, Default)]
pub struct LinterInput {
pub project: Option<Project>,
pub root: PathBuf,
pub url: String,
pub check_sets: Vec<CheckSet>,
pub github_token: String,
}
/// Project's details
#[derive(Debug, Clone, Default)]
pub struct Project {
pub name: String,
pub accepted_at: Option<Date>,
pub maturity: Option<String>,
pub foundation: Foundation,
}
/// Foundation's details
#[derive(Debug, Clone, Default)]
pub struct Foundation {
pub foundation_id: String,
pub landscape_url: Option<String>,
}
/// Check sets define a set of checks that will be run on a given repository.
/// Multiple check sets can be assigned to a repository.
#[derive(Debug, Clone, PartialEq, Eq, Hash, ValueEnum, Serialize, Deserialize, ToSql)]
#[serde(rename_all = "kebab-case")]
#[postgres(name = "check_set")]
pub enum CheckSet {
#[postgres(name = "code")]
Code,
#[postgres(name = "code-lite")]
CodeLite,
#[postgres(name = "community")]
Community,
#[postgres(name = "docs")]
Docs,
}
impl fmt::Display for CheckSet {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let output = match self {
Self::Code => "CODE",
Self::CodeLite => "CODE-LITE",
Self::Community => "COMMUNITY",
Self::Docs => "DOCS",
};
write!(f, "{output}")
}
}
/// CLOMonitor core linter (Linter implementation).
pub struct CoreLinter;
#[allow(clippy::new_without_default)]
impl CoreLinter {
/// Create a new CoreLinter instance.
#[must_use]
pub fn new() -> Self {
Self
}
}
#[async_trait]
impl Linter for CoreLinter {
async fn lint(&self, li: &LinterInput) -> Result<Report> {
// Prepare check input
let ci = CheckInput::new(li).await?;
// Run some async checks concurrently
let (contributing, summary_table, trademark_disclaimer) = tokio::join!(
run_async!(contributing, &ci),
run_async!(summary_table, &ci),
run_async!(trademark_disclaimer, &ci),
);
// Run some sync checks needed in advance
let spdx_id = run!(license_spdx_id, &ci);
let mut spdx_id_value: Option<String> = None;
if let Some(r) = &spdx_id {
spdx_id_value = r.value.clone();
}
// Run the remaining sync checks and build report
let mut report = Report {
documentation: Documentation {
adopters: run!(adopters, &ci),
changelog: run!(changelog, &ci),
code_of_conduct: run!(code_of_conduct, &ci),
contributing,
governance: run!(governance, &ci),
maintainers: run!(maintainers, &ci),
readme: run!(readme, &ci),
roadmap: run!(roadmap, &ci),
summary_table,
website: run!(website, &ci),
},
license: License {
license_approved: license_approved::check(&ci, spdx_id_value),
license_scanning: run!(license_scanning, &ci),
license_spdx_id: spdx_id,
},
best_practices: BestPractices {
artifacthub_badge: run!(artifacthub_badge, &ci),
cla: run!(cla, &ci),
community_meeting: run!(community_meeting, &ci),
dco: run!(dco, &ci),
github_discussions: run!(github_discussions, &ci),
openssf_badge: run!(openssf_badge, &ci),
openssf_scorecard_badge: run!(openssf_scorecard_badge, &ci),
recent_release: run!(recent_release, &ci),
slack_presence: run!(slack_presence, &ci),
},
security: Security {
binary_artifacts: run!(binary_artifacts, &ci),
code_review: run!(code_review, &ci),
dangerous_workflow: run!(dangerous_workflow, &ci),
dependencies_policy: run!(dependencies_policy, &ci),
dependency_update_tool: run!(dependency_update_tool, &ci),
maintained: run!(maintained, &ci),
sbom: run!(sbom, &ci),
security_insights: run!(security_insights, &ci),
security_policy: run!(security_policy, &ci),
signed_releases: run!(signed_releases, &ci),
token_permissions: run!(token_permissions, &ci),
},
legal: Legal {
trademark_disclaimer,
},
};
report.apply_exemptions();
Ok(report)
}
}