Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup openapi generation #4331

Merged
merged 44 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e28c596
generate swagger client
anbraten Nov 1, 2023
e8b6987
Merge remote-tracking branch 'upstream/main' into swagger-client
anbraten Jun 3, 2024
4618dd7
undo
anbraten Jun 3, 2024
09e1102
generate go client
anbraten Jun 3, 2024
7391542
enhance client
anbraten Jun 3, 2024
bbe4533
add streaming
anbraten Jun 3, 2024
1a8cf30
Merge remote-tracking branch 'upstream/main' into swagger-client
anbraten Jul 13, 2024
5fedc18
rm old
anbraten Jul 13, 2024
3a2df08
use oapi-codegen
anbraten Jul 13, 2024
857c7be
add test
anbraten Jul 13, 2024
51bf9ec
Merge remote-tracking branch 'upstream/main' into swagger-client
anbraten Nov 5, 2024
7449415
update go mod
anbraten Nov 5, 2024
99b3df1
Merge remote-tracking branch 'upstream/main' into swagger-client
woodpecker-bot Nov 6, 2024
47ee87e
fix openapi docs
woodpecker-bot Nov 6, 2024
ebaa318
generate client
woodpecker-bot Nov 6, 2024
af4f538
Cleanup openapi generation
woodpecker-bot Nov 6, 2024
e8a9575
undo
woodpecker-bot Nov 6, 2024
a6d60d2
cleanup
woodpecker-bot Nov 6, 2024
4f0479e
undo
woodpecker-bot Nov 6, 2024
e027fe0
fmt
woodpecker-bot Nov 6, 2024
cedb093
check format
woodpecker-bot Nov 6, 2024
205e984
fix
woodpecker-bot Nov 6, 2024
21fcde7
cleanup
woodpecker-bot Nov 6, 2024
5d83fd2
Merge branch 'main' into cleanup-openapi
anbraten Nov 6, 2024
f457a07
rename
anbraten Nov 8, 2024
c59e4bf
fix
anbraten Nov 8, 2024
9f4cab8
fix
anbraten Nov 8, 2024
f7d47ea
Merge branch 'main' into cleanup-openapi
anbraten Nov 8, 2024
ae25cc4
swagger => openapi and move tools
anbraten Nov 18, 2024
4b41bef
Merge branch 'cleanup-openapi' of github.com:anbraten/woodpecker into…
anbraten Nov 18, 2024
fe21516
[pre-commit.ci] auto fixes from pre-commit.com hooks [CI SKIP]
pre-commit-ci[bot] Nov 18, 2024
95fa774
rename to main
anbraten Nov 18, 2024
465927d
Merge branch 'cleanup-openapi' of github.com:anbraten/woodpecker into…
anbraten Nov 18, 2024
d86e4d9
Merge branch 'main' into cleanup-openapi
anbraten Nov 18, 2024
c850ded
fix
6543 Nov 18, 2024
a045d4b
Merge branch 'main' into cleanup-openapi
6543 Nov 18, 2024
d1d6863
vendor more tooling
6543 Nov 18, 2024
0563bde
fix lint err
6543 Nov 18, 2024
e667f82
Merge branch 'main' into cleanup-openapi
anbraten Nov 22, 2024
eb0bee8
undo golang tool
anbraten Nov 22, 2024
bcd0236
Merge branch 'main' into cleanup-openapi
anbraten Nov 22, 2024
1e19b9b
Merge branch 'main' into cleanup-openapi
anbraten Nov 22, 2024
b0dce73
Merge branch 'main' into cleanup-openapi
6543 Nov 22, 2024
feaa591
Merge branch 'main' into cleanup-openapi
anbraten Nov 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
"nosniff",
"ntfy",
"octocat",
"openapi",
"opensource",
"Pacman",
"picus",
Expand Down Expand Up @@ -250,7 +251,7 @@
"flake.lock",
"pnpm-lock.yaml",
"**/node_modules/**/*",
"cmd/server/docs/docs.go",
"cmd/server/openapi/docs.go",
"renovate.json",
// TODO: remove the following
"docs/**/*.js",
Expand Down
5 changes: 3 additions & 2 deletions .ecrc
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"Exclude": [
".git",
"go.mod", "go.sum",
"go.mod",
"go.sum",
"vendor",
"fixtures",
"LICENSE",
"node_modules",
"server/store/datastore/migration/test-files/sqlite.db",
"server/store/datastore/feed.go",
"cmd/server/docs/docs.go",
"cmd/server/openapi/docs.go",
"_test.go",
"Makefile"
]
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ docs/venv

