Skip to content

Commit

Permalink
v0.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
cletqui committed Jun 17, 2024
1 parent f1c8e3b commit 7e034d5
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 38 deletions.
23 changes: 9 additions & 14 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,51 +1,46 @@
# Simple workflow for deploying static content to GitHub Pages
name: Deploy static content to Pages
name: Deploy project to GitHub Pages

on:
# Runs on pushes targeting the default branch
push:
branches: ['main']
branches:
- main

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets the GITHUB_TOKEN permissions to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow one concurrent deployment
concurrency:
group: 'pages'
group: "pages"
cancel-in-progress: true

jobs:
# Single deploy job since we're just deploying
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
cache: "npm"
- name: Install dependencies
run: npm ci
- name: Build
- name: Build project
run: npm run build
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
# Upload dist folder
path: './dist'
path: "./dist"
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ npm run deploy

## TODO

- [ ] Setup tests
- [ ] Setup GitHub Actions (to deploy the website to GitHub, to run tests automatically)
- [ ] Make sure the project is compatible with multiple deployment types (GitHub, Cloudflare, Heroku...)
- [ ] Setup GitHub App (to avoid refreshing PAT every year)
- [ ] Write `README.md`
- [ ] Change the document title dynamically
- [ ] Improve GUI (by adding additional info about the repo)
- [ ] Define unit tests
- [ ] Setup GitHub Actions
- [ ] to deploy the website to GitHub
- [x] to run tests automatically
- [ ] Make sure the project is compatible with multiple deployment types
- [x] Cloudflare
- [ ] GitHub
- [ ] Heroku
- [ ] Setup GitHub App (to avoid refreshing PAT every year)
- [x] Add icons in footer
- [x] Find `MAX_ID` dynamically and store in Cookies
- [x] Require GitHub API (Bearer Auth) for `/api/*` requests
- [ ] Change the document title dynamically
- [ ] Write `README.md`
- [x] Display Template
- [x] Refactor code into smaller modules
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "petithub",
"version": "0.4.2",
"version": "0.4.3",
"private": false,
"description": "PetitHub - Explore obscure GitHub repositories",
"author": {
Expand Down
25 changes: 10 additions & 15 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {
} from "./utils/renderer";
import {
getOctokitInstance,
getRepositories,
getRepos,
verifyToken,
getData,
getRepos,
getRepository,
getRandomRepository,
getMaxId,
} from "./utils/octokit";
import { getCookieId, setCookieId } from "./utils/cookie";
Expand All @@ -42,7 +42,7 @@ app.get(
async (c: Context<{ Bindings: Bindings }>): Promise<Response> => {
const octokit = getOctokitInstance(c);
try {
const repository = await getData(octokit, Number(MAX_ID));
const repository = await getRandomRepository(octokit, Number(MAX_ID));
return c.json(repository);
} catch (error: any) {
console.error("Error fetching repository data:", error);
Expand All @@ -56,17 +56,12 @@ app.get(
async (c: Context<{ Bindings: Bindings }>): Promise<Response> => {
const { id } = c.req.param();
const octokit = getOctokitInstance(c);
const { data, status, url } = await getRepositories(
octokit,
Number(id) - 1
); // (id - 1) because since starts from next id
if (status === 200) {
if (data.length === 0) {
return c.json({ error: "Repository not found" }, 404);
}
return c.json(data[0]);
} else {
return c.json({ error: `${status} error at ${url}` }, 500);
try {
const repository = await getRepository(octokit, Number(id));
return c.json(repository);
} catch (error: any) {
console.error("Error fetching repository data:", error);
return c.json({ error: "Failed to fetch repository data" }, 500);
}
}
);
Expand Down
31 changes: 31 additions & 0 deletions src/tests/__mocks__/octokit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const getOctokitInstance = vi.fn(() => ({
repos: {
get: vi
.fn()
.mockResolvedValue({
data: { id: 123, name: "mock-repo", full_name: "mockuser/mock-repo" },
}),
listForUser: vi
.fn()
.mockResolvedValue({
data: [{ id: 123, name: "mock-repo", full_name: "mockuser/mock-repo" }],
}),
},
}));

export const verifyToken = vi.fn((token: string) => token === "valid-token");
export const getRandomRepository = vi
.fn()
.mockResolvedValue({
id: 123,
name: "mock-repo",
full_name: "mockuser/mock-repo",
});
export const getRepository = vi
.fn()
.mockResolvedValue({
id: 123,
name: "mock-repo",
full_name: "mockuser/mock-repo",
});
export const getMaxId = vi.fn().mockResolvedValue(815471592);
34 changes: 32 additions & 2 deletions src/tests/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import app from "..";
import { verifyToken } from "./__mocks__/octokit";

const MOCK_ENV = {
GITHUB_TOKEN: process.env.GITHUB_TOKEN,
octokit: {},
verifyToken,
};

describe("Testing API", () => {
Expand All @@ -10,6 +12,26 @@ describe("Testing API", () => {
expect(res.status).toBe(401);
expect(await res.text()).toBe("Unauthorized");
});

test("GET /api", async () => {
const res = await app.request(
"/api",
{
headers: {
Authorization: "Bearer valid-token",
},
},
MOCK_ENV
);
expect(res.status).toBe(401);
expect(await res.text()).toBe("Unauthorized");
});

test("POST /api (no API key)", async () => {
const res = await app.request("/api", { method: "POST" });
expect(res.status).toBe(401);
expect(await res.text()).toBe("Unauthorized");
});
});

/*
Expand All @@ -18,16 +40,24 @@ describe("Testing ID", () => {
const res = await app.request("/id", {}, MOCK_ENV);
expect(res.status).toBe(200);
});
test("GET /id/1296269", async () => {
const res = await app.request("/id/1296269", {}, MOCK_ENV);
expect(res.status).toBe(200);
});
});
*/

/*
describe("Testing template", () => {
test("GET /template", async () => {
const res = await app.request("/template");
const res = await app.request("/template", {}, MOCK_ENV);
expect(res.status).toBe(200);
});
});
*/

/*
describe("Testing root", () => {
test("GET /", async () => {
const res = await app.request("/");
Expand Down

0 comments on commit 7e034d5

Please sign in to comment.