From 5dc3379214adb0b9e53b9d7a87238e6d880cea87 Mon Sep 17 00:00:00 2001 From: Alexandro Castro <10711649+AlexcastroDev@users.noreply.github.com> Date: Fri, 29 Sep 2023 17:54:23 +0100 Subject: [PATCH] Improvements on workflow and files (#222) * chore: improvements * chore: add testing workflow * fix: types and lint * chore: change dynamic * chore: improvements * chore: no need repository to check status --- .github/workflows/testing.yml | 22 +++ README.md | 6 + index.ts => api/index.ts | 37 ++-- debug.ts | 2 +- deps.ts | 22 ++- src/Helpers/Retry.ts | 44 +++++ src/Helpers/__tests__/Retry.test.ts | 66 ++++++++ src/Repository/GithubRepository.ts | 25 +++ src/Schemas/index.ts | 61 +++++++ src/Services/GithubApiService.ts | 120 +++++++++++++ src/StaticRenderRegeneration/cache_manager.ts | 81 ++++----- src/Types/Request.ts | 5 + src/Types/index.ts | 1 + src/github_api_client.ts | 159 ------------------ vercel.json | 19 ++- 15 files changed, 446 insertions(+), 224 deletions(-) create mode 100644 .github/workflows/testing.yml rename index.ts => api/index.ts (73%) create mode 100644 src/Helpers/Retry.ts create mode 100644 src/Helpers/__tests__/Retry.test.ts create mode 100644 src/Repository/GithubRepository.ts create mode 100644 src/Schemas/index.ts create mode 100644 src/Services/GithubApiService.ts create mode 100644 src/Types/Request.ts create mode 100644 src/Types/index.ts delete mode 100644 src/github_api_client.ts diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml new file mode 100644 index 0000000..b821553 --- /dev/null +++ b/.github/workflows/testing.yml @@ -0,0 +1,22 @@ +name: Check PR Test + +on: + pull_request: + branches: + - master +jobs: + install-dependencies: + runs-on: ubuntu-latest + steps: + - name: Check if PR is in draft mode + run: | + if [ "${{ github.event.pull_request.draft }}" == "true" ]; then + echo "PR is in draft mode, skipping workflow" + exit 78 + fi + - name: Set up Deno + uses: denolib/setup-deno@v2 + with: + deno-version: "1.37.0" + - name: Run tests + run: deno test --allow-env diff --git a/README.md b/README.md index 4056815..b4727c8 100644 --- a/README.md +++ b/README.md @@ -498,3 +498,9 @@ https://github-profile-trophy.vercel.app/?username=ryo-ma&no-frame=true # Contribution Guide Check [CONTRIBUTING.md](./CONTRIBUTING.md) for more details. + +# Testing + +```bash +deno test --allow-env +``` diff --git a/index.ts b/api/index.ts similarity index 73% rename from index.ts rename to api/index.ts index 0bbbcce..4a095c7 100644 --- a/index.ts +++ b/api/index.ts @@ -1,28 +1,31 @@ -import { GithubAPIClient } from "./src/github_api_client.ts"; -import { Card } from "./src/card.ts"; -import { CONSTANTS, parseParams } from "./src/utils.ts"; -import { COLORS, Theme } from "./src/theme.ts"; -import { Error400, Error404 } from "./src/error_page.ts"; +import { Card } from "../src/card.ts"; +import { CONSTANTS, parseParams } from "../src/utils.ts"; +import { COLORS, Theme } from "../src/theme.ts"; +import { Error400, Error404 } from "../src/error_page.ts"; import "https://deno.land/x/dotenv@v0.5.0/load.ts"; -import {staticRenderRegeneration} from "./src/StaticRenderRegeneration/index.ts"; -const apiEndpoint = Deno.env.get("GITHUB_API") || CONSTANTS.DEFAULT_GITHUB_API; -const client = new GithubAPIClient(apiEndpoint); +import { staticRenderRegeneration } from "../src/StaticRenderRegeneration/index.ts"; +import { GithubRepositoryService } from "../src/Repository/GithubRepository.ts"; +import { GithubApiService } from "../src/Services/GithubApiService.ts"; + +const serviceProvider = new GithubApiService(); +const client = new GithubRepositoryService(serviceProvider).repository; const defaultHeaders = new Headers( { "Content-Type": "image/svg+xml", "Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`, }, -) +); -export default (request: Request) => staticRenderRegeneration(request, { - revalidate: CONSTANTS.REVALIDATE_TIME, - headers: defaultHeaders -}, function (req: Request) { - return app(req); -}); +export default (request: Request) => + staticRenderRegeneration(request, { + revalidate: CONSTANTS.REVALIDATE_TIME, + headers: defaultHeaders, + }, function (req: Request) { + return app(req); + }); -async function app (req: Request): Promise{ +async function app(req: Request): Promise { const params = parseParams(req); const username = params.get("username"); const row = params.getNumberValue("row", CONSTANTS.DEFAULT_MAX_ROW); @@ -83,7 +86,7 @@ async function app (req: Request): Promise{ }, ); } - + // Success Response return new Response( new Card( diff --git a/debug.ts b/debug.ts index ab64cc8..4d1ac0b 100644 --- a/debug.ts +++ b/debug.ts @@ -1,4 +1,4 @@ import { serve } from "https://deno.land/std@0.125.0/http/server.ts"; -import requestHandler from "./index.ts"; +import requestHandler from "./api/index.ts"; serve(requestHandler, { port: 8080 }); diff --git a/deps.ts b/deps.ts index 5d6175a..4b51bfc 100644 --- a/deps.ts +++ b/deps.ts @@ -1 +1,21 @@ -export { soxa } from "https://deno.land/x/soxa@1.4/mod.ts"; +import { Soxa as ServiceProvider } from "https://deno.land/x/soxa@1.4/src/core/Soxa.ts"; +import { defaults } from "https://deno.land/x/soxa@1.4/src/defaults.ts"; +import { + assertEquals, + assertRejects, +} from "https://deno.land/std@0.203.0/assert/mod.ts"; +import { + assertSpyCalls, + spy, +} from "https://deno.land/std@0.203.0/testing/mock.ts"; + +import { CONSTANTS } from "./src/utils.ts"; + +const baseURL = Deno.env.get("GITHUB_API") || CONSTANTS.DEFAULT_GITHUB_API; + +const soxa = new ServiceProvider({ + ...defaults, + baseURL, +}); + +export { assertEquals, assertRejects, assertSpyCalls, soxa, spy }; diff --git a/src/Helpers/Retry.ts b/src/Helpers/Retry.ts new file mode 100644 index 0000000..be1eb9f --- /dev/null +++ b/src/Helpers/Retry.ts @@ -0,0 +1,44 @@ +export type RetryCallbackProps = { + attempt: number; +}; + +type callbackType = (data: RetryCallbackProps) => Promise | T; + +async function* createAsyncIterable( + callback: callbackType, + retries: number, + delay: number, +) { + for (let i = 0; i < retries; i++) { + try { + const data = await callback({ attempt: i }); + yield data; + return; + } catch (e) { + yield null; + console.error(e); + await new Promise((resolve) => setTimeout(resolve, delay)); + } + } +} + +export class Retry { + constructor(private maxRetries = 2, private retryDelay = 1000) {} + async fetch( + callback: callbackType, + ) { + for await ( + const callbackResult of createAsyncIterable( + callback, + this.maxRetries, + this.retryDelay, + ) + ) { + if (callbackResult) { + return callbackResult as T; + } + } + + throw new Error(`Max retries (${this.maxRetries}) exceeded.`); + } +} diff --git a/src/Helpers/__tests__/Retry.test.ts b/src/Helpers/__tests__/Retry.test.ts new file mode 100644 index 0000000..7fcad13 --- /dev/null +++ b/src/Helpers/__tests__/Retry.test.ts @@ -0,0 +1,66 @@ +import { Retry } from "../Retry.ts"; +import { + assertEquals, + assertRejects, + assertSpyCalls, + spy, +} from "../../../deps.ts"; + +type MockResponse = { + value: number; +}; + +Deno.test("Retry.fetch", () => { + const retryInstance = new Retry(); + const callback = spy(retryInstance, "fetch"); + + retryInstance.fetch(() => { + return { value: 1 }; + }); + + assertSpyCalls(callback, 1); +}); + +Deno.test("Should retry", async () => { + let countErrors = 0; + + const callbackError = () => { + countErrors++; + throw new Error("Panic! Threw Error"); + }; + const retries = 3; + const retryInstance = new Retry(retries); + + await assertRejects( + () => { + return retryInstance.fetch(callbackError); + }, + Error, + `Max retries (${retries}) exceeded.`, + ); + + assertEquals(countErrors, 3); +}); + +Deno.test("Should retry the asyncronous callback", async () => { + let countErrors = 0; + + const callbackError = async () => { + countErrors++; + // Mock request in callback + await new Promise((_, reject) => setTimeout(reject, 100)); + }; + + const retries = 3; + const retryInstance = new Retry(retries); + + await assertRejects( + () => { + return retryInstance.fetch(callbackError); + }, + Error, + `Max retries (${retries}) exceeded.`, + ); + + assertEquals(countErrors, 3); +}); diff --git a/src/Repository/GithubRepository.ts b/src/Repository/GithubRepository.ts new file mode 100644 index 0000000..b345fd3 --- /dev/null +++ b/src/Repository/GithubRepository.ts @@ -0,0 +1,25 @@ +import { + GitHubUserActivity, + GitHubUserIssue, + GitHubUserPullRequest, + GitHubUserRepository, + UserInfo, +} from "../user_info.ts"; + +export abstract class GithubRepository { + abstract requestUserInfo(username: string): Promise; + abstract requestUserActivity( + username: string, + ): Promise; + abstract requestUserIssue(username: string): Promise; + abstract requestUserPullRequest( + username: string, + ): Promise; + abstract requestUserRepository( + username: string, + ): Promise; +} + +export class GithubRepositoryService { + constructor(public repository: GithubRepository) {} +} diff --git a/src/Schemas/index.ts b/src/Schemas/index.ts new file mode 100644 index 0000000..2c7e3f3 --- /dev/null +++ b/src/Schemas/index.ts @@ -0,0 +1,61 @@ +export const queryUserActivity = ` + query userInfo($username: String!) { + user(login: $username) { + createdAt + contributionsCollection { + totalCommitContributions + restrictedContributionsCount + totalPullRequestReviewContributions + } + organizations(first: 1) { + totalCount + } + followers(first: 1) { + totalCount + } + } + } +`; + +export const queryUserIssue = ` + query userInfo($username: String!) { + user(login: $username) { + openIssues: issues(states: OPEN) { + totalCount + } + closedIssues: issues(states: CLOSED) { + totalCount + } + } + } +`; + +export const queryUserPullRequest = ` + query userInfo($username: String!) { + user(login: $username) { + pullRequests(first: 1) { + totalCount + } + } + } +`; + +export const queryUserRepository = ` + query userInfo($username: String!) { + user(login: $username) { + repositories(first: 100, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) { + totalCount + nodes { + languages(first: 3, orderBy: {direction:DESC, field: SIZE}) { + nodes { + name + } + } + stargazers { + totalCount + } + } + } + } + } +`; diff --git a/src/Services/GithubApiService.ts b/src/Services/GithubApiService.ts new file mode 100644 index 0000000..4d7a9cc --- /dev/null +++ b/src/Services/GithubApiService.ts @@ -0,0 +1,120 @@ +import { GithubRepository } from "../Repository/GithubRepository.ts"; +import { + GitHubUserActivity, + GitHubUserIssue, + GitHubUserPullRequest, + GitHubUserRepository, + UserInfo, +} from "../user_info.ts"; +import { + queryUserActivity, + queryUserIssue, + queryUserPullRequest, + queryUserRepository, +} from "../Schemas/index.ts"; +import { soxa } from "../../deps.ts"; +import { Retry } from "../Helpers/Retry.ts"; +import { QueryDefaultResponse } from "../Types/index.ts"; +import { CONSTANTS } from "../utils.ts"; + +// Need to be here - Exporting from another file makes array of null +export const TOKENS = [ + Deno.env.get("GITHUB_TOKEN1"), + Deno.env.get("GITHUB_TOKEN2"), +]; + +export class GithubApiService extends GithubRepository { + async requestUserRepository( + username: string, + ): Promise { + return await this.executeQuery(queryUserRepository, { + username, + }); + } + async requestUserActivity( + username: string, + ): Promise { + return await this.executeQuery(queryUserActivity, { + username, + }); + } + async requestUserIssue(username: string): Promise { + return await this.executeQuery(queryUserIssue, { + username, + }); + } + async requestUserPullRequest( + username: string, + ): Promise { + return await this.executeQuery( + queryUserPullRequest, + { username }, + ); + } + async requestUserInfo(username: string): Promise { + // Avoid to call others if one of them is null + const repository = await this.requestUserRepository(username); + if (repository === null) return null; + + const promises = Promise.allSettled([ + this.requestUserActivity(username), + this.requestUserIssue(username), + this.requestUserPullRequest(username), + ]); + const [activity, issue, pullRequest] = await promises; + const status = [ + activity.status, + issue.status, + pullRequest.status, + ]; + + if (status.includes("rejected")) { + console.error(`Can not find a user with username:' ${username}'`); + return null; + } + + return new UserInfo( + (activity as PromiseFulfilledResult).value, + (issue as PromiseFulfilledResult).value, + (pullRequest as PromiseFulfilledResult).value, + repository, + ); + } + + async executeQuery( + query: string, + variables: { [key: string]: string }, + ) { + try { + const retry = new Retry( + TOKENS.length, + CONSTANTS.DEFAULT_GITHUB_RETRY_DELAY, + ); + const response = await retry.fetch>(async ({ attempt }) => { + return await soxa.post("", {}, { + data: { query: query, variables }, + headers: { + Authorization: `bearer ${TOKENS[attempt]}`, + }, + }); + }) as QueryDefaultResponse<{ user: T; errors?: unknown[] }>; + + if (response.data.data.errors) { + throw new Error("Error from Github API", { + cause: response.data.data.errors, + }); + } + + return response?.data?.data?.user ?? null; + } catch (error) { + // TODO: Move this to a logger instance later + if (error instanceof Error && error.cause) { + console.error(JSON.stringify(error.cause, null, 2)); + } else { + console.error(error); + } + + return null; + } + } +} diff --git a/src/StaticRenderRegeneration/cache_manager.ts b/src/StaticRenderRegeneration/cache_manager.ts index 3a93e4b..2f11479 100644 --- a/src/StaticRenderRegeneration/cache_manager.ts +++ b/src/StaticRenderRegeneration/cache_manager.ts @@ -1,49 +1,50 @@ -import { existsSync } from './utils.ts' +import { existsSync } from "./utils.ts"; export class CacheManager { - constructor(private revalidateTime: number, private cacheFile: string) {} - - // Reason to use /tmp/: - // https://github.com/orgs/vercel/discussions/314 - get cacheFilePath(): string { - return `/tmp/${this.cacheFile}`; - } - get cacheFileExists(): boolean { - return existsSync(this.cacheFilePath); - } + constructor(private revalidateTime: number, private cacheFile: string) {} - get cacheFileLastModified(): Date | null { - if (!this.cacheFileExists) { - return null; - } - const fileInfo = Deno.statSync(this.cacheFilePath); - return fileInfo.mtime ?? null; - } + // Reason to use /tmp/: + // https://github.com/orgs/vercel/discussions/314 + get cacheFilePath(): string { + return `/tmp/${this.cacheFile}`; + } + get cacheFileExists(): boolean { + return existsSync(this.cacheFilePath); + } - get cacheFileLastModifiedGetTime(): number | null { - const lastModified = this.cacheFileLastModified; - if (lastModified === null) { - return null; - } - return lastModified.getTime(); + get cacheFileLastModified(): Date | null { + if (!this.cacheFileExists) { + return null; } + const fileInfo = Deno.statSync(this.cacheFilePath); + return fileInfo.mtime ?? null; + } - get isCacheValid(): boolean { - if (this.cacheFileLastModifiedGetTime === null) { - return false; - } - const currentTime = new Date().getTime(); - return currentTime - this.cacheFileLastModifiedGetTime < this.revalidateTime; + get cacheFileLastModifiedGetTime(): number | null { + const lastModified = this.cacheFileLastModified; + if (lastModified === null) { + return null; } + return lastModified.getTime(); + } - async save (response: Response): Promise { - if(response === null) return - // Prevent TypeError: ReadableStream is locked - const text = await response.clone().text() - const data = new TextEncoder().encode(text) - - Deno.writeFile(this.cacheFilePath, data, { create: true }).catch(() => { - console.warn("Failed to save cache file") - }); + get isCacheValid(): boolean { + if (this.cacheFileLastModifiedGetTime === null) { + return false; } -} \ No newline at end of file + const currentTime = new Date().getTime(); + return currentTime - this.cacheFileLastModifiedGetTime < + this.revalidateTime; + } + + async save(response: Response): Promise { + if (response === null) return; + // Prevent TypeError: ReadableStream is locked + const text = await response.clone().text(); + const data = new TextEncoder().encode(text); + + Deno.writeFile(this.cacheFilePath, data, { create: true }).catch(() => { + console.warn("Failed to save cache file"); + }); + } +} diff --git a/src/Types/Request.ts b/src/Types/Request.ts new file mode 100644 index 0000000..5ef517e --- /dev/null +++ b/src/Types/Request.ts @@ -0,0 +1,5 @@ +export type QueryDefaultResponse = { + data: { + data: T; + }; +}; diff --git a/src/Types/index.ts b/src/Types/index.ts new file mode 100644 index 0000000..07f4ba9 --- /dev/null +++ b/src/Types/index.ts @@ -0,0 +1 @@ +export * from './Request.ts' \ No newline at end of file diff --git a/src/github_api_client.ts b/src/github_api_client.ts deleted file mode 100644 index c4b8361..0000000 --- a/src/github_api_client.ts +++ /dev/null @@ -1,159 +0,0 @@ -import { soxa } from "../deps.ts"; -import { UserInfo } from "./user_info.ts"; -import { CONSTANTS } from "./utils.ts"; -import type { - GitHubUserActivity, - GitHubUserIssue, - GitHubUserPullRequest, - GitHubUserRepository, -} from "./user_info.ts"; - -export class GithubAPIClient { - constructor( - private apiEndpoint: string = CONSTANTS.DEFAULT_GITHUB_API, - ) { - } - async requestUserInfo( - username: string, - ): Promise { - // Avoid timeout for the Github API - const results = await Promise.all([ - this.requestUserActivity(username), - this.requestUserIssue(username), - this.requestUserPullRequest(username), - this.requestUserRepository(username), - ]); - if (results.some((r) => r == null)) { - console.error(`Can not find a user with username:'${username}'`); - return null; - } - return new UserInfo(results[0]!, results[1]!, results[2]!, results[3]!); - } - private async requestUserActivity( - username: string, - ): Promise { - const query = ` - query userInfo($username: String!) { - user(login: $username) { - createdAt - contributionsCollection { - totalCommitContributions - restrictedContributionsCount - totalPullRequestReviewContributions - } - organizations(first: 1) { - totalCount - } - followers(first: 1) { - totalCount - } - } - } - `; - return await this.request(query, username); - } - private async requestUserIssue( - username: string, - ): Promise { - const query = ` - query userInfo($username: String!) { - user(login: $username) { - openIssues: issues(states: OPEN) { - totalCount - } - closedIssues: issues(states: CLOSED) { - totalCount - } - } - } - `; - return await this.request(query, username); - } - private async requestUserPullRequest( - username: string, - ): Promise { - const query = ` - query userInfo($username: String!) { - user(login: $username) { - pullRequests(first: 1) { - totalCount - } - } - } - `; - return await this.request(query, username); - } - private async requestUserRepository( - username: string, - ): Promise { - const query = ` - query userInfo($username: String!) { - user(login: $username) { - repositories(first: 100, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) { - totalCount - nodes { - languages(first: 3, orderBy: {direction:DESC, field: SIZE}) { - nodes { - name - } - } - stargazers { - totalCount - } - } - } - } - } - `; - return await this.request(query, username); - } - private async request( - query: string, - username: string, - retryDelay = CONSTANTS.DEFAULT_GITHUB_RETRY_DELAY, - ) { - const tokens = [ - Deno.env.get("GITHUB_TOKEN1"), - Deno.env.get("GITHUB_TOKEN2"), - ]; - const maxRetries = tokens.length; - - const variables = { username: username }; - let response; - - for (let attempt = 0; attempt < maxRetries; attempt++) { - try { - response = await soxa.post( - this.apiEndpoint, - {}, - { - data: { query: query, variables }, - headers: { Authorization: `bearer ${tokens[attempt]}` }, - }, - ); - if (response.data.errors !== undefined) { - throw new Error( - response.data.errors.map((e: { message: string; type: string }) => - e.message - ).join("\n"), - ); - } - if (response.data.data !== undefined) { - return response.data.data.user; - } else { - return null; - } - } catch (error) { - console.error( - `Attempt ${attempt} failed with GITHUB_TOKEN${attempt + 1}:`, - error, - ); - } - - console.log(`Retrying in ${retryDelay / 1000} seconds...`); - await new Promise((resolve) => setTimeout(resolve, retryDelay)); - } - - throw new Error(`Max retries (${maxRetries}) exceeded.`); - } -} diff --git a/vercel.json b/vercel.json index 9bceb93..5288cb8 100644 --- a/vercel.json +++ b/vercel.json @@ -1,7 +1,14 @@ { - "builds":[{ - "src": "**/index.ts", - "use": "vercel-deno@1.1.1" - }], - "routes": [{ "src": "/.*", "dest": "/" }] -} + "public": true, + "functions": { + "api/**/*.[jt]s": { + "runtime": "vercel-deno@3.0.4" + } + }, + "rewrites": [ + { + "source": "/(.*)", + "destination": "/api/$1" + } + ] +} \ No newline at end of file