code.gitea.io/gitea@v1.21.7/routers/api/v1/repo/git_ref.go (about)

     1  // Copyright 2018 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"net/http"
     8  	"net/url"
     9  
    10  	"code.gitea.io/gitea/modules/context"
    11  	api "code.gitea.io/gitea/modules/structs"
    12  	"code.gitea.io/gitea/modules/util"
    13  	"code.gitea.io/gitea/routers/api/v1/utils"
    14  )
    15  
    16  // GetGitAllRefs get ref or an list all the refs of a repository
    17  func GetGitAllRefs(ctx *context.APIContext) {
    18  	// swagger:operation GET /repos/{owner}/{repo}/git/refs repository repoListAllGitRefs
    19  	// ---
    20  	// summary: Get specified ref or filtered repository's refs
    21  	// produces:
    22  	// - application/json
    23  	// parameters:
    24  	// - name: owner
    25  	//   in: path
    26  	//   description: owner of the repo
    27  	//   type: string
    28  	//   required: true
    29  	// - name: repo
    30  	//   in: path
    31  	//   description: name of the repo
    32  	//   type: string
    33  	//   required: true
    34  	// responses:
    35  	//   "200":
    36  	// #   "$ref": "#/responses/Reference" TODO: swagger doesnt support different output formats by ref
    37  	//     "$ref": "#/responses/ReferenceList"
    38  	//   "404":
    39  	//     "$ref": "#/responses/notFound"
    40  
    41  	getGitRefsInternal(ctx, "")
    42  }
    43  
    44  // GetGitRefs get ref or an filteresd list of refs of a repository
    45  func GetGitRefs(ctx *context.APIContext) {
    46  	// swagger:operation GET /repos/{owner}/{repo}/git/refs/{ref} repository repoListGitRefs
    47  	// ---
    48  	// summary: Get specified ref or filtered repository's refs
    49  	// produces:
    50  	// - application/json
    51  	// parameters:
    52  	// - name: owner
    53  	//   in: path
    54  	//   description: owner of the repo
    55  	//   type: string
    56  	//   required: true
    57  	// - name: repo
    58  	//   in: path
    59  	//   description: name of the repo
    60  	//   type: string
    61  	//   required: true
    62  	// - name: ref
    63  	//   in: path
    64  	//   description: part or full name of the ref
    65  	//   type: string
    66  	//   required: true
    67  	// responses:
    68  	//   "200":
    69  	// #   "$ref": "#/responses/Reference" TODO: swagger doesnt support different output formats by ref
    70  	//     "$ref": "#/responses/ReferenceList"
    71  	//   "404":
    72  	//     "$ref": "#/responses/notFound"
    73  
    74  	getGitRefsInternal(ctx, ctx.Params("*"))
    75  }
    76  
    77  func getGitRefsInternal(ctx *context.APIContext, filter string) {
    78  	refs, lastMethodName, err := utils.GetGitRefs(ctx, filter)
    79  	if err != nil {
    80  		ctx.Error(http.StatusInternalServerError, lastMethodName, err)
    81  		return
    82  	}
    83  
    84  	if len(refs) == 0 {
    85  		ctx.NotFound()
    86  		return
    87  	}
    88  
    89  	apiRefs := make([]*api.Reference, len(refs))
    90  	for i := range refs {
    91  		apiRefs[i] = &api.Reference{
    92  			Ref: refs[i].Name,
    93  			URL: ctx.Repo.Repository.APIURL() + "/git/" + util.PathEscapeSegments(refs[i].Name),
    94  			Object: &api.GitObject{
    95  				SHA:  refs[i].Object.String(),
    96  				Type: refs[i].Type,
    97  				URL:  ctx.Repo.Repository.APIURL() + "/git/" + url.PathEscape(refs[i].Type) + "s/" + url.PathEscape(refs[i].Object.String()),
    98  			},
    99  		}
   100  	}
   101  	// If single reference is found and it matches filter exactly return it as object
   102  	if len(apiRefs) == 1 && apiRefs[0].Ref == filter {
   103  		ctx.JSON(http.StatusOK, &apiRefs[0])
   104  		return
   105  	}
   106  	ctx.JSON(http.StatusOK, &apiRefs)
   107  }