aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/routes/api/v1/repo/branches.ts
blob: 99f03275cc423dd545b4e860833c2c61ff060040 (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
import { FastifyInstance, FastifyPluginOptions } from "fastify";
import { Branch } from "../../../../git/branch";
import { Route } from "../../../../types/fastify";
import { BranchSummary as APIBranchSummary, Branch as APIBranch } from "api";

export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void {
	fastify.route<Route>({
		method: "GET",
		url: "/branches",
		handler: async(req, reply) => {
			const branches = await req.repository.branches();

			reply.send({
				data: branches.map(branch => {
					return <APIBranchSummary>{
						id: branch.id,
						name: branch.name
					};
				})
			});
		}
	});

	fastify.route<Route>({
		method: "GET",
		url: "/branches/:branch",
		schema: {
			params: {
				branch: { type: "string" }
			}
		},
		handler: async(req, reply) => {
			const branch = await Branch.lookup(req.repository, req.params.branch);

			if(!branch) {
				reply.code(404).send({ error: "Branch not found!" });
				return;
			}

			const data: APIBranch = {
				id: branch.id,
				name: branch.name,
				latest_commit: await branch.latestCommit()
			};

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

	done();
}