diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c9297ca..ff3a246 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,13 +15,6 @@ jobs: - name: Checkout repository uses: actions/checkout@v6 - - name: Setup Java - if: matrix.os == 'windows-latest' - uses: actions/setup-java@v5 - with: - distribution: temurin - java-version: 21 - - name: Run action uses: ./ diff --git a/.github/workflows/version.yml b/.github/workflows/version.yml new file mode 100644 index 0000000..c18a651 --- /dev/null +++ b/.github/workflows/version.yml @@ -0,0 +1,47 @@ +name: version +on: + schedule: + - cron: '0 0 * * *' + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + +jobs: + version: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v6 + + - name: Update OpenAPI Generator CLI version + id: version + run: python3 scripts/version.py + + - name: Configure Git user + run: | + git config user.name 'github-actions[bot]' + git config user.email 'github-actions[bot]@users.noreply.github.com' + + - name: Check for changes + if: steps.version.outputs.current_version != steps.version.outputs.latest_version + run: | + BRANCH="build/version-$LATEST_VERSION" + git checkout -B $BRANCH + + git commit -am \ + "build(deps): bump openapi-generator-cli from $CURRENT_VERSION to $LATEST_VERSION" \ + -m "Release-As: $(cat version.txt | awk -F. '/[0-9]+\./{$NF++;print}' OFS=.)" \ + -m "https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/$LATEST_VERSION/" + + git push --force origin $BRANCH + + if ! gh pr view $BRANCH --json number >/dev/null 2>&1; then + gh pr create --assignee remarkablemark --fill --reviewer remarkablemark + fi + env: + GH_TOKEN: ${{ github.token }} + CURRENT_VERSION: ${{ steps.version.outputs.current_version }} + LATEST_VERSION: ${{ steps.version.outputs.latest_version }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/README.md b/README.md index c16f886..1878595 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,9 @@ [![test](https://github.com/remarkablemark/setup-openapi/actions/workflows/test.yml/badge.svg)](https://github.com/remarkablemark/setup-openapi/actions/workflows/test.yml) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) -🟢 Set up GitHub Actions workflow with [OpenAPI Generator](https://openapi-generator.tech/docs/installation/). +🟢 Set up GitHub Actions workflow with [OpenAPI Generator CLI](https://openapi-generator.tech/docs/installation/). + +This action installs Java, downloads the OpenAPI Generator CLI JAR, caches it by version, and exposes `openapi-generator-cli` on `PATH`. ## Quick Start @@ -30,7 +32,7 @@ Install OpenAPI Generator CLI tool: - uses: remarkablemark/setup-openapi@v1 ``` -Generate a Ruby client from a valid [petstore.yaml](https://petstore3.swagger.io/) doc: +Generate a Ruby client from an [OpenAPI spec](https://petstore3.swagger.io/): ```yaml - run: openapi-generator-cli generate -i petstore.yaml -g ruby -o /tmp/test/ @@ -38,32 +40,16 @@ Generate a Ruby client from a valid [petstore.yaml](https://petstore3.swagger.io See [action.yml](action.yml) -> [!NOTE] -> On Windows, you'll need to install a higher Java version: -> -> ```yaml -> on: push -> jobs: -> openapi: -> runs-on: windows-latest -> steps: -> - uses: actions/setup-java@v4 -> with: -> distribution: temurin -> java-version: 21 -> - uses: remarkablemark/setup-openapi@v1 -> ``` - ## Inputs ### `version` -**Optional**: The OpenAPI Generator version. Defaults to latest. +**Optional**: The OpenAPI Generator CLI version. Defaults to [7.21.0](https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/7.21.0/). ```yaml - uses: remarkablemark/setup-openapi@v1 with: - version: 7.9.0 + version: 7.21.0 ``` ## License diff --git a/action.yml b/action.yml index 38be09a..7ed205d 100644 --- a/action.yml +++ b/action.yml @@ -1,26 +1,73 @@ name: setup-openapi -description: Set up GitHub Actions workflow with OpenAPI Generator +description: Set up GitHub Actions workflow with OpenAPI Generator CLI author: remarkablemark inputs: version: - description: OpenAPI Generator version - required: false + description: OpenAPI Generator CLI version + default: 7.21.0 runs: using: composite steps: - - name: Install OpenAPI Generator CLI - shell: sh - run: npm install --global @openapitools/openapi-generator-cli + - name: Setup Java + uses: actions/setup-java@f2beeb24e141e01a676f977032f5a29d81c9e27e # v5.1.0 + with: + distribution: temurin + java-version: 21 + + - name: Set OpenAPI Generator CLI paths + id: path + shell: bash + run: | + INSTALL_DIR="${{ runner.tool_cache }}/openapi-generator-cli/$VERSION" + mkdir -p "$INSTALL_DIR" + + echo "INSTALL_DIR=$INSTALL_DIR" >> "$GITHUB_OUTPUT" + echo "JAR_PATH=$INSTALL_DIR/openapi-generator-cli-$VERSION.jar" >> "$GITHUB_OUTPUT" + env: + VERSION: ${{ inputs.version }} + + - name: Cache OpenAPI Generator CLI + uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4 + with: + path: ${{ steps.path.outputs.INSTALL_DIR }} + key: openapi-generator-cli-${{ runner.os }}-${{ inputs.version }} - - name: Set OpenAPI Generator Version - if: inputs.version - shell: sh - run: openapi-generator-cli version-manager set $VERSION + - name: Download OpenAPI Generator CLI + shell: bash + run: | + if [[ ! -f "$JAR_PATH" ]]; then + curl --silent --show-error --fail --location \ + "https://repo.maven.apache.org/maven2/org/openapitools/openapi-generator-cli/$VERSION/openapi-generator-cli-$VERSION.jar" \ + --output "$JAR_PATH" + fi env: + INSTALL_DIR: ${{ steps.path.outputs.INSTALL_DIR }} + JAR_PATH: ${{ steps.path.outputs.JAR_PATH }} VERSION: ${{ inputs.version }} + - name: Install OpenAPI Generator CLI + shell: bash + run: | + BIN_DIR="$RUNNER_TEMP/openapi-generator-cli-bin" + mkdir -p "$BIN_DIR" + + cat > "$BIN_DIR/openapi-generator-cli" < "$BIN_DIR/openapi-generator-cli.cmd" <> "$GITHUB_PATH" + env: + JAR_PATH: ${{ steps.path.outputs.JAR_PATH }} + branding: icon: check-circle color: green diff --git a/scripts/version.py b/scripts/version.py new file mode 100644 index 0000000..4d866ed --- /dev/null +++ b/scripts/version.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 + +import os +import re +import urllib.request +import xml.etree.ElementTree as ET +from pathlib import Path + + +MAVEN_METADATA_URL = "https://repo.maven.apache.org/maven2/org/openapitools/openapi-generator-cli/maven-metadata.xml" + +ROOT_DIR = Path(__file__).resolve().parent.parent +ACTION_FILE = ROOT_DIR / "action.yml" +README_FILE = ROOT_DIR / "README.md" + + +def get_latest_version() -> str: + with urllib.request.urlopen(MAVEN_METADATA_URL) as response: + metadata = response.read() + + root = ET.fromstring(metadata) + release = root.findtext("./versioning/release") + if not release: + raise RuntimeError("Unable to get latest OpenAPI Generator CLI version") + + return release + + +def get_current_version() -> str: + action_text = ACTION_FILE.read_text(encoding="utf-8") + match = re.search(r"default:\s+([0-9]+(?:\.[0-9]+)*)", action_text) + + if not match: + raise RuntimeError(f"Unable to get current version in {ACTION_FILE}") + + return match.group(1) + + +def main() -> None: + current_version = get_current_version() + latest_version = get_latest_version() + github_output = os.environ.get("GITHUB_OUTPUT") + + if github_output: + with open(github_output, "a", encoding="utf-8") as file: + file.write(f"current_version={current_version}\n") + file.write(f"latest_version={latest_version}\n") + + action_text = ACTION_FILE.read_text(encoding="utf-8") + action_text = action_text.replace( + f"default: {current_version}", + f"default: {latest_version}", + count=1, + ) + ACTION_FILE.write_text(action_text, encoding="utf-8") + + readme_text = README_FILE.read_text(encoding="utf-8") + readme_text = readme_text.replace(current_version, latest_version) + README_FILE.write_text(readme_text, encoding="utf-8") + + print( + f"Updated OpenAPI Generator CLI version from {current_version} to {latest_version}" + ) + + +if __name__ == "__main__": + main()