Skip to content

Commit

Permalink
Diff workflow test
Browse files Browse the repository at this point in the history
  • Loading branch information
mephenor committed May 7, 2024
1 parent a9d3538 commit d363bb8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 52 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/monorepo_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,38 @@ name: Monorepo Tests
on: push

jobs:
check-changes:
get-changed-services:
runs-on: ubuntu-latest
outputs:
services: ${{ steps.services-changed.outputs.affected }}

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
filter: tree:0
fetch-depth: 0

- name: Changed Files
id: changed-files
uses: tj-actions/[email protected]

- name: Install Typer to check changed services
id: install-typer
run: pip install typer>=0.9.0

- name: Generate list of changed services
id: services-changed
run: |
echo "affected=$(python3 ./scripts/get_affected_services.py --full)" >> $GITHUB_OUTPUT
echo "affected=$(python3 ./scripts/get_affected_services.py --changed-files ${{ steps.changed-files.outputs.all_changed_files }} )" >> $GITHUB_OUTPUT
test:
if: ${{needs.check-changes.outputs.services}} != "" && ${{needs.check-changes.outputs.services}} != "[]"
needs: check-changes
needs: get-changed-services
if: ${{needs.get-changed-services.outputs.services}} != ""
runs-on: ubuntu-latest
strategy:
matrix:
service: ${{ fromJson(needs.check-changes.outputs.services) }}
service: ${{ fromJson(needs.get-changed-services.outputs.services) }}
fail-fast: false
env:
IFRS_CONFIG_YAML: ./services/ifrs/dev_config.yaml
Expand Down
52 changes: 5 additions & 47 deletions scripts/get_affected_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

"""Determine which services are impacted by current changes."""

import subprocess
import json
from pathlib import Path

import typer
Expand Down Expand Up @@ -65,65 +65,23 @@ def get_top_level_changes(files: list[str]) -> list[str]:
return [file for file in files if not file.startswith("services/")]


def files_in_diff(full: bool) -> list[str]:
"""List files in diff."""
# Command to list names of changed files
if full:
base = "origin/main"
else:
base = "HEAD~1"
change_range = f"{base}...HEAD"
command = f"git diff --name-only {change_range}"

# Execute the command and capture the output
result = subprocess.run(
command, shell=True, text=True, capture_output=True, check=True
)

# The stdout attribute contains the command's output
changed_files = result.stdout.strip().split("\n")

# Print each changed file
return changed_files


def on_main_branch() -> bool:
"""Pointless to execute on main branch, so check for current branch name."""
git_check = "git branch --show-current"
output = subprocess.run(
git_check, shell=True, text=True, capture_output=True, check=True
)
branch_name = output.stdout.strip()
return branch_name == "main"


def main(
*,
full: bool = typer.Option(
False,
help="If set, runs for all changes in branch. Otherwise runs for current commit.",
),
):
def main(*, changed_files: list[str] = typer.Option(..., help="")):
"""Determine if changes require running CI checks for all or a subset of services.
Output is a comma-delimited string of affected services.
"""
if on_main_branch():
return

files = files_in_diff(full)

# In practice, changes should affect either one or all services, but that is not
# assumed to always hold true.
modified_services = get_modified_services(files)
non_service_changes = get_top_level_changes(files)
modified_services = get_modified_services(changed_files)
non_service_changes = get_top_level_changes(changed_files)

affected_services = (
[path.name for path in list_service_dirs()]
if must_run_all(non_service_changes)
else modified_services
)
print(",".join(affected_services))
print(json.dumps(affected_services))


if __name__ == "__main__":
Expand Down

0 comments on commit d363bb8

Please sign in to comment.