ci-openapi.yml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
  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@3e891b0cb619bf60e2c25674b222b8940e2c1c25 # v4.1.0
  23. with:
  24. dotnet-version: '9.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@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.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/net9.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@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
  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@3e891b0cb619bf60e2c25674b222b8940e2c1c25 # v4.1.0
  56. with:
  57. dotnet-version: '9.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@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.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/net9.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@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
  79. with:
  80. name: openapi-head
  81. path: openapi-head
  82. - name: Download openapi-base
  83. uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
  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. # Read and fix markdown
  100. body=$(cat openapi-changes.md)
  101. # Write to workflow summary
  102. echo "$body" >> $GITHUB_STEP_SUMMARY
  103. # Set ApiChanged var
  104. if [ "$body" != '' ]; then
  105. echo "ApiChanged=1" >> "$GITHUB_OUTPUT"
  106. else
  107. echo "ApiChanged=0" >> "$GITHUB_OUTPUT"
  108. fi
  109. # Add header/footer for diff comment
  110. echo '<!--openapi-diff-workflow-comment-->' > openapi-changes-reply.md
  111. echo "<details>" >> openapi-changes-reply.md
  112. echo "<summary>Changes in OpenAPI specification found. Expand to see details.</summary>" >> openapi-changes-reply.md
  113. echo "" >> openapi-changes-reply.md
  114. echo "$body" >> openapi-changes-reply.md
  115. echo "" >> openapi-changes-reply.md
  116. echo "</details>" >> openapi-changes-reply.md
  117. - name: Find difference comment
  118. uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0
  119. id: find-comment
  120. with:
  121. issue-number: ${{ github.event.pull_request.number }}
  122. direction: last
  123. body-includes: openapi-diff-workflow-comment
  124. - name: Reply or edit difference comment (changed)
  125. uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
  126. if: ${{ steps.read-diff.outputs.ApiChanged == '1' }}
  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-path: openapi-changes-reply.md
  132. - name: Edit difference comment (unchanged)
  133. uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
  134. if: ${{ steps.read-diff.outputs.ApiChanged == '0' && steps.find-comment.outputs.comment-id != '' }}
  135. with:
  136. issue-number: ${{ github.event.pull_request.number }}
  137. comment-id: ${{ steps.find-comment.outputs.comment-id }}
  138. edit-mode: replace
  139. body: |
  140. <!--openapi-diff-workflow-comment-->
  141. No changes to OpenAPI specification found. See history of this comment for previous changes.
  142. publish-unstable:
  143. name: OpenAPI - Publish Unstable Spec
  144. if: ${{ github.event_name != 'pull_request_target' && !startsWith(github.ref, 'refs/tags/v') && contains(github.repository_owner, 'jellyfin') }}
  145. runs-on: ubuntu-latest
  146. needs:
  147. - openapi-head
  148. steps:
  149. - name: Set unstable dated version
  150. id: version
  151. run: |-
  152. echo "JELLYFIN_VERSION=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV
  153. - name: Download openapi-head
  154. uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
  155. with:
  156. name: openapi-head
  157. path: openapi-head
  158. - name: Upload openapi.json (unstable) to repository server
  159. uses: appleboy/scp-action@917f8b81dfc1ccd331fef9e2d61bdc6c8be94634 # v0.1.7
  160. with:
  161. host: "${{ secrets.REPO_HOST }}"
  162. username: "${{ secrets.REPO_USER }}"
  163. key: "${{ secrets.REPO_KEY }}"
  164. source: openapi-head/openapi.json
  165. strip_components: 1
  166. target: "/srv/incoming/openapi/unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}"
  167. - name: Move openapi.json (unstable) into place
  168. uses: appleboy/ssh-action@25ce8cbbcb08177468c7ff7ec5cbfa236f9341e1 # v1.1.0
  169. with:
  170. host: "${{ secrets.REPO_HOST }}"
  171. username: "${{ secrets.REPO_USER }}"
  172. key: "${{ secrets.REPO_KEY }}"
  173. debug: false
  174. script_stop: false
  175. script: |
  176. if ! test -d /run/workflows; then
  177. sudo mkdir -p /run/workflows
  178. sudo chown ${{ secrets.REPO_USER }} /run/workflows
  179. fi
  180. (
  181. flock -x -w 300 200 || exit 1
  182. TGT_DIR="/srv/repository/main/openapi"
  183. LAST_SPEC="$( ls -lt ${TGT_DIR}/unstable/ | grep 'jellyfin-openapi' | head -1 | awk '{ print $NF }' )"
  184. # If new and previous spec don't differ (diff retcode 0), remove incoming and finish
  185. if diff /srv/incoming/openapi/unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}/openapi.json ${TGT_DIR}/unstable/${LAST_SPEC} &>/dev/null; then
  186. rm -r /srv/incoming/openapi/unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}
  187. exit 0
  188. fi
  189. # Move new spec into place
  190. sudo mv /srv/incoming/openapi/unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}/openapi.json ${TGT_DIR}/unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}.json
  191. # Delete previous jellyfin-openapi-unstable_previous.json
  192. sudo rm ${TGT_DIR}/jellyfin-openapi-unstable_previous.json
  193. # Move current jellyfin-openapi-unstable.json symlink to jellyfin-openapi-unstable_previous.json
  194. sudo mv ${TGT_DIR}/jellyfin-openapi-unstable.json ${TGT_DIR}/jellyfin-openapi-unstable_previous.json
  195. # Create new jellyfin-openapi-unstable.json symlink
  196. sudo ln -s unstable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}.json ${TGT_DIR}/jellyfin-openapi-unstable.json
  197. # Check that the previous openapi unstable spec link is correct
  198. if [[ "$( readlink ${TGT_DIR}/jellyfin-openapi-unstable_previous.json )" != "unstable/${LAST_SPEC}" ]]; then
  199. sudo rm ${TGT_DIR}/jellyfin-openapi-unstable_previous.json
  200. sudo ln -s unstable/${LAST_SPEC} ${TGT_DIR}/jellyfin-openapi-unstable_previous.json
  201. fi
  202. ) 200>/run/workflows/openapi-unstable.lock
  203. publish-stable:
  204. name: OpenAPI - Publish Stable Spec
  205. if: ${{ startsWith(github.ref, 'refs/tags/v') && contains(github.repository_owner, 'jellyfin') }}
  206. runs-on: ubuntu-latest
  207. needs:
  208. - openapi-head
  209. steps:
  210. - name: Set version number
  211. id: version
  212. run: |-
  213. echo "JELLYFIN_VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
  214. - name: Download openapi-head
  215. uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
  216. with:
  217. name: openapi-head
  218. path: openapi-head
  219. - name: Upload openapi.json (stable) to repository server
  220. uses: appleboy/scp-action@917f8b81dfc1ccd331fef9e2d61bdc6c8be94634 # v0.1.7
  221. with:
  222. host: "${{ secrets.REPO_HOST }}"
  223. username: "${{ secrets.REPO_USER }}"
  224. key: "${{ secrets.REPO_KEY }}"
  225. source: openapi-head/openapi.json
  226. strip_components: 1
  227. target: "/srv/incoming/openapi/stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}"
  228. - name: Move openapi.json (stable) into place
  229. uses: appleboy/ssh-action@25ce8cbbcb08177468c7ff7ec5cbfa236f9341e1 # v1.1.0
  230. with:
  231. host: "${{ secrets.REPO_HOST }}"
  232. username: "${{ secrets.REPO_USER }}"
  233. key: "${{ secrets.REPO_KEY }}"
  234. debug: false
  235. script_stop: false
  236. script: |
  237. if ! test -d /run/workflows; then
  238. sudo mkdir -p /run/workflows
  239. sudo chown ${{ secrets.REPO_USER }} /run/workflows
  240. fi
  241. (
  242. flock -x -w 300 200 || exit 1
  243. TGT_DIR="/srv/repository/main/openapi"
  244. LAST_SPEC="$( ls -lt ${TGT_DIR}/stable/ | grep 'jellyfin-openapi' | head -1 | awk '{ print $NF }' )"
  245. # If new and previous spec don't differ (diff retcode 0), remove incoming and finish
  246. if diff /srv/incoming/openapi/stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}/openapi.json ${TGT_DIR}/stable/${LAST_SPEC} &>/dev/null; then
  247. rm -r /srv/incoming/openapi/stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}
  248. exit 0
  249. fi
  250. # Move new spec into place
  251. sudo mv /srv/incoming/openapi/stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}/openapi.json ${TGT_DIR}/stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}.json
  252. # Delete previous jellyfin-openapi-stable_previous.json
  253. sudo rm ${TGT_DIR}/jellyfin-openapi-stable_previous.json
  254. # Move current jellyfin-openapi-stable.json symlink to jellyfin-openapi-stable_previous.json
  255. sudo mv ${TGT_DIR}/jellyfin-openapi-stable.json ${TGT_DIR}/jellyfin-openapi-stable_previous.json
  256. # Create new jellyfin-openapi-stable.json symlink
  257. sudo ln -s stable/jellyfin-openapi-${{ env.JELLYFIN_VERSION }}.json ${TGT_DIR}/jellyfin-openapi-stable.json
  258. # Check that the previous openapi stable spec link is correct
  259. if [[ "$( readlink ${TGT_DIR}/jellyfin-openapi-stable_previous.json )" != "stable/${LAST_SPEC}" ]]; then
  260. sudo rm ${TGT_DIR}/jellyfin-openapi-stable_previous.json
  261. sudo ln -s stable/${LAST_SPEC} ${TGT_DIR}/jellyfin-openapi-stable_previous.json
  262. fi
  263. ) 200>/run/workflows/openapi-stable.lock