Skip to content

Commit

Permalink
feat(action-precommit): add render-openapi-template action
Browse files Browse the repository at this point in the history
  • Loading branch information
JoffreyPlouvier committed Apr 11, 2023
1 parent 5c3d121 commit 4072c96
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
77 changes: 77 additions & 0 deletions actions/precommit/render-openapi-template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# PreCommit: render-openapi-template

## Behavior

Use pre-commit manual rules to render openapi file and validate it.
Outputs the generated artifact

## Usage

```yaml
jobs:
openapi-render:
runs-on: ubuntu-latest
steps:
- name: "Checkout Code"
uses: "actions/checkout@v3"

- name: "Render OpenAPI ApiGateway file"
uses: "meero-com/github-actions-shared-workflows/actions/precommit/render-openapi-template@main"
with:
OPENAPI_API_NAME: myapi
version_identifier: uniq_identifer_such_commit_sha
```
will output
```yaml
apigw_version: uniq_identifer_such_commit_sha
artifact_name: apigateway-file
artifact_file_name: openapi-myapi-uniq_identifer_such_commit_sha.json
```
Beware of using a `@ref` (`@main` in the example above) which suits your stability requirements in your workflow:

* Use `@main` if you always want to use the latest version of the workflow.
* Use `@<tag>` if you wan to use a specific frozen version of the workflow.

## Requirements

### PreCommit
This action use pre-commit manual hooks.
PreCommit must be configured with following hooks

```yaml
---
repos:
- repo: https://github.com/meero-com/pre-commit-hooks
rev: v1.3.0
hooks:
- id: render-jinja-template
args:
- --env-var-prefix=TMPL_APIGW_
- --templates-dir=apigw/templates
- --template-name=openapi.json.j2
- --output-filename-prefix=apigw/openapi
- --output-filename=myapi
- --output-filename-extension=json
- -e=body_schema
- -e=path_parameters
- -e=parameters
- -e=header_parameters
- -e=cors_methods
- -e=model
- -e=query_parameters
- -e=param_location
- repo: https://github.com/APIDevTools/swagger-cli
rev: v4.0.4
hooks:
- id: swagger-validation
stages: [manual]
files: apigw\/(openapi|swagger).*\.(json|ya?ml)$
```

### env vars

If your template use _prefixecd_ env vars, don't forget to set them up before action execution.
66 changes: 66 additions & 0 deletions actions/precommit/render-openapi-template/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: "Render OpenAPI ApiGateway file"
description: "Render OpenAPI jinja template and validate it with manual pre-commit rules"

inputs:
OPENAPI_API_NAME:
description: "Name of the api (-o of precommit render-jinja-template)"
required: true
version_identifier:
description: "uniq version identifier (commit short sha is perfect)"
required: true
hook_render:
description: Pre-commit hook name for jinja template rendering
default: render-jinja-template
required: false
hook_validate:
description: Pre-commit hook name for swagger validation
default: swagger-validation
required: false

outputs:
apigw_version:
description: "Api Gateway version"
value: ${{ inputs.version_identifier }}
artifact_name:
description: "Api Gateway artifact name"
value: apigateway-file
artifact_file_name:
description: "Api Gateway file artifact name"
value: openapi-${{ inputs.OPENAPI_API_NAME }}-${{ inputs.version_identifier }}.json

env:
PYTHON_VERSION: '3.10'

runs:
using: "composite"
steps:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Generate openapi file
uses: pre-commit/action@v3.0.0
with:
extra_args: ${{ inputs.hook_render }} --hook-stage manual

# temp stage the openapi file (file not seen by pre-commit hook)
- name: stage openapi file
run: git add -f apigw/openapi*
shell: bash

- name: Validate openapi file
uses: pre-commit/action@v3.0.0
with:
extra_args: ${{ inputs.hook_validate }} --hook-stage manual

# cleanup git stage
- name: unstage openapi file
run: git reset HEAD -- apigw/openapi*
shell: bash

- name: Save ApiGateway file as Artifact
uses: actions/upload-artifact@v3
with:
name: apigateway-file
path: apigw/openapi-${{ inputs.OPENAPI_API_NAME }}-${{ inputs.version_identifier }}.json

0 comments on commit 4072c96

Please sign in to comment.