diff --git a/.gitignore b/.gitignore index 0e71136..5323552 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ yarn-error.log* next-env.d.ts .early.coverage +*.api.json diff --git a/src/checks/index.ts b/src/checks/index.ts index d967ffb..16e6bf4 100644 --- a/src/checks/index.ts +++ b/src/checks/index.ts @@ -28,7 +28,7 @@ export default function checks(data: Data) { goodFirstIssue(data.issues), // branches(data.branches), // release(data.release), - // readme(data.communityMetrics), + readme(data.community), // license(data.communityMetrics), // contributing(data.communityMetrics), // pullRequestTemplate(data.communityMetrics), diff --git a/src/checks/readme.js b/src/checks/readme.ts similarity index 52% rename from src/checks/readme.js rename to src/checks/readme.ts index 8ab64dc..d592e2d 100644 --- a/src/checks/readme.js +++ b/src/checks/readme.ts @@ -1,17 +1,21 @@ -export default function readme(communityMetrics) { - let response = { - id: "readme", - href: "/repo/readme", +import { Community } from "@/models/github/community"; +import { StatusCheck } from "@/types/checks"; + +export default function readme(community: Community) { + const response: StatusCheck = { title: "Readme", + status: "unknown", + description: "-", + extra: "-", }; - if (communityMetrics.files?.readme) { + if (community.files?.readme) { response.status = "success"; response.description = "You have a README file."; response.extra = "No action required."; } - if (!communityMetrics.files || !communityMetrics.files.readme) { + if (!community.files || !community.files.readme) { response.status = "error"; response.description = "You do not have a readme.md file in your repo."; response.extra = "This is the most important file in your project."; diff --git a/src/lib/github/getAllApi.ts b/src/lib/github/getAllApi.ts index 5fb396f..aedd7a3 100644 --- a/src/lib/github/getAllApi.ts +++ b/src/lib/github/getAllApi.ts @@ -1,13 +1,19 @@ import Data from "@/models/data"; import getRepoApi from "./getRepoApi"; import getIssuesApi from "./getIssuesApi"; +import getCommunityApi from "./getCommunityApi"; export default async function getAllApi(repoUrl: string): Promise { - const calls = await Promise.all([getRepoApi(repoUrl), getIssuesApi(repoUrl)]); + const calls = await Promise.all([ + getRepoApi(repoUrl), + getIssuesApi(repoUrl), + getCommunityApi(repoUrl), + ]); const data: Data = { repo: calls[0], issues: calls[1], + community: calls[2], }; return data; diff --git a/src/lib/github/getCommunityApi.ts b/src/lib/github/getCommunityApi.ts new file mode 100644 index 0000000..ccd481a --- /dev/null +++ b/src/lib/github/getCommunityApi.ts @@ -0,0 +1,17 @@ +import { Issue } from "@/models/github/issue"; +import extractOwnerRepo from "./extractOwnerRepo"; +import { Community } from "@/models/github/community"; + +export default async function getCommunityApi(repoUrl: string) { + const { owner, repo } = extractOwnerRepo(repoUrl); + const res = await fetch( + `https://api.github.com/repos/${owner}/${repo}/community/profile`, + { + next: { revalidate: 3600 }, + } + ); + + const data: Community = await res.json(); + + return data; +} diff --git a/src/models/data.ts b/src/models/data.ts index eb37ac8..0022194 100644 --- a/src/models/data.ts +++ b/src/models/data.ts @@ -1,7 +1,9 @@ +import { Community } from "./github/community"; import { Issue } from "./github/issue"; import { Repo } from "./github/repo"; export default interface Data { repo: Repo; issues: Issue[]; + community: Community; } diff --git a/src/models/github/community.ts b/src/models/github/community.ts new file mode 100644 index 0000000..8883850 --- /dev/null +++ b/src/models/github/community.ts @@ -0,0 +1,31 @@ +// To parse this data: +// +// import { Convert, Community } from "./file"; +// +// const community = Convert.toCommunity(json); +// +// These functions will throw an error if the JSON doesn't +// match the expected interface, even if the JSON is valid. + +export interface Community { + health_percentage: number; + description: string; + documentation: null; + files: Files; + updated_at: null; +} + +export interface Files { + code_of_conduct: null; + code_of_conduct_file: null; + contributing: null; + issue_template: null; + pull_request_template: null; + license: null; + readme: Readme; +} + +export interface Readme { + url: string; + html_url: string; +}