code.gitea.io/gitea@v1.21.7/routers/api/v1/misc/label_templates.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package misc
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"code.gitea.io/gitea/modules/context"
    10  	repo_module "code.gitea.io/gitea/modules/repository"
    11  	"code.gitea.io/gitea/modules/util"
    12  	"code.gitea.io/gitea/services/convert"
    13  )
    14  
    15  // Shows a list of all Label templates
    16  func ListLabelTemplates(ctx *context.APIContext) {
    17  	// swagger:operation GET /label/templates miscellaneous listLabelTemplates
    18  	// ---
    19  	// summary: Returns a list of all label templates
    20  	// produces:
    21  	// - application/json
    22  	// responses:
    23  	//   "200":
    24  	//     "$ref": "#/responses/LabelTemplateList"
    25  	result := make([]string, len(repo_module.LabelTemplateFiles))
    26  	for i := range repo_module.LabelTemplateFiles {
    27  		result[i] = repo_module.LabelTemplateFiles[i].DisplayName
    28  	}
    29  
    30  	ctx.JSON(http.StatusOK, result)
    31  }
    32  
    33  // Shows all labels in a template
    34  func GetLabelTemplate(ctx *context.APIContext) {
    35  	// swagger:operation GET /label/templates/{name} miscellaneous getLabelTemplateInfo
    36  	// ---
    37  	// summary: Returns all labels in a template
    38  	// produces:
    39  	// - application/json
    40  	// parameters:
    41  	// - name: name
    42  	//   in: path
    43  	//   description: name of the template
    44  	//   type: string
    45  	//   required: true
    46  	// responses:
    47  	//   "200":
    48  	//     "$ref": "#/responses/LabelTemplateInfo"
    49  	//   "404":
    50  	//     "$ref": "#/responses/notFound"
    51  	name := util.PathJoinRelX(ctx.Params("name"))
    52  
    53  	labels, err := repo_module.LoadTemplateLabelsByDisplayName(name)
    54  	if err != nil {
    55  		ctx.NotFound()
    56  		return
    57  	}
    58  
    59  	ctx.JSON(http.StatusOK, convert.ToLabelTemplateList(labels))
    60  }