# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

{%- macro github_header() -%}
# NOTE: must set "Crossbow" as name to have the badge links working in the
# github comment reports!
name: Crossbow
on:
  push:
    branches:
      - "*-github-*"

env:
  ARCHERY_DEBUG: 1
{% endmacro %}

{%- macro github_checkout_arrow(fetch_depth=1, submodules="recursive", action_v="4") -%}
  - name: Checkout Arrow
    uses: actions/checkout@v{{ action_v }}
    with:
      fetch-depth: {{ fetch_depth }}
      path: arrow
      repository: {{ arrow.github_repo }}
      ref: {{ arrow.head }}
      submodules: {{ submodules }}
{% endmacro %}

{%- macro github_login_dockerhub() -%}
  - name: Login to Dockerhub
    uses: docker/login-action@v2
    with:
      username: {{ '${{ secrets.DOCKERHUB_USER }}' }}
      password: {{ '${{ secrets.DOCKERHUB_TOKEN }}' }}
{% endmacro %}

{%- macro github_login_ghcr() -%}
  - name: Login to GitHub Container Registry
    uses: docker/login-action@v2
    with:
      registry: ghcr.io
      username: {{ '${{ github.actor }}' }}
      password: {{ '${{ secrets.GITHUB_TOKEN }}' }}
{% endmacro %}

{%- macro github_install_archery() -%}
  - name: Set up Python
    uses: actions/setup-python@v4
    with:
      cache: 'pip'
      python-version: 3.12
  - name: Install Archery
    shell: bash
    run: pip install -e arrow/dev/archery[all]
{% endmacro %}

{%- macro github_free_space() -%}
  - name: Free up disk space
    if: runner.os == 'Linux' && runner.arch == 'X64'
    shell: bash
    run: |
      arrow/ci/scripts/util_free_space.sh
{% endmacro %}

{%- macro github_upload_releases(pattern) -%}
  - name: Set up Python
    uses: actions/setup-python@v4
    with:
      python-version: 3.12
  - name: Checkout Crossbow
    uses: actions/checkout@v4
    with:
      path: crossbow
      ref: {{ job.branch }}
  - name: Setup Crossbow
    shell: bash
    run: |
      python3 -m pip install -e arrow/dev/archery[crossbow]
      echo "$HOME/.local/bin" >> $GITHUB_PATH
  - name: Upload artifacts
    shell: bash
    run: |
      archery crossbow \
        --queue-path $(pwd)/crossbow \
        --queue-remote {{ queue_remote_url }} \
        upload-artifacts \
        --sha {{ task.branch }} \
        --tag {{ task.tag }} \
      {% if pattern is string %}
        "{{ pattern }}"
      {% elif pattern is iterable %}
        {% for p in pattern %}
        "{{ p }}" {{ "\\" if not loop.last else "" }}
        {% endfor %}
      {% endif %}
    env:
      CROSSBOW_GITHUB_TOKEN: {{ '${{ secrets.CROSSBOW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}' }}
  - name: Verify uploaded artifacts
    shell: bash
    run: |
      archery crossbow \
        --queue-path $(pwd)/crossbow \
        --queue-remote {{ queue_remote_url }} \
        status \
        --task-filter '{{ task.name }}' \
        --no-fetch \
        --validate \
        {{ job.branch }}
    env:
      CROSSBOW_GITHUB_TOKEN: {{ '${{ secrets.CROSSBOW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}' }}
{% endmacro %}

{%- macro github_upload_wheel_scientific_python(pattern) -%}
  {%- if arrow.is_default_branch() -%}
  - name: Upload wheel to Anaconda scientific-python
    shell: bash
    run: |
      # check if completion actually expands to wheel files
      # to prevent empty dirs from failing silently
      if ! compgen -G "{{ pattern }}" > /dev/null; then
          echo "No wheel files found!"
          exit 1
      fi
      python3 -m pip install git+https://github.com/Anaconda-Platform/anaconda-client.git@1.12.3
      anaconda -t ${CROSSBOW_SCIENTIFIC_PYTHON_UPLOAD_TOKEN} upload --force -u scientific-python-nightly-wheels --label main {{ pattern }}
    env:
      CROSSBOW_SCIENTIFIC_PYTHON_UPLOAD_TOKEN: {{ '${{ secrets.CROSSBOW_SCIENTIFIC_PYTHON_UPLOAD_TOKEN }}' }}
  {% endif %}
{% endmacro %}

{%- macro configure_homebrew_arrow(formula) -%}
  - name: Configure Homebrew formula for testing
    env:
      ARROW_FORMULA: ./arrow/dev/tasks/homebrew-formulae/{{ formula }}
    run: |
      brew update || echo "brew update did not finish successfully"
      brew --version
      brew unlink python@2 || true
      brew config
      brew doctor || true
      # The GHA runners install of python > 3.10 is incompatible with brew so we
      # have to force overwriting of the symlinks
      # see https://github.com/actions/runner-images/issues/6868
      brew install --overwrite python@3.12 python@3.11 python@3.10

      set -x
      ARROW_GLIB_FORMULA=$(echo ${ARROW_FORMULA} | sed -e 's/\.rb/-glib.rb/')
      echo "ARROW_GLIB_FORMULA=${ARROW_GLIB_FORMULA}" >> ${GITHUB_ENV}
      for formula in ${ARROW_FORMULA} ${ARROW_GLIB_FORMULA}; do
        if [ ! -f ${formula} ]; then
          continue
        fi
        # Pin the current commit in the formula to test so that
        # we're not always pulling from the tip of the default branch
        sed -i '' -E \
          -e 's@https://github.com/apache/arrow.git", branch: "main"$@{{ arrow.remote }}.git", revision: "{{ arrow.head }}"@' \
          ${formula}
        # Sometimes crossbow gives a remote URL with .git and sometimes not.
        # Make sure there's only one
        sed -i '' -E -e 's@.git.git@.git@' ${formula}
        cat ${formula}
        cp ${formula} $(brew --repository homebrew/core)/Formula/
      done
{% endmacro %}

