diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..fcd9a1d --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,65 @@ +name: Test + +on: + push: + branches: + - main + pull_request: + types: + - opened + - synchronize + workflow_dispatch: + inputs: + debug_enabled: + description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)' + required: false + default: 'false' + +jobs: + test: + runs-on: ubuntu-latest + strategy: + fail-fast: false + + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + # Allow debugging with tmate + - name: Setup tmate session + uses: mxschmitt/action-tmate@v3 + if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled == 'true' }} + with: + limit-access-to-actor: true + - uses: actions/cache@v3 + id: cache + with: + path: ${{ env.pythonLocation }} + key: ${{ runner.os }}-python-${{ env.pythonLocation }}-${{ hashFiles('requirements.txt', 'requirements-test.txt') }}-v0 + - name: Install Dependencies + if: steps.cache.outputs.cache-hit != 'true' + run: python -m pip install -r requirements-test.txt + - run: mkdir coverage + - name: Test + run: bash scripts/test.sh + - run: coverage html --show-contexts --title "Coverage for ${{ github.sha }}" + + - name: Store coverage HTML + uses: actions/upload-artifact@v3 + with: + name: coverage-html + path: htmlcov + + # https://github.com/marketplace/actions/alls-green#why + alls-green: # This job does nothing and is only used for the branch protection + if: always() + needs: + - test + runs-on: ubuntu-latest + steps: + - name: Decide whether the needed jobs succeeded or failed + uses: re-actors/alls-green@release/v1 + with: + jobs: ${{ toJSON(needs) }} diff --git a/.gitignore b/.gitignore index 6d0ca61..e727bef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .mypy_cache .vscode +*.pyc +.DS_Store +.coverage diff --git a/Dockerfile b/Dockerfile index b1e02fe..fd7f936 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.7 +FROM python:3.10 COPY ./requirements.txt /app/requirements.txt diff --git a/README.md b/README.md index 1a3e54c..c937ec9 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} ``` -**Note**: you can also use the GitHub action directly intead of with Docker, but that would take an extra minute: +**Note**: you can also use the GitHub action directly instead of with Docker, but that would take an extra minute: ```YAML # - uses: docker://tiangolo/latest-changes:0.0.3 @@ -53,12 +53,8 @@ After merging a PR to the main branch, it will: ```Markdown ### Latest Changes - - ``` -...including the two breaking lines. - * Right after that, it will add a new list item with the changes: * Using the title from the PR. * **Tip**: make sure the PR has the title you want before merging it. @@ -77,6 +73,40 @@ You can see an example of how it works in this same file, at the bottom, in [Lat As the changes are simply written to a file in your repo, you can later tweak them however you want. You can add links, extend the information, remove irrelevant changes, etc. ✨ +## Using Labels + +You can also use labels in the PRs to configure which sections they should show up in the release notes. + +By default, it will use these labels and headers: + +* `breaking`: `#### Breaking Changes` +* `security`: `#### Security Fixes` +* `feature`: `#### Features` +* `bug`: `#### Fixes` +* `refactor`: `#### Refactors` +* `upgrade`: `#### Upgrades` +* `docs`: `#### Docs` +* `lang-all`: `#### Translations` +* `internal`: `#### Internal` + +So, if you have a PR with a label `feature`, by default, it will show up in the section about features, like: + +> ### Latest Changes +> +> #### Features +> +> * ✨ Add support for Jinja2 templates for latest changes messages. PR [#23](https://github.com/tiangolo/latest-changes/pull/23) by [@tiangolo](https://github.com/tiangolo). + +You can configure the labels and headers used in the GitHub Action `labels` workflow configuration. + +It takes a JSON array of JSON objects that contain a key `label` with the label you would add to each PR, and a key `header` with the header text that should be added to the release notes for that label. + +The order is important, the first label from the list that is found in your PR is the one that will be used. So, if you have a PR that has both labels `feature` and `bug`, if you use the default configuration, it will show up in the section for features as that comes first, if you want it to show up in the section for bugs you would need to change the order of the list of this configuration to have `bug` first. + +Note that this JSON has to be passed as a string because that's the only thing that GitHub Actions support for configurations. + +See the example below in the configuration section. + ## Existing PRs - Running Manually For this GitHub Action to work automatically, the workflow file has to be in the repository _before_ the PR is created, so that the PR also includes it. That's just how GitHub Actions work. @@ -96,9 +126,12 @@ So, in those cases, it won't do everything automatically, you will have to manua You can configure: * `latest_changes_file`: The file to modify with the latest changes. For example: `./docs/latest-changes.rst`. -* `latest_changes_header`: The header to look for before adding a new message. for example: `# CHANGELOG \n\n`. +* `latest_changes_header`: The header to look for before adding a new message. for example: `# CHANGELOG`. * `template_file`: A custom Jinja2 template file to use to generate the message, you could use this to generate a different message or to use a different format, for example, HTML instead of the default Markdown. +* `end_regex`: A RegEx string that marks the end of this release, so it normally matches the start of the header of the next release section, normally the same header level as `latest_changes_header`, so, if the `latest_changes_header` is `### Latest Changes`, the content for the next release below is probably something like `### 0.2.0`, then the `end_regex` should be `^### `. * `debug_logs`: Set to `'true'` to show logs with the current settings. +* `labels`: A JSON array of JSON objects with a `label` that you would put in each PR and the `header` that would be used in the release notes. See the example below. +* `next_section_start`: A RegEx for the start of the next label header section. If the headers start with `#### ` (as in `#### Features`), then this RegEx should match that, like `^#### `. ## Configuration example @@ -108,16 +141,12 @@ You could have a custom Jinja2 template with the message to write at `./.github/ ```Jinja2 This changed: {{pr.title}}. Done by [the GitHub user {{pr.user.login}}]({{pr.user.html_url}}). Check the [Pull Request {{pr.number}} with the changes and stuff]({{pr.html_url}}). now back to code. πŸ€“ - - ``` **Note**: you can use any location in your repository for the Jinja2 template. **Tip**: The `pr` object is a [PyGitHub `PullRequest` object](https://pygithub.readthedocs.io/en/latest/github_objects/PullRequest.html), you can extract any other information you need from it. -Notice that the Jinja2 template has 2 trailing newlines. Jinja2 we need one so that the next message shows below, instead of the same line, and Jinja2 eats one 🀷, so we put 2. - Then you could have a workflow like: ```YAML @@ -144,9 +173,29 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} latest_changes_file: docs/release-notes.md - latest_changes_header: '# Release Notes\n\n' + latest_changes_header: '# Release Notes' template_file: ./.github/workflows/release-notes.jinja2 + # The next release will start with this RegEx, for example "## 0.2.0" + end_regex: '^## ' debug_logs: true + # Here we use a yaml multiline string to pass a JSON array of JSON objects in a more readable way + # In these case we use the same default labels and the same header titles, but the headers use 3 hash symbols instead of the default of 4 + # We also add a custom last label "egg" for PRs with easter eggs. + labels: > + [ + {"label": "breaking", "header": "### Breaking Changes"}, + {"label": "security", "header": "### Security Fixes"}, + {"label": "feature", "header": "### Features"}, + {"label": "bug", "header": "### Fixes"}, + {"label": "refactor", "header": "### Refactors"}, + {"label": "upgrade", "header": "### Upgrades"}, + {"label": "docs", "header": "### Docs"}, + {"label": "lang-all", "header": "### Translations"}, + {"label": "internal", "header": "### Internal"}, + {"label": "egg", "header": "### Easter Eggs"} + ] + # This should match the start of the label headers + next_section_start: '^### ' ``` In this custom config: @@ -171,11 +220,9 @@ docker://tiangolo/latest-changes:0.0.3 ```Markdown # Release Notes - - ``` -**Note**: The `latest_changes_header` is a [regular expression](https://regex101.com/). In this case it has two newlines, and the mesage will be added right after that (without adding an extra newline). +**Note**: The `latest_changes_header` is a [regular expression](https://regex101.com/). In this case it has two newlines, and the message will be added right after that (without adding an extra newline). So it will generate messages like: @@ -191,21 +238,13 @@ And that Markdown will be shown like: > > * This changed: ✨ Add support for Jinja2 templates for changes notes. Done by [the GitHub user tiangolo](https://github.com/tiangolo). Check the [Pull Request 23 with the changes and stuff](https://github.com/tiangolo/latest-changes/pull/23). now back to code. πŸ€“ -**Note**: if you use the default of `### Latest Changes\n\n`, or add one like the one in this example with two newlines, this GitHub action will expect the two newlines to exist. But if your release notes are empty and the file only contains: - -```Markdown -# Release Notes -``` - -then this action won't be able to add the first message. So, make sure the latest changes file has the format expected, for example with the two newlines: - -```Markdown -# Release Notes +* It will expect that the end of the content starts with the regular expression `^## `, normally because that's how the next release starts. This will be used to organize the content in the sections with the headers from the `labels` configuration. +* It will show a lot of debugging information. -``` +* It will use the same default labels and headers plus another one for easter eggs, but with 3 hash symbols instead of the default of 4. -* Lastly, it will show a lot of debugging information. +* It will detect the start of each header section (the ones from the labels) with the regular expression `^### `. ## Protected Branches @@ -281,6 +320,7 @@ So, the commits will still be shown as made by `github-actions`. * πŸ”₯ Remove config pushing to custom branch for debugging. PR [#47](https://github.com/tiangolo/latest-changes/pull/47) by [@tiangolo](https://github.com/tiangolo). * πŸš€ Publish amd64 and arm64 versions, and publish to GitHub Container Registry, fix git in containers. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). * πŸ“ Add docs for using latest-changes with protected branches. PR [#43](https://github.com/tiangolo/latest-changes/pull/43) by [@tiangolo](https://github.com/tiangolo). + ### 0.0.3 * 🚚 Update Python module name to latest_changes to avoid conflicts with any repo directory "app". PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). diff --git a/action.yml b/action.yml index fe1d5f5..6f63217 100644 --- a/action.yml +++ b/action.yml @@ -1,29 +1,51 @@ name: Tiangolo's Latest Changes author: SebastiΓ‘n RamΓ­rez -description: Update the release notes with the "latest changes" right after a PR is merged +description: Update the release notes with the "latest changes" right after a PR is merged. inputs: token: - description: Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }} + description: Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }}. required: true number: description: Optional PR number to call this GitHub Action manually in a workflow. required: false latest_changes_file: - description: The file to add the latest changes + description: The file to add the latest changes. default: README.md required: false latest_changes_header: - description: Header to search for in the latest changes file, this action will add the changes right after that string (including newlines) - default: '### Latest Changes\n\n' + description: Header to search for in the latest changes file, this action will add the changes right after that string. + default: '### Latest Changes' required: false template_file: description: To override the default message with a custom Jinja2 template, use a path relative to the repo. required: false default: /app/latest_changes/latest-changes.jinja2 + end_regex: + description: A RegEx string that marks the end of this release, so it normally matches the start of the header of the next release section, normally the same header level as `latest_changes_header`, so, if the `latest_changes_header` is `### Latest Changes`, the content for the next release below is probably something like `### 0.2.0`, then the `end_regex` should be `^### `. + default: '^### ' + required: false debug_logs: description: Use debug=True to enable more logging, useful to see the object shape for custom Jinja2 templates required: false default: 'false' + labels: + description: A JSON array of JSON objects that contain a key `label` with the label you would add to each PR, and a key `header` with the header text that should be added to the release notes for that label. The order is important, the first label from the list that is found in your PR is the one that will be used. So, if you have a PR that has both labels `feature` and `bug`, if you use the default configuration, it will show up in the section for features, if you want it to show up in the section for bugs you would need to change the order of the list of this configuration to have `bug` first. Note that this JSON has to be passed as a string because that's the only thing that GitHub Actions support for configurations. + required: false + default: > + [ + {"label": "breaking", "header": "#### Breaking Changes"}, + {"label": "security", "header": "#### Security Fixes"}, + {"label": "feature", "header": "#### Features"}, + {"label": "bug", "header": "#### Fixes"}, + {"label": "refactor", "header": "#### Refactors"}, + {"label": "upgrade", "header": "#### Upgrades"}, + {"label": "docs", "header": "#### Docs"}, + {"label": "lang-all", "header": "#### Translations"}, + {"label": "internal", "header": "#### Internal"} + ] + next_section_start: + description: A RegEx for the start of the next label header section. If the headers start with `#### ` (as in `#### Features`), then this RegEx should match that, like `^#### `. + default: '^#### ' runs: using: docker image: Dockerfile diff --git a/latest_changes/__main__.py b/latest_changes/__main__.py index 511475f..5d6a810 100644 --- a/latest_changes/__main__.py +++ b/latest_changes/__main__.py @@ -1,94 +1,3 @@ -import logging -import re -import subprocess -import sys -from pathlib import Path -from typing import Optional +from .main import main -from github import Github -from jinja2 import Template -from pydantic import BaseModel, BaseSettings, SecretStr - - -class Settings(BaseSettings): - github_repository: str - github_event_path: Path - github_event_name: Optional[str] = None - input_token: SecretStr - input_latest_changes_file: Path = Path("README.md") - input_latest_changes_header: str = "### Latest Changes\n\n" - input_template_file: Path = Path(__file__).parent / "latest-changes.jinja2" - input_debug_logs: Optional[bool] = False - - -class PartialGitHubEventInputs(BaseModel): - number: int - - -class PartialGitHubEvent(BaseModel): - number: Optional[int] = None - inputs: Optional[PartialGitHubEventInputs] = None - - -logging.basicConfig(level=logging.INFO) -# Ref: https://github.com/actions/runner/issues/2033 -logging.info("GitHub Actions workaround for git in containers, ref: https://github.com/actions/runner/issues/2033") -safe_directory_config_content = "[safe]\n\tdirectory = /github/workspace" -dotgitconfig_path = Path.home() / ".gitconfig" -dotgitconfig_path.write_text(safe_directory_config_content) -settings = Settings() -if settings.input_debug_logs: - logging.info(f"Using config: {settings.json()}") -g = Github(settings.input_token.get_secret_value()) -repo = g.get_repo(settings.github_repository) -if not settings.github_event_path.is_file(): - logging.error(f"No event file was found at: {settings.github_event_path}") - sys.exit(1) -contents = settings.github_event_path.read_text() -event = PartialGitHubEvent.parse_raw(contents) -if event.number is not None: - number = event.number -elif event.inputs and event.inputs.number: - number = event.inputs.number -else: - logging.error( - f"No PR number was found (PR number or workflow input) in the event file at: {settings.github_event_path}" - ) - sys.exit(1) -pr = repo.get_pull(number) -if not pr.merged: - logging.info("The PR was not merged, nothing else to do.") - sys.exit(0) -if not settings.input_latest_changes_file.is_file(): - logging.error( - f"The latest changes files doesn't seem to exist: {settings.input_latest_changes_file}" - ) - sys.exit(1) -logging.info("Setting up GitHub Actions git user") -subprocess.run(["git", "config", "user.name", "github-actions"], check=True) -subprocess.run(["git", "config", "user.email", "github-actions@github.com"], check=True) -logging.info("Pulling the latest changes, including the latest merged PR (this one)") -subprocess.run(["git", "pull"], check=True) -content = settings.input_latest_changes_file.read_text() -match = re.search(settings.input_latest_changes_header, content) -if not match: - logging.error( - f"The latest changes file at: {settings.input_latest_changes_file} doesn't seem to contain the header RegEx: {settings.input_latest_changes_header}" - ) - sys.exit(1) -template_content = settings.input_template_file.read_text("utf-8") -template = Template(template_content) -message = template.render(pr=pr) -if message in content: - logging.error(f"It seems these PR's latest changes were already added: {number}") - sys.exit(1) -pre_content = content[: match.end()] -post_content = content[match.end() :] -new_content = pre_content + message + post_content -settings.input_latest_changes_file.write_text(new_content) -logging.info(f"Committing changes to: {settings.input_latest_changes_file}") -subprocess.run(["git", "add", str(settings.input_latest_changes_file)], check=True) -subprocess.run(["git", "commit", "-m", "πŸ“ Update release notes"], check=True) -logging.info(f"Pushing changes: {settings.input_latest_changes_file}") -subprocess.run(["git", "push"], check=True) -logging.info("Finished") +main() diff --git a/latest_changes/latest-changes.jinja2 b/latest_changes/latest-changes.jinja2 index 405592c..05bf423 100644 --- a/latest_changes/latest-changes.jinja2 +++ b/latest_changes/latest-changes.jinja2 @@ -1,2 +1 @@ -* {{pr.title}}. PR [#{{pr.number}}]({{pr.html_url}}) by [@{{pr.user.login}}]({{pr.user.html_url}}). - +* {{pr.title}}. PR [#{{pr.number}}]({{pr.html_url}}) by [@{{pr.user.login}}]({{pr.user.html_url}}). \ No newline at end of file diff --git a/latest_changes/main.py b/latest_changes/main.py new file mode 100644 index 0000000..b590097 --- /dev/null +++ b/latest_changes/main.py @@ -0,0 +1,237 @@ +import logging +import re +import subprocess +import sys +from pathlib import Path +from typing import List, Optional, Union + +from github import Github +from github.PullRequest import PullRequest +from jinja2 import Template +from pydantic import BaseModel, SecretStr +from pydantic_settings import BaseSettings + + +class Section(BaseModel): + label: str + header: str + + +class Settings(BaseSettings): + github_repository: str + github_event_path: Path + github_event_name: Optional[str] = None + input_token: SecretStr + input_latest_changes_file: Path = Path("README.md") + input_latest_changes_header: str = "### Latest Changes" + input_template_file: Path = Path(__file__).parent / "latest-changes.jinja2" + input_end_regex: str = "^### " + input_debug_logs: Optional[bool] = False + input_labels: List[Section] = [ + Section(label="breaking", header="#### Breaking Changes"), + Section(label="security", header="#### Security Fixes"), + Section(label="feature", header="#### Features"), + Section(label="bug", header="#### Fixes"), + Section(label="refactor", header="#### Refactors"), + Section(label="upgrade", header="#### Upgrades"), + Section(label="docs", header="#### Docs"), + Section(label="lang-all", header="#### Translations"), + Section(label="internal", header="#### Internal"), + ] + input_next_section_start: str = "^#### " + + +class PartialGitHubEventInputs(BaseModel): + number: int + + +class PartialGitHubEvent(BaseModel): + number: Optional[int] = None + inputs: Optional[PartialGitHubEventInputs] = None + + +class TemplateDataUser(BaseModel): + login: str + html_url: str + + +class TemplateDataPR(BaseModel): + number: int + title: str + html_url: str + user: TemplateDataUser + + +class SectionContent(BaseModel): + label: str + header: str + content: str + index: int + + +logging.basicConfig(level=logging.INFO) + + +def generate_content( + *, + content: str, + settings: Settings, + pr: Union[PullRequest, TemplateDataPR], + labels: list[str], +) -> str: + header_match = re.search( + settings.input_latest_changes_header, content, flags=re.MULTILINE + ) + if not header_match: + raise RuntimeError( + f"The latest changes file at: {settings.input_latest_changes_file} doesn't seem to contain the header RegEx: {settings.input_latest_changes_header}" + ) + template_content = settings.input_template_file.read_text("utf-8") + template = Template(template_content) + message = template.render(pr=pr) + if message in content: + raise RuntimeError( + f"It seems these PR's latest changes were already added: {pr.number}" + ) + pre_header_content = content[: header_match.end()].strip() + post_header_content = content[header_match.end() :].strip() + next_release_match = re.search( + settings.input_end_regex, post_header_content, flags=re.MULTILINE + ) + release_end = ( + len(content) + if not next_release_match + else header_match.end() + next_release_match.start() + ) + release_content = content[header_match.end() : release_end].strip() + post_release_content = content[release_end:].strip() + sections: list[SectionContent] = [] + sectionless_content = "" + for label in settings.input_labels: + label_match = re.search(label.header, release_content) + if not label_match: + continue + next_label_match = re.search( + settings.input_next_section_start, release_content[label_match.end() :] + ) + label_section_end = ( + len(release_content) + if not next_label_match + else label_match.end() + next_label_match.start() + ) + label_content = release_content[label_match.end() : label_section_end].strip() + section = SectionContent( + label=label.label, + header=label.header, + content=label_content, + index=label_match.start(), + ) + sections.append(section) + sections.sort(key=lambda x: x.index) + sections_keys = {section.label: section for section in sections} + if not sections: + sectionless_content = release_content + elif sections[0].index > 0: + sectionless_content = release_content[: sections[0].index].strip() + new_sections: list[SectionContent] = [] + found = False + for label in settings.input_labels: + if label.label in sections_keys: + section = sections_keys[label.label] + else: + section = SectionContent( + label=label.label, + header=label.header, + content="", + index=-1, + ) + sections_keys[label.label] = section + if label.label in labels and not found: + found = True + section.content = f"{message}\n{section.content}".strip() + new_sections.append(section) + if not found: + if sectionless_content: + sectionless_content = f"{message}\n{sectionless_content}" + else: + sectionless_content = f"{message}" + new_release_content = "" + if sectionless_content: + new_release_content = f"{sectionless_content}" + updated_content = "\n\n".join( + [ + f"{section.header}\n\n{section.content}" + for section in new_sections + if section.content + ] + ) + if new_release_content: + if updated_content: + new_release_content += f"\n\n{updated_content}" + else: + new_release_content = updated_content + + new_content = f"{pre_header_content}\n\n{new_release_content}\n\n{post_release_content}".strip() + return new_content + + +def main() -> None: + # Ref: https://github.com/actions/runner/issues/2033 + logging.info( + "GitHub Actions workaround for git in containers, ref: https://github.com/actions/runner/issues/2033" + ) + safe_directory_config_content = "[safe]\n\tdirectory = /github/workspace" + dotgitconfig_path = Path.home() / ".gitconfig" + dotgitconfig_path.write_text(safe_directory_config_content) + settings = Settings() + if settings.input_debug_logs: + logging.info(f"Using config: {settings.json()}") + g = Github(settings.input_token.get_secret_value()) + repo = g.get_repo(settings.github_repository) + if not settings.github_event_path.is_file(): + logging.error(f"No event file was found at: {settings.github_event_path}") + sys.exit(1) + contents = settings.github_event_path.read_text() + event = PartialGitHubEvent.model_validate_json(contents) + if event.number is not None: + number = event.number + elif event.inputs and event.inputs.number: + number = event.inputs.number + else: + logging.error( + f"No PR number was found (PR number or workflow input) in the event file at: {settings.github_event_path}" + ) + sys.exit(1) + pr = repo.get_pull(number) + if not pr.merged: + logging.info("The PR was not merged, nothing else to do.") + sys.exit(0) + if not settings.input_latest_changes_file.is_file(): + logging.error( + f"The latest changes files doesn't seem to exist: {settings.input_latest_changes_file}" + ) + sys.exit(1) + logging.info("Setting up GitHub Actions git user") + subprocess.run(["git", "config", "user.name", "github-actions"], check=True) + subprocess.run( + ["git", "config", "user.email", "github-actions@github.com"], check=True + ) + logging.info( + "Pulling the latest changes, including the latest merged PR (this one)" + ) + subprocess.run(["git", "pull"], check=True) + content = settings.input_latest_changes_file.read_text() + + new_content = generate_content( + content=content, + settings=settings, + pr=pr, + labels=[label.name for label in pr.labels], + ) + settings.input_latest_changes_file.write_text(new_content) + logging.info(f"Committing changes to: {settings.input_latest_changes_file}") + subprocess.run(["git", "add", str(settings.input_latest_changes_file)], check=True) + subprocess.run(["git", "commit", "-m", "πŸ“ Update release notes"], check=True) + logging.info(f"Pushing changes: {settings.input_latest_changes_file}") + subprocess.run(["git", "push"], check=True) + logging.info("Finished") diff --git a/requirements-test.txt b/requirements-test.txt new file mode 100644 index 0000000..2311f7f --- /dev/null +++ b/requirements-test.txt @@ -0,0 +1,3 @@ +-r requirements.txt +pytest +coverage[toml] diff --git a/requirements.txt b/requirements.txt index a5b26d9..51a560c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ PyGithub -pydantic>=1.6.1,<1.7.0 +pydantic>=2.0.0 +pydantic-settings httpx>=0.15.5,<0.16.0 email-validator Jinja2 diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 0000000..a9b357a --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -e +set -x + +coverage run -m pytest tests +coverage report --show-missing +coverage html diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_generate_content.py b/tests/test_generate_content.py new file mode 100644 index 0000000..af74dd5 --- /dev/null +++ b/tests/test_generate_content.py @@ -0,0 +1,721 @@ +import inspect + +import pytest + +from latest_changes.main import ( + Settings, + TemplateDataPR, + TemplateDataUser, + generate_content, +) + + +def test_no_sections(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + * πŸ”₯ Remove config. PR [#47](https://github.com/tiangolo/latest-changes/pull/47) by [@tiangolo](https://github.com/tiangolo). + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + * πŸ“ Add docs. PR [#43](https://github.com/tiangolo/latest-changes/pull/43) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content(content=content, settings=settings, pr=pr, labels=[]) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + ### Latest Changes + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + * πŸ”₯ Remove config. PR [#47](https://github.com/tiangolo/latest-changes/pull/47) by [@tiangolo](https://github.com/tiangolo). + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + * πŸ“ Add docs. PR [#43](https://github.com/tiangolo/latest-changes/pull/43) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_before_release(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content(content=content, settings=settings, pr=pr, labels=[]) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + ### Latest Changes + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_existing_labels_no_label(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + #### Features + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content(content=content, settings=settings, pr=pr, labels=[]) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + ### Latest Changes + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + + #### Features + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_existing_labels_same_label(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + #### Features + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content( + content=content, settings=settings, pr=pr, labels=["feature"] + ) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + ### Latest Changes + + #### Features + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_existing_label_other_label(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + #### Fixes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content( + content=content, settings=settings, pr=pr, labels=["feature"] + ) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + ### Latest Changes + + #### Features + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + + #### Fixes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_existing_label_secondary_label(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + #### Features + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content( + content=content, settings=settings, pr=pr, labels=["bug"] + ) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + ### Latest Changes + + #### Features + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + #### Fixes + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_no_existing_label_label(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content( + content=content, settings=settings, pr=pr, labels=["feature"] + ) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + ### Latest Changes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + #### Features + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_no_existing_label_release_label_label(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + #### Features + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content( + content=content, settings=settings, pr=pr, labels=["feature"] + ) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + ### Latest Changes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + #### Features + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + #### Features + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_custom_label_label(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + #### Custom + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content( + content=content, settings=settings, pr=pr, labels=["feature"] + ) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + ### Latest Changes + + #### Custom + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + #### Features + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_sectionless_content_label(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + #### Fixes + + * πŸ”₯ Remove config. PR [#47](https://github.com/tiangolo/latest-changes/pull/47) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content( + content=content, settings=settings, pr=pr, labels=["feature"] + ) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + ### Latest Changes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + #### Features + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + + #### Fixes + + * πŸ”₯ Remove config. PR [#47](https://github.com/tiangolo/latest-changes/pull/47) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_content_above_latest_changes(): + raw_content = """ + ## Release Notes + + Here's some content. + + ## Some Header + + * Here's a list. + + #### Features + + These are not release notes. + + ### Latest Changes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + #### Fixes + + * πŸ”₯ Remove config. PR [#47](https://github.com/tiangolo/latest-changes/pull/47) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content( + content=content, settings=settings, pr=pr, labels=["feature"] + ) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + Here's some content. + + ## Some Header + + * Here's a list. + + #### Features + + These are not release notes. + + ### Latest Changes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + #### Features + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + + #### Fixes + + * πŸ”₯ Remove config. PR [#47](https://github.com/tiangolo/latest-changes/pull/47) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_multiple_labels(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + #### Fixes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + new_content = generate_content( + content=content, settings=settings, pr=pr, labels=["bug", "feature"] + ) + assert new_content == inspect.cleandoc( + """ + ## Release Notes + + ### Latest Changes + + #### Features + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + + #### Fixes + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + ) + + +def test_no_latest_changes_raises(): + raw_content = """ + ## Release Notes + + Here's some content. + + ## Some Header + + * Here's a list. + + #### Features + + These are not release notes. + + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + + #### Fixes + + * πŸ”₯ Remove config. PR [#47](https://github.com/tiangolo/latest-changes/pull/47) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + with pytest.raises(RuntimeError): + generate_content(content=content, settings=settings, pr=pr, labels=["feature"]) + + +def test_changes_exist_raises(): + raw_content = """ + ## Release Notes + + ### Latest Changes + + * Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo). + * πŸ”₯ Remove config. PR [#47](https://github.com/tiangolo/latest-changes/pull/47) by [@tiangolo](https://github.com/tiangolo). + * πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo). + * πŸ“ Add docs. PR [#43](https://github.com/tiangolo/latest-changes/pull/43) by [@tiangolo](https://github.com/tiangolo). + + ### 0.0.3 + + * 🚚 Update Python module name. PR [#37](https://github.com/tiangolo/latest-changes/pull/37) by [@tiangolo](https://github.com/tiangolo). + * πŸ› Fix default Jinja2 path. PR [#38](https://github.com/tiangolo/latest-changes/pull/38) by [@tiangolo](https://github.com/tiangolo). + """ + + content = inspect.cleandoc(raw_content) + settings = Settings( + github_repository="tiangolo/latest-changes", + github_event_path="event.json", + input_token="secret", + ) + pr = TemplateDataPR( + title="Demo PR", + number=42, + html_url="https://example.com/pr/42", + user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"), + ) + with pytest.raises(RuntimeError): + generate_content(content=content, settings=settings, pr=pr, labels=["feature"])