diff --git a/.github/workflows/preview_imports.yml b/.github/workflows/preview_imports.yml new file mode 100644 index 000000000..ee805e855 --- /dev/null +++ b/.github/workflows/preview_imports.yml @@ -0,0 +1,55 @@ +name: Verify preview imports only preview + +on: + pull_request: + types: + - opened + - reopened + - synchronize + - ready_for_review + paths: + - "haystack/preview/**.py" + +jobs: + verify-imports: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + # With the default value of 1, there are corner cases where tj-actions/changed-files + # fails with a `no merge base` error + fetch-depth: 0 + + - name: Get changed files + id: files + uses: tj-actions/changed-files@v39 + with: + files: | + haystack/preview/**.py + + - name: Check imports + shell: python + run: | + import re + regex = r"^(from haystack|import haystack)(?!\.preview| import preview)(.*)" + + changed_files = "${{ steps.files.outputs.all_changed_files }}".split() + matches = {} + for path in changed_files: + with open(path, "r") as f: + file_matches = [] + for line in f.readlines(): + file_matches.extend(re.finditer(regex, line.strip())) + if file_matches: + matches[path] = file_matches + + for path, match in matches.items(): + print(f"Bad imports in file '{path}'") + for m in match: + print(m.group()) + print() + + if matches: + print("::error:: Imports in haystack.preview can only import from haystack.preview") + import sys; sys.exit(1)