Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(/github/commits): support filter by path #435

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

sidvishnoi marked this conversation as resolved.
Show resolved Hide resolved
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