Skip to content

Commit 82edd24

Browse files
earl-warrenLoïc Dachary
authored andcommitted
[API] Forgejo API /api/forgejo/v1
1 parent af47016 commit 82edd24

File tree

14 files changed

+400
-2
lines changed

14 files changed

+400
-2
lines changed

Makefile

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ else
9696
endif
9797
endif
9898

99-
LDFLAGS := $(LDFLAGS) -X "main.MakeVersion=$(MAKE_VERSION)" -X "main.Version=$(GITEA_VERSION)" -X "main.Tags=$(TAGS)"
99+
# SemVer
100+
FORGEJO_VERSION := v2.0.0
101+
102+
LDFLAGS := $(LDFLAGS) -X "main.MakeVersion=$(MAKE_VERSION)" -X "main.Version=$(GITEA_VERSION)" -X "main.Tags=$(TAGS)" -X "code.gitea.io/gitea/routers/api/forgejo/v1.ForgejoVersion=$(FORGEJO_VERSION)"
100103

101104
LINUX_ARCHS ?= linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64
102105

@@ -147,6 +150,8 @@ ifdef DEPS_PLAYWRIGHT
147150
PLAYWRIGHT_FLAGS += --with-deps
148151
endif
149152

153+
FORGEJO_API_SPEC := public/forgejo/api.v1.yml
154+
150155
SWAGGER_SPEC := templates/swagger/v1_json.tmpl
151156
SWAGGER_SPEC_S_TMPL := s|"basePath": *"/api/v1"|"basePath": "{{AppSubUrl \| JSEscape \| Safe}}/api/v1"|g
152157
SWAGGER_SPEC_S_JSON := s|"basePath": *"{{AppSubUrl \| JSEscape \| Safe}}/api/v1"|"basePath": "/api/v1"|g
@@ -207,6 +212,8 @@ help:
207212
@echo " - generate-license update license files"
208213
@echo " - generate-gitignore update gitignore files"
209214
@echo " - generate-manpage generate manpage"
215+
@echo " - generate-forgejo-api generate the forgejo API from spec"
216+
@echo " - forgejo-api-validate check if the forgejo API matches the specs"
210217
@echo " - generate-swagger generate the swagger spec from code comments"
211218
@echo " - swagger-validate check if the swagger spec is valid"
212219
@echo " - golangci-lint run golangci-lint linter"
@@ -296,6 +303,27 @@ ifneq "$(TAGS)" "$(shell cat $(TAGS_EVIDENCE) 2>/dev/null)"
296303
TAGS_PREREQ := $(TAGS_EVIDENCE)
297304
endif
298305

