{{- if and (ne .host.distro.family "windows") (or (and (stat (joinPath .host.home ".config" "age" "chezmoi.txt")) (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "GITHUB_RUNNER_TOKEN"))) (env "GITHUB_RUNNER_TOKEN")) -}}
#!/usr/bin/env bash
# @file GitHub Runner Registration
# @brief Registers a GitHub action runner with GitHub
# @description
#     This script registers the host as a self-hosted GitHub runner with scope set
#     in the `.user.github.runnerOrg` input in the `.chezmoi.yaml.tmpl` file. If your organization is `megabyte-labs`, then
#     the value of `.user.github.runnerOrg` should be `megabyte-labs`. A self-hosted runner is an application
#     that that allows you to run tasks from GitHub CI.
#
#     This script adds 3 labels to the runner: self-hosted, _hostname_, and _operating-system family_.
#
#     The script automatically acquires the GitHub Action runner token (as long as you specify your `.user.github.runnerOrg` value in `.chezmoi.yaml.tmpl`).
#     In order to authenticate with GitHub, you should have the `GITHUB_TOKEN` environment variable in place with the appropriate permissions
#     specified when you generate the token.
#
#     ## Links
#
#     * [Secrets / Environment variables documentation](https://install.doctor/docs/customization/secrets)

{{ includeTemplate "universal/profile" }}
{{ includeTemplate "universal/logg" }}

GH_RUNNER_PATH="$HOME/.local/github-runner"

### Check if GitHub runner is installed
if [ -f "$GH_RUNNER_PATH/config.sh" ]; then
    if [ -f "$GH_RUNNER_PATH/.runner" ]; then
        logg info "GitHub Actions runner is already configured ($GH_RUNNER_PATH/.runner file is present)"
    else
        logg info 'Creating runner configuration'

        ### Configure labels
        LABELS="self-hosted,{{ .chezmoi.hostname }},{{ .host.distro.family }}"
        if [ '{{ .host.distro.family }}' != '{{ .host.distro.id }}' ]; then
            LABELS="${LABELS},{{ .host.distro.id }}"
        fi
        if command -v VirtualBox > /dev/null; then
            LABELS="${LABELS},virtualbox"
        fi
        if command -v docker > /dev/null; then
            LABELS="${LABELS},docker"
        fi

        if [ -n "$GITHUB_TOKEN" ]; then
            if command -v jq > /dev/null; then
                ### Acquire token
                logg info 'Acquiring runner token'
                RUNNER_TOKEN="$(curl -sSL -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $GITHUB_TOKEN" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/orgs/{{ .user.github.runnerOrg }}/actions/runners/registration-token | jq -r '.token')"

                ### Generate the configuration
                logg info 'Joining GitHub runner to https://github.com/{{ .user.github.runnerOrg }}'
                "$GH_RUNNER_PATH/config.sh" --unattended --url https://github.com/{{ .user.github.runnerOrg }} --token "$RUNNER_TOKEN" --labels "$LABELS" || EXIT_CODE=$?
                if [ -n "$EXIT_CODE" ]; then
                    logg error 'GitHub runner configuration failed' && exit 1
                fi

                ### Install / start the service
                logg info 'Configuring runner service'
                "$GH_RUNNER_PATH/svc.sh" install && logg success 'Successfully installed the GitHub Actions runner service'
                logg info 'Starting runner service'
                "$GH_RUNNER_PATH/svc.sh" start && logg success 'Started the GitHub Actions runner service'
            else
                logg warn 'jq is required by the GitHub runner configuration script'
            fi
        else
            logg warn 'The GITHUB_TOKEN environment variable is not present'
        fi
    fi
else
    logg info "The GitHub Actions runner installation is not present at $GH_RUNNER_PATH"
fi
{{- end }}