Skip to content

Commit

Permalink
fix: Fix slap changelog diff pr update --use github-actions command…
Browse files Browse the repository at this point in the history
… used by the `NklasRosenstein/slap@gha/changelog/update/v2` action to support forks. (#91)

* fix: Fix `slap changelog diff pr update --use github-actions` command used by the `NklasRosenstein/slap@gha/changelog/update/v2` action to support forks.

* try GITHUB_REF as head ref

* need to get rid of the refs prefix

* fix pull request head?

* introduced dedicated branch name variable

* designate to proper refs

* fix assertion

* back to using the head repository directly

* use GIT_ASKPASS

* ensure askpass is executable;

* set gitpython env

* try uppercase

* log askpass script

* assert that GITHUB_TOKEN cannot be empty

* post comment to PR if PR is from a fork

* use "token" instead of "Bearer"

* nvm, GitHub API also uses "Bearer"

* allow failure fetching comments

* use correct session

* add X-GitHub-Api-Version

* log response text

* log GITHUB_REF

* support reading pull request id from GITHUB_PR_ID

* update

* update comment

* retry pushing to forked repo

* test the pull_request and pull_request_target events

* improvement: Store templates that can be created with `slap init` in files instead of in code, improve Python template to include `pycln` and a dummy for running `slap run docs:build`, updated Github actions workflow templates to use `pull_request_target` for changelog validation

* update pyproject.toml template

* upadte docsg

* improvement: add `skip-checks: true` to commits pushed by `slap changelog diff pr update`

* update workflow

* update default
  • Loading branch information
NiklasRosenstein committed May 13, 2023
1 parent 336da40 commit 81d5326
Show file tree
Hide file tree
Showing 18 changed files with 812 additions and 250 deletions.
17 changes: 17 additions & 0 deletions .changelog/_unreleased.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[entries]]
id = "ffb21822-a989-49f8-9e7d-ea8af5c2de2a"
type = "fix"
description = "Fix `slap changelog diff pr update --use github-actions` command used by the `NklasRosenstein/slap@gha/changelog/update/v2` action to support forks."
author = "@NiklasRosenstein"

[[entries]]
id = "84d57dba-1ae8-41a8-86f1-122ef992098e"
type = "improvement"
description = "Store templates that can be created with `slap init` in files instead of in code, improve Python template to include `pycln` and a dummy for running `slap run docs:build`, updated Github actions workflow templates to use `pull_request_target` for changelog validation"
author = "@NiklasRosenstein"

[[entries]]
id = "724c14e9-d83a-43de-95ec-03a02153c907"
type = "improvement"
description = "add `skip-checks: true` to commits pushed by `slap changelog diff pr update`"
author = "@NiklasRosenstein"
28 changes: 28 additions & 0 deletions .github/workflows/changelog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will run on every pull request to the develop branch to do the following:
#
# - Insert the Pull Request URL into new changelog entries.
# - Assert that a new changelog entry has been added in the PR unless a "no changelog" label is added to the PR.

name: "Changelog"

on:
pull_request_target: { branches: [ "develop" ] }

jobs:
changelog-update:
name: "Insert Pull Request URL into new changelog entries"
runs-on: ubuntu-latest
if: github.event_name == 'pull_request_target'
steps:
- uses: actions/checkout@v2
- uses: NiklasRosenstein/slap@gha/changelog/update/v2
with: { pr-id: '${{ github.event.pull_request.number }}' }

assert-new-changelog-entries:
name: "At least one new changelog entry must be added"
runs-on: ubuntu-latest
if: github.base_ref != '' && !contains(github.event.pull_request.labels.*.name, 'no changelog')
steps:
- uses: actions/checkout@v2
- uses: NiklasRosenstein/slap@gha/changelog/assert-added/v2
with: { pr-id: '${{ github.event.pull_request.number }}' }
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,6 @@ jobs:
- run: slap test
- run: slap publish --dry

update-pr:
name: "Insert PR reference into added changelogs."
runs-on: ubuntu-20.04
if: github.base_ref != ''
steps:
- uses: actions/checkout@v2
- run: git fetch && git checkout $GITHUB_HEAD_REF
- uses: actions/setup-python@v2
with: { python-version: "3.10" }
- run: pip install . && slap install --no-venv-check
- run: slap changelog diff pr update --use github-actions

