Skip to content

Commit fed1bab

Browse files
chore: update makefile and add new tool
this tool will generate group-related swagger schemas without repeating the same comments twice in different places
1 parent 46ada3a commit fed1bab

File tree

3 files changed

+15204
-5
lines changed

3 files changed

+15204
-5
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ endif
181181

182182
SWAGGER_SPEC := templates/swagger/v1_json.tmpl
183183
SWAGGER_SPEC_INPUT := templates/swagger/v1_input.json
184+
SWAGGER_SPEC_GROUP_INPUT := templates/swagger/v1_groups.json
184185
SWAGGER_EXCLUDE := code.gitea.io/sdk
185186

186187
TEST_MYSQL_HOST ?= mysql:3306
@@ -293,6 +294,8 @@ generate-swagger: $(SWAGGER_SPEC) ## generate the swagger spec from code comment
293294

294295
$(SWAGGER_SPEC): $(GO_SOURCES) $(SWAGGER_SPEC_INPUT)
295296
$(GO) run $(SWAGGER_PACKAGE) generate spec --exclude "$(SWAGGER_EXCLUDE)" --input "$(SWAGGER_SPEC_INPUT)" --output './$(SWAGGER_SPEC)'
297+
$(GO) generate -v ./build_tools/...
298+
$(GO) run $(SWAGGER_PACKAGE) mixin -o './$(SWAGGER_SPEC)' $(SWAGGER_SPEC) $(SWAGGER_SPEC_GROUP_INPUT)
296299

297300
.PHONY: swagger-check
298301
swagger-check: generate-swagger

build_tools/swagger/main.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//go:generate go run main.go ../../
2+
3+
package main
4+
5+
import (
6+
"encoding/json"
7+
"log"
8+
"os"
9+
"path/filepath"
10+
"regexp"
11+
)
12+
13+
var rxPath = regexp.MustCompile("(?m)^(/repos/\\{owner})/(\\{repo})")
14+
15+
func generatePaths(root string) map[string]any {
16+
pathData := make(map[string]any)
17+
endpoints := make(map[string]any)
18+
fileToRead, err := filepath.Rel(root, "./templates/swagger/v1_json.tmpl")
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
swaggerBytes, err := os.ReadFile(fileToRead)
23+
if err != nil {
24+
log.Fatal(err)
25+
}
26+
raw := make(map[string]any)
27+
err = json.Unmarshal(swaggerBytes, &raw)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
paths := raw["paths"].(map[string]any)
32+
for k, v := range paths {
33+
if !rxPath.MatchString(k) {
34+
// skip if this endpoint does not start with `/repos/{owner}/{repo}`
35+
continue
36+
}
37+
// generate new endpoint path with `/group/{group_id}` in between the `owner` and `repo` params
38+
nk := rxPath.ReplaceAllString(k, "$1/group/{group_id}/$2")
39+
methodMap := v.(map[string]any)
40+
41+
for method, methodSpec := range methodMap {
42+
specMap := methodSpec.(map[string]any)
43+
params := specMap["parameters"].([]any)
44+
params = append(params, map[string]any{
45+
"description": "group ID of the repo",
46+
"name": "group_id",
47+
"type": "integer",
48+
"format": "int64",
49+
"required": true,
50+
"in": "path",
51+
})
52+
// i believe for...range loops create copies of each item that's iterated over,
53+
// so we need to take extra care to ensure we're mutating the original map entry
54+
(methodMap[method].(map[string]any))["parameters"] = params
55+
}
56+
endpoints[nk] = methodMap
57+
}
58+
pathData["paths"] = endpoints
59+
return pathData
60+
}
61+
62+
func writeMapToFile(filename string, data map[string]any) {
63+
bytes, err := json.MarshalIndent(data, "", "\t")
64+
if err != nil {
65+
log.Fatal(err)
66+
}
67+
err = os.WriteFile(filename, bytes, 0666)
68+
if err != nil {
69+
log.Fatal(err)
70+
}
71+
}
72+
73+
func main() {
74+
var err error
75+
root := "../../"
76+
if len(os.Args) > 1 {
77+
root = os.Args[1]
78+
}
79+
err = os.Chdir(root)
80+
if err != nil {
81+
log.Fatal(err)
82+
}
83+
84+
pathData := generatePaths(".")
85+
out := "./templates/swagger/v1_groups.json"
86+
writeMapToFile(out, pathData)
87+
}

0 commit comments

Comments
 (0)