306+
OAPI_CODEGEN_PACKAGE ?= github.com/deepmap/oapi-codegen/cmd/[email protected]
307+
KIN_OPENAPI_CODEGEN_PACKAGE ?= github.com/getkin/kin-openapi/cmd/[email protected]
308+
FORGEJO_API_SERVER = routers/api/forgejo/v1/generated.go
309+
310+
.PHONY: generate-forgejo-api
311+
generate-forgejo-api: $(FORGEJO_API_SPEC)
312+
$(GO) run $(OAPI_CODEGEN_PACKAGE) -package v1 -generate chi-server,types $< > $(FORGEJO_API_SERVER)
313+
314+
.PHONY: forgejo-api-check
315+
forgejo-api-check: generate-forgejo-api
316+
@diff=$$(git diff $(FORGEJO_API_SERVER) ; \
317+
if [ -n "$$diff" ]; then \
318+
echo "Please run 'make generate-forgejo-api' and commit the result:"; \
319+
echo "$${diff}"; \
320+
exit 1; \
321+
fi
322+
323+
.PHONY: forgejo-api-validate
324+
forgejo-api-validate:
325+
$(GO) run $(KIN_OPENAPI_CODEGEN_PACKAGE) $(FORGEJO_API_SPEC)
326+
299327
.PHONY: generate-swagger
300328
generate-swagger: $(SWAGGER_SPEC)
301329

@@ -331,7 +359,7 @@ checks: checks-frontend checks-backend
331359
checks-frontend: lockfile-check svg-check
332360

333361
.PHONY: checks-backend
334-
checks-backend: tidy-check swagger-check fmt-check misspell-check swagger-validate
362+
checks-backend: tidy-check swagger-check fmt-check misspell-check forgejo-api-validate swagger-validate
335363

336364
.PHONY: lint
337365
lint: lint-frontend lint-backend

public/forgejo/api.v1.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Forgejo API
4+
description: |-
5+
Forgejo REST API
6+
7+
contact:
8+
9+
license:
10+
name: MIT
11+
url: https://codeberg.org/forgejo/forgejo/src/branch/forgejo/LICENSE
12+
version: 1.0.0
13+
externalDocs:
14+
description: Find out more about Forgejo
15+
url: http://forgejo.org
16+
servers:
17+
- url: /api/forgejo/v1
18+
paths:
19+
/version:
20+
get:
21+
summary: API version
22+
description: Semantic version of the Forgejo API
23+
operationId: getVersion
24+
responses:
25+
'200':
26+
description: successful operation
27+
content:
28+
application/json:
29+
schema:
30+
type: array
31+
items:
32+
$ref: '#/components/schemas/Version'
33+
components:
34+
schemas:
35+
Version:
36+
type: object
37+
properties:
38+
number:
39+
type: string
40+

routers/api/forgejo/v1/api.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2023 The Forgejo Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1
5+
6+
import (
7+
gocontext "context"
8+
9+
"code.gitea.io/gitea/modules/web"
10+
)
11+
12+
func Routes(ctx gocontext.Context) *web.Route {
13+
m := web.NewRoute()
14+
forgejo := NewForgejo()
15+
m.Get("", Root)
16+
m.Get("/version", forgejo.GetVersion)
17+
return m
18+
}

routers/api/forgejo/v1/forgejo.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
package v1
4+
5+
import (
6+
"net/http"
7+
8+
"code.gitea.io/gitea/modules/json"
9+
)
10+
11+
type Forgejo struct{}
12+
13+
var _ ServerInterface = &Forgejo{}
14+
15+
func NewForgejo() *Forgejo {
16+
return &Forgejo{}
17+
}
18+
19+
var ForgejoVersion = "development"
20+
21+
func (f *Forgejo) GetVersion(w http.ResponseWriter, r *http.Request) {
22+
w.WriteHeader(http.StatusOK)
23+
_ = json.NewEncoder(w).Encode(Version{&ForgejoVersion})
24+
}

routers/api/forgejo/v1/generated.go

Lines changed: 167 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routers/api/forgejo/v1/root.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright The Forgejo Authors.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1
5+
6+
import (
7+
"net/http"
8+
)
9+
10+
func Root(w http.ResponseWriter, r *http.Request) {
11+
// https://www.rfc-editor.org/rfc/rfc8631
12+
w.Header().Set("Link", "</assets/forgejo/api.v1.yml>; rel=\"service-desc\"")
13+
w.WriteHeader(http.StatusNoContent)
14+
}

routers/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"code.gitea.io/gitea/modules/translation"
3232
"code.gitea.io/gitea/modules/util"
3333
"code.gitea.io/gitea/modules/web"
34+
forgejo "code.gitea.io/gitea/routers/api/forgejo/v1"
3435
packages_router "code.gitea.io/gitea/routers/api/packages"
3536
apiv1 "code.gitea.io/gitea/routers/api/v1"
3637
"code.gitea.io/gitea/routers/common"
@@ -184,6 +185,7 @@ func NormalRoutes(ctx context.Context) *web.Route {
184185

185186
r.Mount("/", web_routers.Routes(ctx))
186187
r.Mount("/api/v1", apiv1.Routes(ctx))
188+
r.Mount("/api/forgejo/v1", forgejo.Routes(ctx))
187189
r.Mount("/api/internal", private.Routes())
188190
if setting.Packages.Enabled {
189191
r.Mount("/api/packages", packages_router.Routes(ctx))

routers/web/misc/swagger-forgejo.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 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/base"
10+
"code.gitea.io/gitea/modules/context"
11+
)
12+
13+
// tplSwagger swagger page template
14+
const tplForgejoSwagger base.TplName = "swagger/forgejo-ui"
15+
16+
func SwaggerForgejo(ctx *context.Context) {
17+
ctx.Data["APIVersion"] = "v1"
18+
ctx.HTML(http.StatusOK, tplForgejoSwagger)
19+
}

routers/web/web.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ func Routes(ctx gocontext.Context) *web.Route {
211211
if setting.API.EnableSwagger {
212212
// Note: The route moved from apiroutes because it's in fact want to render a web page
213213
routes.Get("/api/swagger", append(common, misc.Swagger)...) // Render V1 by default
214+
routes.Get("/api/forgejo/swagger", append(common, misc.SwaggerForgejo)...)
214215
}
215216

216217
// TODO: These really seem like things that could be folded into Contexter or as helper functions

templates/swagger/forgejo-ui.tmpl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Forgejo API</title>
6+
<link href="{{AssetUrlPrefix}}/css/swagger.css?v={{AssetVersion}}" rel="stylesheet">
7+
</head>
8+
<body>
9+
<a class="swagger-back-link" href="{{AppUrl}}">{{svg "octicon-reply"}}{{.locale.Tr "return_to_gitea"}}</a>
10+
<div id="swagger-ui" data-source="{{AssetUrlPrefix}}/forgejo/api.{{.APIVersion}}.yml"></div>
11+
<script src="{{AssetUrlPrefix}}/js/forgejoswagger.js?v={{AssetVersion}}"></script>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)