### Generated by CI ###
docs/docs/40-cli.md
docs/swagger.json
docs/openapi.json
6 changes: 3 additions & 3 deletions .woodpecker/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ steps:
- make lint
when: *when

check-swagger:
check-openapi:
depends_on:
- vendor
image: *golang_image
commands:
- 'make generate-swagger'
- 'make generate-openapi'
- 'DIFF=$(git diff | head)'
- '[ -n "$DIFF" ] && { echo "swagger not up to date, exec `make generate-swagger` and commit"; exit 1; } || true'
- '[ -n "$DIFF" ] && { echo "openapi not up to date, exec `make generate-openapi` and commit"; exit 1; } || true'
when: *when

lint-license-header:
Expand Down
17 changes: 7 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ clean: ## Clean build artifacts
clean-all: clean ## Clean all artifacts
rm -rf ${DIST_DIR} web/dist docs/build docs/node_modules web/node_modules
# delete generated
rm -rf docs/docs/40-cli.md docs/swagger.json
rm -rf docs/docs/40-cli.md docs/openapi.json

.PHONY: generate
generate: install-tools generate-swagger ## Run all code generations
generate: install-tools generate-openapi ## Run all code generations
CGO_ENABLED=0 go generate ./...

generate-swagger: install-tools ## Run swagger code generation
swag init -g server/api/ -g cmd/server/swagger.go --outputTypes go -output cmd/server/docs
CGO_ENABLED=0 go generate cmd/server/swagger.go
generate-openapi: install-tools ## Run openapi code generation and format it
go run github.com/swaggo/swag/cmd/swag fmt
CGO_ENABLED=0 go generate cmd/server/openapi.go

generate-license-header: install-tools
addlicense -c "Woodpecker Authors" -ignore "vendor/**" **/*.go
Expand All @@ -134,9 +134,6 @@ install-tools: ## Install development tools
hash gofumpt > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go install mvdan.cc/gofumpt@latest; \
fi ; \
hash swag > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go install github.com/swaggo/swag/cmd/swag@latest; \
fi ; \
hash addlicense > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go install github.com/google/addlicense@latest; \
fi ; \
Expand Down Expand Up @@ -196,7 +193,7 @@ test: test-agent test-server test-server-datastore test-cli test-lib ## Run all
build-ui: ## Build UI
(cd web/; pnpm install --frozen-lockfile; pnpm build)

build-server: build-ui generate-swagger ## Build server
build-server: build-ui generate-openapi ## Build server
CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -tags '$(TAGS)' -ldflags '${LDFLAGS}' -o ${DIST_DIR}/woodpecker-server${BIN_SUFFIX} go.woodpecker-ci.org/woodpecker/v2/cmd/server

build-agent: ## Build agent
Expand Down Expand Up @@ -343,6 +340,6 @@ spellcheck:
.PHONY: docs
docs: ## Generate docs (currently only for the cli)
CGO_ENABLED=0 go generate cmd/cli/app.go
CGO_ENABLED=0 go generate cmd/server/swagger.go
CGO_ENABLED=0 go generate cmd/server/openapi.go

endif
7 changes: 5 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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"
)
Expand All @@ -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")
Expand Down
38 changes: 38 additions & 0 deletions cmd/server/openapi.go
Original file line number Diff line number Diff line change
@@ -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()
}
39 changes: 16 additions & 23 deletions cmd/server/docs/docs.go → cmd/server/openapi/docs.go
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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}}"
Expand Down Expand Up @@ -122,7 +122,7 @@ const docTemplate = `{
{
"type": "integer",
"description": "the agent's id",
"name": "agent",
"name": "agent_id",
"in": "path",
"required": true
}
Expand Down Expand Up @@ -156,7 +156,7 @@ const docTemplate = `{
{
"type": "integer",
"description": "the agent's id",
"name": "agent",
"name": "agent_id",
"in": "path",
"required": true
}
Expand Down Expand Up @@ -187,7 +187,7 @@ const docTemplate = `{
{
"type": "integer",
"description": "the agent's id",
"name": "agent",
"name": "agent_id",
"in": "path",
"required": true
},
Expand Down Expand Up @@ -232,7 +232,7 @@ const docTemplate = `{
{
"type": "integer",
"description": "the agent's id",
"name": "agent",
"name": "agent_id",
"in": "path",
"required": true
}
Expand Down Expand Up @@ -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"
}
]
}
}
}
}
Expand Down Expand Up @@ -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: "{{",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions cmd/server/openapi_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
39 changes: 0 additions & 39 deletions cmd/server/swagger.go

This file was deleted.

14 changes: 0 additions & 14 deletions cmd/server/swagger_test.go

This file was deleted.

Loading