---
name: Update Nix flake.lock
on:
  workflow_dispatch:
  schedule:
    - cron: "10 4 * * 1"

jobs:
  lockfile:
    name: Update Nix lockfile
    if: github.repository_owner == 'akvorado'
    runs-on: ubuntu-latest
    strategy:
      matrix:
        source:
          - nixpkgs
          - asn2org
          - iana-assignments
    steps:
      - uses: actions/checkout@v6
        with:
          persist-credentials: false
      - uses: cachix/install-nix-action@v31
      - name: Check current IANA assignments
        if: matrix.source == 'iana-assignments'
        run: |
          nix build .\#ianaServiceNames
          make orchestrator/clickhouse/data/tcp.csv orchestrator/clickhouse/data/udp.csv SERVICES_URL=result
          mkdir before~
          mv orchestrator/clickhouse/data/tcp.csv orchestrator/clickhouse/data/udp.csv before~
      - name: Update Nix lockfile
        run: nix flake update ${{ matrix.source }}
      - name: Check new IANA assignments
        if: matrix.source == 'iana-assignments'
        run: |
          nix build .\#ianaServiceNames
          make orchestrator/clickhouse/data/tcp.csv orchestrator/clickhouse/data/udp.csv SERVICES_URL=result
          mkdir after~
          mv orchestrator/clickhouse/data/tcp.csv orchestrator/clickhouse/data/udp.csv after~
          ! diff -u before~ after~ || {
            echo No diff...
            git reset --hard
          }
      - name: Create pull request
        uses: peter-evans/create-pull-request@v7
        with:
          add-paths: flake.lock
          branch: nix/flake-lock-update-${{ matrix.source }}
          labels: dependencies
          commit-message: "build: flake.lock automated update for ${{ matrix.source }}"
          title: "build: ${{ matrix.source }} update"
          body: ""
