Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a55801ad authored by Philipp Kewisch's avatar Philipp Kewisch
Browse files

Add a print mode to setup_release_automation

parent a142e166
Loading
Loading
Loading
Loading
+118 −22
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import argparse
import requests
import nacl.encoding
import nacl.public
import textwrap

PUBLISH_APPROVERS = ["kewisch", "cketti", "wmontwe"]

@@ -193,6 +194,35 @@ def set_github_environment_secret(
        )


def print_github_environment_variable(repo, environment_name):
    url = (
        f"https://api.github.com/repos/{repo}/environments/{environment_name}/variables"
    )
    headers = {
        "Authorization": f"token {GITHUB_TOKEN}",
        "Accept": "application/vnd.github.v3+json",
    }
    response = requests.get(url, headers=headers)
    data = response.json()

    if response.status_code == 200:
        for variable in data["variables"]:
            value = variable["value"]
            if value[0] in "{[":
                try:
                    value = textwrap.indent(
                        json.dumps(json.loads(value), indent=2), "\t\t"
                    ).lstrip()
                except:
                    pass

            print(f"\t{variable['name']}={value}")
    else:
        raise Exception(
            f"Unexpected response getting variables from {environment_name}: {response.status_code} {response.text}"
        )


def set_github_environment_variable(repo, name, value, environment_name):
    url = (
        f"https://api.github.com/repos/{repo}/environments/{environment_name}/variables"
@@ -220,6 +250,52 @@ def set_github_environment_variable(repo, name, value, environment_name):
        )


def print_github_environment(repo, environment_name):
    url = f"https://api.github.com/repos/{repo}/environments/{environment_name}"
    headers = {
        "Authorization": f"token {GITHUB_TOKEN}",
        "Accept": "application/vnd.github.v3+json",
    }
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        data = response.json()
        print(f"Environment {environment_name}")
        print("\tProtection rules")
        needs_branch_policies = False
        for rule in data["protection_rules"]:
            if rule["type"] == "branch_policy":
                continue

            print(f"\t\tType: {rule['type']}")
            if rule["type"] == "required_reviewers":
                reviewers = ", ".join(
                    map(
                        lambda reviewer: reviewer["reviewer"]["login"],
                        rule["reviewers"],
                    )
                )
                print(f"\t\t\tReviewers: {reviewers}")

        print(f"\t\tBranch policy: {data['deployment_branch_policy']}")
        if (
            data["deployment_branch_policy"]
            and data["deployment_branch_policy"]["custom_branch_policies"]
        ):
            url += "/deployment-branch-policies"
            response = requests.get(url, headers=headers)
            if response.status_code == 200:
                policies = map(
                    lambda policy: policy["name"], response.json()["branch_policies"]
                )
                print("\t\tBranches: " + ", ".join(policies))

    else:
        raise Exception(
            f"Unexpected response getting variables from {environment_name}: {response.status_code} {response.text}"
        )


# Function to create GitHub environment if it doesn't exist
def create_github_environment(repo, environment_name, branches=None, approvers=None):
    url = f"https://api.github.com/repos/{repo}/environments/{environment_name}"
@@ -391,6 +467,7 @@ def create_release_environment(repo, branches):
        repo, "RELEASER_APP_CLIENT_ID", releaser_client_id, environment
    )


def create_matrix_environment(repo, branches):
    environment = "notify_matrix"

@@ -430,6 +507,9 @@ def main():
        required=True,
        help="GitHub repository in the format 'owner/repo'.",
    )
    parser.add_argument(
        "--print", "-p", action="store_true", help="Print instead of set"
    )
    parser.add_argument(
        "--skip", "-s", action="append", help="Skip this named environment"
    )
@@ -453,10 +533,7 @@ def main():
    includeset = set(
        list(CHANNEL_ENVIRONMENTS.keys())
        + list(SIGNING_ENVIRONMENTS.keys())
        + [
            "publish_hold",
            "publish_release",
        ]
        + ["publish_hold", "publish_release", "notify_matrix"]
    )
    if args.skip:
        for skip in args.skip:
@@ -467,6 +544,9 @@ def main():

    # Publish hold environment
    if "publish_hold" in includeset:
        if args.print:
            print_github_environment(args.repo, "publish_hold")
        else:
            create_github_environment(
                args.repo, "publish_hold", approvers=PUBLISH_APPROVERS
            )
@@ -476,6 +556,10 @@ def main():
        if environment_name not in includeset:
            continue

        if args.print:
            print(f"Environment {environment_name}")
            print_github_environment_variable(args.repo, environment_name)
        else:
            create_github_environment(
                args.repo, environment_name, branches=[data["branch"]]
            )
@@ -483,13 +567,19 @@ def main():
            for name, value in data["variables"].items():
                if isinstance(value, dict) or isinstance(value, list):
                    value = json.dumps(value)
            set_github_environment_variable(args.repo, name, value, environment_name)

                set_github_environment_variable(
                    args.repo, name, value, environment_name
                )

    # Signing environments
    for environment_name, data in SIGNING_ENVIRONMENTS.items():
        if environment_name not in includeset:
            continue

        if args.print:
            print_github_environment(args.repo, environment_name)
        else:
            if not os.path.exists(data["props"]):
                print(f"Skipping {environment_name}: Missing key .properties file")
                continue
@@ -500,10 +590,16 @@ def main():

    # Publish environment
    if "publish_release" in includeset:
        if args.print:
            print_github_environment(args.repo, "publish_release")
        else:
            create_release_environment(args.repo, ["main", "beta", "release"])

    # Notify
    if "notify_matrix" in includeset:
        if args.print:
            print_github_environment(args.repo, "notify_matrix")
        else:
            create_matrix_environment(args.repo, ["main", "beta", "release"])