name: codegen

on:
  push:
    branches-ignore: 'create-pull-request/**'

  schedule:
    - cron: 00 4 * * *

  workflow_dispatch:

jobs:
  codegen:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - uses: dtolnay/rust-toolchain@nightly
        with:
          components: rustfmt, clippy

      - uses: Swatinem/rust-cache@v2

      - name: Install headers
        run: |
          set -euxo pipefail
          sudo apt -y update
          sudo apt -y install libelf-dev libc6-dev libc6-dev-{arm64,armel,loong64,riscv64,ppc64el,s390x,mips}-cross

      - run: cargo xtask codegen
      # aya-ebpf-bindings aren't emitted directly from bindgen and so aren't formatted.
      - run: cargo fmt --all
      - run: cargo xtask public-api --bless

      - id: libbpf
        working-directory: xtask/libbpf
        run: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"

      - uses: peter-evans/create-pull-request@v7
        with:
          branch: create-pull-request/codegen
          commit-message: |
            aya-obj, aya-ebpf-bindings: regenerate

            libbpf commit: ${{ steps.libbpf.outputs.sha }}
          title: 'aya-obj, aya-ebpf-bindings: regenerate'
          body: |
            **Automated changes**

            libbpf commit: ${{ steps.libbpf.outputs.sha}}
