Skip to content

Commit

Permalink
Moving GitHub PR to gh CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
pgoslatara committed Aug 15, 2024
1 parent 7d510a4 commit 0898770
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 168 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export DBT_PROFILES_DIR=dbt_project
export DBT_PROJECT_DIR=dbt_project
export GITHUB_TOKEN=<TOKEN> # Only required if you are developing the function that posts comments on GitHub PRs, https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token
3 changes: 0 additions & 3 deletions .github/workflows/ci_pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ jobs:
- name: Run pytest (integration tests)
run: make test-integration

- name: Run pytest (github tests)
run: make test-github

e2e-tests:
needs: [pre-commit]
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ cython_debug/
changed_files.txt
coverage.json
dbt.duckdb
github-comment.md
pytest-coverage.txt

dbt_project/dbt_packages
Expand Down
10 changes: 9 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ runs:
ghcr.io/godatadriven/dbt-bouncer:v0.0.0 \
--config-file /app/${{ inputs.config-file }} \
${{ steps.assemble-output-file-param.outputs.output-file-param }} \
--send-pr-comment ${{ inputs.send-pr-comment }}
--create-pr-comment-file ${{ inputs.send-pr-comment }}
- name: Send PR comment
if: ${{ inputs.send-pr-comment }} == 'true'
shell: bash
run: >
gh pr comment ${{ github.pull_request.number }} \
--repo ${{ github.repository }} \
--body-file /app/github-comment.md
9 changes: 0 additions & 9 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,3 @@ test-unit:
--cov=src/dbt_bouncer/ \
--numprocesses 5 \
./tests/unit

test-github:
poetry run pytest \
-c ./tests \
--junitxml=coverage.xml \
--cov-report=term-missing:skip-covered \
--cov=src/dbt_bouncer/ \
--numprocesses 5 \
./tests/github
101 changes: 0 additions & 101 deletions src/dbt_bouncer/github.py

This file was deleted.

22 changes: 13 additions & 9 deletions src/dbt_bouncer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@
required=False,
type=Path,
)
@click.option(
"--create-pr-comment-file",
default=False,
help="Create a `github-comment.md` file that will be sent to GitHub as a PR comment. Defaults to True when run as a GitHub Action.",
required=False,
type=click.BOOL,
)
@click.option(
"--output-file",
default=None,
help="Location of the json file where check metadata will be saved.",
required=False,
type=Path,
)
@click.option(
"--send-pr-comment",
default=False,
help="Send a comment to the GitHub PR with a list of failed checks. Defaults to True when run as a GitHub Action.",
required=False,
type=click.BOOL,
)
@click.version_option()
def cli(config_file: Path, output_file: Union[None, Path], send_pr_comment: bool):
def cli(
config_file: Path,
create_pr_comment_file: bool,
output_file: Union[None, Path],
):
logger.info(f"Running dbt-bouncer ({version()})...")

# Validate output file has `.json` extension
Expand Down Expand Up @@ -168,13 +172,13 @@ def cli(config_file: Path, output_file: Union[None, Path], send_pr_comment: bool
bouncer_config=config,
catalog_nodes=project_catalog_nodes,
catalog_sources=project_catalog_sources,
create_pr_comment_file=create_pr_comment_file,
exposures=project_exposures,
macros=project_macros,
manifest_obj=manifest_obj,
models=project_models,
output_file=output_file,
run_results=project_run_results,
send_pr_comment=send_pr_comment,
sources=project_sources,
tests=project_tests,
)
Expand Down
8 changes: 4 additions & 4 deletions src/dbt_bouncer/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@
import pytest
from tabulate import tabulate

from dbt_bouncer.github import send_github_comment_failed_checks
from dbt_bouncer.logger import logger
from dbt_bouncer.runner_plugins import (
FixturePlugin,
GenerateTestsPlugin,
ResultsCollector,
)
from dbt_bouncer.utils import create_github_comment_file


