ci-openapi.yml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. name: OpenAPI
  2. on:
  3. push:
  4. branches:
  5. - master
  6. tags:
  7. - 'v*'
  8. pull_request_target:
  9. permissions: {}
  10. jobs:
  11. openapi-head:
  12. name: OpenAPI - HEAD
  13. runs-on: ubuntu-latest
  14. permissions: read-all
  15. steps:
  16. - name: Checkout repository
  17. uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
  18. with:
  19. ref: ${{ github.event.pull_request.head.sha }}
  20. repository: ${{ github.event.pull_request.head.repo.full_name }}
  21. - name: Setup .NET
  22. uses: actions/setup-dotnet@4d6c8fcf3c8f7a60068d26b594648e99df24cee3 # v4.0.0
  23. with:
  24. dotnet-version: '8.0.x'
  25. - name: Generate openapi.json
  26. run: dotnet test tests/Jellyfin.Server.Integration.Tests/Jellyfin.Server.Integration.Tests.csproj -c Release --filter "Jellyfin.Server.Integration.Tests.OpenApiSpecTests"
  27. - name: Upload openapi.json
  28. uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3
  29. with:
  30. name: openapi-head
  31. retention-days: 14
  32. if-no-files-found: error
  33. path: tests/Jellyfin.Server.Integration.Tests/bin/Release/net8.0/openapi.json
  34. openapi-base:
  35. name: OpenAPI - BASE
  36. if: ${{ github.base_ref != '' }}
  37. runs-on: ubuntu-latest
  38. permissions: read-all
  39. steps:
  40. - name: Checkout repository
  41. uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
  42. with:
  43. ref: ${{ github.event.pull_request.head.sha }}
  44. repository: ${{ github.event.pull_request.head.repo.full_name }}
  45. fetch-depth: 0
  46. - name: Checkout common ancestor
  47. env:
  48. HEAD_REF: ${{ github.head_ref }}
  49. run: |
  50. git remote add upstream https://github.com/${{ github.event.pull_request.base.repo.full_name }}
  51. git -c protocol.version=2 fetch --prune --progress --no-recurse-submodules upstream +refs/heads/*:refs/remotes/upstream/* +refs/tags/*:refs/tags/*
  52. ANCESTOR_REF=$(git merge-base upstream/${{ github.base_ref }} origin/$HEAD_REF)
  53. git checkout --progress --force $ANCESTOR_REF
  54. - name: Setup .NET
  55. uses: actions/setup-dotnet@4d6c8fcf3c8f7a60068d26b594648e99df24cee3 # v4.0.0
  56. with:
  57. dotnet-version: '8.0.x'
  58. - name: Generate openapi.json
  59. run: dotnet test tests/Jellyfin.Server.Integration.Tests/Jellyfin.Server.Integration.Tests.csproj -c Release --filter "Jellyfin.Server.Integration.Tests.OpenApiSpecTests"
  60. - name: Upload openapi.json
  61. uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3
  62. with:
  63. name: openapi-base
  64. retention-days: 14
  65. if-no-files-found: error
  66. path: tests/Jellyfin.Server.Integration.Tests/bin/Release/net8.0/openapi.json
  67. openapi-diff:
  68. permissions:
  69. pull-requests: write # to create or update comment (peter-evans/create-or-update-comment)
  70. name: OpenAPI - Difference
  71. if: ${{ github.event_name == 'pull_request_target' }}
  72. runs-on: ubuntu-latest
  73. needs:
  74. - openapi-head
  75. - openapi-base
  76. steps:
  77. - name: Download openapi-head
  78. uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4.1.7
  79. with:
  80. name: openapi-head
  81. path: openapi-head
  82. - name: Download openapi-base
  83. uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4.1.7
  84. with:
  85. name: openapi-base
  86. path: openapi-base
  87. - name: Workaround openapi-diff issue
  88. run: |
  89. sed -i 's/"allOf"/"oneOf"/g' openapi-head/openapi.json
  90. sed -i 's/"allOf"/"oneOf"/g' openapi-base/openapi.json
  91. - name: Calculate OpenAPI difference
  92. uses: docker://openapitools/openapi-diff
  93. continue-on-error: true
  94. with:
  95. args: --fail-on-changed --markdown openapi-changes.md openapi-base/openapi.json openapi-head/openapi.json
  96. - id: read-diff
  97. name: Read openapi-diff output
  98. run: |
  99. body=$(cat openapi-changes.md)
  100. body="${body//'%'/'%25'}"
  101. body="${body//$'\n'/'%0A'}"
  102. body="${body//$'\r'/'%0D'}"
  103. echo ::set-output name=body::$body
  104. - name: Find difference comment
  105. uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0
  106. id: find-comment
  107. with:
  108. issue-number: ${{ github.event.pull_request.number }}
  109. direction: last
  110. body-includes: openapi-diff-workflow-comment
  111. - name: Reply or edit difference comment (changed)
  112. uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
  113. if: ${{ steps.read-diff.outputs.body != '' }}
  114. with:
  115. issue-number: ${{ github.event.pull_request.number }}
  116. comment-id: ${{ steps.find-comment.outputs.comment-id }}
  117. edit-mode: replace
  118. body: |
  119. <!--openapi-diff-workflow-comment-->
  120. <details>
  121. <summary>Changes in OpenAPI specification found. Expand to see details.</summary>
  122. ${{ steps.read-diff.outputs.body }}
  123. </details>
  124. - name: Edit difference comment (unchanged)
  125. uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
  126. if: ${{ steps.read-diff.outputs.body == '' && steps.find-comment.outputs.comment-id != '' }}
  127. with:
  128. issue-number: ${{ github.event.pull_request.number }}
  129. comment-id: ${{ steps.find-comment.outputs.comment-id }}
  130. edit-mode: replace
  131. body: |
  132. <!--openapi-diff-workflow-comment-->
  133. No changes to OpenAPI specification found. See history of this comment for previous changes.
  134. publish_unstable:
  135. name: OpenAPI - Publish Unstable Spec
  136. if: |
  137. github.event_name != 'pull_request_target' &&
  138. ${{ ! startsWith(github.ref, 'refs/tags/v') }} &&
  139. contains(github.repository_owner, 'jellyfin')
  140. runs-on: ubuntu-latest
  141. needs:
  142. - openapi-head
  143. steps:
  144. - name: Set unstable dated version
  145. id: version
  146. run: |-
  147. echo "JELLYFIN_VERSION=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV
  148. - name: Download openapi-head
  149. uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4.1.7
  150. with:
  151. name: openapi-head
  152. path: openapi-head
  153. - name: Upload openapi.json (unstable) to repository server
  154. uses: appleboy/scp-action@917f8b81dfc1ccd331fef9e2d61bdc6c8be94634 # v0.1.7
  155. with:
  156. host: "${{ secrets.REPO_HOST }}"
  157. username: "${{ secrets.REPO_USER }}"
  158. key: "${{ secrets.REPO_KEY }}"
  159. source: openapi-head/openapi.json
  160. strip_components: 1
  161. target: "/srv/incoming/openapi/unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}"
  162. - name: Move openapi.json (unstable) into place
  163. uses: appleboy/ssh-action@029f5b4aeeeb58fdfe1410a5d17f967dacf36262 # v1.0.3
  164. with:
  165. host: "${{ secrets.REPO_HOST }}"
  166. username: "${{ secrets.REPO_USER }}"
  167. key: "${{ secrets.REPO_KEY }}"
  168. debug: false
  169. script_stop: false
  170. script: |
  171. if ! test -d /run/workflows; then
  172. sudo mkdir -p /run/workflows
  173. sudo chown ${{ secrets.REPO_USER }} /run/workflows
  174. fi
  175. (
  176. flock -x -w 300 200 || exit 1
  177. TGT_DIR="/srv/repository/main/openapi"
  178. LAST_SPEC="$( ls -lt ${TGT_DIR}/unstable/ | grep 'jellyfin-openapi' | head -1 | awk '{ print $NF }' )"
  179. # If new and previous spec don't differ (diff retcode 0), remove incoming and finish
  180. if diff /srv/incoming/openapi/unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}/openapi.json ${TGT_DIR}/unstable/${LAST_SPEC} &>/dev/null; then
  181. rm -r /srv/incoming/openapi/unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}
  182. exit 0
  183. fi
  184. # Move new spec into place
  185. sudo mv /srv/incoming/openapi/unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}/openapi.json ${TGT_DIR}/unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}.json
  186. # Delete previous jellyfin-openapi-unstable_previous.json
  187. sudo rm ${TGT_DIR}/jellyfin-openapi-unstable_previous.json
  188. # Move current jellyfin-openapi-unstable.json symlink to jellyfin-openapi-unstable_previous.json
  189. sudo mv ${TGT_DIR}/jellyfin-openapi-unstable.json ${TGT_DIR}/jellyfin-openapi-unstable_previous.json
  190. # Create new jellyfin-openapi-unstable.json symlink
  191. sudo ln -s unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}.json ${TGT_DIR}/jellyfin-openapi-unstable.json
  192. # Check that the previous openapi unstable spec link is correct
  193. if [[ "$( readlink ${TGT_DIR}/jellyfin-openapi-unstable_previous.json )" != "unstable/${LAST_SPEC}" ]]; then
  194. sudo rm ${TGT_DIR}/jellyfin-openapi-unstable_previous.json
  195. sudo ln -s unstable/${LAST_SPEC} ${TGT_DIR}/jellyfin-openapi-unstable_previous.json
  196. fi
  197. ) 200>/run/workflows/openapi-unstable.lock
  198. publish_stable:
  199. name: OpenAPI - Publish Stable Spec
  200. if: |
  201. startsWith(github.ref, 'refs/tags/v') &&
  202. contains(github.repository_owner, 'jellyfin')
  203. runs-on: ubuntu-latest
  204. needs:
  205. - openapi-head
  206. steps:
  207. - name: Set version number
  208. id: version
  209. run: |-
  210. echo "JELLYFIN_VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
  211. - name: Download openapi-head
  212. uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4.1.7
  213. with:
  214. name: openapi-head
  215. path: openapi-head
  216. - name: Upload openapi.json (stable) to repository server
  217. uses: appleboy/scp-action@917f8b81dfc1ccd331fef9e2d61bdc6c8be94634 # v0.1.7
  218. with:
  219. host: "${{ secrets.REPO_HOST }}"
  220. username: "${{ secrets.REPO_USER }}"
  221. key: "${{ secrets.REPO_KEY }}"
  222. source: openapi-head/openapi.json
  223. strip_components: 1
  224. target: "/srv/incoming/openapi/stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}"
  225. - name: Move openapi.json (stable) into place
  226. uses: appleboy/ssh-action@029f5b4aeeeb58fdfe1410a5d17f967dacf36262 # v1.0.3
  227. with:
  228. host: "${{ secrets.REPO_HOST }}"
  229. username: "${{ secrets.REPO_USER }}"
  230. key: "${{ secrets.REPO_KEY }}"
  231. debug: false
  232. script_stop: false
  233. script: |
  234. if ! test -d /run/workflows; then
  235. sudo mkdir -p /run/workflows
  236. sudo chown ${{ secrets.REPO_USER }} /run/workflows
  237. fi
  238. (
  239. flock -x -w 300 200 || exit 1
  240. TGT_DIR="/srv/repository/main/openapi"
  241. LAST_SPEC="$( ls -lt ${TGT_DIR}/stable/ | grep 'jellyfin-openapi' | head -1 | awk '{ print $NF }' )"
  242. # If new and previous spec don't differ (diff retcode 0), remove incoming and finish
  243. if diff /srv/incoming/openapi/stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}/openapi.json ${TGT_DIR}/stable/${LAST_SPEC} &>/dev/null; then
  244. rm -r /srv/incoming/openapi/stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}
  245. exit 0
  246. fi
  247. # Move new spec into place
  248. sudo mv /srv/incoming/openapi/stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}/openapi.json ${TGT_DIR}/stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}.json
  249. # Delete previous jellyfin-openapi-stable_previous.json
  250. sudo rm ${TGT_DIR}/jellyfin-openapi-stable_previous.json
  251. # Move current jellyfin-openapi-stable.json symlink to jellyfin-openapi-stable_previous.json
  252. sudo mv ${TGT_DIR}/jellyfin-openapi-stable.json ${TGT_DIR}/jellyfin-openapi-stable_previous.json
  253. # Create new jellyfin-openapi-stable.json symlink
  254. sudo ln -s stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}.json ${TGT_DIR}/jellyfin-openapi-stable.json
  255. # Check that the previous openapi stable spec link is correct
  256. if [[ "$( readlink ${TGT_DIR}/jellyfin-openapi-stable_previous.json )" != "stable/${LAST_SPEC}" ]]; then
  257. sudo rm ${TGT_DIR}/jellyfin-openapi-stable_previous.json
  258. sudo ln -s stable/${LAST_SPEC} ${TGT_DIR}/jellyfin-openapi-stable_previous.json
  259. fi
  260. ) 200>/run/workflows/openapi-stable.lock