# Copyright 2025 The Forgejo Authors
# SPDX-License-Identifier: MIT
#
# FORGEJO_CASCADING_PR_ORIGIN_TOKEN is a token from the https://code.forgejo.org/cascading-pr user
# with scope write:issue read:repository read:user
# FORGEJO_CASCADING_PR_DESTINATION_TOKEN is a token from the https://codeberg.org/forgejo-cascading-pr user
# with scope write:issue write:repository read:user
#
# To modify this workflow:
#
#  - push it to the wip-cascade branch on the repository
#    otherwise it will not have access to the secrets required to push
#    the cascading PR
#
#  - once it works, open a pull request for the sake of keeping track
#    of the change even if the PR won't run it because it will use
#    whatever is in the default branch instead
#
#  - after it is merged, double check it works by setting the
#    label on a pull request (any pull request will do)
#
name: cascade

on:
  push:
    branches:
      - 'main'
      - 'wip-cascade'
  pull_request_target:
    types:
      - synchronize
      - labeled
      - closed

enable-email-notifications: true

jobs:
  debug:
    if: >
      vars.DEBUG == 'yes'
    runs-on: docker
    container:
      image: data.forgejo.org/oci/node:22-bookworm
    steps:
      - name: event
        run: |
          cat <<'EOF'
          ${{ toJSON(forge.event.pull_request.labels.*.name) }}
          EOF
          cat <<'EOF'
          push => ${{ forge.event_name == 'push' && ( forge.ref_name == 'main' || forge.ref_name == 'wip-cascade') }}
          pull_request_target synchornized => ${{ ( forge.event.action == 'synchronized' && contains(forge.event.pull_request.labels.*.name, 'run-forgejo-tests') ) }}
          pull_request_target label_updated => ${{ ( forge.event.action == 'label_updated' && forge.event.label.name == 'run-forgejo-tests' ) }}
          contains => ${{ contains(forge.event.pull_request.labels.*.name, 'run-forgejo-tests') }}
          contains boolean => ${{ contains(forge.event.pull_request.labels.*.name, 'run-forgejo-tests') == true }}
          EOF
          cat <<'EOF'
          ${{ toJSON(forge) }}
          EOF

  forgejo:
    #
    # Always run when a commit is pushed to the main or wip-cascade branch
    # If this is a pull request, run
    #   - when the `run-forgejo-tests` label is set (label_updated) (but not if another label is set or if a label is removed)
    #   - when a new commit is pushed to the pull request (synchronized) if the `run-forgejo-tests` is already present
    #   - when the pull request is closed, which also happens when it is merged, so that the Forgejo pull request is closed
    #
    if: >
      vars.ROLE == 'forgejo-coding' && (
        (
          forge.event_name == 'push' && ( forge.ref_name == 'main' || forge.ref_name == 'wip-cascade')
        ) || (
          forge.event_name == 'pull_request_target' && (
            forge.event.action == 'closed' ||
            ( forge.event.action == 'synchronized' && contains(forge.event.pull_request.labels.*.name, 'run-forgejo-tests') ) ||
            ( forge.event.action == 'label_updated' && forge.event.label.name == 'run-forgejo-tests' )
          )
        )
      )

    runs-on: docker
    container:
      image: data.forgejo.org/oci/node:22-bookworm
    steps:
      - uses: https://data.forgejo.org/actions/checkout@v4
        with:
          fetch-depth: '0'
          show-progress: 'false'
      - uses: https://data.forgejo.org/actions/setup-go@v5
        with:
          go-version-file: go.mod
      - uses: https://data.forgejo.org/actions/cascading-pr@v2.3.0
        with:
          origin-url: ${{ forge.server_url }}
          origin-repo: ${{ forge.repository }}
          origin-token: ${{ secrets.FORGEJO_CASCADING_PR_ORIGIN_TOKEN }}
          origin-pr: ${{ forge.event.pull_request.number }}
          origin-ref: ${{ forge.event_name == 'push' && forge.event.ref || '' }}
          destination-url: https://codeberg.org
          destination-fork-repo: forgejo-cascading-pr/forgejo
          destination-repo: forgejo/forgejo
          destination-branch: forgejo
          destination-token: ${{ secrets.FORGEJO_CASCADING_PR_DESTINATION_TOKEN }}
          prefix: runner
          close: true
          verbose: ${{ vars.VERBOSE == 'yes' }}
          debug: ${{ vars.DEBUG == 'yes' }}
          wait-iteration: 120
          update: .forgejo/cascading-forgejo
