From d1a1b7dc947063aef5f8375a6a1e03246b272c84 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 18 Aug 2021 17:29:55 +0200 Subject: Implemented caching for certain API endpoints, Added documentation & made backend-fixes --- packages/server/src/routes/api/v1/data.ts | 119 ++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 packages/server/src/routes/api/v1/data.ts (limited to 'packages/server/src/routes/api/v1/data.ts') diff --git a/packages/server/src/routes/api/v1/data.ts b/packages/server/src/routes/api/v1/data.ts new file mode 100644 index 0000000..97f07ff --- /dev/null +++ b/packages/server/src/routes/api/v1/data.ts @@ -0,0 +1,119 @@ +import { + LogCommit, + Patch as APIPatch, + Commit as APICommit, Tag as APITag, + RepositorySummary as APIRepositorySummary, + Repository as APIRepository, + BranchSummary, + Branch as APIBranch +} from "api"; +import { Branch, Commit, Patch, Repository, Tag } from "../../../git"; + +export async function getLogCommits(commits: Commit[]): Promise { + return Promise.all(commits.map(async(commit: Commit) => { + const stats = await commit.stats(); + + const is_signed = await commit.isSigned(); + + return { + id: commit.id, + author: { + name: commit.author().name, + email: commit.author().email, + fingerprint: await commit.author().fingerprint().catch(() => null) + }, + isSigned: is_signed, + signatureVerified: is_signed ? await commit.verifySignature().catch(() => false) : null, + message: commit.message, + date: commit.date, + insertions: stats.insertions, + deletions: stats.deletions, + files_changed: stats.files_changed + }; + })); +} + +export async function getCommit(commit: Commit): Promise { + const stats = await commit.stats(); + + const is_signed = await commit.isSigned(); + + const patches = await (await commit.diff()).patches().catch(() => null); + + return { + message: commit.message, + author: { + name: commit.author().name, + email: commit.author().email, + fingerprint: await commit.author().fingerprint().catch(() => null) + }, + isSigned: is_signed, + signatureVerified: is_signed ? await commit.verifySignature().catch(() => false) : null, + date: commit.date, + insertions: stats.insertions, + deletions: stats.deletions, + files_changed: stats.files_changed, + too_large: Boolean(!patches), + diff: patches + ? await Promise.all(patches.map(async(patch: Patch) => { + return { + additions: patch.additions, + deletions: patch.deletions, + from: patch.from, + to: patch.to, + too_large: await patch.isTooLarge(), + hunks: await patch.getHunks().catch(() => null) + }; + })) + : null + }; +} + +export function getTags(tags: Tag[]): Promise { + return Promise.all(tags.map(async(tag: Tag) => { + const author = await tag.author(); + return { + name: tag.name, + author: { + name: author.name, + email: author.email + }, + date: await tag.date() + }; + })); +} + +export function getRepositories(repositories: Repository[]): Promise { + return Promise.all(repositories.map(async repository => { + return { + name: repository.name.short, + description: await repository.description(), + last_updated: (await repository.head()).date + }; + })); +} + +export async function getRepository(repository: Repository): Promise { + return { + name: repository.name.short, + description: await repository.description(), + has_readme: await (await repository.tree()).findExists("README.md") + }; +} + +export function getBranches(branches: Branch[]): BranchSummary[] { + return branches.map(branch => { + return { + id: branch.id, + name: branch.name + }; + }); +} + +export async function getBranch(branch: Branch): Promise { + return { + id: branch.id, + name: branch.name, + latest_commit: await branch.latestCommit() + }; +} \ No newline at end of file -- cgit v1.2.3-18-g5258