Skip to content

Commit

Permalink
Remove organization concept
Browse files Browse the repository at this point in the history
This is some groundwork to eventually use the landscape YAML file as
data source.

Related to #471

Signed-off-by: Sergio Castaño Arteaga <[email protected]>
Signed-off-by: Cintia Sanchez Garcia <[email protected]>
Co-authored-by: Sergio Castaño Arteaga <[email protected]>
Co-authored-by: Cintia Sanchez Garcia <[email protected]>
  • Loading branch information
tegioz and cynthia-sg committed Aug 30, 2022
1 parent 04071b0 commit a31aa7b
Show file tree
Hide file tree
Showing 40 changed files with 330 additions and 654 deletions.
19 changes: 6 additions & 13 deletions .gitpod/sample_data.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
insert into organization (organization_id, name, home_url, logo_url, foundation)
values ('00000001-0000-0000-0000-000000000000', 'artifact-hub', 'https://artifacthub.io/', 'https://raw.githubusercontent.com/cncf/artwork/master/projects/artifacthub/icon/color/artifacthub-icon-color.svg', 'cncf');
insert into organization (organization_id, name, home_url, logo_url, foundation)
values ('00000002-0000-0000-0000-000000000000', 'containerd', 'https://containerd.io', 'https://raw.githubusercontent.com/cncf/artwork/master/projects/containerd/icon/color/containerd-icon-color.svg', 'cncf');
insert into organization (organization_id, name, home_url, logo_url, foundation)
values ('00000003-0000-0000-0000-000000000000', 'core-dns', 'https://coredns.io', 'https://raw.githubusercontent.com/cncf/artwork/master/projects/coredns/icon/color/coredns-icon-color.svg', 'cncf');

insert into project (project_id, name, display_name, description, category, devstats_url, maturity, organization_id)
values ('00000000-0001-0000-0000-000000000000', 'artifact-hub', 'Artifact Hub', 'Artifact Hub is a web-based application that enables finding, installing, and publishing packages and configurations for CNCF projects.', 'app definition', 'https://artifacthub.devstats.cncf.io/', 'sandbox', '00000001-0000-0000-0000-000000000000');
insert into project (project_id, name, description, category, devstats_url, maturity, organization_id)
values ('00000000-0002-0000-0000-000000000000', 'containerd', 'An industry-standard container runtime with an emphasis on simplicity, robustness and portability.', 'runtime', 'https://containerd.devstats.cncf.io', 'graduated', '00000002-0000-0000-0000-000000000000');
insert into project (project_id, name, display_name, category, description, devstats_url, maturity, organization_id)
values ('00000000-0003-0000-0000-000000000000', 'core-dns', 'CoreDNS', 'CoreDNS is a DNS server. It is written in Go. It can be used in a multitude of environments because of its flexibility.', 'orchestration', 'https://coredns.devstats.cncf.io', 'graduated', '00000003-0000-0000-0000-000000000000');
insert into project (project_id, name, display_name, description, category, devstats_url, maturity, foundation_id)
values ('00000000-0001-0000-0000-000000000000', 'artifact-hub', 'Artifact Hub', 'Artifact Hub is a web-based application that enables finding, installing, and publishing packages and configurations for CNCF projects.', 'app definition', 'https://artifacthub.devstats.cncf.io/', 'sandbox', 'cncf');
insert into project (project_id, name, description, category, devstats_url, maturity, foundation_id)
values ('00000000-0002-0000-0000-000000000000', 'containerd', 'An industry-standard container runtime with an emphasis on simplicity, robustness and portability.', 'runtime', 'https://containerd.devstats.cncf.io', 'graduated', 'cncf');
insert into project (project_id, name, display_name, category, description, devstats_url, maturity, foundation_id)
values ('00000000-0003-0000-0000-000000000000', 'core-dns', 'CoreDNS', 'CoreDNS is a DNS server. It is written in Go. It can be used in a multitude of environments because of its flexibility.', 'orchestration', 'https://coredns.devstats.cncf.io', 'graduated', 'cncf');

