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

chore: Check whether version in PR is already released #157

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/check_version_availability.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Check package version availability

on:
workflow_call:

jobs:
check_version_availability:
name: Check version availability
runs-on: ubuntu-latest
if: (!startsWith(github.event.pull_request.title, 'docs:'))

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.8"

- name: Install dependencies
run: make install-dev

- name: Check version availability
run: make check-version-availability
4 changes: 4 additions & 0 deletions .github/workflows/run_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
pull_request:

jobs:
check_version_availability:
name: Check version availability
uses: ./.github/workflows/check_version_availability.yaml

lint_and_type_checks:
name: Run lint and type checks
uses: ./.github/workflows/lint_and_type_checks.yaml
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: clean install-dev build publish twine-check lint unit-tests integration-tests type-check check-code format check-async-docstrings fix-async-docstrings check-changelog-entry build-api-reference
.PHONY: clean install-dev build publish twine-check lint unit-tests integration-tests type-check check-code format check-async-docstrings fix-async-docstrings check-version-availability check-changelog-entry build-api-reference

# This is default for local testing, but GitHub workflows override it to a higher value in CI
INTEGRATION_TESTS_CONCURRENCY = 1
Expand Down Expand Up @@ -44,6 +44,9 @@ check-async-docstrings:
fix-async-docstrings:
python3 scripts/fix_async_docstrings.py

check-version-availability:
python3 scripts/check_version_availability.py

check-changelog-entry:
python3 scripts/check_version_in_changelog.py

Expand Down
13 changes: 13 additions & 0 deletions scripts/check_version_availability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3
from utils import get_current_package_version, get_published_package_versions

# Checks whether the current package version number was not already used in a published release.
if __name__ == '__main__':
current_version = get_current_package_version()

# Load the version numbers of the currently published versions from PyPI
published_versions = get_published_package_versions()

# We don't want to try to publish a version with the same version number as an already released stable version
if current_version in published_versions:
raise RuntimeError(f'The current version {current_version} was already released!')
15 changes: 2 additions & 13 deletions scripts/update_version_for_prerelease.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env python3

import json
import re
import sys
import urllib.request

from utils import PACKAGE_NAME, get_current_package_version, set_current_package_version
from utils import get_current_package_version, get_published_package_versions, set_current_package_version

# Checks whether the current package version number was not already used in a published release,
# and if not, modifies the package version number in pyproject.toml
Expand All @@ -32,16 +30,7 @@
raise RuntimeError(f'The current version {current_version} does not match the proper semver format for stable releases (X.Y.Z)')

# Load the version numbers of the currently published versions from PyPI
# If the URL returns 404, it means the package has no releases yet (which is okay in our case)
package_info_url = f'https://pypi.org/pypi/{PACKAGE_NAME}/json'
try:
conn = urllib.request.urlopen(package_info_url)
package_data = json.load(urllib.request.urlopen(package_info_url))
published_versions = list(package_data['releases'].keys())
except urllib.error.HTTPError as e:
if e.code != 404:
raise e
published_versions = []
published_versions = get_published_package_versions()

# We don't want to publish a prerelease version with the same version number as an already released stable version
if current_version in published_versions:
Expand Down
16 changes: 16 additions & 0 deletions scripts/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import pathlib
import re
import urllib.request

PACKAGE_NAME = 'apify_client'
REPO_ROOT = pathlib.Path(__file__).parent.resolve() / '..'
Expand Down Expand Up @@ -47,3 +49,17 @@ def sync_to_async_docstring(docstring: str) -> str:
for (pattern, replacement) in substitutions:
res = re.sub(pattern, replacement, res, flags=re.M)
return res


# Load the version numbers of the currently published versions from PyPI
def get_published_package_versions() -> list:
package_info_url = f'https://pypi.org/pypi/{PACKAGE_NAME}/json'
try:
package_data = json.load(urllib.request.urlopen(package_info_url))
published_versions = list(package_data['releases'].keys())
# If the URL returns 404, it means the package has no releases yet (which is okay in our case)
except urllib.error.HTTPError as e:
if e.code != 404:
raise e
published_versions = []
return published_versions