Skip to content

Commit

Permalink
Add set_active_docs.py to set what versions are visible
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Feb 26, 2021
1 parent 579a668 commit 411a2ff
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tools/set_active_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import sys

import packaging.version
import requests

current_version = packaging.version.parse(sys.stdin.readline().strip("v\n"))
project = "praw"
headers = {"Authorization": f"token {os.environ.get('READTHEDOCS_TOKEN')}"}

response = requests.get(
f"https://readthedocs.org/api/v3/projects/{project}/versions?active=true",
headers=headers,
)
active_versions = response.json()
versions = [
packaging.version.parse(slug["slug"].strip("v"))
for slug in active_versions["results"]
if not slug["hidden"]
]

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:
print(f"Version {version!s} was hidden successfully.")

0 comments on commit 411a2ff

Please sign in to comment.