2
1

openapi-clients.sh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/bash
  2. # Required environment vars
  3. # =========================
  4. # API_LANGS
  5. # A ':' delimited list of the client lib languages to be generated
  6. # API_GIT_USER
  7. # The user that will be used to push/pull from the APIs repos
  8. # API_GIT_EMAIL
  9. # The git email
  10. # GIT_TOKEN
  11. # A personal access token for github or gilab for pushing to repos
  12. # !!!This is a secret and shouldn't be logged publicly!!!
  13. # (Optional environment vars)
  14. # ===========================
  15. # API_COMMIT_MSG
  16. # A message to use when committing to the lib repo
  17. # API_PATH_PREFIX
  18. # Will be used for building the URL to the repo and path to checkout.
  19. # !!! End with a slash "/", otherwise the prefix will be tacked onto the language
  20. # API_URL_USERNAME
  21. # The username to use building the URL to the git repo.
  22. # Default: API_GIT_USER
  23. # API_REPO_HOST
  24. # Whoever's hosting the repo e.g gitlab.com, github.com, etc.
  25. # Default: framagit.org
  26. # Unofficial bash strict mode
  27. # https://web.archive.org/web/20190115051613/https://redsymbol.net/articles/unofficial-bash-strict-mode/
  28. set -euo pipefail
  29. IFS=$'\n\t '
  30. # Set default values
  31. API_URL_USERNAME="${API_URL_USERNAME:-$API_GIT_USER}"
  32. API_PATH_PREFIX="${API_PATH_PREFIX:-}"
  33. API_REPO_HOST=${API_REPO_HOST:-framagit.org}
  34. echo "API_GIT_USER='${API_GIT_USER}'"
  35. echo "API_URL_USERNAME='${API_URL_USERNAME}'"
  36. echo "API_LANGS='${API_LANGS}'"
  37. git config --global user.email "${API_GIT_EMAIL}"
  38. git config --global user.name "${API_GIT_USER}"
  39. for lang in ${API_LANGS//:/ } ; do
  40. (
  41. echo "Generating client API libs for $lang"
  42. lang_dir="support/openapi/${lang}"
  43. out_dir_prefix="dist/api/${API_PATH_PREFIX}"
  44. out_dir="${out_dir_prefix}/${lang}"
  45. git_repo_id="${API_PATH_PREFIX}${lang}"
  46. host_path="${API_REPO_HOST}/${API_URL_USERNAME}/${git_repo_id}.git"
  47. git_remote="https://${API_GIT_USER}:${GIT_TOKEN}@${host_path}"
  48. if ! [ -e "$out_dir" ] ; then
  49. # Make sure the prefix exists before cloning the repo
  50. mkdir -p "${out_dir_prefix}"
  51. git clone "https://${host_path}" "$out_dir"
  52. fi
  53. npx @openapitools/openapi-generator-cli generate \
  54. -i support/doc/api/openapi.yaml \
  55. -c "${lang_dir}/def.yaml" \
  56. -t "${lang_dir}" \
  57. -g "$lang" \
  58. --skip-validate-spec \
  59. --git-host "${API_REPO_HOST}" \
  60. --git-user-id "${API_URL_USERNAME}" \
  61. --git-repo-id "${git_repo_id}" \
  62. -o "${out_dir}"
  63. # Commit and push changes to the remote
  64. cd "$out_dir"
  65. git remote set-url origin "$git_remote"
  66. # Make sure something has changed
  67. if [[ $(git status -s | wc -l) = 0 ]] ; then
  68. echo "No changes from previous version"
  69. continue
  70. fi
  71. git add .
  72. git commit -m "${API_COMMIT_MSG:-"Minor update $lang"}"
  73. git push
  74. )
  75. done