Skip to content

Commit 25dc155

Browse files
authored
Add API for Label templates (#24602)
This adds API that allows getting the Label templates of the Gitea Instance
1 parent abcf5a7 commit 25dc155

File tree

7 files changed

+258
-0
lines changed

7 files changed

+258
-0
lines changed

modules/structs/issue_label.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@ type IssueLabelsOption struct {
4444
// list of label IDs
4545
Labels []int64 `json:"labels"`
4646
}
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+
}

routers/api/v1/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ func Routes(ctx gocontext.Context) *web.Route {
723723
m.Get("/gitignore/templates/{name}", misc.GetGitignoreTemplateInfo)
724724
m.Get("/licenses", misc.ListLicenseTemplates)
725725
m.Get("/licenses/{name}", misc.GetLicenseTemplateInfo)
726+
m.Get("/label/templates", misc.ListLabelTemplates)
727+
m.Get("/label/templates/{name}", misc.GetLabelTemplate)
726728
m.Group("/settings", func() {
727729
m.Get("/ui", settings.GetGeneralUISettings)
728730
m.Get("/api", settings.GetGeneralAPISettings)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

routers/api/v1/swagger/misc.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,17 @@ type swaggerResponseStringSlice struct {
4848
// in:body
4949
Body []string `json:"body"`
5050
}
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+
}

services/convert/issue.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
issues_model "code.gitea.io/gitea/models/issues"
1414
repo_model "code.gitea.io/gitea/models/repo"
1515
user_model "code.gitea.io/gitea/models/user"
16+
"code.gitea.io/gitea/modules/label"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/setting"
1819
api "code.gitea.io/gitea/modules/structs"
@@ -238,3 +239,24 @@ func ToAPIMilestone(m *issues_model.Milestone) *api.Milestone {
238239
}
239240
return apiMilestone
240241
}
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+
}

templates/swagger/v1_json.tmpl

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

0 commit comments

Comments
 (0)