{%- macro github_change_r_pkg_version(is_fork, version) -%}
  - name: Modify version
    shell: bash
    run: |
      cd arrow/r
      sed -i.bak -E -e \
        's/(^Version: )([0-9]+\.[0-9]+\.[0-9]+).*$/\1{{ version }}/' \
        DESCRIPTION
      head DESCRIPTION
      rm -f DESCRIPTION.bak
{% endmacro %}

{%- macro github_test_r_src_pkg() -%}
  source("https://raw.githubusercontent.com/apache/arrow/HEAD/ci/etc/rprofile")

  # always remove arrow (mainly for self-hosted runners)
  try(remove.packages("arrow"), silent = TRUE)

  install.packages(
    "arrow",
    repos = c(getOption("arrow.dev_repo"), getOption("repos")),
    verbose = TRUE,
    type = "source",
    INSTALL_opts = "--build"
  )

  library(arrow)
  read_parquet(system.file("v0.7.1.parquet", package = "arrow"))
  print(arrow_info())

  # Our Version should always be > CRAN so we would detect a CRAN version here.
  stopifnot(packageVersion("arrow") == {{ '"${{needs.source.outputs.pkg_version}}"' }})
{% endmacro %}

{%- macro github_setup_local_r_repo(get_nix, get_win, get_mac=False) -%}
# TODO: improve arg handling
  - name: Setup local repo
    shell: bash
    run: mkdir repo
  {% if get_win %}
  - name: Get windows binary
    uses: actions/download-artifact@v4
    with:
      name: r-libarrow-windows-x86_64
      path: repo/libarrow
  {% endif %}
  {% if get_nix %}
  - name: Get Linux binary
    uses: actions/download-artifact@v4
    with:
      name: r-libarrow-linux-x86_64
      path: repo/libarrow
  {% endif %}
  {% if get_mac %}
    {% for arch in ["x86_64", "arm64"] %}
  - name: Get macOS {{ arch }} binary
    uses: actions/download-artifact@v4
    with:
      name: r-libarrow-darwin-{{ arch }}
      path: repo/libarrow
    {% endfor %}
  {% endif %}
  - name: Get src pkg
    uses: actions/download-artifact@v4
    with:
      name: r-pkg__src__contrib
      path: repo/src/contrib
  - name: Update repo index
    shell: Rscript {0}
    run: |
      # getwd() is necessary as this macro is used within jobs using a docker container
      tools::write_PACKAGES(file.path(getwd(), "repo/src/contrib", fsep = "/"), type = "source", verbose = TRUE)
  - name: Show repo
    shell: bash
    # tree not available in git-bash on windows
    run: |
      ls -R repo
  - name: Add repos to .Rprofile
    shell: Rscript {0}
    run: |
      profile_path <- file.path(getwd(), ".Rprofile")
      repo <- paste0("file://", getwd(), "/repo")
      str <- paste0("options(arrow.repo = '", repo, "/libarrow/' )")
      print(str)
      write(str, file = profile_path, append = TRUE)
      str <- paste0("options(arrow.dev_repo = '", repo, "' )")
      print(str)
      write(str, file = profile_path, append = TRUE)
      # Set envvar for later steps by appending to $GITHUB_ENV
      write(paste0("R_PROFILE_USER=", profile_path), file = Sys.getenv("GITHUB_ENV"), append = TRUE)
{% endmacro %}

{# Detect if we are using a fork or the upstream repo #}
  {% set is_upstream_b = arrow.github_repo == 'apache/arrow' %}
  {# use filter to cast to string and convert to lowercase to match yaml boolean #}
  {% set is_fork = (not is_upstream_b)|lower %}

{% set r_release = {"ver": "4.2", "rt" : "42"} %}
{% set r_oldrel = {"ver": "4.1", "rt" : "40"} %}

{%- macro github_set_env(env) -%}
  {% if env is defined %}
    env:
    {% for key, value in env.items() %}
      {{ key }}: "{{ value }}"
    {% endfor %}
  {% endif %}
{%- endmacro -%}

{% macro github_set_sccache_envvars(sccache_key_prefix = "sccache") %}
  {% set sccache_vars =  {
            "AWS_ACCESS_KEY_ID": '${{ secrets.AWS_ACCESS_KEY_ID }}',
            "AWS_SECRET_ACCESS_KEY": '${{ secrets.AWS_SECRET_ACCESS_KEY }}',
            "SCCACHE_BUCKET": '${{ secrets.SCCACHE_BUCKET }}',
            "SCCACHE_REGION": '${{ secrets.SCCACHE_REGION }}',
            "SCCACHE_S3_KEY_PREFIX": sccache_key_prefix
          }
 %}
  {% for key, value in sccache_vars.items() %}
  {{ key }}: "{{ value }}"
  {% endfor %}
{% endmacro %}
