name: Kernel Update

on:
  workflow_dispatch:
    inputs:
      tag:
        description: 'Tag of the Linux Kernel to update to'
        required: true
      version:
        description: 'Custom version number'
        required: false
      release:
        description: 'Custom release number'
        required: false

jobs:
  update:
    name: Update
    runs-on: [ubuntu-latest]
    steps:
    - name: Checkout Sources
      uses: actions/checkout@v3
      with:
          submodules: recursive
    - name: Setup System Dependencies
      run: sudo apt update && sudo apt install devscripts
    - name: Update Kernel
      id: update
      run: |
        TAG=${{ github.event.inputs.tag }}
        VERSION=${{ github.event.inputs.version }}
        RELEASE=${{ github.event.inputs.release }}
        VERSION_OPT=${VERSION:+-v ${VERSION}}
        RELEASE_OPT=${RELEASE:+-r ${RELEASE}}
        ./scripts/update.sh -t ${{ github.event.inputs.tag }} $VERSION_OPT $RELEASE_OPT
        echo "version=$(scripts/version.sh -L)" >> $GITHUB_OUTPUT
        echo "full=$(scripts/version.sh)" >> $GITHUB_OUTPUT
        changelog=$(dpkg-parsechangelog -c 1 -l debian/changelog)
        echo "changelog<<EOF\n$changelog\nEOF" >> $GITHUB_OUTPUT
        echo "branch=${GITHUB_REF##*/}" >> $GITHUB_OUTPUT
    - name: Create Pull Request
      uses: peter-evans/create-pull-request@v4
      with:
        token: ${{ secrets.PAT }} # Custom token needed to recursively trigger workflows
        author: Fabian Mastenbroek <mail.fabianm@gmail.com>
        commit-message: |
          Add Linux ${{ steps.update.outputs.version }}

          This change updates the kernel to Linux ${{ steps.update.outputs.version }}.
        branch: staging/v${{ steps.update.outputs.full }}
        branch-suffix: short-commit-hash
        delete-branch: true
        title: "Add Linux ${{ steps.update.outputs.version }}"
        body: |
          Automated pull request to update the kernel to Linux ${{ steps.update.outputs.version }}.

          **Changelog:**
          ```
          ${{ steps.update.outputs.changelog }}
          ```
        labels: |
          release