diff options
Diffstat (limited to 'packages/server/src/api/v1/repo/log.ts')
-rw-r--r-- | packages/server/src/api/v1/repo/log.ts | 64 |
1 files changed, 51 insertions, 13 deletions
diff --git a/packages/server/src/api/v1/repo/log.ts b/packages/server/src/api/v1/repo/log.ts index f692b00..d21cfa8 100644 --- a/packages/server/src/api/v1/repo/log.ts +++ b/packages/server/src/api/v1/repo/log.ts @@ -1,23 +1,46 @@ import { FastifyInstance, FastifyPluginOptions } from "fastify"; -import { GitAPI } from "../../git"; +import { Commit } from "../../../git/commit"; +import { Patch } from "../../../git/patch"; import { Route } from "../../../fastify_types"; import { verifySHA } from "../../util"; -export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { - const git: GitAPI = opts.config.git; +async function commitMap(commit: Commit) { + const stats = await commit.stats(); + return { + id: commit.id, + author: { + name: commit.author.name, + email: commit.author.email + }, + message: commit.message, + date: commit.date, + insertions: stats.insertions, + deletions: stats.deletions, + files_changed: stats.files_changed + }; +} + +async function patchMap(patch: Patch) { + return { + additions: patch.additions, + deletions: patch.deletions, + from: patch.from, + to: patch.to, + too_large: patch.too_large, + hunks: await patch.getHunks() + }; +} +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { fastify.route<Route>({ method: "GET", url: "/log", handler: async(req, reply) => { - const log = await git.getLog(req.params.repo); + const commits = (await (await req.repository).commits()); - if(log.length === 0) { - reply.code(500).send({ error: "Internal server error!" }); - return; - } - - reply.send({ data: log }); + reply.send({ + data: await Promise.all(commits.map(commitMap)) + }); } }); @@ -25,14 +48,29 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do method: "GET", url: "/log/:commit", handler: async(req, reply) => { - const commit_verification = await verifySHA(git, req.params.repo, req.params.commit); + const commit_verification = await verifySHA(await req.repository, req.params.commit); if(commit_verification.success === false && commit_verification.code) { reply.code(commit_verification.code).send({ error: commit_verification.message }); } - const commit = await git.getCommit(req.params.repo, req.params.commit); + const commit = await Commit.lookup(await req.repository, req.params.commit); + + const stats = await commit.stats(); - reply.send({ data: commit }); + reply.send({ + data: { + message: commit.message, + author: { + name: commit.author.name, + email: commit.author.email + }, + date: commit.date, + insertions: stats.insertions, + deletions: stats.deletions, + files_changed: stats.files_changed, + diff: await Promise.all((await (await commit.diff()).getPatches()).map(patchMap)) + } + }); } }); |