From d373db76591e7179ceb382362fa07564f9fe7ef9 Mon Sep 17 00:00:00 2001 From: Justin Ibarra Date: Thu, 24 Feb 2022 14:49:01 -0900 Subject: [PATCH] Ensure github module is installed before running PR commands (#1777) * Ensure github module is installed before running PR commands * move go and elastic-package assertions to top of command * update error msg for missing pkg * remove redundant github assertion Co-authored-by: Colson Wilhoit <48036388+DefSecSentinel@users.noreply.github.com> --- detection_rules/devtools.py | 25 ++++++++++++++++--------- detection_rules/ghwrap.py | 9 ++++++--- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/detection_rules/devtools.py b/detection_rules/devtools.py index 3a0caadce..cf2da197a 100644 --- a/detection_rules/devtools.py +++ b/detection_rules/devtools.py @@ -379,14 +379,15 @@ def kibana_commit(ctx, local_repo: str, github_repo: str, ssh: bool, kibana_dire def kibana_pr(ctx: click.Context, label: Tuple[str, ...], assign: Tuple[str, ...], draft: bool, fork_owner: str, token: str, **kwargs): """Create a pull request to Kibana.""" + github = GithubClient(token) + client = github.authenticated_client + repo = client.get_repo(kwargs["github_repo"]) + branch_name, commit_hash = ctx.invoke(kibana_commit, push=True, **kwargs) if fork_owner: branch_name = f'{fork_owner}:{branch_name}' - client = GithubClient(token).authenticated_client - repo = client.get_repo(kwargs["github_repo"]) - title = f"[Detection Engine] Adds {current_stack_version()} rules" body = textwrap.dedent(f""" ## Summary @@ -435,6 +436,18 @@ def integrations_pr(ctx: click.Context, local_repo: str, token: str, draft: bool pkg_directory: str, base_branch: str, remote: str, branch_name: Optional[str], github_repo: str, assign: Tuple[str, ...], label: Tuple[str, ...]): """Create a pull request to publish the Fleet package to elastic/integrations.""" + github = GithubClient(token) + github.assert_github() + client = github.authenticated_client + repo = client.get_repo(github_repo) + + # Use elastic-package to format and lint + gopath = utils.gopath() + assert gopath is not None, "$GOPATH isn't set" + + err = 'elastic-package missing, run: go install github.com/elastic/elastic-package@latest and verify go bin path' + assert subprocess.check_output(['elastic-package'], stderr=subprocess.DEVNULL), err + local_repo = os.path.abspath(local_repo) stack_version = Package.load_configs()["name"] package_version = Package.load_configs()["registry_data"]["version"] @@ -496,10 +509,6 @@ def integrations_pr(ctx: click.Context, local_repo: str, token: str, draft: bool save_changelog() - # Use elastic-package to format and lint - gopath = utils.gopath() - assert gopath is not None, "$GOPATH isn't set" - def elastic_pkg(*args): """Run a command with $GOPATH/bin/elastic-package in the package directory.""" prev = os.path.abspath(os.getcwd()) @@ -519,8 +528,6 @@ def integrations_pr(ctx: click.Context, local_repo: str, token: str, draft: bool git("push", "--set-upstream", remote, branch_name) # Create a pull request (not done yet, but we need the PR number) - client = GithubClient(token).authenticated_client - repo = client.get_repo(github_repo) body = textwrap.dedent(f""" ## What does this PR do? Update the Security Rules package to version {package_version}. diff --git a/detection_rules/ghwrap.py b/detection_rules/ghwrap.py index a9aa4f60e..e6c3fd3ff 100644 --- a/detection_rules/ghwrap.py +++ b/detection_rules/ghwrap.py @@ -103,14 +103,17 @@ class GithubClient: def __init__(self, token: Optional[str] = None): """Get an unauthenticated client, verified authenticated client, or a default client.""" - if not Github: - raise ModuleNotFoundError('Missing PyGithub - try running `pip install -r requirements-dev.txt`') - + self.assert_github() self.client: Github = Github(token) self.unauthenticated_client = Github() self.__token = token self.__authenticated_client = None + @classmethod + def assert_github(cls): + if not Github: + raise ModuleNotFoundError('Missing PyGithub - try running `pip install -r requirements-dev.txt`') + @property def authenticated_client(self) -> Github: if not self.__token: