Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add set_active_docs.py to set what versions are visible
(cherry picked from commit 2a92aef95af7079f70fd8c4812fa86e9c814d352)
- Loading branch information
1 parent
bfcc7f0
commit e28a228
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
jobs: | ||
set_active_docs: | ||
name: Set Active Docs | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.x' | ||
- name: Install dependencies | ||
run: pip install packaging requests | ||
- env: | ||
READTHEDOCS_TOKEN: ${{ secrets.READTHEDOCS_TOKEN }} | ||
name: Set Active Docs | ||
run: python tools/set_active_docs.py | ||
name: Set Active Docs | ||
on: | ||
release: | ||
types: [published] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#!/usr/bin/env python3 | ||
import os | ||
import re | ||
import sys | ||
import time | ||
|
||
import packaging.version | ||
import requests | ||
|
||
PROJECT = "asyncpraw" | ||
HEADERS = {"Authorization": f"token {os.environ.get('READTHEDOCS_TOKEN')}"} | ||
|
||
|
||
def fetch_versions(): | ||
response = requests.get( | ||
f"https://readthedocs.org/api/v3/projects/{PROJECT}/versions?active=true", | ||
headers=HEADERS, | ||
) | ||
versions = None | ||
if response.status_code == 200: | ||
active_versions = response.json() | ||
versions = [ | ||
packaging.version.parse(slug["slug"].strip("v")) | ||
for slug in active_versions["results"] | ||
if not slug["hidden"] and not slug["slug"] in ["current", "latest"] | ||
] | ||
if versions is None: | ||
sys.stderr.write("Failed to get current active versions\n") | ||
return versions | ||
|
||
|
||
def main(): | ||
with open(f"{PROJECT}/const.py") as fp: | ||
current_version = packaging.version.parse( | ||
re.search('__version__ = "([^"]+)"', fp.read()).group(1) | ||
) | ||
|
||
versions = fetch_versions() | ||
if versions is None: | ||
return 1 | ||
max_retry_count = 5 | ||
retry_count = 0 | ||
while current_version not in versions: | ||
versions = fetch_versions() | ||
if versions is None: | ||
return 1 | ||
if current_version in versions: | ||
break | ||
else: | ||
if retry_count >= max_retry_count: | ||
sys.stderr.write("Current version failed to build\n") | ||
return 1 | ||
sys.stdout.write("Waiting 30 seconds for build to finish\n") | ||
retry_count += 1 | ||
time.sleep(30) | ||
aggregated_versions = {} | ||
for version in versions: | ||
aggregated_versions.setdefault(version.major, []) | ||
aggregated_versions[version.major].append(version) | ||
|
||
latest_major_versions = [ | ||
max(aggregated_versions[major]) for major in aggregated_versions | ||
] | ||
major_versions = [version.major for version in versions] | ||
is_new_major = major_versions.count(current_version.major) == 1 | ||
|
||
for version in versions: | ||
if (is_new_major and version not in latest_major_versions) or ( | ||
(version.major, version.minor) | ||
== ( | ||
current_version.major, | ||
current_version.minor, | ||
) | ||
and version.micro != current_version.micro | ||
): | ||
response = requests.patch( | ||
f"https://readthedocs.org/api/v3/projects/{PROJECT}/versions/v{version}/", | ||
json={"active": True, "hidden": True}, | ||
headers=HEADERS, | ||
) | ||
if response.status_code == 204: | ||
sys.stderr.write(f"Version {version!s} was hidden successfully\n") | ||
else: | ||
sys.stderr.write(f"Failed to hide version {version}\n") | ||
return 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |