Skip to content

Commit

Permalink
Add new command to validate Sleuth actions file
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdon committed May 25, 2021
1 parent 03a97ca commit 0d4ce90
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
6 changes: 4 additions & 2 deletions sleuth/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import click

from sleuth.commands.deploy import deploy
from sleuth.commands.validate import validate
from sleuth.version import version


Expand All @@ -15,13 +16,14 @@ class Context:
@click.group()
@click.version_option(version)
@click.option("-k", "--api-key", required=True, help="the Sleuth API key")
@click.option("--baseurl", default="https://app.sleuth.io", help="The Sleuth base URL")
@click.option("--baseurl", default="https://app.sleuth.io", help="The Sleuth base URL", hidden=True)
@click.pass_context
def main(ctx, api_key, baseurl):
ctx.obj = Context(baseurl=baseurl, api_key=api_key)


main.add_command(deploy)
main.add_command(validate)

if __name__ == "__main__":
main()
main()
73 changes: 73 additions & 0 deletions sleuth/commands/validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from __future__ import annotations

import json
import typing
from dataclasses import dataclass

import click
import requests

if typing.TYPE_CHECKING:
from sleuth.cli import Context


@dataclass
class DeploymentContext:
root: Context
organization: str
deployment: str


@click.command()
@click.option("-o", "--organization", required=True, help="the Sleuth organization slug")
@click.option("-d", "--deployment", required=True, help="the Sleuth deployment slug")
@click.argument("path", required=True)
@click.pass_obj
def validate(ctx: Context, organization, deployment, path):
"""Validate a Sleuth actions rules.yml file"""

deployment_context = DeploymentContext(ctx, organization, deployment)
try:
rules = validate_rules(deployment_context, path)["validateRules"]["rules"]
except Exception as e:
raise click.ClickException(str(e))

click.echo(f"Validated {len(rules)} rule(s)")


def validate_rules(context: DeploymentContext, path: str) -> typing.Dict:
operations = {
"query": f"""
mutation ($file: Upload!) {{
validateRules(orgSlug:"{context.organization}", deploymentSlug:"{context.deployment}", file: $file) {{
rules {{
title
}}
}}
}}
""",
"variables": {"file": None},
}
values = {
"operations": json.dumps(operations),
"map": json.dumps({"0": ["variables.file"]}),
}
files = {"0": open(path, "rb")}
headers = {"AUTHORIZATION": f"apikey {context.root.api_key}"}
resp = requests.post(
f"{context.root.baseurl}/graphql",
data=values,
files=files,
headers=headers,
)
if resp.status_code == 401:
print(f" Response: {resp.text}")
raise ValueError("Unable to authenticate to Sleuth")
elif resp.status_code != 200:
raise ValueError(f"Unexpected response: {resp.text}")

data = resp.json()
if data.get("errors"):
raise ValueError(f"Errors in response: {data['errors']}")

return resp.json()["data"]

0 comments on commit 0d4ce90

Please sign in to comment.