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

@@ -68,7 +68,7 @@
"command": "bash", "command": "bash",
"args": [ "args": [
"-c", "-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": {} "env": {}
} }

View File

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