Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9cedc45

Browse files
authored
Merge pull request #23 from php-openapi/add-linter
Add Spectral Linter
2 parents a710f74 + aac5ad4 commit 9cedc45

File tree

8 files changed

+893
-153
lines changed

8 files changed

+893
-153
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor
2+
/node_modules
23

34
/docker-compose.override.yml
45

‎.gitlab-ci.dist.yml‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# .gitlab-ci.yml example
2+
# rename this file and adjust to your needs to enable Gitlab CI
3+
stages:
4+
- lint
5+
- test
6+
7+
# cache composer vendor
8+
cache:
9+
paths:
10+
- vendor/
11+
12+
# run linter against the openapi specification
13+
lint-api-spec:
14+
stage: lint
15+
interruptible: true
16+
before_script:
17+
- make start-docker
18+
- make node_modules/.bin/spectral
19+
script:
20+
- make lint
21+
after_script:
22+
- make stop-docker
23+
24+
# run generator and make sure no changes are uncommitted
25+
check-generator-changes:
26+
stage: lint
27+
interruptible: true
28+
before_script:
29+
- make start-docker
30+
- echo "Waiting for mariadb to start up..."
31+
- docker-compose exec -T backend-php timeout 60s sh -c "while ! (mysql -uapi_test -papisecret -h db-test --execute 'SELECT 1;' > /dev/null 2>&1); do echo -n '.'; sleep 0.1 ; done; echo 'ok'" || (docker-compose ps; docker-compose logs; exit 1)
32+
- make run COMMAND="./yii migrate/up --interactive=0"
33+
script:
34+
- make run COMMAND="./yii gii/api --interactive=0 --overwrite"
35+
- git diff --exit-code
36+
- git status -s && test -z "$(git status -s)"
37+
after_script:
38+
- make stop-docker
39+
40+
# run tests
41+
tests:
42+
stage: test
43+
before_script:
44+
- make start-docker
45+
- echo "Waiting for mariadb to start up..."
46+
- docker-compose exec -T backend-php timeout 60s sh -c "while ! (mysql -uapi_test -papisecret -h db-test --execute 'SELECT 1;' > /dev/null 2>&1); do echo -n '.'; sleep 0.1 ; done; echo 'ok'" || (docker-compose ps; docker-compose logs; exit 1)
47+
script:
48+
- make test
49+
after_script:
50+
- make stop-docker
51+
- sudo rm -rf docker/_data/*
52+
artifacts:
53+
paths:
54+
- tests/_output
55+
exclude:
56+
- tests/_output/.gitignore
57+
when: always
58+
expire_in: 2 week
59+
reports:
60+
# make the report available in Gitlab UI. see https://docs.gitlab.com/ee/ci/unit_test_reports.html
61+
junit:
62+
- tests/_output/*.xml
63+
64+
variables:
65+
GIT_STRATEGY: fetch
66+
GIT_SUBMODULE_STRATEGY: recursive
67+
# solve docker timeout issues
68+
# https://github.com/docker/compose/issues/4486
69+
DOCKER_CLIENT_TIMEOUT: 300
70+
COMPOSE_HTTP_TIMEOUT: 300
71+

‎.spectral.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This file configures the spectral OpenAPI linter
2+
#
3+
# The default config extends the pre-defined ruleset for OpenAPI 3.
4+
# Adjust this file to define your own custom rules.
5+
#
6+
# See https://meta.stoplight.io/docs/spectral/01baf06bdd05a-create-a-ruleset
7+
8+
extends: ["spectral:oas"]

‎Makefile‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# set user to "root" to run commands as root in docker
22
USER=$$(whoami)
33
# The docker command to execute commands directly in docker
4-
DOCKER=docker-compose exec -T --user="$(USER)" backend-php
4+
DOCKER=docker-compose exec -T -w /app --user="$(USER)" backend-php
55
# The PHP binary to use, you may add arguments to PHP here
66
PHP=php
77
# directories writeable by webserver
@@ -20,9 +20,10 @@ default:
2020
@echo " make stop-docker stop docker environment"
2121
@echo " make cli run bash in docker environment"
2222
@echo " make bash alias for 'make cli'"
23+
@echo " make lint Run OpenAPI linter"
2324

2425

25-
.PHONY: start stop start-docker stop-docker clean test bash cli
26+
.PHONY: start stop start-docker stop-docker clean test bash cli lint lint-php lint-js
2627

2728

2829
## PHP runtime ##
@@ -81,6 +82,19 @@ backend/config/cookie-validation.key:
8182
test -s $@ || php -r 'echo bin2hex(random_bytes(20));' > $@
8283

8384

85+
# Lint API spec
86+
87+
lint: lint-php lint-js
88+
89+
lint-php:
90+
$(DOCKER) ./vendor/bin/php-openapi validate openapi/schema.yaml
91+
92+
lint-js: node_modules/.bin/spectral
93+
docker-compose run -T --no-deps --rm --user="$$(id -u)" -e FORCE_COLOR=1 -w /app nodejs ./node_modules/.bin/spectral lint openapi/schema.yaml -f stylish --ruleset .spectral.yml
94+
95+
node_modules/.bin/spectral: package.json
96+
docker-compose run -T --no-deps --rm --user="$$(id -u)" -e FORCE_COLOR=1 -w /app nodejs npm install
97+
8498
## Docker Runtime Tests ##
8599

86100
test: tests/_data/dump.sql

‎README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ Your API is now available at `http://localhost:8337/`. Try to access an endpoint
150150
...
151151
]
152152

153+
154+
## Linting the OpenAPI specification
155+
156+
You can run a linter to check the validity of your OpenAPI specification file:
157+
158+
make lint-js # run spectral lint
159+
make lint-php # run php-openapi validate
160+
make lint # run both of the above commands
161+
162+
You can find more information on spectral at <https://www.npmjs.com/package/@stoplight/spectral-cli#-documentation-and-community>.
163+
164+
153165
## Application structure <span id="structure"></span>
154166

155167
This application template consists of 3 application tiers:

‎docker-compose.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@ services:
5252
mailcatcher:
5353
image: nisenabe/mailcatcher
5454

55+
# javascript container for running JS OpenAPI linter
56+
nodejs:
57+
image: node
58+
command: 'tail -f /dev/null'
59+
volumes:
60+
- ./:/app

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /