|
| 1 | +name: Release API diff |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + api-diff: |
| 12 | + runs-on: windows-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Set up .NET |
| 16 | + uses: actions/setup-dotnet@v4 |
| 17 | + with: |
| 18 | + dotnet-version: 8.0.x |
| 19 | + |
| 20 | + - name: Install generator |
| 21 | + run: dotnet tool install --global dotMorten.OmdGenerator |
| 22 | + |
| 23 | + - name: Find previous published release |
| 24 | + id: previous_release |
| 25 | + uses: actions/github-script@v7 |
| 26 | + with: |
| 27 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 28 | + script: | |
| 29 | + const releases = await github.paginate(github.rest.repos.listReleases, { |
| 30 | + owner: context.repo.owner, |
| 31 | + repo: context.repo.repo, |
| 32 | + per_page: 100 |
| 33 | + }); |
| 34 | +
|
| 35 | + const currentReleaseId = context.payload.release.id; |
| 36 | + const publishedReleases = releases.filter(release => !release.draft); |
| 37 | + const currentIndex = publishedReleases.findIndex(release => release.id === currentReleaseId); |
| 38 | +
|
| 39 | + if (currentIndex === -1 || currentIndex === publishedReleases.length - 1) { |
| 40 | + core.setOutput('tag', ''); |
| 41 | + return; |
| 42 | + } |
| 43 | +
|
| 44 | + core.setOutput('tag', publishedReleases[currentIndex + 1].tag_name); |
| 45 | +
|
| 46 | + - name: Generate API diff |
| 47 | + if: steps.previous_release.outputs.tag != '' |
| 48 | + shell: bash |
| 49 | + run: | |
| 50 | + generateomd \ |
| 51 | + /source=src/WinUIEx/ \ |
| 52 | + /gitRepo=${{ github.server_url }}/${{ github.repository }} \ |
| 53 | + /sourceRef=${{ github.event.release.tag_name }} \ |
| 54 | + /compareRef=${{ steps.previous_release.outputs.tag }} \ |
| 55 | + /format=md \ |
| 56 | + /output=api-diff |
| 57 | +
|
| 58 | + - name: Check whether API changes were found |
| 59 | + if: steps.previous_release.outputs.tag != '' |
| 60 | + id: api_diff |
| 61 | + shell: bash |
| 62 | + run: | |
| 63 | + if grep -q '^namespace ' api-diff.md; then |
| 64 | + echo "has_changes=true" >> "$GITHUB_OUTPUT" |
| 65 | + else |
| 66 | + echo "has_changes=false" >> "$GITHUB_OUTPUT" |
| 67 | + fi |
| 68 | +
|
| 69 | + - name: Update release notes |
| 70 | + if: steps.previous_release.outputs.tag != '' |
| 71 | + uses: actions/github-script@v7 |
| 72 | + env: |
| 73 | + SECTION_MARKER: <!-- dotnet-omd-api-diff --> |
| 74 | + HAS_CHANGES: ${{ steps.api_diff.outputs.has_changes }} |
| 75 | + PREVIOUS_TAG: ${{ steps.previous_release.outputs.tag }} |
| 76 | + with: |
| 77 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + script: | |
| 79 | + const fs = require('fs'); |
| 80 | + const marker = process.env.SECTION_MARKER; |
| 81 | + const hasChanges = process.env.HAS_CHANGES === 'true'; |
| 82 | + const previousTag = process.env.PREVIOUS_TAG; |
| 83 | + const diff = fs.readFileSync('api-diff.md', 'utf8').trim(); |
| 84 | +
|
| 85 | + const release = context.payload.release; |
| 86 | + const existingBody = release.body || ''; |
| 87 | +
|
| 88 | + const section = hasChanges |
| 89 | + ? [ |
| 90 | + marker, |
| 91 | + `## API changes since ${previousTag}`, |
| 92 | + '', |
| 93 | + diff |
| 94 | + ].join('\n') |
| 95 | + : [ |
| 96 | + marker, |
| 97 | + `## API changes since ${previousTag}`, |
| 98 | + '', |
| 99 | + 'No API changes are detected since the previous release.' |
| 100 | + ].join('\n'); |
| 101 | +
|
| 102 | + const markerIndex = existingBody.indexOf(marker); |
| 103 | + const body = markerIndex >= 0 |
| 104 | + ? existingBody.slice(0, markerIndex).trimEnd() + '\n\n' + section |
| 105 | + : (existingBody.trim() |
| 106 | + ? existingBody.trimEnd() + '\n\n' + section |
| 107 | + : section); |
| 108 | +
|
| 109 | + await github.rest.repos.updateRelease({ |
| 110 | + owner: context.repo.owner, |
| 111 | + repo: context.repo.repo, |
| 112 | + release_id: release.id, |
| 113 | + tag_name: release.tag_name, |
| 114 | + name: release.name, |
| 115 | + body, |
| 116 | + draft: release.draft, |
| 117 | + prerelease: release.prerelease |
| 118 | + }); |
| 119 | +``` |
0 commit comments