build-decision/src/build_decision/cli.py
author Andrew Halberstadt <ahal@pm.me>
Fri, 25 Mar 2022 19:20:21 +0000
changeset 1301 7e8c1a39f2b3fb40ca19b0a5da39834fd3f6f32d
parent 1067 1be83c6c9a239d01dd3e68f6b9f25c5c89690883
permissions -rw-r--r--
No Bug - Enable 'generic' and 'cancel-all' actions for application-services, r=releng-reviewers,gbrown Differential Revision: https://phabricator.services.mozilla.com/D142114

# -*- coding: utf-8 -*-

# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.

import functools

from .repository import Repository
from .secrets import get_secret
from .util.cli import CLI

app = CLI("Build decision tasks")


def repo_arguments(app):
    def decorator(func):
        @app.argument("--repo-url", required=True)
        @app.argument("--project", required=True)
        @app.argument("--level", required=True)
        @app.argument("--repository-type", required=True)
        @app.argument("--trust-domain", required=True)
        @app.argument("--github-token-secret")
        @functools.wraps(func)
        def wrapper(args):
            repository = {}
            for argument in (
                "repo_url",
                "project",
                "level",
                "repository_type",
                "trust_domain",
            ):
                repository[argument] = args.pop(argument)
            github_token_secret = args.pop("github_token_secret", None)
            if github_token_secret:
                repository["github_token"] = get_secret(
                    github_token_secret, secret_key="token"
                )
            args["repository"] = Repository(**repository)
            func(args)

        return wrapper

    return decorator


@app.command("hg-push", help="Create an hg-push decision task.")
@repo_arguments(app)
@app.argument("--taskcluster-yml-repo")
@app.argument("--dry-run", action="store_true")
def hg_push(options):
    from .hg_push import build_decision

    if options["taskcluster_yml_repo"]:
        taskcluster_yml_repo = Repository(
            repo_url=options["taskcluster_yml_repo"],
            repository_type="hg",
        )
    else:
        taskcluster_yml_repo = None
    build_decision(
        repository=options["repository"],
        taskcluster_yml_repo=taskcluster_yml_repo,
        dry_run=options["dry_run"],
    )


@app.command("cron", help="Process `.cron.yml`.")
@repo_arguments(app)
@app.argument("--branch")
@app.argument("--force-run")
@app.argument("--dry-run", action="store_true")
def cron(options):
    from .cron import run

    run(
        repository=options["repository"],
        branch=options["branch"],
        force_run=options["force_run"],
        dry_run=options["dry_run"],
    )


main = app.main