diff --git a/taskboot/cli.py b/taskboot/cli.py index 5cff02d..dbd0346 100644 --- a/taskboot/cli.py +++ b/taskboot/cli.py @@ -14,6 +14,7 @@ from taskboot.build import build_hook from taskboot.build import build_image from taskboot.cargo import cargo_publish +from taskboot.git import git_push from taskboot.github import github_release from taskboot.push import heroku_release from taskboot.push import push_artifacts @@ -269,6 +270,26 @@ def main(): ) deploy_pypi.set_defaults(func=publish_pypi) + # Push on a repository + git_push_cmd = commands.add_parser( + "git-push", help="Push the commits of a branch on a repository", + ) + git_push_cmd.add_argument( + "--force-push", action="store_true", help="Force push the branch", + ) + git_push_cmd.add_argument( + "repository", + type=str, + help="Repository name to use (example: github.com/mozilla/task-boot)", + ) + git_push_cmd.add_argument( + "user", type=str, help="User login to use", + ) + git_push_cmd.add_argument( + "branch", type=str, help="The name of the branch to use", + ) + git_push_cmd.set_defaults(func=git_push) + # Deploy as a github release github_release_cmd = commands.add_parser( "github-release", help="Create a GitHub release and publish assets" diff --git a/taskboot/git.py b/taskboot/git.py new file mode 100644 index 0000000..3b0485a --- /dev/null +++ b/taskboot/git.py @@ -0,0 +1,35 @@ +# -*- 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 logging +import subprocess + +from taskboot.config import Configuration + +logger = logging.getLogger(__name__) + + +def git_push(target, args): + """ + Push commits on a repository + """ + + # Load config from file/secret + config = Configuration(args) + assert config.has_github_auth(), "Missing GitHub authentication" + + # Set remote repository + repo_link = "https://{}:{}@{}.git".format( + args.user, config.github["token"], args.repository + ) + subprocess.run(["git", "remote", "set-url", "origin", repo_link]) + + # Push on repository + if args.force_push: + command = ["git", "push", "-f", "origin", args.branch] + else: + command = ["git", "push", "origin", args.branch] + + subprocess.run(command, check=True)