Cleanup openapi generation (#4331)

Co-authored-by: oauth <woodpecker-bot@obermui.de>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
Anbraten
2024-11-23 09:17:19 +01:00
committed by GitHub
parent 8e0aac4cab
commit 07c8765b72
26 changed files with 296 additions and 235 deletions

View File

@@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !generate
// +build !generate
package main
import (
@@ -22,7 +25,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v3"
_ "go.woodpecker-ci.org/woodpecker/v2/cmd/server/docs"
_ "go.woodpecker-ci.org/woodpecker/v2/cmd/server/openapi"
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
"go.woodpecker-ci.org/woodpecker/v2/version"
)
@@ -46,7 +49,7 @@ func main() {
}
app.Flags = flags
setupSwaggerStaticConfig()
setupOpenAPIStaticConfig()
if err := app.Run(ctx, os.Args); err != nil {
log.Error().Err(err).Msgf("error running server")

38
cmd/server/openapi.go Normal file
View File

@@ -0,0 +1,38 @@
// Copyright 2023 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"go.woodpecker-ci.org/woodpecker/v2/cmd/server/openapi"
"go.woodpecker-ci.org/woodpecker/v2/version"
)
// Generate docs/openapi.json via:
//go:generate go run github.com/swaggo/swag/cmd/swag init -g cmd/server/openapi.go --outputTypes go -output openapi -d ../../
//go:generate go run openapi_json_gen.go openapi.go
//go:generate go run github.com/getkin/kin-openapi/cmd/validate@latest ../../docs/openapi.json
// setupOpenAPIStaticConfig initializes static content (version) for the OpenAPI config.
//
// @title Woodpecker CI API
// @description Woodpecker is a simple, yet powerful CI/CD engine with great extensibility.
// @description To get a personal access token (PAT) for authentication, please log in your Woodpecker server,
// @description and go to you personal profile page, by clicking the user icon at the top right.
// @BasePath /api
// @contact.name Woodpecker CI
// @contact.url https://woodpecker-ci.org/
func setupOpenAPIStaticConfig() {
openapi.SwaggerInfo.Version = version.String()
}

View File

@@ -1,5 +1,5 @@
// Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs
// Package openapi Code generated by swaggo/swag. DO NOT EDIT
package openapi
import "github.com/swaggo/swag"
@@ -10,7 +10,7 @@ const docTemplate = `{
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"contact": {
"name": "Woodpecker CI Community",
"name": "Woodpecker CI",
"url": "https://woodpecker-ci.org/"
},
"version": "{{.Version}}"
@@ -122,7 +122,7 @@ const docTemplate = `{
{
"type": "integer",
"description": "the agent's id",
"name": "agent",
"name": "agent_id",
"in": "path",
"required": true
}
@@ -156,7 +156,7 @@ const docTemplate = `{
{
"type": "integer",
"description": "the agent's id",
"name": "agent",
"name": "agent_id",
"in": "path",
"required": true
}
@@ -187,7 +187,7 @@ const docTemplate = `{
{
"type": "integer",
"description": "the agent's id",
"name": "agent",
"name": "agent_id",
"in": "path",
"required": true
},
@@ -232,7 +232,7 @@ const docTemplate = `{
{
"type": "integer",
"description": "the agent's id",
"name": "agent",
"name": "agent_id",
"in": "path",
"required": true
}
@@ -4554,22 +4554,15 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"type": "object",
"properties": {
"source": {
"type": "string"
},
{
"type": "object",
"properties": {
"source": {
"type": "string"
},
"version": {
"type": "string"
}
}
"version": {
"type": "string"
}
]
}
}
}
}
@@ -5791,10 +5784,10 @@ const docTemplate = `{
var SwaggerInfo = &swag.Spec{
Version: "",
Host: "",
BasePath: "",
BasePath: "/api",
Schemes: []string{},
Title: "",
Description: "",
Title: "Woodpecker CI API",
Description: "Woodpecker is a simple, yet powerful CI/CD engine with great extensibility.\nTo get a personal access token (PAT) for authentication, please log in your Woodpecker server,\nand go to you personal profile page, by clicking the user icon at the top right.",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",

View File

@@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// ************************************************************************************************
// This is a generator tool, to update the Markdown documentation for the woodpecker-ci.org website
// ************************************************************************************************
// *********************************************************
// This is a generator tool, to update the openapi.json file
// *********************************************************
//go:build generate
// +build generate
@@ -30,23 +30,24 @@ import (
"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi2conv"
"go.woodpecker-ci.org/woodpecker/v2/cmd/server/docs"
"go.woodpecker-ci.org/woodpecker/v2/cmd/server/openapi"
)
func main() {
// set swagger infos
setupSwaggerStaticConfig()
// set openapi infos
setupOpenAPIStaticConfig()
basePath := path.Join("..", "..")
filePath := path.Join(basePath, "docs", "swagger.json")
filePath := path.Join(basePath, "docs", "openapi.json")
// generate swagger file
// generate openapi file
f, err := os.Create(filePath)
if err != nil {
panic(err)
}
defer f.Close()
doc := docs.SwaggerInfo.ReadDoc()
doc := openapi.SwaggerInfo.ReadDoc()
doc, err = removeHost(doc)
if err != nil {
panic(err)
@@ -56,6 +57,8 @@ func main() {
panic(err)
}
fmt.Println("generated openapi.json")
// convert to OpenApi3
if err := toOpenApi3(filePath, filePath); err != nil {
fmt.Printf("converting '%s' from openapi v2 to v3 failed\n", filePath)

View File

@@ -0,0 +1,14 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v2/cmd/server/openapi"
)
func TestSetupOpenApiStaticConfig(t *testing.T) {
setupOpenAPIStaticConfig()
assert.Equal(t, "/api", openapi.SwaggerInfo.BasePath)
}

View File

@@ -1,39 +0,0 @@
// Copyright 2023 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"go.woodpecker-ci.org/woodpecker/v2/cmd/server/docs"
"go.woodpecker-ci.org/woodpecker/v2/version"
)
// Generate docs/swagger.json via:
//go:generate go run woodpecker_docs_gen.go swagger.go
//go:generate go run github.com/getkin/kin-openapi/cmd/validate@latest ../../docs/swagger.json
// setupSwaggerStaticConfig initializes static content only (contacts, title and description)
// for dynamic configuration of e.g. hostname, etc. see router.setupSwaggerConfigAndRoutes
//
// @contact.name Woodpecker CI Community
// @contact.url https://woodpecker-ci.org/
func setupSwaggerStaticConfig() {
docs.SwaggerInfo.BasePath = "/api"
docs.SwaggerInfo.InfoInstanceName = "api"
docs.SwaggerInfo.Title = "Woodpecker CI API"
docs.SwaggerInfo.Version = version.String()
docs.SwaggerInfo.Description = "Woodpecker is a simple, yet powerful CI/CD engine with great extensibility.\n" +
"To get a personal access token (PAT) for authentication, please log in your Woodpecker server,\n" +
"and go to you personal profile page, by clicking the user icon at the top right."
}

View File

@@ -1,14 +0,0 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v2/cmd/server/docs"
)
func TestSetupSwaggerStaticConfig(t *testing.T) {
setupSwaggerStaticConfig()
assert.Equal(t, "/api", docs.SwaggerInfo.BasePath)
}