aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-08-08 15:33:22 +0200
committerHampusM <hampus@hampusmat.com>2021-08-08 15:33:22 +0200
commitf2d55a9f05f5df7960d7f6dd37d1ef96c762d65a (patch)
tree737a61da8603520f9afec553791a222f88bc4ff3
parent17c20d77600407209908de5541415f2c4a85f5e3 (diff)
Added a tree commit history API endpoint
-rw-r--r--docs_src/API/v1/repos.md27
-rw-r--r--packages/server/src/git/tree_entry.ts4
-rw-r--r--packages/server/src/routes/api/v1/repo/index.ts34
-rw-r--r--packages/server/src/routes/api/v1/repo/log.ts21
-rw-r--r--packages/server/src/routes/api/v1/repo/map.ts18
5 files changed, 81 insertions, 23 deletions
diff --git a/docs_src/API/v1/repos.md b/docs_src/API/v1/repos.md
index 9ddfc12..e5c24b9 100644
--- a/docs_src/API/v1/repos.md
+++ b/docs_src/API/v1/repos.md
@@ -41,7 +41,7 @@ Retrieves a repository.
**Method:** GET
**Description:**<br>
-Retrieves a repository tree.
+Retrieves a repository's tree.
**Parameters:**<br>
@@ -60,6 +60,29 @@ Retrieves a repository tree.
<br>
+## /repos/:repo/tree/history
+**Method:** GET
+
+**Description:**<br>
+Retrieves a tree entry's commit history.
+
+**Parameters:**<br>
+
+| Name | Location | Description | Required | Schema |
+|--------|----------|------------------|----------|--------|
+| repo | path | The repository | true | string |
+| path | query | Path in the tree | true | string |
+| branch | query | A branch | false | string |
+
+**Response:**<br>
+
+| Code | Description | Schema |
+|---------|---------------------------------|---------------------------------------|
+| 200 | The tree entry's commit history | [\[Commit\]](/docs/modules/api.html#Commit) |
+| 400-599 | An error | [Error](/docs/modules/api.html#Error) |
+
+<br>
+
## /repos/:repo/tags
**Method:** GET
@@ -164,4 +187,4 @@ Retrieves a branch from a repository.
| Code | Description | Schema |
|---------|-------------|--------------------------------------------|
| 200 | A branch | [Branch](/docs/interfaces/api.Branch.html) |
-| 400-599 | An error | [Error](/docs/modules/api.html#Error) | \ No newline at end of file
+| 400-599 | An error | [Error](/docs/modules/api.html#Error) |
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 {
@@ -101,6 +102,37 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
fastify.route<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<Route>({
+ method: "GET",
url: "/tags",
handler: async(req, reply) => {
const tags = await req.repository.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 <APILogCommit>{
- 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 <APIPatch>{
@@ -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<LogCommit> {
+ const stats = await commit.stats();
+ return <LogCommit>{
+ 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
+ };
+}