This package provides a GitHub commits loader for Astro. It allows you to load commits from any GitHub repository and use them as content in your Astro project.
npm install @ascorbic/github-commitsThis package requires Astro 5.0.0 or later.
You can use the GitHub loader in your content collection configuration:
// src/content/config.ts
import { defineCollection } from "astro:content";
import { githubLoader } from "@ascorbic/github-commits";
const commits = defineCollection({
loader: githubLoader({
repo: "owner/repo",
token: import.meta.env.GITHUB_TOKEN,
perPage: 15,
fetchFilesFor: 5,
}),
});
export const collections = { commits };You can then use these like any other content collection in Astro:
---
import { getCollection } from "astro:content";
const commits = await getCollection("commits");
---
<ul>
{commits.map((commit) => (
<li>
<strong>{commit.data.shortSha}</strong> - {commit.data.message}
<br />
<small>by {commit.data.author} on {commit.data.date.toDateString()}</small>
</li>
))}
</ul>The githubLoader function takes an object with the following options:
repo(required): GitHub repository in the format "owner/repo"token(optional): GitHub personal access token for higher API rate limitsperPage(optional): Number of commits to fetch per page (default: 15)timeoutMs(optional): Request timeout in milliseconds (default: 8000)fetchFilesFor(optional): Number of recent commits to fetch files for (default: 0)
Each commit item contains:
sha: Full commit SHAshortSha: Shortened 7-character SHAmessage: Commit message (first line)author: Author namedate: Commit datefiles: Array of changed files (if fetchFilesFor > 0)
Each file contains:
filename: File pathstatus: File status (added, modified, deleted)changes: Total changesadditions: Number of additionsdeletions: Number of deletions