name: Synchronize Project Metadata
on:
  push:
    branches-ignore:
      - "devel"
    paths:
      - "*" # Will trigger on new directories and changes to files in root of repository
      - ".gitea/workflows/sync_meta.yaml"
      - ".obs/common.py"

jobs:
  sync-prj-meta:
    runs-on: tumbleweed
    steps:
      - name: Setup OSC
        run: |
          zypper in -y python3-jinja2
          mkdir -p ~/.config/osc
          cat >~/.config/osc/oscrc <<'EOF'
          [general]
          apiurl = https://api.opensuse.org

          [https://api.opensuse.org]
          user=${{ vars.OBS_USERNAME }}
          pass=${{ secrets.OBS_PASSWORD }}
          EOF
    # Waiting on PR to get merged for support in upstream action/checkout action
      - uses: 'https://github.com/yangskyboxlabs/action-checkout@sha256'
        name: Checkout repository
        with:
          object-format: 'sha256'
      - name: "Update or create OBS Project"
        run: |
          PROJECT="$(grep PROJECT .obs/common.py | sed 's/PROJECT = "\(.*\)"/\1/')"
          set -o pipefail
          if meta="$(osc meta prj "${PROJECT}" 2>/dev/null | sha256sum)"; then
            new_meta="$(python3 .obs/render_meta.py)"
            if [ "${meta}" != "$(echo "${new_meta}" | sha256sum)" ]; then
              echo "${new_meta}" | osc meta prj "${PROJECT}" -F -
            fi
            python3 .obs/sync_packages.py
          else
            # Create the projects
            bash .obs/create_projects.sh
          fi
