# Copyright 2022 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

# A reusable workflow to extract settings from a repository.
# To enable a setting, create a "GitHub Environment" with the same name.
#
# This enables per-repo settings that aren't copied to a fork.  This is better
# than "vars" or "secrets", since those would require the use of
# `pull_request_target` instead of `pull_request` triggers, which come with
# additional risks such as the bypassing of "require approval" rules for
# workflows.
#
# Without a setting for flags like "self_hosted", test workflows for a fork
# would time out waiting for self-hosted runners that the fork doesn't have.
name: Settings

# Runs when called from another workflow.
on:
  workflow_call:
    outputs:
      self_hosted:
        description: "Enable jobs requiring a self-hosted runner."
        value: ${{ jobs.settings.outputs.self_hosted }}
      debug:
        description: "Enable SSH debugging when a workflow fails."
        value: ${{ jobs.settings.outputs.debug }}

jobs:
  settings:
    runs-on: ubuntu-latest
    outputs:
      self_hosted: ${{ steps.settings.outputs.self_hosted }}
      debug: ${{ steps.settings.outputs.debug }}
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - id: settings
        run: |
          environments=$(gh api /repos/${{ github.repository }}/environments)
          for name in self_hosted debug; do
            exists=$(echo $environments | jq ".environments[] | select(.name == \"$name\")")
            if [[ "$exists" != "" ]]; then
              echo "$name=true" >> $GITHUB_OUTPUT
              echo "\"$name\" enabled."
            else
              echo "$name=" >> $GITHUB_OUTPUT
              echo "\"$name\" disabled."
            fi
          done
