-
Notifications
You must be signed in to change notification settings - Fork 224
Adding Query Validation Workflow #5514
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
Draft
ntanwar-sumo
wants to merge
34
commits into
main
Choose a base branch
from
doc_pr_validations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 7 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
6bcc8f2
Adding Query Validation Workflow
ntanwar-sumo 187b52f
Merge branch 'main' into doc_pr_validations
ntanwar-sumo e645bf9
Potential fix for code scanning alert no. 10: Workflow does not conta…
ntanwar-sumo fac336c
changes
ntanwar-sumo 088acb9
changes
ntanwar-sumo 0e8ea7e
changes
ntanwar-sumo 872b8d1
changes
ntanwar-sumo 71268b6
changes
ntanwar-sumo 6991012
changes
ntanwar-sumo ecd58ae
chnages-checking for incorrect query
ntanwar-sumo d2c9a2c
changes
ntanwar-sumo 994b81a
changes
ntanwar-sumo 2dc87df
changes
ntanwar-sumo 18fda2d
changes
ntanwar-sumo 318734d
Merge branch 'main' into doc_pr_validations
ntanwar-sumo 2b09da4
changes
ntanwar-sumo b9c4d0e
Merge branch 'doc_pr_validations' of github.com:SumoLogic/sumologic-d…
ntanwar-sumo 3b44020
changes
ntanwar-sumo db58bb8
Fix header update order in SumoLogicClient init
ntanwar-sumo f61e18b
changes
ntanwar-sumo 95e4d85
changes
ntanwar-sumo ae88c80
changes
ntanwar-sumo 52d50f1
chnages
ntanwar-sumo 62433df
chnage
ntanwar-sumo 1ddefbc
changes
ntanwar-sumo 886d6db
New changes
ntanwar-sumo 3eb3520
Updated changes
ntanwar-sumo 458f024
new changes
ntanwar-sumo e4b7a76
changes
ntanwar-sumo a92d5a5
Changes
ntanwar-sumo 09972fa
Add missing reducer functions to where operator documentation
ntanwar-sumo b631559
Fix validation script to only process changed files
ntanwar-sumo 362d343
Fix SQL validation to exclude Markdown table content
ntanwar-sumo 899c089
Add comprehensive debugging to identify file processing issue
ntanwar-sumo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: Validate SQL Queries | ||
on: | ||
pull_request: | ||
paths: | ||
- 'docs/**/*.md' # Only trigger when documentation changes | ||
- 'scripts/validate_*.py' # Or when validation scripts change | ||
|
||
jobs: | ||
validate-queries: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 # Required for git diff detection | ||
|
||
- name: Debug filesystem | ||
if: ${{ always() }} | ||
run: | | ||
echo "Workspace contents:" | ||
ls -R | ||
echo "Scripts directory:" | ||
ls -la scripts/ | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: "3.10" | ||
|
||
- name: Install dependencies | ||
run: pip install requests pyyaml | ||
|
||
- name: Validate queries | ||
working-directory: ./scripts | ||
env: | ||
SUMO_LOGIC_ACCESS_ID: ${{ secrets.SUMO_LOGIC_ACCESS_ID }} | ||
SUMO_LOGIC_ACCESS_KEY: ${{ secrets.SUMO_LOGIC_ACCESS_KEY }} | ||
run: | | ||
python validate_queries.py | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import os | ||
import requests | ||
from datetime import datetime, timedelta | ||
|
||
class SumoLogicClient: | ||
def __init__(self): | ||
self.base_url = "https://api.sumologic.com/api/v1" | ||
self.session = requests.Session() | ||
self.session.auth = ( | ||
os.getenv("SUMO_LOGIC_ACCESS_ID"), | ||
os.getenv("SUMO_LOGIC_ACCESS_KEY") | ||
) | ||
|
||
def test_query(self, query): | ||
"""Execute a query in Sumo Logic and check for results""" | ||
job_id = self._create_search_job(query) | ||
status = self._wait_for_job(job_id) | ||
return self._check_results(job_id) if status == "DONE GATHERING RESULTS" else False | ||
|
||
def _create_search_job(self, query): | ||
end_time = datetime.utcnow() | ||
start_time = end_time - timedelta(hours=24) | ||
payload = { | ||
"query": query, | ||
"from": start_time.isoformat() + "Z", | ||
"to": end_time.isoformat() + "Z", | ||
"timeZone": "UTC" | ||
} | ||
response = self.session.post(f"{self.base_url}/search/jobs", json=payload) | ||
response.raise_for_status() | ||
return response.json()["id"] | ||
|
||
def _wait_for_job(self, job_id, max_attempts=10): | ||
for _ in range(max_attempts): | ||
response = self.session.get(f"{self.base_url}/search/jobs/{job_id}") | ||
response.raise_for_status() | ||
status = response.json()["state"] | ||
if status in ["DONE GATHERING RESULTS", "CANCELLED"]: | ||
return status | ||
time.sleep(3) | ||
return "TIMEOUT" | ||
|
||
def _check_results(self, job_id): | ||
response = self.session.get(f"{self.base_url}/search/jobs/{job_id}/messages") | ||
response.raise_for_status() | ||
return len(response.json()["messages"]) > 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python3 | ||
import re | ||
import sys | ||
from pathlib import Path | ||
from sumologic_client import SumoLogicClient | ||
|
||
def find_sql_blocks_in_pr(): | ||
"""Detect changed SQL blocks without file modifications""" | ||
changed_files = sys.argv[1:] if len(sys.argv) > 1 else [ | ||
str(p) for p in Path("docs").rglob("*.md") | ||
if "search-query-language" in str(p) | ||
] | ||
|
||
sql_blocks = [] | ||
for file in changed_files: | ||
content = Path(file).read_text() | ||
sql_blocks.extend([ | ||
(file, sql.strip()) | ||
for sql in re.findall(r'```sql\n(.*?)```', content, re.DOTALL) | ||
]) | ||
return sql_blocks | ||
|
||
def validate_queries(): | ||
client = SumoLogicClient() | ||
failed = False | ||
|
||
for file, query in find_sql_blocks_in_pr(): | ||
print(f"Validating SQL in {file}...") | ||
try: | ||
if not client.test_query(query): | ||
print(f"::error file={file},title=Query Validation Failed::Query returned no results") | ||
failed = True | ||
except Exception as e: | ||
print(f"::error file={file},title=Query Execution Failed::{str(e)}") | ||
failed = True | ||
|
||
if failed: | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
validate_queries() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.