Skip to content

Commit

Permalink
feat: reduce http requests to post content github repo (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
axross committed May 9, 2021
1 parent 7059b46 commit 99fbe6e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 294 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/before-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
NEXT_PUBLIC_SELF_ORIGIN: ${{ secrets.NEXT_PUBLIC_SELF_ORIGIN }}
NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN: ${{ secrets.NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN }}
GITHUB_CONTENT_REPO: ${{ secrets._GITHUB_CONTENT_REPO }}
GITHUB_ACCESS_TOKEN: ${{ secrets._GITHUB_ACCESS_TOKEN }}
GITHUB_CONTENT_REF: ${{ secrets._GITHUB_CONTENT_REF }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down Expand Up @@ -43,7 +43,7 @@ jobs:
NEXT_PUBLIC_SELF_ORIGIN: ${{ secrets.NEXT_PUBLIC_SELF_ORIGIN }}
NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN: ${{ secrets.NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN }}
GITHUB_CONTENT_REPO: ${{ secrets._GITHUB_CONTENT_REPO }}
GITHUB_ACCESS_TOKEN: ${{ secrets._GITHUB_ACCESS_TOKEN }}
GITHUB_CONTENT_REF: ${{ secrets._GITHUB_CONTENT_REF }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:
NEXT_PUBLIC_SELF_ORIGIN: ${{ secrets.NEXT_PUBLIC_SELF_ORIGIN }}
NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN: ${{ secrets.NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN }}
GITHUB_CONTENT_REPO: ${{ secrets._GITHUB_CONTENT_REPO }}
GITHUB_ACCESS_TOKEN: ${{ secrets._GITHUB_ACCESS_TOKEN }}
GITHUB_CONTENT_REF: ${{ secrets._GITHUB_CONTENT_REF }}
E2E_TEST_TARGET_ORIGIN: ${{ secrets.E2E_TEST_TARGET_ORIGIN }}
steps:
- uses: actions/checkout@v2
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
NEXT_PUBLIC_SELF_ORIGIN: ${{ secrets.NEXT_PUBLIC_SELF_ORIGIN }}
NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN: ${{ secrets.NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN }}
GITHUB_CONTENT_REPO: ${{ secrets._GITHUB_CONTENT_REPO }}
GITHUB_ACCESS_TOKEN: ${{ secrets._GITHUB_ACCESS_TOKEN }}
GITHUB_CONTENT_REF: ${{ secrets._GITHUB_CONTENT_REF }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
NEXT_PUBLIC_SELF_ORIGIN: ${{ secrets.NEXT_PUBLIC_SELF_ORIGIN }}
NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN: ${{ secrets.NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN }}
GITHUB_CONTENT_REPO: ${{ secrets._GITHUB_CONTENT_REPO }}
GITHUB_ACCESS_TOKEN: ${{ secrets._GITHUB_ACCESS_TOKEN }}
GITHUB_CONTENT_REF: ${{ secrets._GITHUB_CONTENT_REF }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:
NEXT_PUBLIC_SELF_ORIGIN: ${{ secrets.NEXT_PUBLIC_SELF_ORIGIN }}
NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN: ${{ secrets.NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN }}
GITHUB_CONTENT_REPO: ${{ secrets._GITHUB_CONTENT_REPO }}
GITHUB_ACCESS_TOKEN: ${{ secrets._GITHUB_ACCESS_TOKEN }}
GITHUB_CONTENT_REF: ${{ secrets._GITHUB_CONTENT_REF }}
E2E_TEST_TARGET_ORIGIN: ${{ secrets.E2E_TEST_TARGET_ORIGIN }}
steps:
- uses: actions/checkout@v2
Expand Down
80 changes: 12 additions & 68 deletions adapters/post-repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Octokit } from "@octokit/core";
import matter from "gray-matter";
import {
generateMarkdown,
Expand All @@ -8,84 +7,30 @@ import {

const owner = process.env.GITHUB_CONTENT_REPO!.split("/")[0]!;
const repo = process.env.GITHUB_CONTENT_REPO!.split("/")[1]!;
const postPathRegexp = /^posts\/([a-z-]+)\/([a-z]{2}-[a-z]{2}).md$/;

async function getDefaultBranchName(): Promise<string> {
const octokit = new Octokit({
auth: process.env.GITHUB_ACCESS_TOKEN,
});

try {
const {
data: { default_branch },
} = await octokit.request("GET /repos/{owner}/{repo}", {
owner: "axross",
repo: "posts",
});

return default_branch;
} catch (error) {
console.error("send the error to sentry");

throw new Error();
}
}
const ref = process.env.GITHUB_CONTENT_REF;

export async function getAllPosts({
locale: requestedLocale,
locale,
}: {
locale?: string;
locale: string;
}): Promise<
{
slug: string;
title: string;
description: string;
coverImageUrl: string;
tags: string[];
firstPublishedAt: string;
lastPublishedAt: string;
tableOfContents: { id: string; level: number; text: string }[];
body: string;
}[]
> {
const octokit = new Octokit({ auth: process.env.GITHUB_ACCESS_TOKEN });
const defaultBranch = await getDefaultBranchName();
const {
data: { tree },
} = await octokit.request("GET /repos/{owner}/{repo}/git/trees/{tree_sha}", {
owner,
repo,
tree_sha: defaultBranch,
recursive: "true",
});

return await Promise.all(
tree
.filter((node) => {
if (node.type !== "blob") {
return false;
}

const match = node.path!.match(postPathRegexp);

if (!match) {
return false;
}
const response = await fetch(
`https://raw.githubusercontent.com/${owner}/${repo}/${ref}/${locale}.json`
);

if (match[2]! !== requestedLocale) {
return false;
}
if (response.status !== 200) {
return [];
}

return true;
})
.map(async (node) => {
const match = node.path!.match(postPathRegexp);
const slug = match![1]!;
const locale = match![2]!;
const posts = await response.json();

return (await getPost({ slug, locale }))!;
})
);
return posts.map((post: any) => ({ ...post, slug: post.id }));
}

export async function getPost({
Expand All @@ -105,9 +50,8 @@ export async function getPost({
tableOfContents: { id: string; level: number; text: string }[];
body: string;
} | null> {
const defaultBranch = await getDefaultBranchName();
const response = await fetch(
`https://raw.githubusercontent.com/${owner}/${repo}/${defaultBranch}/posts/${slug}/${locale}.md`
`https://raw.githubusercontent.com/${owner}/${repo}/${ref}/posts/${slug}/${locale}.md`
);

if (response.status !== 200) {
Expand Down

0 comments on commit 99fbe6e

Please sign in to comment.