OpenAPI clients generation
authorRigel Kent <sendmemail@rigelk.eu>
Fri, 14 Feb 2020 13:14:14 +0000 (14:14 +0100)
committerRigel Kent <sendmemail@rigelk.eu>
Fri, 14 Feb 2020 13:14:14 +0000 (14:14 +0100)
See merge request framasoft/peertube/PeerTube!28, which continued https://github.com/Chocobozzz/PeerTube/pull/2060

.gitlab-ci.yml
package.json
scripts/openapi-clients.sh [new file with mode: 0755]
scripts/openapi-peertube-version.sh
support/doc/api/openapi.yaml
support/openapi/go.yaml [new file with mode: 0644]
support/openapi/kotlin.yaml [new file with mode: 0644]
support/openapi/python.yaml [new file with mode: 0644]
yarn.lock

index e9c1898f3245224a0efcaf8628268e4317928716..0b7332e0b0bf611e5fb65dafe6f73bee8e6a5ffb 100644 (file)
@@ -4,6 +4,7 @@ stages:
   - build-and-lint
   - test
   - docker-nightly
+  - clients
 
 #before_script:
 #  - 'sed -i -z "s/database:\n  hostname: ''localhost''/database:\n  hostname: ''postgres''/" config/test.yaml'
@@ -84,6 +85,21 @@ cache:
 #  script:
 #    - NODE_PENDING_JOB_WAIT=1000 npm run ci -- api-$CI_NODE_INDEX
 
+build-openapi-clients:
+  stage: clients
+  only:
+    refs:
+      - master
+      - schedules
+    changes:
+      - support/doc/api/openapi.yaml
+  script: 
+    - apt-get update -qq
+    - apt-get -yqqq install openjdk-8-jre
+    - yarn install --pure-lockfile
+    - scripts/openapi-peertube-version.sh
+    - scripts/openapi-clients.sh
+
 build-nightly:
   stage: docker-nightly
   only:
index da8ee789740b6b70d67aa6749d8d25bfcbc0d38e..3311f7addd18f91afb099a276654ebac6f4be877 100644 (file)
@@ -73,6 +73,7 @@
     "release": "scripty",
     "release-embed-api": "scripty",
     "nightly": "scripty",
