2
1

openapi-clients.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 openapi-generator generate \
  54. -i support/doc/api/openapi.yaml \
  55. -c "${lang_dir}/def.yaml" \
  56. -t "${lang_dir}" \
  57. -g "$lang" \
  58. --git-host "${API_REPO_HOST}" \
  59. --git-user-id "${API_URL_USERNAME}" \
  60. --git-repo-id "${git_repo_id}" \
  61. -o "${out_dir}"
  62. # Commit and push changes to the remote
  63. cd "$out_dir"
  64. git remote set-url origin "$git_remote"
  65. # Make sure something has changed
  66. if [[ $(git status -s | wc -l) = 0 ]] ; then
  67. echo "No changes from previous version"
  68. continue
  69. fi
  70. git add .
  71. git commit -m "${API_COMMIT_MSG:-"Minor update $lang"}"
  72. git push
  73. )
  74. done