From 96c8bfe9deb5dfdda7190eeff08a01e5780dc07f Mon Sep 17 00:00:00 2001 From: GitHub Copilot Date: Thu, 8 May 2025 08:38:05 -0400 Subject: [PATCH] trying several things to correct the PAT inclusion. things are broken and I'm trying to resolve that. --- .devcontainer/devcontainer.json | 2 +- .devcontainer/library-scripts/load-env.sh | 68 ++++++++++++++--------- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 0662457..80202ab 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -68,7 +68,7 @@ "command": "bash", "args": [ "-c", - "source ${containerWorkspaceFolder}/.devcontainer/library-scripts/load-env.sh && docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN=\"$GITHUB_PERSONAL_ACCESS_TOKEN\" ghcr.io/github/github-mcp-server" + "source ${containerWorkspaceFolder}/.devcontainer/library-scripts/load-env.sh && if [ -n \"${GITHUB_PERSONAL_ACCESS_TOKEN}\" ]; then docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN=\"${GITHUB_PERSONAL_ACCESS_TOKEN}\" ghcr.io/github/github-mcp-server; else echo 'Error: GITHUB_PERSONAL_ACCESS_TOKEN not set' >&2; exit 1; fi" ], "env": {} } diff --git a/.devcontainer/library-scripts/load-env.sh b/.devcontainer/library-scripts/load-env.sh index cae2b47..410cbd9 100755 --- a/.devcontainer/library-scripts/load-env.sh +++ b/.devcontainer/library-scripts/load-env.sh @@ -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