trying several things to correct the PAT inclusion. things are broken and I'm trying to resolve that.

This commit is contained in:
GitHub Copilot
2025-05-08 08:38:05 -04:00
parent a58c60c437
commit 96c8bfe9de
2 changed files with 43 additions and 27 deletions

View File

@@ -8,37 +8,53 @@ set -o errtrace
set -o nounset
# Catch the error in case mysqldump fails (but gzip succeeds) in `mysqldump |gzip`
set -o pipefail
# Turn on traces, useful while debugging but commented out by default
# set -o xtrace
# Load environment variables from .env file
readonly ENV_FILE="/workspaces/finance/.devcontainer/.env"
# Try to get PAT from environment first
pat="${GITHUB_PERSONAL_ACCESS_TOKEN:-}"
if [[ ! -f "${ENV_FILE}" ]]; then
echo >&2 "Error: Environment file not found: ${ENV_FILE}"
exit 1
fi
echo "Loading environment variables from ${ENV_FILE}"
# Read the env file line by line to handle special characters correctly
while IFS= read -r line || [[ -n "${line}" ]]; do
# Skip comments and empty lines
[[ "${line}" =~ ^[[:space:]]*# ]] && continue
[[ -z "${line}" ]] && continue
# Export the variable
if [[ "${line}" =~ ^[[:alpha:]][[:alnum:]_]*= ]]; then
export "${line}"
else
echo >&2 "Warning: Skipping invalid line: ${line}"
# If not in environment, try to get from GitHub CLI
if [[ -z "${pat}" ]] || [[ "${#pat}" -lt 30 ]]; then
if command -v gh &> /dev/null; then
if gh auth status &> /dev/null; then
pat=$(gh auth token)
if [[ -n "${pat}" ]] && [[ "${#pat}" -gt 30 ]]; then
# Save the token to .env file if it's valid
ENV_FILE="/workspaces/finance/.devcontainer/.env"
echo "# GitHub Finance Devcontainer Configuration" > "${ENV_FILE}"
echo "GITHUB_PERSONAL_ACCESS_TOKEN=${pat}" >> "${ENV_FILE}"
chmod 600 "${ENV_FILE}"
fi
fi
fi
done < "${ENV_FILE}"
fi
# Verify PAT loaded correctly (without printing the actual token)
if [[ -n "${GITHUB_PERSONAL_ACCESS_TOKEN:-}" ]]; then
# If still not found, try .env file
if [[ -z "${pat}" ]] || [[ "${#pat}" -lt 30 ]]; then
ENV_FILE="/workspaces/finance/.devcontainer/.env"
if [[ -f "${ENV_FILE}" ]]; then
# Try to extract PAT from .env file
env_pat=$(grep -E '^GITHUB_PERSONAL_ACCESS_TOKEN=.+' "${ENV_FILE}" | cut -d'=' -f2-)
# Clean up the extracted PAT
env_pat="${env_pat#\"}"
env_pat="${env_pat%\"}"
env_pat="${env_pat#\'}"
env_pat="${env_pat%\'}"
env_pat="${env_pat%%[[:space:]]}"
env_pat="${env_pat##[[:space:]]}"
if [[ -n "${env_pat}" ]] && [[ "${#env_pat}" -gt 30 ]]; then
pat="${env_pat}"
fi
fi
fi
# Export the PAT if valid
if [[ -n "${pat}" ]] && [[ "${#pat}" -gt 30 ]]; then
export GITHUB_PERSONAL_ACCESS_TOKEN="${pat}"
echo "GitHub Personal Access Token loaded successfully"
exit 0
else
echo >&2 "ERROR: GitHub Personal Access Token not loaded"
echo >&2 "ERROR: Could not find valid GitHub Personal Access Token"
exit 1
fi