def runner(
bouncer_config: Dict[str, List[Dict[str, str]]],
catalog_nodes: List[Dict[str, str]],
catalog_sources: List[Dict[str, str]],
create_pr_comment_file: bool,
exposures: List[Dict[str, str]],
macros: List[Dict[str, str]],
manifest_obj: Dict[str, str],
models: List[Dict[str, str]],
output_file: Union[None, Path],
run_results: List[Dict[str, str]],
send_pr_comment: bool,
sources: List[Dict[str, str]],
tests: List[Dict[str, str]],
checks_dir: Optional[Union[None, Path]] = Path(__file__).parent / "checks",
Expand Down Expand Up @@ -95,8 +95,8 @@ def runner(
)
)

if send_pr_comment:
send_github_comment_failed_checks(failed_checks=failed_checks)
if create_pr_comment_file:
create_github_comment_file(failed_checks=failed_checks)

if output_file is not None:
coverage_file = Path().cwd() / output_file
Expand Down
23 changes: 23 additions & 0 deletions src/dbt_bouncer/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import contextlib
import json
import os
import re
from pathlib import Path
from typing import List, Literal
Expand All @@ -11,6 +12,28 @@
from dbt_bouncer.logger import logger


def create_github_comment_file(failed_checks: List[List[str]]) -> None:
"""
Create a markdown file containing a comment for GitHub.
"""

md_formatted_comment = make_markdown_table(
[["Check name", "Failure message"]] + list(sorted(failed_checks))
)

md_formatted_comment = f"## **Failed `dbt-bouncer`** checks\n\n{md_formatted_comment}\n\nSent from this [GitHub Action workflow run](https://github.com/{os.environ['GITHUB_REPOSITORY']}/actions/runs/{os.environ['GITHUB_RUN_ID']})." # Would like to be more specific and include the job ID, but it's not exposed as an environment variable: https://github.com/actions/runner/issues/324

logger.debug(f"{md_formatted_comment=}")

if os.environ.get("CI", None):
comment_file = "/app/github-comment.md"
else:
comment_file = "github-comment.md"

with open(comment_file, "w") as f:
f.write(md_formatted_comment)


def find_missing_meta_keys(meta_config, required_keys) -> List[str]:
"""
Find missing keys in a meta config.
Expand Down
34 changes: 0 additions & 34 deletions tests/github/test_github.py

This file was deleted.

6 changes: 3 additions & 3 deletions tests/unit/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_runner_coverage(caplog, tmp_path):
},
catalog_nodes=[],
catalog_sources=[],
create_pr_comment_file=False,
exposures=[],
macros=[],
manifest_obj=parse_manifest(
Expand Down Expand Up @@ -49,7 +50,6 @@ def test_runner_coverage(caplog, tmp_path):
],
output_file=tmp_path / "coverage.json",
run_results=[],
send_pr_comment=False,
sources=[],
tests=[],
checks_dir=Path("./src/dbt_bouncer/checks"),
Expand All @@ -73,6 +73,7 @@ def test_runner_failure():
},
catalog_nodes=[],
catalog_sources=[],
create_pr_comment_file=False,
exposures=[],
macros=[],
manifest_obj=parse_manifest(
Expand Down Expand Up @@ -107,7 +108,6 @@ def test_runner_failure():
],
output_file=None,
run_results=[],
send_pr_comment=False,
sources=[],
tests=[],
checks_dir=Path("./src/dbt_bouncer/checks"),
Expand All @@ -124,6 +124,7 @@ def test_runner_success():
},
catalog_nodes=[],
catalog_sources=[],
create_pr_comment_file=False,
exposures=[],
macros=[],
manifest_obj=parse_manifest(
Expand Down Expand Up @@ -158,7 +159,6 @@ def test_runner_success():
],
output_file=None,
run_results=[],
send_pr_comment=False,
sources=[],
tests=[],
checks_dir=Path("./src/dbt_bouncer/checks"),
Expand Down
Loading

0 comments on commit 0898770

Please sign in to comment.