require-changelog:
name: "Require added changelog entries on Pull Requests."
runs-on: ubuntu-20.04
if: github.base_ref != '' && !contains(github.event.pull_request.labels.*.name, 'no changelog') # Only on pull_request or pull_request_target events, and if the PR is not labeled
steps:
- uses: actions/checkout@v2
- run: git fetch && git checkout $GITHUB_HEAD_REF
- uses: actions/setup-python@v2
with: { python-version: "3.10" }
- run: pip install . && slap install --no-venv-check
- run: slap changelog diff assert-added --use github-actions

test-pep508:
runs-on: ubuntu-20.04
strategy:
Expand Down
81 changes: 10 additions & 71 deletions docs/content/guides/github.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,22 @@
title: GitHub
---

# Using Slap in GitHub repositories
[0]: https://github.com/NiklasRosenstein/slap

Most of Slap's functionality is independent of the repository hosting service that you use. However, Slap comes with
some built-in utities to make integration with GitHub easier.
# Using Slap in GitHub repositories

## GitHub Actions

### Install Slap

[0]: https://github.com/NiklasRosenstein/slap/tree/github-action/install/v1

The [`NiklasRosenstein/slap@gha/install/v1`][0] action installs Slap for you. It does this by setting up
Python 3.10 and installing Slap via Pipx.
Slap comes with a set of helpful GitHub actions that you can use in your workflows. These actions are available on
the [`NiklasRosenstein/slap`][0] repository. The templates generated by `slap init -t github` make use of these
actions to provide a good out-of-the-box experience.

!!! note

Use this action _before_ you run your own step of `actions/setup-python@v2` as after this action the current
Python version will be 3.10.

The `version` option defaults to `*`, which installs the latest version of Slap. It is recommended that you
pick an exact version for your configuration to avoid surprises.

```yaml title=".github/workflows/python.yml"
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: NiklasRosenstein/slap@gha/install/v1
with: { version: '*' }
- uses: actions/setup-python@v2
with: { python-version: "3.x" }
- run: slap install --no-venv-check
- run: slap test
```yaml title=".github/workflows/python.yaml"
@cat ../../../src/slap/templates/github-workflows/.github/workflows/python.yaml
```

### Update Changelogs

[1]: https://github.com/NiklasRosenstein/slap/tree/gha/changelog/update/v2

The `slap changelog diff pr update` command updates the PR references of changelogs added between two Git revisions. In
addition, by passing `--use github-actions`, there is almost no need for any additional configuration inside of a
GitHub action run for a Pull Request event. The [`NiklasRosenstein/slap@gha/changelog/update/v2`][1] action
makes automatically updated changelogs a breeze:

```yaml title=".github/workflows/python.yml"
on: [ pull_request ]
jobs:
changelog-update:
name: "Insert the Pull Request URL into new changelog entries"
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- uses: actions/checkout@v2
- uses: NiklasRosenstein/slap@gha/changelog/update/v2
with: { version: '*' }
```yaml title=".github/workflows/changelog.yaml"
@cat ../../../src/slap/templates/github-workflows/.github/workflows/changelog.yaml
```

## Assert Changelogs

[2]: https://github.com/NiklasRosenstein/slap/tree/gha/changelog/assert-added/v2

The `slap changelog diff assert-added` command is similar to the `slap changelog diff pr update` command in that it
inspects the diff of changelogs between to Git versions, but it fails if no new changelog entry was added to the
unreleased changelog.

We recommend that you use the GitHub Action [`NiklasRosenstein/slap@gha/changelog/assert-added/v2`][2].

```yaml title=".github/workflows/python.yml"
on: [ pull_request ]
jobs:
assert-new-changelog-entries:
name: "Assert that new changelog entries have been added"
runs-on: ubuntu-latest
if: github.base_ref != '' && !contains(github.event.pull_request.labels.*.name, 'no changelog')
steps:
- uses: actions/checkout@v2
- uses: NiklasRosenstein/slap@gha/changelog/assert-added/v2
with: { version: '*' }
```
These files demonstrate the usage of the GitHub actions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ twine = "^3.7.0"
tqdm = "^4.64.0"
build = "^0.8.0"
"nr.python.environment" = "^0.1.4"
gitpython = "^3.1.31"

[tool.poetry.dev-dependencies]
black = "^22.3.0"
Expand Down Expand Up @@ -117,7 +118,7 @@ github = "slap.ext.repository_hosts.github:GithubRepositoryHost"
typed = true