+    "openapi-clients": "scripty",
     "client-report": "scripty",
     "swagger-cli": "swagger-cli",
     "sass-lint": "sass-lint"
     "youtube-dl": "^3.0.2"
   },
   "devDependencies": {
+    "@openapitools/openapi-generator-cli": "^1.0.10-4.2.3",
     "@types/apicache": "^1.2.0",
     "@types/async": "^3.0.0",
     "@types/async-lock": "^1.1.0",
diff --git a/scripts/openapi-clients.sh b/scripts/openapi-clients.sh
new file mode 100755 (executable)
index 0000000..b031cea
--- /dev/null
@@ -0,0 +1,81 @@
+#!/bin/bash
+# Required environment vars
+# =========================
+# API_LANGS
+#   A ':' delimited list of the client lib languages to be generated
+# API_GIT_USER
+#   The user that will be used to push/pull from the APIs repos
+# API_GIT_EMAIL
+#   The git email
+# GIT_TOKEN
+#   A personal access token for github or gilab for pushing to repos
+#   !!!This is a secret and shouldn't be logged publicly!!!
+
+# (Optional environment vars)
+# ===========================
+# API_COMMIT_MSG
+#   A message to use when committing to the lib repo
+# API_PATH_PREFIX
+#   Will be used for building the URL to the repo and path to checkout.
+#   !!! End with a slash "/", otherwise the prefix will be tacked onto the language
+# API_URL_USERNAME
+#   The username to use building the URL to the git repo.
+#   Default: API_GIT_USER
+# API_REPO_HOST
+#   Whoever's hosting the repo e.g gitlab.com, github.com, etc.
+#   Default: framagit.org
+
+# Unofficial bash strict mode
+# https://web.archive.org/web/20190115051613/https://redsymbol.net/articles/unofficial-bash-strict-mode/
+set -euo pipefail
+IFS=$'\n\t '
+
+# Set default values
+API_URL_USERNAME="${API_URL_USERNAME:-$API_GIT_USER}"
+API_PATH_PREFIX="${API_PATH_PREFIX:-}"
+API_REPO_HOST=${API_REPO_HOST:-framagit.org}
+
+echo "API_GIT_USER='${API_GIT_USER}'"
+echo "API_URL_USERNAME='${API_URL_USERNAME}'"
+echo "API_LANGS='${API_LANGS}'"
+
+git config --global user.email "${API_GIT_EMAIL}"
+git config --global user.name "${API_GIT_USER}"
+
+for lang in ${API_LANGS//:/ } ; do
+(
+    echo "Generating client API libs for $lang"
+
+    out_dir_prefix="dist/api/${API_PATH_PREFIX}"
+    out_dir="${out_dir_prefix}/${lang}"
+    git_repo_id="${API_PATH_PREFIX}${lang}"
+    host_path="${API_REPO_HOST}/${API_URL_USERNAME}/${git_repo_id}.git"
+    git_remote="https://${API_GIT_USER}:${GIT_TOKEN}@${host_path}"
+    if ! [ -e "$out_dir" ] ; then
+        # Make sure the prefix exists before cloning the repo
+        mkdir -p "${out_dir_prefix}"
+        git clone "https://${host_path}" "$out_dir"
+    fi
+
+    npx openapi-generator generate \
+        -i support/doc/api/openapi.yaml \
+        -c "support/openapi/${lang}.yaml" \
+        -g "$lang" \
+        --git-host "${API_REPO_HOST}" \
+        --git-user-id "${API_URL_USERNAME}" \
+        --git-repo-id "${git_repo_id}" \
+        -o "${out_dir}"
+
+    # Commit and push changes to the remote
+    cd "$out_dir"
+    git remote set-url origin "$git_remote"
+    # Make sure something has changed
+  if [[ $(git status -s | wc -l) = 0 ]] ; then
+        echo "No changes from previous version"
+        continue
+    fi
+    git add .
+    git commit -m "${API_COMMIT_MSG:-"Minor update $lang"}"
+    git push
+)
+done
index 4eb481e6435b277f47feb89722edd00d6aac9aaa..49154a5f3767148ae932b3037d7120707737a009 100755 (executable)
@@ -4,3 +4,8 @@
 PACKAGE_VERSION=$(node -p "require('./package.json').version")
 
 sed -i "s/\(^\s*\)version: .*/\1version: $PACKAGE_VERSION/" ./support/doc/api/openapi.yaml
+
+# Sets the package version for libs
+echo "packageVersion: $PACKAGE_VERSION" >> ./support/openapi/go.yaml
+echo "artifactVersion: $PACKAGE_VERSION" >> ./support/openapi/kotlin.yaml
+echo "packageVersion: $PACKAGE_VERSION" >> ./support/openapi/python.yaml
index b1e5f90f861e935870786b6bd71a74a57ad20151..180f65bcf25c90cf2ae58e5a39eeaa4f63b877f6 100644 (file)
@@ -13,6 +13,7 @@ info:
     altText: PeerTube Project Homepage
   description: |
     # Introduction
+
     The PeerTube API is built on HTTP(S). Our API is RESTful. It has predictable
     resource URLs. It returns HTTP response codes to indicate errors. It also
     accepts and returns JSON in the HTTP body. You can use your favorite
@@ -22,11 +23,13 @@ info:
     which generates a client SDK in the language of your choice.
 
     # Authentication
+
     When you sign up for an account, you are given the possibility to generate
     sessions, and authenticate using this session token. One session token can
     currently be used at a time.
 
     # Errors
+
     The API uses standard HTTP status codes to indicate the success or failure
     of the API call. The body of the response will be JSON in the following
     format.
diff --git a/support/openapi/go.yaml b/support/openapi/go.yaml
new file mode 100644 (file)
index 0000000..7136da9
--- /dev/null
@@ -0,0 +1,3 @@
+# https://openapi-generator.tech/docs/generators/go
+
+packageName: peertube
diff --git a/support/openapi/kotlin.yaml b/support/openapi/kotlin.yaml
new file mode 100644 (file)
index 0000000..7a01ae6
--- /dev/null
@@ -0,0 +1,6 @@
+# https://openapi-generator.tech/docs/generators/kotlin
+
+artifactId: peertube-api
+groupId: org.peertube
+packageName: org.peertube.client
+
diff --git a/support/openapi/python.yaml b/support/openapi/python.yaml
new file mode 100644 (file)
index 0000000..819a466
--- /dev/null
@@ -0,0 +1,5 @@
+# https://openapi-generator.tech/docs/generators/python
+
+packageName: peertube
+projectName: peertube
+
index 68f281e4524e9145768f6f9df0694604b8e14030..00959d0f5e05015f85ed1870e03f09151d2dbcb1 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
     esutils "^2.0.2"
     js-tokens "^4.0.0"
 
+"@openapitools/openapi-generator-cli@^1.0.10-4.2.3":
+  version "1.0.10-4.2.3"
+  resolved "https://registry.yarnpkg.com/@openapitools/openapi-generator-cli/-/openapi-generator-cli-1.0.10-4.2.3.tgz#4b934362efb9262fc236d4fb81b381e29bdb8245"
+  integrity sha512-Xo4rbz3aPiObpVS8oBUR0pHWYU5WBG/bmsJg16yCG7/vpGE8o1YsROTWMFfaMepWfxESrLP5XOqlBkZqbre7dw==
+
 "@types/apicache@^1.2.0":
   version "1.2.2"
   resolved "https://registry.yarnpkg.com/@types/apicache/-/apicache-1.2.2.tgz#b820659b1d95e66ec0e71dcd0317e9d30f0c154b"