From 2ceb6e8c986f7e77f0d74aaac065fc969d39fc3e Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 21 Jun 2021 14:12:05 +0200 Subject: Added branch & tag API endpoints, tag tarball downloading and added handlers to the api --- packages/server/src/api/v1.ts | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'packages/server/src/api/v1.ts') diff --git a/packages/server/src/api/v1.ts b/packages/server/src/api/v1.ts index e6391a0..b75c473 100644 --- a/packages/server/src/api/v1.ts +++ b/packages/server/src/api/v1.ts @@ -6,6 +6,13 @@ import { GitAPI } from "./git"; export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: any) { const git = new GitAPI(opts.config.settings.base_dir); + fastify.setErrorHandler((err, req, reply) => { + reply.code(500).send({ error: "Internal server error!" }); + }); + fastify.setNotFoundHandler((req, reply) => { + reply.code(404).send({ error: "Endpoint not found!" }); + }) + fastify.route({ method: "GET", url: "/info", @@ -107,6 +114,43 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do } }); + fastify_repo.route({ + method: "GET", + url: "/branches", + handler: async(req, reply) => { + const params: any = req.params; + const branches = await git.getBranches(params.repo); + + reply.send({ data: branches }); + } + }); + + fastify_repo.route({ + method: "GET", + url: "/branches/:branch", + handler: async(req, reply) => { + const params: any = req.params; + const branch = await git.getBranch(params.repo, params.branch); + + if(!branch) { + reply.code(404).send({ error: "Branch not found!" }); + return; + } + + reply.send({ data: branch }); + } + }); + + fastify_repo.route({ + method: "GET", + url: "/tags", + handler: async(req, reply) => { + const params: any = req.params; + const refs = await git.getTags(params.repo); + reply.send({ data: refs }); + } + }); + done_repo(); }, { prefix: "/repos/:repo" }); -- cgit v1.2.3-18-g5258