Skip to content

Commit 36a5d4c

Browse files
authored
Add API for gitignore templates (#22783)
This implements the [Gitignores template API of GitHub](https://docs.github.com/en/rest/gitignore?apiVersion=2022-11-28) in Gitea
1 parent 50133b0 commit 36a5d4c

File tree

6 files changed

+207
-0
lines changed

6 files changed

+207
-0
lines changed

modules/structs/miscellaneous.go

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ type ServerVersion struct {
7272
Version string `json:"version"`
7373
}
7474

75+
// GitignoreTemplateInfo name and text of a gitignore template
76+
type GitignoreTemplateInfo struct {
77+
Name string `json:"name"`
78+
Source string `json:"source"`
79+
}
80+
7581
// LicensesListEntry is used for the API
7682
type LicensesTemplateListEntry struct {
7783
Key string `json:"key"`

routers/api/v1/api.go

+2
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,8 @@ func Routes(ctx gocontext.Context) *web.Route {
719719
m.Post("/markup", bind(api.MarkupOption{}), misc.Markup)
720720
m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
721721
m.Post("/markdown/raw", misc.MarkdownRaw)
722+
m.Get("/gitignore/templates", misc.ListGitignoresTemplates)
723+
m.Get("/gitignore/templates/{name}", misc.GetGitignoreTemplateInfo)
722724
m.Get("/licenses", misc.ListLicenseTemplates)
723725
m.Get("/licenses/{name}", misc.GetLicenseTemplateInfo)
724726
m.Group("/settings", func() {

routers/api/v1/misc/gitignore.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
"code.gitea.io/gitea/modules/options"
11+
repo_module "code.gitea.io/gitea/modules/repository"
12+
"code.gitea.io/gitea/modules/structs"
13+
"code.gitea.io/gitea/modules/util"
14+
)
15+
16+
// Shows a list of all Gitignore templates
17+
func ListGitignoresTemplates(ctx *context.APIContext) {
18+
// swagger:operation GET /gitignore/templates miscellaneous listGitignoresTemplates
19+
// ---
20+
// summary: Returns a list of all gitignore templates
21+
// produces:
22+
// - application/json
23+
// responses:
24+
// "200":
25+
// "$ref": "#/responses/GitignoreTemplateList"
26+
ctx.JSON(http.StatusOK, repo_module.Gitignores)
27+
}
28+
29+
// SHows information about a gitignore template
30+
func GetGitignoreTemplateInfo(ctx *context.APIContext) {
31+
// swagger:operation GET /gitignore/templates/{name} miscellaneous getGitignoreTemplateInfo
32+
// ---
33+
// summary: Returns information about a gitignore template
34+
// produces:
35+
// - application/json
36+
// parameters:
37+
// - name: name
38+
// in: path
39+
// description: name of the template
40+
// type: string
41+
// required: true
42+
// responses:
43+
// "200":
44+
// "$ref": "#/responses/GitignoreTemplateInfo"
45+
// "404":
46+
// "$ref": "#/responses/notFound"
47+
name := util.PathJoinRelX(ctx.Params("name"))
48+
49+
text, err := options.Gitignore(name)
50+
if err != nil {
51+
ctx.NotFound()
52+
return
53+
}
54+
55+
ctx.JSON(http.StatusOK, &structs.GitignoreTemplateInfo{Name: name, Source: string(text)})
56+
}

routers/api/v1/swagger/misc.go

+14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ type swaggerResponseServerVersion struct {
1414
Body api.ServerVersion `json:"body"`
1515
}
1616

17+
// GitignoreTemplateList
18+
// swagger:response GitignoreTemplateList
19+
type swaggerResponseGitignoreTemplateList struct {
20+
// in:body
21+
Body []string `json:"body"`
22+
}
23+
24+
// GitignoreTemplateInfo
25+
// swagger:response GitignoreTemplateInfo
26+
type swaggerResponseGitignoreTemplateInfo struct {
27+
// in:body
28+
Body api.GitignoreTemplateInfo `json:"body"`
29+
}
30+
1731
// LicenseTemplateList
1832
// swagger:response LicenseTemplateList
1933
type swaggerResponseLicensesTemplateList struct {

templates/swagger/v1_json.tmpl

+76
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
"testing"
10+
11+
"code.gitea.io/gitea/modules/options"
12+
repo_module "code.gitea.io/gitea/modules/repository"
13+
api "code.gitea.io/gitea/modules/structs"
14+
"code.gitea.io/gitea/tests"
15+
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
func TestAPIListGitignoresTemplates(t *testing.T) {
20+
defer tests.PrepareTestEnv(t)()
21+
22+
req := NewRequest(t, "GET", "/api/v1/gitignore/templates")
23+
resp := MakeRequest(t, req, http.StatusOK)
24+
25+
// This tests if the API returns a list of strings
26+
var gitignoreList []string
27+
DecodeJSON(t, resp, &gitignoreList)
28+
}
29+
30+
func TestAPIGetGitignoreTemplateInfo(t *testing.T) {
31+
defer tests.PrepareTestEnv(t)()
32+
33+
// If Gitea has for some reason no Gitignore templates, we need to skip this test
34+
if len(repo_module.Gitignores) == 0 {
35+
return
36+
}
37+
38+
// Use the first template for the test
39+
templateName := repo_module.Gitignores[0]
40+
41+
urlStr := fmt.Sprintf("/api/v1/gitignore/templates/%s", templateName)
42+
req := NewRequest(t, "GET", urlStr)
43+
resp := MakeRequest(t, req, http.StatusOK)
44+
45+
var templateInfo api.GitignoreTemplateInfo
46+
DecodeJSON(t, resp, &templateInfo)
47+
48+
// We get the text of the template here
49+
text, _ := options.Gitignore(templateName)
50+
51+
assert.Equal(t, templateInfo.Name, templateName)
52+
assert.Equal(t, templateInfo.Source, string(text))
53+
}

0 commit comments

Comments
 (0)