Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure README is synched with code #43

Merged
merged 12 commits into from
Oct 10, 2023
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ repos:
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]

- repo: local
hooks:
- id: readme.py
name: readme.py
language: system
entry: python .tools/readme.py
files: "README.md|src/changelist/default_config.toml|.github/workflows/label-check.yaml|.github/workflows/milestone-merged-prs.yaml|.tools/readme.py"
55 changes: 55 additions & 0 deletions .tools/readme.py
lagru marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Update README.md, used by pre-commit."""

import re
lagru marked this conversation as resolved.
Show resolved Hide resolved
from pathlib import Path

PROJECT_ROOT = Path(__file__).parent.parent


def get_section_info(file):
p = Path(file)
name = p.parts[-1]
ext = p.suffix[1:]
begin = rf"<!--- begin {name} --->\n"
end = rf"<!--- end {name} --->\n"

with open(PROJECT_ROOT / file) as fh:
section = fh.read()

if ext == "toml":
section = begin + rf"\n````{ext}\n" + section + r"````\n\n" + end
else:
section = begin + rf"\n```{ext}\n" + section + r"```\n\n" + end

return begin, end, section


def main():
with open(PROJECT_ROOT / "README.md") as fh:
readme = fh.read()

# default_config.toml
begin, end, section = get_section_info("src/changelist/default_config.toml")
rx = re.compile(begin + ".*?" + end, re.DOTALL)
# Regex substitution replaces r"\\" with to r"\", compensate
section = section.replace(r"\\", r"\\\\")
readme = rx.sub(section, readme)

# label-check.yaml
begin, end, section = get_section_info(".github/workflows/label-check.yaml")
rx = re.compile(begin + ".*?" + end, re.DOTALL)
readme = rx.sub(section, readme)

# milestone-merged-prs.yaml
begin, end, section = get_section_info(
".github/workflows/milestone-merged-prs.yaml"
)
rx = re.compile(begin + ".*?" + end, re.DOTALL)
readme = rx.sub(section, readme)

with open(PROJECT_ROOT / "README.md", "w") as fh:
fh.write(readme)


if __name__ == "__main__":
main()
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ changelist can be configured from two sources, in order of precedence:
If a configuration option is not specified in either file above, changelist
falls back to the following configuration:

<!--- Changes to the following block are overridden by a pre-commit hook! --->
<!--- begin default_config.toml --->
jarrodmillman marked this conversation as resolved.
Show resolved Hide resolved

````toml
# Default changelist configuration as supported in pyproject.toml
[tool.changelist]
Expand Down Expand Up @@ -109,6 +112,8 @@ pr_summary_regex = "^```release-note\\s*(?P<summary>[\\s\\S]*?\\w[\\s\\S]*?)\\s*
".*Maintenance.*" = "Maintenance"
````

<!--- end default_config.toml --->

## Set up your repository

To categorize merged PRs in the changelist with the default configuration, each
Expand All @@ -122,14 +127,17 @@ we recommend adding an action that fails CI if the label is missing.

To do so, place the following in `.github/workflows/label-check.yaml`:

<!--- Changes to the following block are overridden by a pre-commit hook! --->
<!--- begin label-check.yaml --->

```yaml
name: Labels

on:
pull_request:
types:
- opened
- repoened
- reopened
- labeled
- unlabeled
- synchronize
Expand All @@ -146,6 +154,8 @@ jobs:
run: exit 1
```

<!--- end label-check.yaml --->

### Milestones

Often, it is helpful to have milestones that reflect the actual PRs
Expand All @@ -154,6 +164,9 @@ next open milestone to any merged PR.

To do so, place the following in `.github/workflows/milestone-merged-prs.yaml`:

<!--- Changes to the following block are overridden by a pre-commit hook! --->
<!--- begin milestone-merged-prs.yaml --->

```yaml
name: Milestone

Expand All @@ -175,4 +188,6 @@ jobs:
force: true
```

<!--- end milestone-merged-prs.yaml --->

See https://github.com/scientific-python/attach-next-milestone-action for more information.