insert into repository (repository_id, name, url, check_sets, project_id)
values ('00000000-0000-0001-0000-000000000000', 'artifact-hub', 'https://github.com/artifacthub/hub', '{community,code}', '00000000-0001-0000-0000-000000000000');
Expand Down
26 changes: 8 additions & 18 deletions clomonitor-apiserver/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,20 @@ pub(crate) trait DB {
async fn project(
&self,
foundation: &str,
org: &str,
project: &str,
) -> Result<Option<JsonString>>;

/// Get project's rating.
async fn project_rating(
&self,
foundation: &str,
org: &str,
project: &str,
) -> Result<Option<String>>;

/// Get project's score.
async fn project_score(
&self,
foundation: &str,
org: &str,
project: &str,
) -> Result<Option<Score>>;

Expand Down Expand Up @@ -72,16 +69,15 @@ impl DB for PgDB {
async fn project(
&self,
foundation: &str,
org: &str,
project: &str,
) -> Result<Option<JsonString>> {
let row = self
.pool
.get()
.await?
.query_one(
"select get_project($1::text, $2::text, $3::text)::text",
&[&foundation, &org, &project],
"select get_project($1::text, $2::text)::text",
&[&foundation, &project],
)
.await?;
let project: Option<String> = row.get(0);
Expand All @@ -91,7 +87,6 @@ impl DB for PgDB {
async fn project_rating(
&self,
foundation: &str,
org: &str,
project: &str,
) -> Result<Option<String>> {
let rows = self
Expand All @@ -102,12 +97,10 @@ impl DB for PgDB {
"
select rating
from project p
join organization o using (organization_id)
where o.foundation::text = $1::text
and o.name = $2::text
and p.name = $3::text
where p.foundation_id = $1::text
and p.name = $2::text
",
&[&foundation, &org, &project],
&[&foundation, &project],
)
.await?;
if rows.len() != 1 {
Expand All @@ -120,7 +113,6 @@ impl DB for PgDB {
async fn project_score(
&self,
foundation: &str,
org: &str,
project: &str,
) -> Result<Option<Score>> {
let rows = self
Expand All @@ -131,12 +123,10 @@ impl DB for PgDB {
"
select score
from project p
join organization o using (organization_id)
where o.foundation::text = $1::text
and o.name = $2::text
and p.name = $3::text
where p.foundation_id = $1::text
and p.name = $2::text
",
&[&foundation, &org, &project],
&[&foundation, &project],
)
.await?;
if rows.len() != 1 {
Expand Down
21 changes: 10 additions & 11 deletions clomonitor-apiserver/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ pub const REPORT_SUMMARY_HEIGHT: u32 = 470;
/// Handler that returns the information needed to render the project's badge.
pub(crate) async fn badge(
Extension(db): Extension<DynDB>,
Path((foundation, org, project)): Path<(String, String, String)>,
Path((foundation, project)): Path<(String, String)>,
) -> impl IntoResponse {
// Get project rating from database
let rating = db
.project_rating(&foundation, &org, &project)
.project_rating(&foundation, &project)
.await
.map_err(internal_error)?;
if rating.is_none() {
Expand Down Expand Up @@ -121,19 +121,18 @@ pub(crate) async fn index(
pub(crate) async fn index_project(
Extension(cfg): Extension<Arc<Config>>,
Extension(tmpl): Extension<Arc<Tera>>,
Path((foundation, org, project)): Path<(String, String, String)>,
Path((foundation, project)): Path<(String, String)>,
) -> impl IntoResponse {
let mut ctx = Context::new();
ctx.insert("title", &project);
ctx.insert("description", INDEX_META_DESCRIPTION_PROJECT);
ctx.insert(
"image",
&format!(
"{}/projects/{}/{}/{}/report-summary.png",
"{}/projects/{}/{}/report-summary.png",
cfg.get_string("apiserver.baseURL")
.expect("base url not found"),
&foundation,
&org,
&project
),
);
Expand All @@ -151,11 +150,11 @@ pub(crate) async fn index_project(
/// Handler that returns some information about the requested project.
pub(crate) async fn project(
Extension(db): Extension<DynDB>,
Path((foundation, org, project)): Path<(String, String, String)>,
Path((foundation, project)): Path<(String, String)>,
) -> impl IntoResponse {
// Get project from database
let project = db
.project(&foundation, &org, &project)
.project(&foundation, &project)
.await
.map_err(internal_error)?;

Expand Down Expand Up @@ -190,11 +189,11 @@ impl ReportSummaryTemplate {
/// Handler that returns a PNG image with the project's report summary.
pub(crate) async fn report_summary_png(
Extension(db): Extension<DynDB>,
Path((foundation, org, project)): Path<(String, String, String)>,
Path((foundation, project)): Path<(String, String)>,
) -> impl IntoResponse {
// Get project score from database
let score = db
.project_score(&foundation, &org, &project)
.project_score(&foundation, &project)
.await
.map_err(internal_error)?;
if score.is_none() {
Expand Down Expand Up @@ -231,12 +230,12 @@ pub(crate) async fn report_summary_png(
/// Handler that returns an SVG image with the project's report summary.
pub(crate) async fn report_summary_svg(
Extension(db): Extension<DynDB>,
Path((foundation, org, project)): Path<(String, String, String)>,
Path((foundation, project)): Path<(String, String)>,
Query(params): Query<HashMap<String, String>>,
) -> impl IntoResponse {
// Get project score from database
let score = db
.project_score(&foundation, &org, &project)
.project_score(&foundation, &project)
.await
.map_err(internal_error)?;

Expand Down
57 changes: 28 additions & 29 deletions clomonitor-apiserver/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ pub(crate) fn setup(cfg: Arc<Config>, db: DynDB) -> Result<Router> {
// Setup API routes
let api_routes = Router::new()
.route("/projects/search", get(search_projects))
.route("/projects/:foundation/:org/:project", get(project))
.route("/projects/:foundation/:org/:project/badge", get(badge))
.route("/projects/:foundation/:project", get(project))
.route("/projects/:foundation/:project/badge", get(badge))
.route(
"/projects/:foundation/:org/:project/report-summary",
"/projects/:foundation/:project/report-summary",
get(report_summary_svg),
)
.route("/stats", get(stats));

// Setup router
let mut router = Router::new()
.route("/", get(index))
.route("/projects/:foundation/:org/:project", get(index_project))
.route("/projects/:foundation/:project", get(index_project))
.route(
"/projects/:foundation/:org/:project/report-summary.png",
"/projects/:foundation/:project/report-summary.png",
get(report_summary_png),
)
.route("/data/repositories.csv", get(repositories_checks))
Expand Down Expand Up @@ -123,24 +123,23 @@ mod tests {

const TESTDATA_PATH: &str = "src/testdata";
const FOUNDATION: &str = "cncf";
const ORG: &str = "artifact-hub";
const PROJECT: &str = "artifact-hub";

#[tokio::test]
async fn badge_found() {
let mut db = MockDB::new();
db.expect_project_rating()
.with(eq(FOUNDATION), eq(ORG), eq(PROJECT))
.with(eq(FOUNDATION), eq(PROJECT))
.times(1)
.returning(|_: &str, _: &str, _: &str| {
.returning(|_: &str, _: &str| {
Box::pin(future::ready(Ok(Some("a".to_string()))))
});

let response = setup_test_router(db)
.oneshot(
Request::builder()
.method("GET")
.uri(format!("/api/projects/{FOUNDATION}/{ORG}/{PROJECT}/badge"))
.uri(format!("/api/projects/{FOUNDATION}/{PROJECT}/badge"))
.body(Body::empty())
.unwrap(),
)
Expand Down Expand Up @@ -174,15 +173,15 @@ mod tests {
async fn badge_not_found() {
let mut db = MockDB::new();
db.expect_project_rating()
.with(eq(FOUNDATION), eq(ORG), eq(PROJECT))
.with(eq(FOUNDATION), eq(PROJECT))
.times(1)
.returning(|_: &str, _: &str, _: &str| Box::pin(future::ready(Ok(None))));
.returning(|_: &str, _: &str| Box::pin(future::ready(Ok(None))));

let response = setup_test_router(db)
.oneshot(
Request::builder()
.method("GET")
.uri(format!("/api/projects/{FOUNDATION}/{ORG}/{PROJECT}/badge"))
.uri(format!("/api/projects/{FOUNDATION}/{PROJECT}/badge"))
.body(Body::empty())
.unwrap(),
)
Expand Down Expand Up @@ -280,7 +279,7 @@ mod tests {
.oneshot(
Request::builder()
.method("GET")
.uri(format!("/projects/{FOUNDATION}/{ORG}/{PROJECT}"))
.uri(format!("/projects/{FOUNDATION}/{PROJECT}"))
.body(Body::empty())
.unwrap(),
)
Expand All @@ -298,7 +297,7 @@ mod tests {
render_index(
PROJECT,
INDEX_META_DESCRIPTION_PROJECT,
"http://localhost:8000/projects/cncf/artifact-hub/artifact-hub/report-summary.png"
"http://localhost:8000/projects/cncf/artifact-hub/report-summary.png"
)
);
}
Expand All @@ -307,9 +306,9 @@ mod tests {
async fn project_found() {
let mut db = MockDB::new();
db.expect_project()
.with(eq(FOUNDATION), eq(ORG), eq(PROJECT))
.with(eq(FOUNDATION), eq(PROJECT))
.times(1)
.returning(|_, _, _| {
.returning(|_, _| {
Box::pin(future::ready(Ok(Some(
r#"{"project": "info"}"#.to_string(),
))))
Expand All @@ -319,7 +318,7 @@ mod tests {
.oneshot(
Request::builder()
.method("GET")
.uri(format!("/api/projects/{FOUNDATION}/{ORG}/{PROJECT}"))
.uri(format!("/api/projects/{FOUNDATION}/{PROJECT}"))
.body(Body::empty())
.unwrap(),
)
Expand All @@ -342,15 +341,15 @@ mod tests {
async fn project_not_found() {
let mut db = MockDB::new();
db.expect_project()
.with(eq(FOUNDATION), eq(ORG), eq(PROJECT))
.with(eq(FOUNDATION), eq(PROJECT))
.times(1)
.returning(|_: &str, _: &str, _: &str| Box::pin(future::ready(Ok(None))));
.returning(|_: &str, _: &str| Box::pin(future::ready(Ok(None))));

let response = setup_test_router(db)
.oneshot(
Request::builder()
.method("GET")
.uri(format!("/api/projects/{FOUNDATION}/{ORG}/{PROJECT}"))
.uri(format!("/api/projects/{FOUNDATION}/{PROJECT}"))
.body(Body::empty())
.unwrap(),
)
Expand All @@ -364,16 +363,16 @@ mod tests {
async fn report_summary_png_not_found() {
let mut db = MockDB::new();
db.expect_project_score()
.with(eq(FOUNDATION), eq(ORG), eq(PROJECT))
.with(eq(FOUNDATION), eq(PROJECT))
.times(1)
.returning(|_: &str, _: &str, _: &str| Box::pin(future::ready(Ok(None))));
.returning(|_: &str, _: &str| Box::pin(future::ready(Ok(None))));

let response = setup_test_router(db)
.oneshot(
Request::builder()
.method("GET")
.uri(format!(
"/projects/{FOUNDATION}/{ORG}/{PROJECT}/report-summary.png"
"/projects/{FOUNDATION}/{PROJECT}/report-summary.png"
))
.body(Body::empty())
.unwrap(),
Expand All @@ -388,9 +387,9 @@ mod tests {
async fn report_summary_svg_found() {
let mut db = MockDB::new();
db.expect_project_score()
.with(eq(FOUNDATION), eq(ORG), eq(PROJECT))
.with(eq(FOUNDATION), eq(PROJECT))
.times(1)
.returning(|_: &str, _: &str, _: &str| {
.returning(|_: &str, _: &str| {
let score = Score {
global: 80.0,
documentation: Some(80.0),
Expand All @@ -405,7 +404,7 @@ mod tests {
Request::builder()
.method("GET")
.uri(format!(
"/api/projects/{FOUNDATION}/{ORG}/{PROJECT}/report-summary"
"/api/projects/{FOUNDATION}/{PROJECT}/report-summary"
))
.body(Body::empty())
.unwrap(),
Expand All @@ -429,16 +428,16 @@ mod tests {
async fn report_summary_svg_not_found() {
let mut db = MockDB::new();
db.expect_project_score()
.with(eq(FOUNDATION), eq(ORG), eq(PROJECT))
.with(eq(FOUNDATION), eq(PROJECT))
.times(1)
.returning(|_: &str, _: &str, _: &str| Box::pin(future::ready(Ok(None))));
.returning(|_: &str, _: &str| Box::pin(future::ready(Ok(None))));

let response = setup_test_router(db)
.oneshot(
Request::builder()
.method("GET")
.uri(format!(
"/api/projects/{FOUNDATION}/{ORG}/{PROJECT}/report-summary"
"/api/projects/{FOUNDATION}/{PROJECT}/report-summary"
))
.body(Body::empty())
.unwrap(),
Expand Down
Loading

0 comments on commit a31aa7b

Please sign in to comment.