aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/routes/api/v1/repo/log.ts
blob: edca0b3d1bd46e654d83c11fb9549fd7b1733d81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { FastifyInstance, FastifyPluginOptions } from "fastify";
import { Commit } from "../../../../git/commit";
import { Patch } from "../../../../git/patch";
import { Route } from "../../../../types/fastify";
import { verifySHA } from "../../util";
import { Patch as APIPatch, Commit as APICommit } from "api";
import { commitMap } from "./map";

async function patchMap(patch: Patch) {
	return <APIPatch>{
		additions: patch.additions,
		deletions: patch.deletions,
		from: patch.from,
		to: patch.to,
		too_large: await patch.isTooLarge(),
		hunks: await patch.getHunks()
	};
}

export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void {
	fastify.route<Route>({
		method: "GET",
		url: "/log",
		schema: {
			querystring: {
				count: { type: "number" }
			}
		},
		handler: async(req, reply) => {
			const commits = await req.repository.commits(Number(req.query.count));

			reply.send({
				data: await Promise.all(commits.map(commitMap))
			});
		}
	});

	fastify.route<Route>({
		method: "GET",
		url: "/log/:commit",
		handler: async(req, reply) => {
			const commit_verification = await verifySHA(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 Commit.lookup(req.repository, req.params.commit);

			const stats = await commit.stats();

			const is_signed = await commit.isSigned();

			const data: APICommit = {
				message: commit.message,
				author: {
					name: commit.author().name,
					email: commit.author().email,
					fingerprint: await commit.author().fingerprint().catch(() => null)
				},
				isSigned: is_signed,
				signatureVerified: is_signed ? await commit.verifySignature().catch(() => false) : null,
				date: commit.date,
				insertions: stats.insertions,
				deletions: stats.deletions,
				files_changed: stats.files_changed,
				diff: await Promise.all((await (await commit.diff()).patches()).map(patchMap))
			};

			reply.send({
				data: data
			});
		}
	});

	done();
}