Skip to content

Commit

Permalink
add support for path param for GH commits
Browse files Browse the repository at this point in the history
  • Loading branch information
jnurthen committed Aug 2, 2024
1 parent a6fa5e0 commit e9e9c92
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
9 changes: 7 additions & 2 deletions routes/github/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getCommits } from "./lib/commits.js";

export default async function route(req: Request, res: Response) {
const { org, repo } = req.params;
const { from, to } = req.query;
const { from, to , path } = req.query;
if (!from || typeof from !== "string") {
res.set("Content-Type", "text/plain");
return res.status(400).send("query parameter 'from' is required");
Expand All @@ -14,12 +14,17 @@ export default async function route(req: Request, res: Response) {
const msg = "optional query parameter 'to' must be a single numeric string";
return res.status(400).send(msg);
}
if (typeof path !== "undefined" && typeof path !== "string") {
res.set("Content-Type", "text/plain");
const msg = "optional query parameter 'path' must be a string";
return res.status(400).send(msg);
}

res.set("Cache-Control", `max-age=${seconds("30m")}`);

try {
const commits: { hash: string; message: string }[] = [];
for await (const commit of getCommits(org, repo, from, to)) {
for await (const commit of getCommits(org, repo, from, to, path)) {
commits.push({
hash: commit.abbreviatedOid,
message: commit.messageHeadline,
Expand Down
11 changes: 7 additions & 4 deletions routes/github/lib/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export async function* getCommits(
repo: string,
fromRef: string,
toRef = "HEAD",
path?: string
) {
const cacheKey = `${org}/${repo}@${fromRef}..${toRef}`;
const cacheKey = path ? `${org}/${repo}/${path}@${fromRef}..${toRef}` : `${org}/${repo}@${fromRef}..${toRef}`;
const cached = await cache.get(cacheKey);
const { since, commits } = cached || {
since: await getCommitDate(org, repo, fromRef),
Expand All @@ -61,7 +62,7 @@ export async function* getCommits(
const newCacheEntry = { since: "", commits };
let cursor: string | undefined;
do {
const data = await getCommitsSince(org, repo, since, toRef, cursor);
const data = await getCommitsSince(org, repo, since, toRef, cursor, path);
yield* data.commits;
cursor = data.cursor;

Expand Down Expand Up @@ -116,6 +117,7 @@ async function getCommitsSince(
since: string,
toRef: string,
cursor?: string,
path?: string,
) {
const query = `
query(
Expand All @@ -124,11 +126,12 @@ async function getCommitsSince(
$since: GitTimestamp!
$toRef: String!
$cursor: String
$path: String
) {
repository(owner: $org, name: $repo) {
object(expression: $toRef) {
... on Commit {
history(since: $since, after: $cursor) {
history(since: $since, after: $cursor, path: $path) {
nodes {
messageHeadline
abbreviatedOid
Expand All @@ -145,7 +148,7 @@ async function getCommitsSince(
}
`;

const variables = { org, repo, since, toRef, cursor };
const variables = { org, repo, since, toRef, cursor, path };
const { repository } = await requestData<HistoryResponse>(query, variables);

const { nodes: commits, pageInfo } = repository.object.history;
Expand Down

0 comments on commit e9e9c92

Please sign in to comment.