# This test runs against packaging PRs and verifies that
# patches apply and unit tests pass.
#
# TODO: add full build-package / sbuild test
name: "Packaging (downstream branch) - patches apply cleanly unit tests pass"

on:
  pull_request:
    branches:
      - 'ubuntu/**'

concurrency:
  group: 'ci-${{ github.workflow }}-${{ github.ref }}'
  cancel-in-progress: true

defaults:
  run:
    shell: sh -ex {0}

jobs:
  patch-conflicts-upstream:
    runs-on: ubuntu-24.04
    name: Check patches apply cleanly and unit tests pass
    steps:

      - name: Setup - checkout branch
        uses: actions/checkout@v4

      - name: Setup - install dependencies
        run: |
          # This stage is slow, and unnecessecary when no series exists, so
          # only run it if necessary to save some cycles.
          # Github Actions doesn't appear to have a simple mechanism for
          # early exit without failure
          if [ ! -f debian/patches/series ]; then
            echo "no patches, skipping"
            exit 0
          fi
          sudo DEBIAN_FRONTEND=noninteractive apt-get update
          sudo DEBIAN_FRONTEND=noninteractive apt-get -y install tox quilt

      - name: Setup - Configure quilt
        run: |
          # Github Actions doesn't appear to have a simple mechanism for
          # early exit without failure
          if [ ! -f debian/patches/series ]; then
            echo "no patches, skipping"
            exit 0
          fi
          # The quilt default setting is --fuzz=2, but debian packaging has
          # stricter requirements
          sudo sed -i 's/QUILT_PUSH_ARGS=.*$/QUILT_PUSH_ARGS="--fuzz=0"/g' /etc/quilt.quiltrc
          # Standardize patches to use this format. Sorted patches reduce patch size.
          sudo sed -i 's/QUILT_REFRESH_ARGS=.*$/QUILT_REFRESH_ARGS="-p ab --no-timestamps --no-index --sort"/g' /etc/quilt.quiltrc
          # quilt defaults to QUILT_PATCHES=patches, but debian uses debian/patches
          sudo sed -i 's|.*QUILT_PATCHES=.*$|QUILT_PATCHES=debian/patches|g' /etc/quilt.quiltrc

      - name: Run test - apply patches and run unit tests
        run: |
          # Github Actions doesn't appear to have a simple mechanism for
          # early exit without failure
          if [ ! -f debian/patches/series ]; then
            echo "no patches, skipping"
            exit 0
          fi
          quilt push -a
          tox -e py3
          quilt pop -a --refresh
      - name: Enforce sorted patches
        run: |
          # Github Actions doesn't appear to have a simple mechanism for
          # early exit without failure
          if [ ! -f debian/patches/series ]; then
            echo "no patches, skipping"
            exit 0
          fi
          # check for any changes from the refresh above
          if [ -n "$(git diff)" ]; then
            # if patches were refreshed then they weren't sorted
            exit 1
          fi
