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>

(cherry picked from commit d373db7659)
This commit is contained in:
Justin Ibarra
2022-02-24 14:49:01 -09:00
committed by github-actions[bot]
parent aab23636e8
commit ca5f2d4018
2 changed files with 22 additions and 12 deletions
+16 -9
View File
@@ -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}.
+6 -3
View File
@@ -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: