diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b87b9e3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.8 + +RUN python -m pip install --upgrade pip +RUN pip install pipx + +# Sets the user to the same user that the workflow runner uses so that it can access the generated client +USER 1001 + +COPY entrypoint.sh /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file diff --git a/README.md b/README.md index a8540eb..a9364eb 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,51 @@ # openapi-python-client-action -GitHub Action to generate a python client package from an openapi spec +The official GitHub Action for [openapi-python-client](https://github.com/triaxtec/openapi-python-client) - generates a modern Python client package from an OpenAPI document + +## Inputs + +### `openapi-python-client-version` + +The version of the openapi-python-client package to use. If unspecified the latest released version will be used. + +### `openapi-file` + +The path (with respect to the current directory/the workspace) to the OpenAPI document (both JSON and YAML are supported). Defaults to just "openapi.json" i.e. a file in the current directory called openapi.json. + +### `openapi-url` + +The url of the OpenAPI document. Overrides `openapi-file` - If unspecified the value of the `openapi-file` input (which defaults to just `openapi.json`) will be used to generate the client. + +### `config-file` + +The path (with respect to the current directory/the workspace) to the config.yml to be used with openapi-python-client. Configuaration is not required so if this is unspecified then no configuration will be passed along. See [openapi-python-client's README](https://github.com/triaxtec/openapi-python-client#configuration) for available configuration + +## Outputs + +No outputs are returned. +The generated client is placed in the current directory. The name of the package (unless configured differently) will be `title-client` where "title" comes from the field with the same name within the `info` section of the openapi document. + +## Example usage +```yaml +jobs: + generate-python-client: + runs-on: ubuntu-latest + name: Example + steps: + + # Checkout your code + - name: Checkout + uses: actions/checkout@v2 + + # Generate your openapi document (if you don't write it manually) + + # Use the action to generate a client package + # This uses all defaults (latest version, openapi.json in the current workspace, no configuration) + - name: Generate Python Client + uses: triaxtec/openapi-python-client-action@v1 + + # Do something with the generated client (likely publishing it somewhere) + # Here we assume that the info/title in the openapi document was "example-project" + - name: Do something with the client + run: | + cd example-project-client +``` diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..9b9b625 --- /dev/null +++ b/action.yml @@ -0,0 +1,32 @@ +name: "Generate A Python Client Package From An OpenAPI file" +description: "Generates a Python client package from an OpenAPI document using the openapi-python-client project" +branding: + icon: "target" + color: "green" + +inputs: + openapi-python-client-version: + description: The version of openapi-python-client to use + required: false + default: NOT_SPECIFIED + openapi-file: + description: The path to the OpenAPI document to generate a client library for e.g. docs/openapi.json + required: false + default: openapi.json + openapi-url: + description: The url of the OpenAPI document to generate a client library for e.g. https://petstore3.swagger.io/api/v3/openapi.json + required: false + default: NOT_SPECIFIED + config-file: + description: Path to the config file to be passed along to openapi-python-client + required: false + default: NOT_SPECIFIED + +runs: + using: "docker" + image: "Dockerfile" + args: + - ${{ inputs.openapi-python-client-version }} + - ${{ inputs.openapi-file }} + - ${{ inputs.openapi-url }} + - ${{ inputs.config-file }} diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..0dbaa2a --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +openapi_python_client_version=$1 +openapi_file_path=$2 +openapi_url=$3 +config_file_path=$4 + +if [[ "$openapi_python_client_version" != "NOT_SPECIFIED" ]]; then + version_arg="--spec openapi-python-client==${openapi_python_client_version}" +else + version_arg="" +fi + +if [[ "$config_file_path" != "NOT_SPECIFIED" ]]; then + config_arg="--config ${config_file_path}" +else + config_arg="" +fi + +openapi_document_path_or_url_arg="--path ${openapi_file_path}" +if [[ "$openapi_url" != "NOT_SPECIFIED" ]]; then + openapi_document_path_or_url_arg="--url ${openapi_url}" +fi + +pipx run ${version_arg} openapi-python-client ${config_arg} generate ${openapi_document_path_or_url_arg} \ No newline at end of file