From d081e51b1e179c6c04e06b059d5544996ee7e454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Thu, 10 Jun 2021 09:17:34 +0000 Subject: [PATCH] Add remove publishers script --- scripts/check/owner.py | 13 ++++++-- scripts/remove_publishers.py | 57 ++++++++++++++++++++++++++++++++++++ scripts/remove_repo.py | 1 + 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 scripts/remove_publishers.py diff --git a/scripts/check/owner.py b/scripts/check/owner.py index e40d8ff..cffe696 100644 --- a/scripts/check/owner.py +++ b/scripts/check/owner.py @@ -3,6 +3,7 @@ import os from scripts.changed.repo import get_repo from scripts.helpers.event import get_event +from scripts.remove_publishers import REMOVED_PUBLISHERS from aiogithubapi import GitHub, AIOGitHubAPIException TOKEN = os.getenv("GITHUB_TOKEN") @@ -13,15 +14,23 @@ async def check(): repo = get_repo() event = get_event() actor = event["pull_request"]["user"]["login"] + repo_owner = repo.split("/")[0].lower() - if repo.split("/")[0].lower() == actor.lower(): + for removed in REMOVED_PUBLISHERS: + if repo_owner == removed["publisher"].lower(): + exit( + f"::error::'{repo_owner}' is not allowed to publish default repositories" + ) + + if repo_owner == actor.lower(): print(f"'{actor}' is the owner of '{repo}'") return try: async with GitHub(TOKEN) as github: request = await github.client.get( - f"/repos/{repo}/contributors", headers={}, + f"/repos/{repo}/contributors", + headers={}, ) contributors = [ {"login": x["login"], "contributions": x["contributions"]} diff --git a/scripts/remove_publishers.py b/scripts/remove_publishers.py new file mode 100644 index 0000000..91a418f --- /dev/null +++ b/scripts/remove_publishers.py @@ -0,0 +1,57 @@ +import json + +# Information https://hacs.xyz/docs/publish/remove +REMOVED_PUBLISHERS = [] + +CATEGORIES = [ + "appdaemon", + "integration", + "netdaemon", + "plugin", + "python_script", + "theme", +] + +TO_REMOVE = {category: [] for category in CATEGORIES} +for entry in REMOVED_PUBLISHERS: + for category in CATEGORIES: + with open(category, "r") as cat_file: + content = json.loads(cat_file.read()) + for key in content.copy(): + if entry["publisher"] in key.lower(): + print(f"Found {key} in {category}") + TO_REMOVE[category].append( + {"repository": key, "link": entry["link"]} + ) + +with open("blacklist", "r") as blacklist_file: + blacklistcontent = json.loads(blacklist_file.read()) + +with open("removed", "r") as removed_file: + removedcontent = json.loads(removed_file.read()) + +for category in TO_REMOVE: + if len(TO_REMOVE[category]) != 0: + with open(category, "r") as cat_file: + categorycontent = json.loads(cat_file.read()) + + for entry in TO_REMOVE[category]: + blacklistcontent.append(entry["repository"]) + removedcontent.append( + {**entry, "reason": "Author removed", "removal_type": "removal"} + ) + categorycontent.remove(entry["repository"]) + + with open(category, "w") as cat_file: + cat_file.write( + json.dumps(sorted(categorycontent, key=str.casefold), indent=2) + ) + + +with open("blacklist", "w") as blacklist_file: + blacklist_file.write( + json.dumps(sorted(blacklistcontent, key=str.casefold), indent=2) + ) + +with open("removed", "w") as removed_file: + removed_file.write(json.dumps(removedcontent, indent=2)) diff --git a/scripts/remove_repo.py b/scripts/remove_repo.py index d69f16b..04cb495 100644 --- a/scripts/remove_repo.py +++ b/scripts/remove_repo.py @@ -1,6 +1,7 @@ import sys import json +# Information https://hacs.xyz/docs/publish/remove if len(sys.argv) < 3: print(