Skip to content

Commit 77d0c69

Browse files
committed
add date to config pages
closes #2949
1 parent 426e79c commit 77d0c69

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

sites/configs/src/content.config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ const configs = defineCollection({
8484
}),
8585
schema: z.object({
8686
extension: z.literal('md').or(z.literal('config')),
87+
sha: z.string(),
88+
lastCommit: z.string().datetime(),
8789
}),
8890
});
8991

sites/configs/src/pages/configs/[configslug].astro

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ const gh_url = `https://github.com/nf-core/configs/blob/master/docs/${configslug
128128
</a>`
129129
: "",
130130
},
131+
{ title: "last updated", value: config.data.lastCommit?.toString() },
131132
]}
132133
/>
133134
)

sites/main-site/utils/loaders.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// originally from https://github.com/gingerchew/astro-github-file-loader
22
import type { AstroConfig, MarkdownHeading } from "astro";
33
import type { Loader, LoaderContext } from "astro/loaders";
4+
import { octokit } from "@components/octokit.js";
45

56
type GithubTreeLeaf = {
67
path: string;
@@ -55,10 +56,15 @@ function createProcessors(processors: Processors) {
5556

5657
export function githubFileLoader({ org, repo, ref, processors, path }: PolicyLoaderConfig): Loader {
5758
const baseUrl = `https://raw.githubusercontent.com/${org}/${repo}/${ref}`;
58-
const apiUrl = `https://api.github.com/repos/${org}/${repo}/git/trees/${ref}?recursive=1`;
5959

6060
const get = async <T>(filepath: string, type: "json" | "text"): Promise<T> => {
6161
try {
62+
// If this is a GitHub API request, use octokit
63+
if (filepath.startsWith('https://api.github.com')) {
64+
const response = await octokit.request('GET ' + filepath.replace('https://api.github.com', ''));
65+
return response.data as T;
66+
}
67+
// Otherwise use fetch for raw content
6268
const response = await fetch(filepath);
6369
if (!response.ok) throw new Error(`HTTP ${response.status}`);
6470
return type === "json" ? await response.json() : await response.text();
@@ -76,9 +82,15 @@ export function githubFileLoader({ org, repo, ref, processors, path }: PolicyLoa
7682
// Get the last tree SHA we processed
7783
const lastTreeSha = meta.get('lastTreeSha');
7884

79-
// Fetch current tree data
80-
const treeData = await get<GithubTreeData>(apiUrl, "json");
81-
const currentTreeSha = treeData.hash;
85+
// Fetch current tree data using octokit directly
86+
const { data: treeData } = await octokit.rest.git.getTree({
87+
owner: org,
88+
repo: repo,
89+
tree_sha: ref,
90+
recursive: '1'
91+
});
92+
93+
const currentTreeSha = treeData.sha;
8294

8395
// If tree hasn't changed, we can skip processing
8496
if (lastTreeSha && lastTreeSha === currentTreeSha) {
@@ -93,14 +105,14 @@ export function githubFileLoader({ org, repo, ref, processors, path }: PolicyLoa
93105

94106
for await (const leaf of treeData.tree) {
95107
// Skip directories and .github files
96-
if (leaf.type === "tree" || leaf.path.includes(".github/")) continue;
108+
if (leaf.type === "tree" || leaf.path?.includes(".github/")) continue;
97109

98110
const pathMatch =
99-
(path && typeof path === "string" && leaf.path.includes(path)) ||
100-
(path && typeof path !== "string" && path.test(leaf.path));
111+
(path && typeof path === "string" && leaf.path?.includes(path)) ||
112+
(path && typeof path !== "string" && path.test(leaf.path ?? ""));
101113
if (!pathMatch) continue;
102114

103-
const [id, extension] = leaf.path.split(".");
115+
const [id, extension] = leaf.path?.split(".") ?? [];
104116
processedIds.add(id);
105117

106118
// Check if we already have this file with the same SHA
@@ -117,6 +129,9 @@ export function githubFileLoader({ org, repo, ref, processors, path }: PolicyLoa
117129

118130
const { html, metadata } = await $[extension as keyof Processors](body, config);
119131

132+
// get the last commit date for the file using GitHub API
133+
const lastCommit = await get<any>(`https://api.github.com/repos/${org}/${repo}/commits?path=${leaf.path}&sha=${ref}`, "json");
134+
120135
store.set({
121136
id,
122137
data: {
@@ -125,7 +140,8 @@ export function githubFileLoader({ org, repo, ref, processors, path }: PolicyLoa
125140
org,
126141
repo,
127142
ref,
128-
sha: leaf.sha // Store SHA for future comparisons
143+
sha: leaf.sha, // Store SHA for future comparisons
144+
lastCommit: lastCommit[0].commit.committer.date,
129145
},
130146
body,
131147
rendered: { html, metadata },

0 commit comments

Comments
 (0)