Skip to content

Commit

Permalink
Merge branch 'main' into scope-transfer-enforce-limits
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Feb 29, 2024
2 parents 8507880 + ac860c0 commit f24cbe1
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 31 deletions.
22 changes: 19 additions & 3 deletions api/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,22 +293,38 @@ fn generate_score(
|| main_entrypoint_doc
.is_some_and(|doc| doc.doc.as_ref().is_some_and(|doc| !doc.is_empty())),
has_readme_examples,
all_entrypoints_docs: all_entrypoints_have_module_doc(doc_nodes_by_url),
all_entrypoints_docs: all_entrypoints_have_module_doc(
doc_nodes_by_url,
main_entrypoint,
readme.is_some(),
),
percentage_documented_symbols: percentage_of_symbols_with_docs(
doc_nodes_by_url,
),
all_fast_check,
}
}

fn all_entrypoints_have_module_doc(doc_nodes_by_url: &DocNodesByUrl) -> bool {
'modules: for (_specifier, nodes) in doc_nodes_by_url {
fn all_entrypoints_have_module_doc(
doc_nodes_by_url: &DocNodesByUrl,
main_entrypoint: Option<ModuleSpecifier>,
has_readme: bool,
) -> bool {
'modules: for (specifier, nodes) in doc_nodes_by_url {
for node in nodes {
if node.kind == DocNodeKind::ModuleDoc {
continue 'modules;
}
}

if main_entrypoint
.as_ref()
.is_some_and(|main_entrypoint| main_entrypoint == specifier)
&& has_readme
{
continue 'modules;
}

return false;
}

Expand Down
6 changes: 3 additions & 3 deletions api/src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ pub struct ApiPackageScore {
impl ApiPackageScore {
pub const MAX_SCORE: u32 = 18;

pub fn normalized_score(&self) -> u32 {
(self.total * 10) / Self::MAX_SCORE
pub fn score_percentage(&self) -> u32 {
(self.total * 100) / Self::MAX_SCORE
}
}

Expand Down Expand Up @@ -445,7 +445,7 @@ impl From<PackageWithGitHubRepoAndMeta> for ApiPackage {
updated_at: package.updated_at,
created_at: package.created_at,
version_count: package.version_count as u64,
score: score.normalized_score(),
score: score.score_percentage(),
latest_version: package.latest_version,
when_featured: package.when_featured,
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/orama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl OramaClient {
package: &Package,
meta: &PackageVersionMeta,
) -> Result<(), anyhow::Error> {
let score = ApiPackageScore::from((meta, package)).normalized_score();
let score = ApiPackageScore::from((meta, package)).score_percentage();

let id = format!("@{}/{}", package.scope, package.name);
let res = self
Expand Down
6 changes: 4 additions & 2 deletions frontend/fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import * as $admin_scopes from "./routes/admin/scopes.tsx";
import * as $admin_users from "./routes/admin/users.tsx";
import * as $admin_waitlist from "./routes/admin/waitlist.tsx";
import * as $auth from "./routes/auth.tsx";
import * as $badges_package from "./routes/badges/package.ts";
import * as $badges_package_score from "./routes/badges/package_score.ts";
import * as $docs_id_ from "./routes/docs/[...id].tsx";
import * as $docs_index from "./routes/docs/index.ts";
import * as $gfm_css from "./routes/gfm.css.ts";
Expand All @@ -40,7 +42,6 @@ import * as $package_settings from "./routes/package/settings.tsx";
import * as $package_source from "./routes/package/source.tsx";
import * as $package_symbols from "./routes/package/symbols.tsx";
import * as $package_versions from "./routes/package/versions.tsx";
import * as $package_badge from "./routes/package_badge.ts";
import * as $packages from "./routes/packages.tsx";
import * as $status from "./routes/status.tsx";
import * as $user_id_ from "./routes/user/[id].tsx";
Expand Down Expand Up @@ -86,6 +87,8 @@ const manifest = {
"./routes/admin/users.tsx": $admin_users,
"./routes/admin/waitlist.tsx": $admin_waitlist,
"./routes/auth.tsx": $auth,
"./routes/badges/package.ts": $badges_package,
"./routes/badges/package_score.ts": $badges_package_score,
"./routes/docs/[...id].tsx": $docs_id_,
"./routes/docs/index.ts": $docs_index,
"./routes/gfm.css.ts": $gfm_css,
Expand All @@ -105,7 +108,6 @@ const manifest = {
"./routes/package/source.tsx": $package_source,
"./routes/package/symbols.tsx": $package_symbols,
"./routes/package/versions.tsx": $package_versions,
"./routes/package_badge.ts": $package_badge,
"./routes/packages.tsx": $packages,
"./routes/status.tsx": $status,
"./routes/user/[id].tsx": $user_id_,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
import type { Handlers, RouteConfig } from "$fresh/server.ts";
import { accepts } from "$oak_commons";
import { Package } from "../utils/api_types.ts";
import { path } from "../utils/api.ts";
import { State } from "../util.ts";
import { Package } from "../../utils/api_types.ts";
import { path } from "../../utils/api.ts";
import { State } from "../../util.ts";

export const handler: Handlers<unknown, State> = {
async GET(req, ctx) {
Expand All @@ -27,11 +27,12 @@ export const handler: Handlers<unknown, State> = {
schemaVersion: 1,
label: "",
// namedLogo: "jsr", TODO: add icon to shields.io or simple-icons. temporary solution below.
logoSvg:
'<svg xmlns="http://www.w3.org/2000/svg" width="638" height="638" fill="none"><g fill-rule="evenodd"><path fill="#121417" d="M637.272 196v196h-98v98h-343v-49h-196V245h98v-98h343v49h196Z"/><path fill="#F7DF1E" d="M100.101 343h47.171V196h49v196H51.102v-98H100.1v49ZM588.272 245v98h-49v-49h-49v147h-49V245h147ZM294.272 245v49h98v147h-147v-49h98v-49h-98V196h147v49h-98Z"/></g></svg>',
logoSvg: await Deno.readTextFile(
new URL("../../static/logo.svg", import.meta.url),
),
message: packageResp.data.latestVersion,
labelColor: "#121417",
color: "#F7DF1E",
labelColor: "rgb(8,51,68)",
color: "rgb(247,223,30)",
}),
{
headers: {
Expand Down
66 changes: 66 additions & 0 deletions frontend/routes/badges/package_score.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
import type { Handlers, RouteConfig } from "$fresh/server.ts";
import { accepts } from "$oak_commons";
import { Package } from "../../utils/api_types.ts";
import { path } from "../../utils/api.ts";
import { State } from "../../util.ts";

export const handler: Handlers<unknown, State> = {
async GET(req, ctx) {
if (
accepts(req, "application/json", "text/html", "image/*") ===
"application/json"
) {
const packageResp = await ctx.state.api.get<Package>(
path`/scopes/${ctx.params.scope}/packages/${ctx.params.package}`,
);

if (!packageResp.ok) {
if (packageResp.code === "packageNotFound") {
return new Response(null, { status: 404 });
} else {
throw packageResp;
}
} else {
return new Response(
JSON.stringify({
schemaVersion: 1,
label: "",
// namedLogo: "jsr", TODO: add icon to shields.io or simple-icons. temporary solution below.
logoSvg: await Deno.readTextFile(
new URL("../../static/logo.svg", import.meta.url),
),
message: `${packageResp.data.score}%`,
labelColor: "rgb(8,51,68)",
color: "rgb(247,223,30)",
}),
{
headers: {
"content-type": "application/json",
},
},
);
}
} else {
const url = new URL(req.url);
url.protocol = "https:";

const shieldsUrl = new URL("https://img.shields.io/endpoint");
shieldsUrl.search = url.search;
shieldsUrl.searchParams.set("url", url.href);

const res = await fetch(shieldsUrl);

return new Response(res.body, {
status: res.status,
headers: {
"content-type": res.headers.get("content-type")!,
},
});
}
},
};

export const config: RouteConfig = {
routeOverride: "/badges/@:scope/:package/score",
};
4 changes: 2 additions & 2 deletions frontend/routes/package/(_components)/PackageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export function PackageHeader(
class="flex items-center gap-2 select-none"
>
<span>score</span>
<div class="rounded-full bg-jsr-cyan-200 size-12 text-center leading-[3rem]">
<div class="rounded-full ring-1 ring-jsr-cyan-950 bg-jsr-cyan-200 size-12 text-center leading-[3rem]">
<span class="font-bold">{pkg.score}</span>
<span class="text-xs">/10</span>
<span class="text-xs">%</span>
</div>
</a>

Expand Down
23 changes: 10 additions & 13 deletions frontend/routes/package/score.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ export default function Score(
) {
const isStaff = state.user?.isStaff || false;
const canEdit = data.member?.isAdmin || isStaff;
const scorePercentage = Math.floor((data.score.total / MAX_SCORE) * 100);
const scoreColorClass = scorePercentage >= 90
const scoreColorClass = data.package.score >= 90
? "bg-green-500"
: scorePercentage >= 60
: data.package.score >= 60
? "bg-yellow-500"
: "bg-red-500";

Expand Down Expand Up @@ -66,10 +65,10 @@ export default function Score(
</div>
<div
class={`flex w-full max-w-32 items-center justify-center aspect-square rounded-full p-1.5 ${scoreColorClass}`}
style={`background-image: conic-gradient(transparent, transparent ${scorePercentage}%, white ${scorePercentage}%)`}
style={`background-image: conic-gradient(transparent, transparent ${data.package.score}%, white ${data.package.score}%)`}
>
<span class="rounded-full w-full h-full bg-white flex justify-center items-center text-center text-3xl font-bold">
{scorePercentage}%
{data.package.score}%
</span>
</div>
<div class="text-gray-500 text-sm text-center mt-6">
Expand Down Expand Up @@ -97,7 +96,6 @@ export default function Score(
/>
<ScoreItem
value={data.score.percentageDocumentedSymbols}
max={5}
scoreValue={5}
explanation="Has docs in all symbols"
/>
Expand Down Expand Up @@ -128,12 +126,7 @@ export default function Score(
}

function ScoreItem(
props:
& { explanation: string; scoreValue: number }
& ({ value: boolean } | {
value: number;
max: number;
}),
props: { explanation: string; value: boolean | number; scoreValue: number },
) {
let status: "complete" | "partial" | "missing";
if (typeof props.value === "boolean") {
Expand All @@ -160,7 +153,11 @@ function ScoreItem(

<div class="text-sm text-gray-400 pt-[0.2em]">
{typeof props.value === "number"
? <span>{Math.floor(props.max * props.value)}/{props.max}</span>
? (
<span>
{Math.floor(props.scoreValue * props.value)}/{props.scoreValue}
</span>
)
: <span>{props.value ? props.scoreValue : 0}/{props.scoreValue}
</span>}
</div>
Expand Down

0 comments on commit f24cbe1

Please sign in to comment.