[tool.slap.test]
mypy = "dmypy run src -- --namespace-packages"
mypy = "dmypy run src -- --namespace-packages --exclude src/slap/templates"
pytest = "pytest tests/ -vv"
check = "slap check"
isort = "isort src/ tests/ --check-only"
Expand Down
146 changes: 13 additions & 133 deletions src/slap/ext/application/init.py
Original file line number Diff line number Diff line change
@@ -1,148 +1,28 @@
import datetime
import textwrap
from pathlib import Path
from typing import Iterable

from slap.application import Application, Command, argument, option
from slap.plugins import ApplicationPlugin
from slap.util.external.licenses import get_spdx_license_details, wrap_license_text
from slap.util.vcs import get_git_author

TEMPLATES = {
"poetry": {
"pyproject.toml": """
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
TEMPLATES = ["poetry", "github-workflows"]

[tool.poetry]
name = "{name}"
version = "0.1.0"
description = ""
authors = ["{author_name} <{author_email}>"]
license = "{license}"
readme = "readme.md"
packages = [{{ include = "{path}", from = "src" }}]
classifiers = []
keywords = []

[tool.poetry.urls]
# "Bug Tracker" = ""
# Documentation = ""
# Homepage = ""
# Repository = ""
[tool.poetry.dependencies]
python = "^3.6"
[tool.poetry.dev-dependencies]
black = "*"
flake8 = "*"
isort = "*"
mypy = "*"
pytest = "*"
[tool.slap]
typed = true
[tool.slap.test]
check = "slap check"
mypy = "dmypy run src/"
pytest = "pytest tests/ -vv"
black = "black --check src/ tests/"
isort = "isort --check-only src/ tests/"
flake8 = "flake8 src/ tests/"
[tool.slap.run]
fmt = "black src/ tests/ && isort src/ tests/"
[tool.mypy]
python_version = "3.6"
explicit_package_bases = true
mypy_path = ["src"]
namespace_packages = true
pretty = true
show_error_codes = true
show_error_context = true
strict = true
warn_no_return = true
warn_redundant_casts = true
warn_unreachable = true
warn_unused_ignores = true
[tool.isort]
profile = "black"
line_length = 120
combine_as_imports = true
[tool.black]
line-length = 120
""",
"LICENSE": "",
"readme.md": """
# {name}
""",
".flake8": """
[flake8]
max-line-length = 120
# Black can yield formatted code that triggers these Flake8 warnings.
ignore=
# line break before binary operator
W503,
# line break after binary operator
W504,
""",
".gitignore": """
__pycache__/
.dmypy.json
.venv/
*.egg-info/
/.vscode
/build
/dist
poetry.lock
""",
"src/{path}/__init__.py": """
__version__ = "0.1.0"
""",
"tests/test_import.py": """
def test_import():
exec("from {dotted_name} import *")
""",
"src/{path}/py.typed": "",
},
"github": {
".github/workflows/python-package.yml": """
name: Python application
on:
push: {{ branches: [ develop ], tags: [ "*" ] }}
pull_request: {{ branches: [ develop ] }}
def load_template(name: str) -> Iterable[tuple[str, str]]:
"""
Loads a template, iterating over all its files.
"""

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: [ "3.6", "3.7", "3.8", "3.9", "3.10", "3.x" ]
steps:
- uses: actions/checkout@v2
- uses: python-slap/slap-cli@gha/install/v1
- uses: actions/setup-python@v2
with: {{ python-version: "${{{{ matrix.python-version }}}}" }}
- run: slap install -vv --no-venv-check
- run: slap test
import slap

update-changelog:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- uses: actions/checkout@v2
- uses: python-slap/slap-cli@gha/changelog/update/v1
""",
},
}
path = Path(slap.__file__).parent / "templates" / name
for filename in path.glob("**/*"):
if filename.is_dir():
continue
yield str(filename.relative_to(path)), filename.read_text()


class InitCommandPlugin(ApplicationPlugin, Command):
Expand Down Expand Up @@ -238,7 +118,7 @@ def handle(self) -> int:
"package": self.option("name").replace(".", "_").replace("-", "_"),
}
)
for filename, content in TEMPLATES[template].items():
for filename, content in load_template(template):
if filename == "LICENSE":
content = get_spdx_license_details(self.option("license")).license_text
content = wrap_license_text(content).replace("<year>", str(scope["year"]))
Expand Down
Loading

0 comments on commit 81d5326

Please sign in to comment.