Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regenerate command #47

Merged
merged 2 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions swaggertosdk/restapi/github_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from enum import Enum
import logging
import os
from pathlib import Path
import tempfile

from github import UnknownObjectException, GithubException

Expand All @@ -10,8 +13,13 @@
from swaggertosdk.github_tools import (
get_or_create_pull,
DashboardCommentableObject,
manage_git_folder,
configure_user,
user_from_token
)

from git import Repo

_LOGGER = logging.getLogger("swaggertosdk.restapi.github_handler")

# How many context tag I authorize in a PR to accept it
Expand Down Expand Up @@ -226,3 +234,57 @@ def rest_pr_management(rest_pr, sdk_repo, sdk_tag, sdk_default_base=_DEFAULT_SDK
context_pr.html_url
)
dashboard.create_comment(msg)

def clean_sdk_pr(rest_pr, sdk_repo):
"""Look for the SDK pr created by this RestPR and wipe it.
"""
# Extract some metadata as variables
rest_repo = rest_pr.base.repo
# "repo" can be None if fork has been deleted.
is_from_a_fork = rest_pr.head.repo is None or rest_pr.head.repo.full_name != rest_repo.full_name

#
# Compute the "head" of future SDK PR.
#
if is_from_a_fork:
sdk_pr_head = _SDK_PR_TEMPLATE.format(rest_pr.number)
else:
sdk_pr_head = _SDK_PR_TEMPLATE.format(rest_pr.head.ref)

#
# Close all PRs from this branch
#
sdk_prs = list(sdk_repo.get_pulls(
head=sdk_repo.owner.login+":"+sdk_pr_head,
))
for sdk_pr in sdk_prs:
sdk_pr.edit(state="closed")
break
else:
return "Didn't find the SDK PR"

#
# Delete the branch. I need to clone the
#
gh_token = os.environ["GH_TOKEN"]
login = user_from_token(gh_token).login
credentials_part = '{user}:{token}@'.format(
user=login,
token=gh_token
)
upstream_url = 'https://{credentials}github.com/{sdk_git_id}.git'.format(
credentials=credentials_part,
sdk_git_id=sdk_pr.head.repo.full_name
)
with tempfile.TemporaryDirectory() as temp_dir, \
manage_git_folder(gh_token, Path(temp_dir) / Path("sdk"), sdk_repo.full_name) as sdk_folder:

sdk_repo = Repo(str(sdk_folder))
configure_user(gh_token, sdk_repo)

upstream = sdk_repo.create_remote('upstream', url=upstream_url)
upstream.fetch()
msg = upstream.push(":"+sdk_pr_head) # old style delete

_LOGGER.debug(msg)

19 changes: 18 additions & 1 deletion swaggertosdk/restapi/restbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from github import Github

from .bot_framework import order
from .github_handler import rest_pr_management
from .github_handler import rest_pr_management, clean_sdk_pr

_LOGGER = logging.getLogger("swaggertosdk.restapi.restbot")

Expand Down Expand Up @@ -35,3 +35,20 @@ def rebuild(self, issue, repotag=None):
repotag or self.repotag,
self.sdk_default_base
)

@order
def regenerate(self, issue, repotag=None):
if not issue.pull_request:
return "Rebuild makes no sense if not a PR"
if repotag and self.repotag != repotag:
_LOGGER.info("Skipping rebuild from bot, since repotag doesn't match: %s %s",
self.repotag,
repotag)
return # Do NOT return a string, I don't want to talk in the PR

rest_pr = issue.repository.get_pull(issue.number)
github_con = Github(self.gh_token)
sdk_repo = github_con.get_repo(self.sdkid)

clean_sdk_pr(rest_pr, sdk_repo)
return self.rebuild(issue, repotag)