From f2d55a9f05f5df7960d7f6dd37d1ef96c762d65a Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 8 Aug 2021 15:33:22 +0200 Subject: Added a tree commit history API endpoint --- packages/server/src/git/tree_entry.ts | 4 +-- packages/server/src/routes/api/v1/repo/index.ts | 34 ++++++++++++++++++++++++- packages/server/src/routes/api/v1/repo/log.ts | 21 +++------------ packages/server/src/routes/api/v1/repo/map.ts | 18 +++++++++++++ 4 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 packages/server/src/routes/api/v1/repo/map.ts (limited to 'packages') diff --git a/packages/server/src/git/tree_entry.ts b/packages/server/src/git/tree_entry.ts index 182543e..a217e29 100644 --- a/packages/server/src/git/tree_entry.ts +++ b/packages/server/src/git/tree_entry.ts @@ -1,5 +1,5 @@ import { Commit } from "./commit"; -import { Revwalk as NodeGitRevwalk, TreeEntry as NodeGitTreeEntry } from "nodegit"; +import { Revwalk as NodeGitRevwalk, TreeEntry as NodeGitTreeEntry, Commit as NodeGitCommit } from "nodegit"; import { Repository } from "./repository"; import { Tree } from "./tree"; import { Blob } from "./blob"; @@ -55,7 +55,7 @@ export abstract class BaseTreeEntry { rev_walk.pushRef(`refs/heads/${this._owner.branch_name}`); const file_hist = await rev_walk.fileHistoryWalk(this.path, commit_cnt); - const commit_history = file_hist.map(hist_entry => new Commit(this._owner, hist_entry.commit)); + const commit_history = await Promise.all(file_hist.map(async hist_entry => new Commit(this._owner, await NodeGitCommit.lookup(this._owner.ng_repository, hist_entry.commit)))); return count ? commit_history.slice(0, count) diff --git a/packages/server/src/routes/api/v1/repo/index.ts b/packages/server/src/routes/api/v1/repo/index.ts index f5f9030..3b115d4 100644 --- a/packages/server/src/routes/api/v1/repo/index.ts +++ b/packages/server/src/routes/api/v1/repo/index.ts @@ -9,6 +9,7 @@ import log from "./log"; import { verifyRepoName } from "../../util"; import { Tree as APITree, Tag as APITag, TreeEntry as APITreeEntry } from "api"; import { BaseError } from "../../../../git/error"; +import { commitMap } from "./map"; declare module "fastify" { interface FastifyRequest { @@ -99,6 +100,37 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do } }); + fastify.route({ + method: "GET", + url: "/tree/history", + handler: async(req, reply) => { + const tree = await req.repository.tree().catch((err: BaseError) => err); + + if(tree instanceof BaseError) { + reply.code(tree.code).send({ error: tree.message }); + return; + } + + if(Object.keys(req.query).length === 0) { + reply.code(400).send({ error: "Missing query parameter 'path'!" }); + return; + } + + const tree_path = req.query.path; + + const tree_entry = await tree.find(tree_path).catch((err: BaseError) => err); + + if(tree_entry instanceof BaseError) { + reply.code(tree_entry.code).send({ error: tree_entry.message }); + return; + } + + const history = await tree_entry.history(); + + reply.send({ data: await Promise.all(history.map(commitMap)) }); + } + }); + fastify.route({ method: "GET", url: "/tags", @@ -111,4 +143,4 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do }); done(); -} \ No newline at end of file +} diff --git a/packages/server/src/routes/api/v1/repo/log.ts b/packages/server/src/routes/api/v1/repo/log.ts index 76706b3..b2eea42 100644 --- a/packages/server/src/routes/api/v1/repo/log.ts +++ b/packages/server/src/routes/api/v1/repo/log.ts @@ -3,23 +3,8 @@ import { Commit } from "../../../../git/commit"; import { Patch } from "../../../../git/patch"; import { Route } from "../../../../types/fastify"; import { verifySHA } from "../../util"; -import { LogCommit as APILogCommit, Patch as APIPatch, Commit as APICommit } from "api"; - -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 - }; -} +import { Patch as APIPatch, Commit as APICommit } from "api"; +import { commitMap } from "./map"; async function patchMap(patch: Patch) { return { @@ -78,4 +63,4 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do }); done(); -} \ No newline at end of file +} diff --git a/packages/server/src/routes/api/v1/repo/map.ts b/packages/server/src/routes/api/v1/repo/map.ts new file mode 100644 index 0000000..520161e --- /dev/null +++ b/packages/server/src/routes/api/v1/repo/map.ts @@ -0,0 +1,18 @@ +import { Commit } from "../../../../git/commit"; +import { LogCommit } from "api"; + +export async function commitMap(commit: Commit): Promise { + 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 + }; +} -- cgit v1.2.3-18-g5258