|
| 1 | +name: create a new release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + create_release: |
| 13 | + runs-on: ubuntu-22.04 |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v5 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + - name: Fetch tags |
| 22 | + run: git fetch --tags --force |
| 23 | + |
| 24 | + - name: Prepare release metadata |
| 25 | + id: prepare_release |
| 26 | + env: |
| 27 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 28 | + run: | |
| 29 | + python3 <<'PY' |
| 30 | + import os |
| 31 | + import pathlib |
| 32 | + import re |
| 33 | + import subprocess |
| 34 | +
|
| 35 | + github_output = pathlib.Path(os.environ["GITHUB_OUTPUT"]) |
| 36 | + release_notes_path = pathlib.Path("release_notes.md") |
| 37 | +
|
| 38 | + def write_output(name, value): |
| 39 | + with github_output.open("a", encoding="utf-8") as output_file: |
| 40 | + print(f"{name}={value}", file=output_file) |
| 41 | +
|
| 42 | + def capitalize_sentences(text): |
| 43 | + """Capitalize the first alphabetical character of each sentence.""" |
| 44 | + result = [] |
| 45 | + capitalize_next = True |
| 46 | + for character in text: |
| 47 | + if capitalize_next and character.isalpha(): |
| 48 | + result.append(character.upper()) |
| 49 | + capitalize_next = False |
| 50 | + else: |
| 51 | + result.append(character) |
| 52 | +
|
| 53 | + if character in ".!?": |
| 54 | + capitalize_next = True |
| 55 | + return "".join(result) |
| 56 | +
|
| 57 | + version = pathlib.Path("VERSION").read_text(encoding="utf-8").strip() |
| 58 | + if not re.fullmatch(r"\d+\.\d+\.\d+", version): |
| 59 | + raise SystemExit(f"VERSION must contain x.y.z, found: {version}") |
| 60 | +
|
| 61 | + tag_name = f"v{version}" |
| 62 | +
|
| 63 | + changelog_lines = pathlib.Path("CHANGELOG.md").read_text( |
| 64 | + encoding="utf-8" |
| 65 | + ).splitlines() |
| 66 | +
|
| 67 | + latest_section = [] |
| 68 | + started = False |
| 69 | + version_heading = re.compile(r"^\d+\.\d+\.\d+\b") |
| 70 | +
|
| 71 | + for line in changelog_lines: |
| 72 | + if not started: |
| 73 | + if line.strip(): |
| 74 | + started = True |
| 75 | + latest_section.append(line) |
| 76 | + continue |
| 77 | +
|
| 78 | + if version_heading.match(line.strip()): |
| 79 | + break |
| 80 | +
|
| 81 | + latest_section.append(line) |
| 82 | +
|
| 83 | + while latest_section and not latest_section[-1].strip(): |
| 84 | + latest_section.pop() |
| 85 | +
|
| 86 | + if not latest_section: |
| 87 | + raise SystemExit("Could not extract the latest release notes from CHANGELOG.md") |
| 88 | +
|
| 89 | + latest_heading = latest_section[0].strip() |
| 90 | + if not latest_heading.startswith(version): |
| 91 | + raise SystemExit( |
| 92 | + "The top section in CHANGELOG.md does not match the version in VERSION" |
| 93 | + ) |
| 94 | +
|
| 95 | + normalized_lines = [latest_section[0].strip(), ""] |
| 96 | + for line in latest_section[1:]: |
| 97 | + stripped = line.strip() |
| 98 | + if not stripped: |
| 99 | + normalized_lines.append("") |
| 100 | + continue |
| 101 | +
|
| 102 | + bullet_match = re.match(r"^([*-]\s+)(.*)$", stripped) |
| 103 | + if bullet_match: |
| 104 | + prefix, content = bullet_match.groups() |
| 105 | + normalized_lines.append(f"{prefix}{capitalize_sentences(content.strip())}") |
| 106 | + continue |
| 107 | +
|
| 108 | + normalized_lines.append(capitalize_sentences(stripped)) |
| 109 | +
|
| 110 | + release_notes_path.write_text( |
| 111 | + "\n".join(normalized_lines).strip() + "\n", |
| 112 | + encoding="utf-8", |
| 113 | + ) |
| 114 | +
|
| 115 | + existing_tag = subprocess.run( |
| 116 | + ["git", "tag", "--list", tag_name], |
| 117 | + check=True, |
| 118 | + capture_output=True, |
| 119 | + text=True, |
| 120 | + ).stdout.strip() |
| 121 | +
|
| 122 | + release_exists = subprocess.run( |
| 123 | + ["gh", "release", "view", tag_name], |
| 124 | + capture_output=True, |
| 125 | + text=True, |
| 126 | + ).returncode == 0 |
| 127 | +
|
| 128 | + write_output("tag_name", tag_name) |
| 129 | + write_output("tag_exists", "true" if existing_tag else "false") |
| 130 | + write_output("release_exists", "true" if release_exists else "false") |
| 131 | + PY |
| 132 | +
|
| 133 | + - name: Create and push tag |
| 134 | + if: steps.prepare_release.outputs.tag_exists == 'false' |
| 135 | + run: | |
| 136 | + git config user.name "github-actions[bot]" |
| 137 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 138 | + git tag "${{ steps.prepare_release.outputs.tag_name }}" |
| 139 | + git push origin "${{ steps.prepare_release.outputs.tag_name }}" |
| 140 | +
|
| 141 | + - name: Create GitHub release |
| 142 | + if: steps.prepare_release.outputs.release_exists == 'false' |
| 143 | + env: |
| 144 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 145 | + run: | |
| 146 | + gh release create "${{ steps.prepare_release.outputs.tag_name }}" \ |
| 147 | + --title "${{ steps.prepare_release.outputs.tag_name }}" \ |
| 148 | + --notes-file release_notes.md \ |
| 149 | + --latest |
| 150 | +
|
| 151 | + - name: Skip existing tag and release |
| 152 | + if: steps.prepare_release.outputs.tag_exists == 'true' && steps.prepare_release.outputs.release_exists == 'true' |
| 153 | + run: | |
| 154 | + echo "Tag ${{ steps.prepare_release.outputs.tag_name }} and its release already exist. Skipping." |
0 commit comments