|
| 1 | +name: public/tidy3d/python-client-build-changelog-pr |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + run_workflow: |
| 7 | + description: 'Set to true to build changelog and create a PR' |
| 8 | + required: true |
| 9 | + type: boolean |
| 10 | + default: true |
| 11 | + source_branch: |
| 12 | + description: 'Source branch to checkout and build changelog from' |
| 13 | + required: false |
| 14 | + type: string |
| 15 | + default: 'develop' |
| 16 | + target_branch: |
| 17 | + description: 'Target branch for the generated changelog PR' |
| 18 | + required: false |
| 19 | + type: string |
| 20 | + default: 'develop' |
| 21 | + release_version: |
| 22 | + description: 'Optional release version override (for example: 2.11.0)' |
| 23 | + required: false |
| 24 | + type: string |
| 25 | + default: '' |
| 26 | + release_date: |
| 27 | + description: 'Optional release date override in YYYY-MM-DD (defaults to UTC today)' |
| 28 | + required: false |
| 29 | + type: string |
| 30 | + default: '' |
| 31 | + previous_version: |
| 32 | + description: 'Optional previous release version override (for example: 2.10.2)' |
| 33 | + required: false |
| 34 | + type: string |
| 35 | + default: '' |
| 36 | + |
| 37 | +permissions: |
| 38 | + contents: read |
| 39 | + |
| 40 | +jobs: |
| 41 | + build-changelog-pr: |
| 42 | + if: github.event.inputs.run_workflow == 'true' |
| 43 | + runs-on: ubuntu-latest |
| 44 | + permissions: |
| 45 | + contents: write |
| 46 | + pull-requests: write |
| 47 | + steps: |
| 48 | + - name: Checkout repository |
| 49 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 50 | + with: |
| 51 | + ref: ${{ github.event.inputs.source_branch || 'develop' }} |
| 52 | + fetch-depth: 0 |
| 53 | + submodules: false |
| 54 | + persist-credentials: false |
| 55 | + |
| 56 | + - name: Set up Python |
| 57 | + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 |
| 58 | + with: |
| 59 | + python-version: '3.10' |
| 60 | + |
| 61 | + - name: Install Poetry |
| 62 | + uses: snok/install-poetry@76e04a911780d5b312d89783f7b1cd627778900a # v1.4.1 |
| 63 | + with: |
| 64 | + version: 2.1.1 |
| 65 | + virtualenvs-create: true |
| 66 | + virtualenvs-in-project: true |
| 67 | + |
| 68 | + - name: Install dependencies |
| 69 | + run: | |
| 70 | + set -e |
| 71 | + poetry install --extras dev --no-interaction |
| 72 | +
|
| 73 | + - name: Build changelog |
| 74 | + id: build-changelog |
| 75 | + env: |
| 76 | + INPUT_RELEASE_VERSION: ${{ github.event.inputs.release_version }} |
| 77 | + INPUT_RELEASE_DATE: ${{ github.event.inputs.release_date }} |
| 78 | + INPUT_PREVIOUS_VERSION: ${{ github.event.inputs.previous_version }} |
| 79 | + SOURCE_BRANCH: ${{ github.event.inputs.source_branch || 'develop' }} |
| 80 | + run: | |
| 81 | + set -euo pipefail |
| 82 | +
|
| 83 | + if [[ ! -d changelog.d ]]; then |
| 84 | + echo "changelog.d/ directory not found; skipping changelog build." |
| 85 | + echo "changes_detected=false" >> "$GITHUB_OUTPUT" |
| 86 | + exit 0 |
| 87 | + fi |
| 88 | +
|
| 89 | + fragment_count=$(find changelog.d -maxdepth 1 -type f -name '*.md' ! -name 'README.md' ! -name 'template.md' | wc -l) |
| 90 | + if [[ "$fragment_count" -eq 0 ]]; then |
| 91 | + echo "No changelog fragments found in changelog.d/." |
| 92 | + echo "changes_detected=false" >> "$GITHUB_OUTPUT" |
| 93 | + exit 0 |
| 94 | + fi |
| 95 | +
|
| 96 | + relver="${INPUT_RELEASE_VERSION}" |
| 97 | + if [[ -z "$relver" ]]; then |
| 98 | + relver=$(poetry version -s | sed -E 's/\.dev[0-9]+$//') |
| 99 | + fi |
| 100 | + relver=$(echo "$relver" | sed -E 's/^v//') |
| 101 | + if [[ -z "$relver" ]]; then |
| 102 | + echo "::error::Release version is empty after normalization." |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | +
|
| 106 | + reldate="${INPUT_RELEASE_DATE}" |
| 107 | + if [[ -z "$reldate" ]]; then |
| 108 | + reldate=$(date -u +%F) |
| 109 | + fi |
| 110 | +
|
| 111 | + prevver="${INPUT_PREVIOUS_VERSION}" |
| 112 | + prevver=$(echo "$prevver" | sed -E 's/^v//') |
| 113 | + if [[ -z "$prevver" ]]; then |
| 114 | + prevver=$(git tag --merged HEAD --list 'v*' --sort=-v:refname | sed 's/^v//' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | grep -v "^${relver}$" | head -n1 || true) |
| 115 | + fi |
| 116 | + if [[ -z "$prevver" ]]; then |
| 117 | + prevver=$(grep -E '^## \[[0-9]+\.[0-9]+\.[0-9]+\] - ' CHANGELOG.md | sed -E 's/^## \[([0-9]+\.[0-9]+\.[0-9]+)\].*/\1/' | grep -v "^${relver}$" | head -n1 || true) |
| 118 | + fi |
| 119 | +
|
| 120 | + safe_source_branch=$(echo "$SOURCE_BRANCH" | tr '/ ' '--') |
| 121 | +
|
| 122 | + echo "Building changelog for version ${relver} on ${reldate}" |
| 123 | + poetry run towncrier build --yes --version "${relver}" --date "${reldate}" |
| 124 | +
|
| 125 | + if [[ -n "$prevver" ]]; then |
| 126 | + poetry run python scripts/changelog_refs.py --version "${relver}" --previous-version "${prevver}" |
| 127 | + else |
| 128 | + poetry run python scripts/changelog_refs.py --version "${relver}" |
| 129 | + fi |
| 130 | +
|
| 131 | + if git diff --quiet -- CHANGELOG.md changelog.d; then |
| 132 | + echo "changes_detected=false" >> "$GITHUB_OUTPUT" |
| 133 | + else |
| 134 | + echo "changes_detected=true" >> "$GITHUB_OUTPUT" |
| 135 | + fi |
| 136 | + previous_version_output="${prevver}" |
| 137 | + if [[ -z "$previous_version_output" ]]; then |
| 138 | + previous_version_output="auto-detected" |
| 139 | + fi |
| 140 | + echo "release_version=${relver}" >> "$GITHUB_OUTPUT" |
| 141 | + echo "release_date=${reldate}" >> "$GITHUB_OUTPUT" |
| 142 | + echo "previous_version=${previous_version_output}" >> "$GITHUB_OUTPUT" |
| 143 | + echo "safe_source_branch=${safe_source_branch}" >> "$GITHUB_OUTPUT" |
| 144 | +
|
| 145 | + - name: Create Pull Request |
| 146 | + if: steps.build-changelog.outputs.changes_detected == 'true' |
| 147 | + uses: peter-evans/create-pull-request@c0f553fe549906ede9cf27b5156039d195d2ece0 # v8.1.0 |
| 148 | + with: |
| 149 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 150 | + commit-message: "chore(changelog): :robot: build release notes for ${{ steps.build-changelog.outputs.release_version }}" |
| 151 | + title: "chore(changelog): :robot: build release notes for ${{ steps.build-changelog.outputs.release_version }}" |
| 152 | + body: | |
| 153 | + This pull request was automatically generated by a GitHub Action. |
| 154 | +
|
| 155 | + It builds `CHANGELOG.md` from Towncrier fragments and updates compare reference links. |
| 156 | +
|
| 157 | + Source branch: `${{ github.event.inputs.source_branch || 'develop' }}` |
| 158 | + Target branch: `${{ github.event.inputs.target_branch || 'develop' }}` |
| 159 | + Version: `${{ steps.build-changelog.outputs.release_version }}` |
| 160 | + Previous version: `${{ steps.build-changelog.outputs.previous_version }}` |
| 161 | + Date: `${{ steps.build-changelog.outputs.release_date }}` |
| 162 | + branch: "chore/build-changelog-${{ steps.build-changelog.outputs.safe_source_branch }}-${{ github.run_id }}" |
| 163 | + base: "${{ github.event.inputs.target_branch || 'develop' }}" |
| 164 | + delete-branch: true |
0 commit comments