From 47bd423b86c631e9091ff220143292e4e59b5f68 Mon Sep 17 00:00:00 2001 From: Laurent Mazuel Date: Mon, 26 Feb 2018 10:56:55 -0800 Subject: [PATCH] Add generic git to sdk bot --- swaggertosdk/restapi/sdkbot.py | 29 +++++++++++++++++++++++++++-- tests/test_sdk_bot.py | 11 +++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/test_sdk_bot.py diff --git a/swaggertosdk/restapi/sdkbot.py b/swaggertosdk/restapi/sdkbot.py index fce8652..2975bd7 100644 --- a/swaggertosdk/restapi/sdkbot.py +++ b/swaggertosdk/restapi/sdkbot.py @@ -71,9 +71,9 @@ def pr_message_for_package(sdk_pr, package_name): class GithubHandler: - def __init__(self): + def __init__(self, gh_token=None): # I need a token to do PR. Nothing to do with the bot. - self.gh_token = os.environ["GH_TOKEN"] + self.gh_token = os.environ["GH_TOKEN"] if not gh_token else gh_token @order def install(self, issue): @@ -112,6 +112,31 @@ class GithubHandler: return "Rebase done and pushed to the branch" + @order + def git(self, issue, *git_parameters): + if not issue.pull_request: + return "Rebase is just supported in PR for now" + + pr_obj = issue.repository.get_pull(issue.number) + + branch_name = pr_obj.head.ref + branched_sdk_id = pr_obj.head.repo.full_name+'@'+branch_name + + with tempfile.TemporaryDirectory() as temp_dir, \ + manage_git_folder(self.gh_token, Path(temp_dir) / Path("sdk"), branched_sdk_id) as sdk_folder: + + sdk_repo = Repo(str(sdk_folder)) + configure_user(self.gh_token, sdk_repo) + + command = getattr(sdk_repo.git, git_parameters[0]) + initial_answer = command(*git_parameters[1:]) + _LOGGER.debug(initial_answer) + msg = sdk_repo.git.push(force=True) + _LOGGER.debug(msg) + + return initial_answer + + @order def rebuild(self, issue, project_pattern): if not issue.pull_request: diff --git a/tests/test_sdk_bot.py b/tests/test_sdk_bot.py new file mode 100644 index 0000000..d6a251a --- /dev/null +++ b/tests/test_sdk_bot.py @@ -0,0 +1,11 @@ +from swaggertosdk.restapi.sdkbot import GithubHandler + + +def test_sdk_bot_git(github_client, github_token): + handler = GithubHandler(github_token) + + repo = github_client.get_repo("lmazuel/TestingRepo") + issue = repo.get_issue(11) + + output = handler.git(issue, "show", "2a0c2f0285117ccb07b6f9c32749d6c50abed70b") + assert "commit 2a0c2f0285117ccb07b6f9c32749d6c50abed70b" in output