aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/routes/api/v1/repo/log.ts
blob: 019f57915075cd99f4a7cb166d18d3500ae2e7d8 (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
import { FastifyPluginCallback } from "fastify";
import { sources } from "cache";
import { Commit } from "git/commit";
import { Route, FastifyPluginOptions } from "types/fastify";
import { verifySHA } from "../../util";
import { getCommit, getLogCommits } from "../data";

const log: FastifyPluginCallback<FastifyPluginOptions> = (fastify, opts, done) => {
	fastify.route<Route>({
		method: "GET",
		url: "/log",
		schema: {
			querystring: {
				count: { type: "number", minimum: 1 }
			}
		},
		handler: async(req, reply) => {
			const commits = await req.repository.commits(Number(req.query.count) || undefined);

			reply.send({
				data: await (opts.config.cache
					? opts.config.cache.receive(sources.LogCommitsSource, req.repository, Number(req.query.count) || undefined)
					: getLogCommits(commits))
			});
		}
	});

	fastify.route<Route>({
		method: "GET",
		url: "/log/:commit",
		schema: {
			params: {
				commit: { type: "string" }
			}
		},
		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);

			reply.send({
				data: await (opts.config.cache
					? opts.config.cache.receive(sources.CommitSource, req.repository, commit)
					: getCommit(commit))
			});
		}
	});

	done();
};

export default log;