diff options
-rw-r--r-- | docs_src/API/v1/repos.md | 9 | ||||
-rw-r--r-- | packages/server/src/git/commit.ts | 17 | ||||
-rw-r--r-- | packages/server/src/git/repository.ts | 11 | ||||
-rw-r--r-- | packages/server/src/routes/api/v1/index.ts | 6 | ||||
-rw-r--r-- | packages/server/src/routes/api/v1/repo/log.ts | 7 | ||||
-rw-r--r-- | test/unit/commit.unit.test.ts | 20 | ||||
-rw-r--r-- | test/unit/repository.unit.test.ts | 8 | ||||
-rw-r--r-- | test/util.ts | 14 |
8 files changed, 55 insertions, 37 deletions
diff --git a/docs_src/API/v1/repos.md b/docs_src/API/v1/repos.md index f7bb5ef..e2cf246 100644 --- a/docs_src/API/v1/repos.md +++ b/docs_src/API/v1/repos.md @@ -113,10 +113,11 @@ Retrieves a repository's log. **Parameters:**<br> -| Name | Location | Description | Required | Schema | -|--------|----------|----------------|----------|--------| -| repo | path | The repository | true | string | -| branch | query | A branch | false | string | +| Name | Location | Description | Required | Schema | +|--------|----------|-------------------|----------|--------| +| repo | path | The repository | true | string | +| branch | query | A branch | false | string | +| count | query | Number of commits | false | number | **Response:**<br> 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<Commit> { 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<Commit[]> { + 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<Commit[]> { - 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<Commit[]> { + 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<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)) diff --git a/test/unit/commit.unit.test.ts b/test/unit/commit.unit.test.ts index d302ea8..0d8c918 100644 --- a/test/unit/commit.unit.test.ts +++ b/test/unit/commit.unit.test.ts @@ -1,6 +1,6 @@ import { Repository } from "server/src/git/repository"; import { Commit } from "server/src/git/commit"; -import { EnvironmentVariables, expectCommitProperties } from "../util"; +import { EnvironmentVariables } from "../util"; import { Diff } from "server/src/git/diff"; import { Tree } from "server/src/git/tree"; @@ -15,14 +15,12 @@ describe("Commit", () => { describe("Class methods", () => { it("Should look up a commit", async() => { - expect.assertions(8); + expect.assertions(2); const commit = await Commit.lookup(repository, env.AVAIL_COMMIT); expect(commit).toBeDefined(); expect(commit).toBeInstanceOf(Commit); - - expectCommitProperties(commit); }); it("Should look up a nonexistant commit and throw", async() => { @@ -42,6 +40,20 @@ describe("Commit", () => { await expect(Commit.lookupExists(repository, env.UNAVAIL_COMMIT)).resolves.toBeFalsy(); }); + + it("Should get multiple commits", async() => { + expect.hasAssertions(); + + const commits = await Commit.getMultiple(repository); + + expect(commits).toBeDefined(); + expect(commits).toHaveLength(20); + + for(const commit of commits) { + expect(commit).toBeDefined(); + expect(commit).toBeInstanceOf(Commit); + } + }); }); describe("Instance methods", () => { diff --git a/test/unit/repository.unit.test.ts b/test/unit/repository.unit.test.ts index 752cd59..9aaa748 100644 --- a/test/unit/repository.unit.test.ts +++ b/test/unit/repository.unit.test.ts @@ -3,7 +3,7 @@ import { Commit } from "server/src/git/commit"; import { Tree } from "server/src/git/tree"; import { Branch } from "server/src/git/branch"; import { Tag } from "server/src/git/tag"; -import { EnvironmentVariables, expectCommitProperties } from "../util"; +import { EnvironmentVariables } from "../util"; import { ServerError } from "server/src/git/error"; const env = process.env as EnvironmentVariables; @@ -105,14 +105,12 @@ describe("Repository", () => { }); it("Should get the head commit", async() => { - expect.assertions(8); + expect.assertions(2); const master_commit = await repository.head(); expect(master_commit).toBeDefined(); expect(master_commit).toBeInstanceOf(Commit); - - expectCommitProperties(master_commit); }); it("Should get the commits", async() => { @@ -125,8 +123,6 @@ describe("Repository", () => { for(const commit of commits) { expect(commit).toBeDefined(); expect(commit).toBeInstanceOf(Commit); - - expectCommitProperties(commit); } }); diff --git a/test/util.ts b/test/util.ts index 30a5c60..9e62ec9 100644 --- a/test/util.ts +++ b/test/util.ts @@ -1,5 +1,3 @@ -import { Commit, CommitAuthor } from "server/src/git/commit"; - export type EnvironmentVariables = { GIT_DIR: string, AVAIL_REPO: string, @@ -9,16 +7,4 @@ export type EnvironmentVariables = { UNAVAIL_OBJECT: string, AVAIL_COMMIT: string, UNAVAIL_COMMIT: string -} - -export function expectCommitProperties(commit: Commit): void { - expect(commit).toHaveProperty("id"); - expect(commit).toHaveProperty("author"); - - const author = commit.author(); - expect(author).toBeInstanceOf(CommitAuthor); - - expect(commit).toHaveProperty("date"); - expect(commit).toHaveProperty("message"); - expect(commit).toHaveProperty("isSigned"); }
\ No newline at end of file |