name: Renovate
on:
  schedule:
    # Offset by 12 minutes to avoid busy times on the hour
    - cron: 12 */4 * * *

  pull_request:
    paths:
      - .github/renovate-config.json5
      - .github/workflows/renovate.yml
    types:
      - edited
      - opened
      - ready_for_review
      - synchronize

  push:
    branches:
      - main
    paths:
      - .github/renovate-config.json5
      - .github/workflows/renovate.yml

  workflow_dispatch:
    inputs:
      dry-run:
        description: "Run Renovate in dry-run mode"
        required: false
        default: false
        type: boolean

jobs:
  renovate:
    permissions:
      contents: read
      id-token: write
    runs-on: ubuntu-24.04
    timeout-minutes: 5

    steps:
      - name: Checkout Code
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false
          sparse-checkout: |
            .github/renovate-config.json5

      - name: Retrieve renovate secrets
        id: get-secrets
        uses: grafana/shared-workflows/actions/get-vault-secrets@a37de51f3d713a30a9e4b21bcdfbd38170020593 # get-vault-secrets/v1.3.0
        with:
          common_secrets: |
            GRAFANA_RENOVATE_APP_ID=grafana-renovate-app:app-id
            GRAFANA_RENOVATE_PRIVATE_KEY=grafana-renovate-app:private-key

      - name: Generate token
        id: generate-token
        uses: actions/create-github-app-token@a8d616148505b5069dccd32f177bb87d7f39123b # v2.1.1
        with:
          app-id: ${{ env.GRAFANA_RENOVATE_APP_ID }}
          private-key: ${{ env.GRAFANA_RENOVATE_PRIVATE_KEY }}

      - name: Self-hosted Renovate
        uses: renovatebot/github-action@6927a58a017ee9ac468a34a5b0d2a9a9bd45cac3 # v43.0.11
        with:
          configurationFile: .github/renovate-config.json5
          # renovate: datasource=docker depName=ghcr.io/renovatebot/renovate
          renovate-version: 39.42.4@sha256:c5d718e312cdacc0746e37f13c215ff498be28c51e50efd24c070ae29f5b636a
          token: ${{ steps.generate-token.outputs.token }}
        env:
          LOG_LEVEL: ${{ github.event_name == 'pull_request' && 'debug' || 'info' }}
          # For pull requests, this means we'll get the dependencies of the PR's
          # branch, so you can fix/change things and see the results in the PR's
          # run. By default, Renovate will clone the main/default branch.
          RENOVATE_BASE_BRANCHES: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || null }}
          # Dry run if the event is pull_request, or workflow_dispatch AND the dry-run input is true
          RENOVATE_DRY_RUN: ${{ (github.event_name == 'pull_request' || (github.event_name == 'workflow_dispatch' && github.event.inputs.dry-run == 'true')) && 'full' || null }}
          RENOVATE_PLATFORM: github
          RENOVATE_REPOSITORIES: ${{ github.repository }}
          RENOVATE_USERNAME: GrafanaRenovateBot
