loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add API for Label templates (#24602)

This adds API that allows getting the Label templates of the Gitea
Instance

authored by

JakobDev and committed by
GitHub
25dc1556 abcf5a7b

+258
+10
modules/structs/issue_label.go
··· 44 44 // list of label IDs 45 45 Labels []int64 `json:"labels"` 46 46 } 47 + 48 + // LabelTemplate info of a Label template 49 + type LabelTemplate struct { 50 + Name string `json:"name"` 51 + // example: false 52 + Exclusive bool `json:"exclusive"` 53 + // example: 00aabb 54 + Color string `json:"color"` 55 + Description string `json:"description"` 56 + }
+2
routers/api/v1/api.go
··· 723 723 m.Get("/gitignore/templates/{name}", misc.GetGitignoreTemplateInfo) 724 724 m.Get("/licenses", misc.ListLicenseTemplates) 725 725 m.Get("/licenses/{name}", misc.GetLicenseTemplateInfo) 726 + m.Get("/label/templates", misc.ListLabelTemplates) 727 + m.Get("/label/templates/{name}", misc.GetLabelTemplate) 726 728 m.Group("/settings", func() { 727 729 m.Get("/ui", settings.GetGeneralUISettings) 728 730 m.Get("/api", settings.GetGeneralAPISettings)
+60
routers/api/v1/misc/label_templates.go
··· 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 + }
+14
routers/api/v1/swagger/misc.go
··· 48 48 // in:body 49 49 Body []string `json:"body"` 50 50 } 51 + 52 + // LabelTemplateList 53 + // swagger:response LabelTemplateList 54 + type swaggerResponseLabelTemplateList struct { 55 + // in:body 56 + Body []string `json:"body"` 57 + } 58 + 59 + // LabelTemplateInfo 60 + // swagger:response LabelTemplateInfo 61 + type swaggerResponseLabelTemplateInfo struct { 62 + // in:body 63 + Body []api.LabelTemplate `json:"body"` 64 + }
+22
services/convert/issue.go
··· 13 13 issues_model "code.gitea.io/gitea/models/issues" 14 14 repo_model "code.gitea.io/gitea/models/repo" 15 15 user_model "code.gitea.io/gitea/models/user" 16 + "code.gitea.io/gitea/modules/label" 16 17 "code.gitea.io/gitea/modules/log" 17 18 "code.gitea.io/gitea/modules/setting" 18 19 api "code.gitea.io/gitea/modules/structs" ··· 238 239 } 239 240 return apiMilestone 240 241 } 242 + 243 + // ToLabelTemplate converts Label to API format 244 + func ToLabelTemplate(label *label.Label) *api.LabelTemplate { 245 + result := &api.LabelTemplate{ 246 + Name: label.Name, 247 + Exclusive: label.Exclusive, 248 + Color: strings.TrimLeft(label.Color, "#"), 249 + Description: label.Description, 250 + } 251 + 252 + return result 253 + } 254 + 255 + // ToLabelTemplateList converts list of Label to API format 256 + func ToLabelTemplateList(labels []*label.Label) []*api.LabelTemplate { 257 + result := make([]*api.LabelTemplate, len(labels)) 258 + for i := range labels { 259 + result[i] = ToLabelTemplate(labels[i]) 260 + } 261 + return result 262 + }
+89
templates/swagger/v1_json.tmpl
··· 929 929 } 930 930 } 931 931 }, 932 + "/label/templates": { 933 + "get": { 934 + "produces": [ 935 + "application/json" 936 + ], 937 + "tags": [ 938 + "miscellaneous" 939 + ], 940 + "summary": "Returns a list of all label templates", 941 + "operationId": "listLabelTemplates", 942 + "responses": { 943 + "200": { 944 + "$ref": "#/responses/LabelTemplateList" 945 + } 946 + } 947 + } 948 + }, 949 + "/label/templates/{name}": { 950 + "get": { 951 + "produces": [ 952 + "application/json" 953 + ], 954 + "tags": [ 955 + "miscellaneous" 956 + ], 957 + "summary": "Returns all labels in a template", 958 + "operationId": "getLabelTemplateInfo", 959 + "parameters": [ 960 + { 961 + "type": "string", 962 + "description": "name of the template", 963 + "name": "name", 964 + "in": "path", 965 + "required": true 966 + } 967 + ], 968 + "responses": { 969 + "200": { 970 + "$ref": "#/responses/LabelTemplateInfo" 971 + }, 972 + "404": { 973 + "$ref": "#/responses/notFound" 974 + } 975 + } 976 + } 977 + }, 932 978 "/licenses": { 933 979 "get": { 934 980 "produces": [ ··· 18858 18904 }, 18859 18905 "x-go-package": "code.gitea.io/gitea/modules/structs" 18860 18906 }, 18907 + "LabelTemplate": { 18908 + "description": "LabelTemplate info of a Label template", 18909 + "type": "object", 18910 + "properties": { 18911 + "color": { 18912 + "type": "string", 18913 + "x-go-name": "Color", 18914 + "example": "00aabb" 18915 + }, 18916 + "description": { 18917 + "type": "string", 18918 + "x-go-name": "Description" 18919 + }, 18920 + "exclusive": { 18921 + "type": "boolean", 18922 + "x-go-name": "Exclusive", 18923 + "example": false 18924 + }, 18925 + "name": { 18926 + "type": "string", 18927 + "x-go-name": "Name" 18928 + } 18929 + }, 18930 + "x-go-package": "code.gitea.io/gitea/modules/structs" 18931 + }, 18861 18932 "LicenseTemplateInfo": { 18862 18933 "description": "LicensesInfo contains information about a License", 18863 18934 "type": "object", ··· 21794 21865 "type": "array", 21795 21866 "items": { 21796 21867 "$ref": "#/definitions/Label" 21868 + } 21869 + } 21870 + }, 21871 + "LabelTemplateInfo": { 21872 + "description": "LabelTemplateInfo", 21873 + "schema": { 21874 + "type": "array", 21875 + "items": { 21876 + "$ref": "#/definitions/LabelTemplate" 21877 + } 21878 + } 21879 + }, 21880 + "LabelTemplateList": { 21881 + "description": "LabelTemplateList", 21882 + "schema": { 21883 + "type": "array", 21884 + "items": { 21885 + "type": "string" 21797 21886 } 21798 21887 } 21799 21888 },
+61
tests/integration/api_label_templates_test.go
··· 1 + // Copyright 2023 The Gitea Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package integration 5 + 6 + import ( 7 + "fmt" 8 + "net/http" 9 + "net/url" 10 + "strings" 11 + "testing" 12 + 13 + repo_module "code.gitea.io/gitea/modules/repository" 14 + api "code.gitea.io/gitea/modules/structs" 15 + "code.gitea.io/gitea/tests" 16 + 17 + "github.com/stretchr/testify/assert" 18 + ) 19 + 20 + func TestAPIListLabelTemplates(t *testing.T) { 21 + defer tests.PrepareTestEnv(t)() 22 + 23 + req := NewRequest(t, "GET", "/api/v1/label/templates") 24 + resp := MakeRequest(t, req, http.StatusOK) 25 + 26 + var templateList []string 27 + DecodeJSON(t, resp, &templateList) 28 + 29 + for i := range repo_module.LabelTemplateFiles { 30 + assert.Equal(t, repo_module.LabelTemplateFiles[i].DisplayName, templateList[i]) 31 + } 32 + } 33 + 34 + func TestAPIGetLabelTemplateInfo(t *testing.T) { 35 + defer tests.PrepareTestEnv(t)() 36 + 37 + // If Gitea has for some reason no Label templates, we need to skip this test 38 + if len(repo_module.LabelTemplateFiles) == 0 { 39 + return 40 + } 41 + 42 + // Use the first template for the test 43 + templateName := repo_module.LabelTemplateFiles[0].DisplayName 44 + 45 + urlStr := fmt.Sprintf("/api/v1/label/templates/%s", url.PathEscape(templateName)) 46 + req := NewRequest(t, "GET", urlStr) 47 + resp := MakeRequest(t, req, http.StatusOK) 48 + 49 + var templateInfo []api.LabelTemplate 50 + DecodeJSON(t, resp, &templateInfo) 51 + 52 + labels, err := repo_module.LoadTemplateLabelsByDisplayName(templateName) 53 + assert.NoError(t, err) 54 + 55 + for i := range labels { 56 + assert.Equal(t, strings.TrimLeft(labels[i].Color, "#"), templateInfo[i].Color) 57 + assert.Equal(t, labels[i].Description, templateInfo[i].Description) 58 + assert.Equal(t, labels[i].Exclusive, templateInfo[i].Exclusive) 59 + assert.Equal(t, labels[i].Name, templateInfo[i].Name) 60 + } 61 + }