diff options
author | Lauris BH <lauris@nix.lv> | 2018-11-27 23:52:20 +0200 |
---|---|---|
committer | techknowlogick <hello@techknowlogick.com> | 2018-11-27 16:52:20 -0500 |
commit | 08bf443016bae30690417b4835076709ef36e3b0 (patch) | |
tree | ece591d95416dd85e726dce15e0ab52872a17b06 /routers | |
parent | 294904321cb6de535237a6a156d5c4ec462bc117 (diff) | |
download | gitea-08bf443016bae30690417b4835076709ef36e3b0.tar.gz gitea-08bf443016bae30690417b4835076709ef36e3b0.zip |
Implement git refs API for listing references (branches, tags and other) (#5354)
* Inital routes to git refs api
* Git refs API implementation
* Update swagger
* Fix copyright
* Make swagger happy add basic test
* Fix test
* Fix test again :)
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/api.go | 4 | ||||
-rw-r--r-- | routers/api/v1/repo/git_ref.go | 115 | ||||
-rw-r--r-- | routers/api/v1/swagger/repo.go | 14 |
3 files changed, 133 insertions, 0 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index c5f01d91d8..0be8f84836 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -573,6 +573,10 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/status", repo.GetCombinedCommitStatusByRef) m.Get("/statuses", repo.GetCommitStatusesByRef) }) + m.Group("/git", func() { + m.Get("/refs", repo.GetGitAllRefs) + m.Get("/refs/*", repo.GetGitRefs) + }) }, repoAssignment()) }) diff --git a/routers/api/v1/repo/git_ref.go b/routers/api/v1/repo/git_ref.go new file mode 100644 index 0000000000..7d60b8e0e7 --- /dev/null +++ b/routers/api/v1/repo/git_ref.go @@ -0,0 +1,115 @@ +// Copyright 2018 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "code.gitea.io/gitea/modules/context" + + "code.gitea.io/git" + api "code.gitea.io/sdk/gitea" +) + +// GetGitAllRefs get ref or an list all the refs of a repository +func GetGitAllRefs(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/git/refs repository repoListAllGitRefs + // --- + // summary: Get specified ref or filtered repository's refs + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // description: name of the repo + // type: string + // required: true + // responses: + // "200": + // "$ref": "#/responses/Reference" + // "$ref": "#/responses/ReferenceList" + // "404": + // "$ref": "#/responses/notFound" + + getGitRefsInternal(ctx, "") +} + +// GetGitRefs get ref or an filteresd list of refs of a repository +func GetGitRefs(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/git/refs/{ref} repository repoListGitRefs + // --- + // summary: Get specified ref or filtered repository's refs + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // description: name of the repo + // type: string + // required: true + // - name: ref + // in: path + // description: part or full name of the ref + // type: string + // required: true + // responses: + // "200": + // "$ref": "#/responses/Reference" + // "$ref": "#/responses/ReferenceList" + // "404": + // "$ref": "#/responses/notFound" + + getGitRefsInternal(ctx, ctx.Params("*")) +} + +func getGitRefsInternal(ctx *context.APIContext, filter string) { + gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) + if err != nil { + ctx.Error(500, "OpenRepository", err) + return + } + if len(filter) > 0 { + filter = "refs/" + filter + } + + refs, err := gitRepo.GetRefsFiltered(filter) + if err != nil { + ctx.Error(500, "GetRefsFiltered", err) + return + } + + if len(refs) == 0 { + ctx.Status(404) + return + } + + apiRefs := make([]*api.Reference, len(refs)) + for i := range refs { + apiRefs[i] = &api.Reference{ + Ref: refs[i].Name, + URL: ctx.Repo.Repository.APIURL() + "/git/" + refs[i].Name, + Object: &api.GitObject{ + SHA: refs[i].Object.String(), + Type: refs[i].Type, + // TODO: Add commit/tag info URL + //URL: ctx.Repo.Repository.APIURL() + "/git/" + refs[i].Type + "s/" + refs[i].Object.String(), + }, + } + } + // If single reference is found and it matches filter exactly return it as object + if len(apiRefs) == 1 && apiRefs[0].Ref == filter { + ctx.JSON(200, &apiRefs[0]) + return + } + ctx.JSON(200, &apiRefs) +} diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go index 9decc8f588..3e3b0ff5a4 100644 --- a/routers/api/v1/swagger/repo.go +++ b/routers/api/v1/swagger/repo.go @@ -36,6 +36,20 @@ type swaggerResponseBranchList struct { Body []api.Branch `json:"body"` } +// Reference +// swagger:response Reference +type swaggerResponseReference struct { + // in:body + Body api.Reference `json:"body"` +} + +// ReferenceList +// swagger:response ReferenceList +type swaggerResponseReferenceList struct { + // in:body + Body []api.Reference `json:"body"` +} + // Hook // swagger:response Hook type swaggerResponseHook struct { |