From 4a88a2fc7891d41c612c3bad105ca94963c9c8f0 Mon Sep 17 00:00:00 2001 From: Lucas de Sousa Rosa Date: Wed, 29 Nov 2023 22:47:18 -0300 Subject: [PATCH 1/4] Add a post-commit hook for automatic tagging **TL;DR: this post-commit hook ensures that valid versions are automatically tagged based on changes in the `pyproject.toml` file.** This post-commit hook automates the tagging of the project based on changes in the `pyproject.toml` file. Here's a breakdown of what it does: **1. Extracting the version number:** * `git diff HEAD^..HEAD`: This line compares the current commit (`HEAD`) to its immediate predecessor (`HEAD^`). * `-- "$(git rev-parse --show-toplevel)"/pyproject.toml`: This specifies the `pyproject.toml` file within the project root directory. * `grep -m 1 '^\+.*version'`: This searches for the first line starting with a "+" and containing the word "version". * `sed -s 's/[^A-Z0-9\.\-]//g'`: This removes any characters except numbers, letters, dots, and hyphens from the matched line. * `version=`: Finally, the extracted version string is stored in the `version` variable. **2. Validating the version format:** * `if [[ ! $version =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)(\-[A-Z]+\.[0-9]+)?$ ]]; then`: This checks if the extracted version string matches a specific format: * `^`: Starts with the beginning of the string. * `([0-9]+)`: Matches one or more digits (major version). * `\.`: Matches a literal dot. * `([0-9]+)`: Matches one or more digits (minor version). * `\.`: Matches a literal dot. * `([0-9]+)`: Matches one or more digits (patch version). * `(\-[A-Z]+\.[0-9]+)?`: This is optional and matches a hyphen followed by one or more uppercase letters and a dot and another number (pre-release + build information). * `$`: Matches the end of the string. * If the format is invalid, it logs a message and exits with an error code. **3. Creating the tag:** * `git tag -a "v$version"`: This creates a new annotated tag named `v$version` (with the prefix "v") using the extracted version number. * ``-m "`git log -1 --format=%s`"``: This sets the tag message with the full commit message of the current commit. * `echo "Created a new tag, v$version"`: This prints a confirmation message. Co-authored-by: Darwish Ahmad Herati <13837531+daherati@users.noreply.github.com> --- .git-hooks/post-commit | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100755 .git-hooks/post-commit diff --git a/.git-hooks/post-commit b/.git-hooks/post-commit new file mode 100755 index 0000000..043e9ca --- /dev/null +++ b/.git-hooks/post-commit @@ -0,0 +1,10 @@ +#! /bin/bash +version=`git diff HEAD^..HEAD -- "$(git rev-parse --show-toplevel)"/pyproject.toml | grep -m 1 '^\+.*version' | sed -s 's/[^A-Z0-9\.\-]//g'` + +if [[ ! $version =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)(\-[A-Z]+\.[0-9]+)?$ ]]; then + echo -e "Skip tag: invalid version '$version'" + exit 1 +fi + +git tag -a "v$version" -m "`git log -1 --format=%s`" +echo "Created a new tag, v$version" \ No newline at end of file From 125ee2bcfd02e2f4422fd8cb41b606ca8d565f7f Mon Sep 17 00:00:00 2001 From: Lucas de Sousa Rosa Date: Fri, 1 Dec 2023 18:33:47 -0300 Subject: [PATCH 2/4] Add action to make a release and publish to PyPI This GitHub Actions workflow automates two tasks: 1. **Create Release:** - Triggered when a new version tag (formatted as `vX.X.X`) is pushed. - Creates a release with the tag name, marking it as the latest version. 2. **Publish to PyPI:** - Runs after the release is created successfully. - Builds and publishes the Python package to PyPI using Poetry. - Ignores development requirements during the publishing process. This ensures that each new version gets a release on GitHub and the corresponding Python package is published on PyPI. Co-authored-by: Darwish Ahmad Herati <13837531+daherati@users.noreply.github.com> Co-authored-by: mateuslatrova <57113699+mateuslatrova@users.noreply.github.com> --- .github/workflows/release_and_publish.yml | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .github/workflows/release_and_publish.yml diff --git a/.github/workflows/release_and_publish.yml b/.github/workflows/release_and_publish.yml new file mode 100644 index 0000000..7d6375e --- /dev/null +++ b/.github/workflows/release_and_publish.yml @@ -0,0 +1,32 @@ +name: Create and publish a release + +on: + push: + tags: + - 'v*.*.*' + +jobs: + create_release: + name: Create Release + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + - uses: ncipollo/release-action@v1 + with: + name: Release ${{ github.ref_name }} + makeLatest: true + + publish_to_pypi: + name: Publish to PyPI + needs: create_release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Build and publish to PyPI + uses: JRubics/poetry-publish@v1.17 + with: + pypi_token: ${{ secrets.PYPI_TOKEN }} + ignore_dev_requirements: "yes" \ No newline at end of file From 089db9673d36e8458c77c9bf2c013760ad575f1a Mon Sep 17 00:00:00 2001 From: Lucas de Sousa Rosa Date: Sat, 2 Dec 2023 23:33:12 -0300 Subject: [PATCH 3/4] Add support for poetry-bumpversion plugin --- pyproject.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 7dd9b6a..609de27 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,8 @@ tox = "^4.11.3" coverage = "^7.3.2" sphinx = { version = "^7.2.6", python = ">=3.9" } +[tool.poetry_bumpversion.file."pipreqs/__init__.py"] + [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" \ No newline at end of file From 703d809387114745b79af0918b7715efc703a294 Mon Sep 17 00:00:00 2001 From: Lucas de Sousa Rosa Date: Sun, 10 Dec 2023 12:19:14 -0300 Subject: [PATCH 4/4] Update CONTRIBUTING.rst file - Add instructions to setup git-hooks and poetry-bumpversion plugin --- CONTRIBUTING.rst | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index f23ef0d..9c5138a 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -66,14 +66,19 @@ Ready to contribute? Here's how to set up `pipreqs` for local development. 3. Pipreqs is developed using Poetry. Refer to the `documentation `_ to install Poetry in your local environment. Next, you should install pipreqs's dependencies:: $ poetry install --with dev + $ poetry self add poetry-bumpversion -4. Create a branch for local development:: +4. Configure `./git-hooks/` directory so that it becomes visible to git hooks (this hook depends on `bash`, `sed` and `grep` - tools usually included in any linux distribution):: + + $ git config core.hooksPath .git-hooks + +5. Create a branch for local development:: $ git checkout -b name-of-your-bugfix-or-feature Now you can make your changes locally. -5. When you're done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:: +6. When you're done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:: $ poetry run flake8 pipreqs tests $ poetry run python -m unittest discover @@ -81,13 +86,13 @@ Ready to contribute? Here's how to set up `pipreqs` for local development. To test all versions of python using tox you need to have them installed and for this two options are recommended: `pyenv` or `asdf`. -6. Commit your changes and push your branch to GitHub:: +7. Commit your changes and push your branch to GitHub:: $ git add . $ git commit -m "Your detailed description of your changes." $ git push origin name-of-your-bugfix-or-feature -7. Submit a pull request through the GitHub website. +8. Submit a pull request through the GitHub website. Pull Request Guidelines -----------------------