Skip to content

Commit

Permalink
Avoid Temporal types in the content type declaration.
Browse files Browse the repository at this point in the history
Follow withastro/docs#5227 for an eventual explanation.
  • Loading branch information
jyasskin committed Oct 29, 2023
1 parent 485c896 commit 5e8b192
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions frontend/src/content/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { RepoSummary } from '@lib/repo-summaries';
import { RepoSummaryInContent } from '@lib/repo-summaries';
import { defineCollection } from 'astro:content';

const githubCollection = defineCollection({
type: "data",
schema: RepoSummary,
schema: RepoSummaryInContent,
});

export const collections = {
Expand Down
30 changes: 24 additions & 6 deletions frontend/src/lib/repo-summaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,54 @@ import { z } from 'zod';
export const SloType = z.enum(["triage", "urgent", "important", "none"]);
export type SloType = z.infer<typeof SloType>;

const checkInstant = z.string().refine(val => {
try { Temporal.Instant.from(val); return true; } catch { return false; }
});
const checkDuration = z.string().refine(val => {
try { Temporal.Duration.from(val); return true; } catch { return false; }
});
const duration = z.string().transform(val => Temporal.Duration.from(val));
const instant = z.string().transform(val => Temporal.Instant.from(val));

export const IssueSummary = z.object({
export const IssueSummaryInContent = z.object({
url: z.string().url(),
title: z.string(),
author: z.string().optional(),
createdAt: instant,
createdAt: checkInstant,
pull_request: z.object({ draft: z.boolean().default(false) }).optional(),
labels: z.array(z.string()),
sloTimeUsed: duration,
sloTimeUsed: checkDuration,
whichSlo: SloType,
stats: z.object({
numTimelineItems: z.number(),
numComments: z.number().optional(),
numLabels: z.number(),
}).optional(),
})
export type IssueSummaryInContent = z.infer<typeof IssueSummaryInContent>;

export const IssueSummary = IssueSummaryInContent.extend({
createdAt: instant,
sloTimeUsed: duration,
});
export type IssueSummary = z.infer<typeof IssueSummary>;

export const RepoSummary = z.object({
cachedAt: instant,
export const RepoSummaryInContent = z.object({
cachedAt: checkInstant,
org: z.string(),
repo: z.string(),
issues: IssueSummary.array(),
issues: IssueSummaryInContent.array(),
labelsPresent: z.boolean(),
stats: z.object({
numLabels: z.number(),
numIssues: z.number(),
numPRs: z.number(),
}).optional(),
});
export type RepoSummaryInContent = z.infer<typeof RepoSummaryInContent>;

export const RepoSummary = RepoSummaryInContent.extend({
cachedAt: instant,
issues: IssueSummary.array(),
});
export type RepoSummary = z.infer<typeof RepoSummary>;
3 changes: 2 additions & 1 deletion frontend/src/pages/[org]/[repo].astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
import Issue from "@components/Issue.astro";
import Layout from "@layouts/Layout.astro";
import { IssueSummary } from "@lib/repo-summaries";
import { cmpByTimeUsed, groupBySlo } from "@lib/slo";
import type {
GetStaticPaths,
Expand Down Expand Up @@ -30,7 +31,7 @@ const {
importantViolations,
urgent,
urgentViolations,
} = groupBySlo(details.issues);
} = groupBySlo(IssueSummary.array().parse(details.issues));
urgent.push(...urgentViolations);
urgent.sort(cmpByTimeUsed);
important.push(...importantViolations);
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { groupBySlo } from "@lib/slo";
import * as ghLabels from "@lib/triage-labels";
import { getCollection } from "astro:content";
import Layout from "../layouts/Layout.astro";
import { IssueSummary } from "@lib/repo-summaries";
const repos = await getCollection("github");
Expand All @@ -21,7 +22,7 @@ const andFormatter = new Intl.ListFormat("en", {
});
const repoSummaries = repos.map((repo) => {
const groups = groupBySlo(repo.data.issues);
const groups = groupBySlo(IssueSummary.array().parse(repo.data.issues));
const triageViolations = groups.triageViolations.length;
const urgentViolations = groups.urgentViolations.length;
Expand Down

0 comments on commit 5e8b192

Please sign in to comment.