From 9736749a779661a95211a91200617a03a1b9a702 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 15 Aug 2021 15:03:44 +0200 Subject: Added a count query parameter to the log API endpoint --- packages/server/src/git/commit.ts | 17 ++++++++++++++++- packages/server/src/git/repository.ts | 11 ++++------- packages/server/src/routes/api/v1/index.ts | 6 ++++++ packages/server/src/routes/api/v1/repo/log.ts | 7 ++++++- 4 files changed, 32 insertions(+), 9 deletions(-) (limited to 'packages/server') diff --git a/packages/server/src/git/commit.ts b/packages/server/src/git/commit.ts index 6b3c38b..6ef02ef 100644 --- a/packages/server/src/git/commit.ts +++ b/packages/server/src/git/commit.ts @@ -1,4 +1,4 @@ -import { Commit as NodeGitCommit, Oid as NodeGitOid } from "nodegit"; +import { Commit as NodeGitCommit, Oid as NodeGitOid, Revwalk as NodeGitRevwalk } from "nodegit"; import { Author } from "api"; import { Diff } from "./diff"; import { Repository } from "./repository"; @@ -209,4 +209,19 @@ export class Commit { public static async branchCommit(owner: Repository): Promise { return new Commit(owner, await owner.ng_repository.getBranchCommit(owner.branch_name)); } + + /** + * Returns a number of commits in a repository + * + * @param owner - The repository which the commits are in + * @param [count=20] - The number of commits to get + * @returns An array of commit instances + */ + public static async getMultiple(owner: Repository, count = 20): Promise { + const walker = NodeGitRevwalk.create(owner.ng_repository); + + walker.pushRef(`refs/heads/${owner.branch_name}`); + + return Promise.all((await walker.getCommits(count)).map(commit => new Commit(owner, commit))); + } } \ No newline at end of file diff --git a/packages/server/src/git/repository.ts b/packages/server/src/git/repository.ts index 9e83281..c1410ab 100644 --- a/packages/server/src/git/repository.ts +++ b/packages/server/src/git/repository.ts @@ -1,4 +1,4 @@ -import { Object as NodeGitObject, Oid as NodeGitOid, Repository as NodeGitRepository, Revwalk as NodeGitRevwalk } from "nodegit"; +import { Object as NodeGitObject, Oid as NodeGitOid, Repository as NodeGitRepository } from "nodegit"; import { Request, connect } from "./http"; import { basename, dirname } from "path"; import { getDirectory, getFile } from "./misc"; @@ -81,14 +81,11 @@ export class Repository { /** * Returns the repository's commits * + * @param [count=20] - The number of commits to get * @returns An array of commit instances */ - public async commits(): Promise { - const walker = NodeGitRevwalk.create(this.ng_repository); - - walker.pushRef(`refs/heads/${this.branch_name}`); - - return Promise.all((await walker.getCommitsUntil(() => true)).map(commit => new Commit(this, commit))); + public async commits(count?: number): Promise { + return Commit.getMultiple(this, count); } /** diff --git a/packages/server/src/routes/api/v1/index.ts b/packages/server/src/routes/api/v1/index.ts index fa7d8ab..fb9cd8a 100644 --- a/packages/server/src/routes/api/v1/index.ts +++ b/packages/server/src/routes/api/v1/index.ts @@ -9,6 +9,12 @@ import { ServerError } from "../../../git/error"; function setHandlers(fastify: FastifyInstance): void { fastify.setErrorHandler((err, req, reply) => { console.log(err); + + if(err.validation) { + reply.code(400).send({ error: `${err.validation[0].dataPath} ${err.validation[0].message}` }); + return; + } + reply.code(500).send({ error: "Internal server error!" }); }); fastify.setNotFoundHandler((req, reply) => { diff --git a/packages/server/src/routes/api/v1/repo/log.ts b/packages/server/src/routes/api/v1/repo/log.ts index aaad042..edca0b3 100644 --- a/packages/server/src/routes/api/v1/repo/log.ts +++ b/packages/server/src/routes/api/v1/repo/log.ts @@ -21,8 +21,13 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do fastify.route({ method: "GET", url: "/log", + schema: { + querystring: { + count: { type: "number" } + } + }, handler: async(req, reply) => { - const commits = await req.repository.commits(); + const commits = await req.repository.commits(Number(req.query.count)); reply.send({ data: await Promise.all(commits.map(commitMap)) -- cgit v1.2.3-18-g5258