#!/usr/bin/env bash
# CyberTwin export recipe: GitHub security settings and security alerts for one repository.
# Read-only: every call is a GET through the GitHub CLI (gh api). Runs on your
# machine, writes one local file, sends nothing to CyberTwin. Upload
# github-security.json at /upload.
# Commands reviewed 2026-09-28, the file layout 2026-11-11, against
# https://docs.github.com/en/rest/repos/repos#get-a-repository
# https://docs.github.com/en/rest/branches/branches#get-a-branch
# https://docs.github.com/en/rest/orgs/orgs#get-an-organization
# https://docs.github.com/en/rest/code-scanning/code-scanning
# https://docs.github.com/en/rest/secret-scanning/secret-scanning
# https://docs.github.com/en/rest/dependabot/alerts
# https://docs.github.com/en/rest/branches/branch-protection
# https://docs.github.com/en/rest/actions/permissions
#
# The file keeps each answer GitHub returns whole, under one name per call
# (repository, organization, default_branch, branch_protection,
# code_scanning_default_setup, private_vulnerability_reporting,
# actions_workflow_permissions, code_scanning_alerts, secret_scanning_alerts,
# dependabot_alerts). An answer GitHub refuses (403, 404) is left out of the
# file, never replaced by an empty one. Each secret scanning alert loses its
# secret field; nothing else is changed.
#
# Needs: gh (signed in with `gh auth login`) and jq. The token must be able to
# read the repository's security alerts; the org's two-factor setting is shown
# to org owners alone, and is left out when yours cannot see it.
#
# Usage: GH_REPO=octo-org/payments-api ./github-security-export.sh
set -euo pipefail
: "${GH_REPO:?set GH_REPO, e.g. octo-org/payments-api}"
out="github-security.json"
owner="${GH_REPO%%/*}"

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

# One GET into a file. An answer GitHub refuses (an endpoint that is off, or
# not visible to this token, answers 403 or 404) leaves the file empty, and the
# last step leaves that member out of github-security.json.
get() { gh api -H "Accept: application/vnd.github+json" "$1" > "$tmp/$2" 2>/dev/null || : > "$tmp/$2"; }
# Every page of an alert list, merged into one list. A refused list leaves the
# file empty, so the member is left out (never written as an empty list).
alerts() {
  if gh api -H "Accept: application/vnd.github+json" --paginate "$1?${3:-}per_page=100" > "$tmp/$2.pages" 2>/dev/null; then
    jq -s 'add // []' "$tmp/$2.pages" > "$tmp/$2"
  else
    : > "$tmp/$2"
  fi
}

get "repos/${GH_REPO}" repo
branch=$(jq -r '.default_branch // "main"' "$tmp/repo" 2>/dev/null || echo main)
get "orgs/${owner}" org
get "repos/${GH_REPO}/branches/${branch}" branch
# Branch protection is asked for when the branch answer says protected: true, and not otherwise.
if [ "$(jq -r '.protected' "$tmp/branch" 2>/dev/null)" = "true" ]; then
  get "repos/${GH_REPO}/branches/${branch}/protection" prot
else
  : > "$tmp/prot"
fi
get "repos/${GH_REPO}/code-scanning/default-setup" setup
get "repos/${GH_REPO}/private-vulnerability-reporting" pvr
get "repos/${GH_REPO}/actions/permissions/workflow" wf
alerts "repos/${GH_REPO}/code-scanning/alerts" cs
# hide_secret=true asks GitHub to leave the leaked value out; the secret field
# is also deleted below, so the file never carries the value. Deleting that one
# field is the one change this script makes to an answer GitHub returns: kept as a
# safeguard in case GitHub returns the value all the same (review of 11 Nov 2026,
# minor 7, an exception to keeping vendor output unchanged that the lead notes).
alerts "repos/${GH_REPO}/secret-scanning/alerts" ss "hide_secret=true&"
alerts "repos/${GH_REPO}/dependabot/alerts" da

jq -n \
  --slurpfile repo "$tmp/repo" --slurpfile org "$tmp/org" --slurpfile branch "$tmp/branch" \
  --slurpfile prot "$tmp/prot" --slurpfile setup "$tmp/setup" --slurpfile pvr "$tmp/pvr" \
  --slurpfile wf "$tmp/wf" --slurpfile cs "$tmp/cs" --slurpfile ss "$tmp/ss" --slurpfile da "$tmp/da" \
  '{
    repository: $repo[0],
    organization: $org[0],
    default_branch: $branch[0],
    branch_protection: $prot[0],
    code_scanning_default_setup: $setup[0],
    private_vulnerability_reporting: $pvr[0],
    actions_workflow_permissions: $wf[0],
    code_scanning_alerts: $cs[0],
    secret_scanning_alerts: ($ss[0] | if . == null then null else map(del(.secret)) end),
    dependabot_alerts: $da[0]
  } | with_entries(select(.value != null))' > "$out"

echo "Wrote $out. Upload it at /upload."
