Skip to content

Commit 9f397f3

Browse files
LTSCommerceclaude
andcommitted
fix(github): properly parse scopes and use additive refresh
Rewrote PhpStorm token function based on official gh CLI documentation: BEFORE (broken): - Parsed human-readable text output with sed/grep/tr hacks - Resulted in malformed scope strings with quotes and duplicates - Tried to pass ALL scopes to gh auth refresh (unnecessary) - Failed with "invalid_scope" errors AFTER (correct): - Uses gh auth status --json hosts for structured data - Parses scopes array with jq (reliable JSON parsing) - Only adds MISSING scopes (gh auth refresh --scopes is additive) - Preserves all existing scopes automatically Key insight from documentation: --scopes flag is ADDITIVE, not replacement. No need to manually merge scopes - just pass missing ones. Based on official documentation: - gh auth refresh: https://cli.github.com/manual/gh_auth_refresh - gh auth status: https://cli.github.com/manual/gh_auth_status - JSON output: cli/cli#8637 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
1 parent 4a3f165 commit 9f397f3

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

‎playbooks/imports/optional/common/play-github-cli-multi.yml‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -669,42 +669,39 @@
669669
# Switch to the account
670670
gh auth switch --hostname github.com --user "{{ username }}" 2>/dev/null
671671
672-
# Get current token scopes
673-
local current_scopes=$(gh auth status 2>&1 | grep "Token scopes:" | sed "s/.*Token scopes: '//" | sed "s/'$//" | tr ',' '\n' | sort | tr '\n' ',' | sed 's/,$//')
672+
# Get current token scopes using JSON output for reliable parsing
673+
# Use jq to extract the scopes array and convert to comma-separated string
674+
local current_scopes=$(gh auth status --json hosts --jq '.["github.com"].scopes | join(",")')
674675
675676
# Required scopes for PhpStorm
676-
local required_scopes="gist,read:org,repo,user:email"
677+
local required_scopes=("gist" "read:org" "repo" "user:email")
677678
678679
# Check if all required scopes are present
679-
local missing_scopes=""
680-
local needs_refresh=false
680+
local missing_scopes=()
681681
682-
for scope in $(echo "$required_scopes" | tr ',' ' '); do
682+
for scope in "${required_scopes[@]}"; do
683683
if ! echo ",$current_scopes," | grep -q ",$scope,"; then
684-
missing_scopes="$missing_scopes $scope"
685-
needs_refresh=true
684+
missing_scopes+=("$scope")
686685
fi
687686
done
688687
689-
if [ "$needs_refresh" = true ]; then
690-
echo "Current token scopes: $current_scopes"
688+
if [ ${#missing_scopes[@]} -gt 0 ]; then
689+
echo "Current token scopes: $(echo "$current_scopes" | tr ',' ', ')"
691690
echo ""
692-
echo "Missing PhpStorm-required scopes:$missing_scopes"
693-
echo ""
694-
echo "Refreshing token to add missing scopes..."
695-
echo "This will preserve all existing scopes and add:"
696-
for scope in $missing_scopes; do
691+
echo "Missing PhpStorm-required scopes:"
692+
for scope in "${missing_scopes[@]}"; do
697693
echo " - $scope"
698694
done
699695
echo ""
696+
echo "Adding missing scopes (existing scopes will be preserved)..."
700697
echo "You may need to re-authenticate in your browser..."
701698
echo ""
702699
703-
# Combine current scopes with required scopes (deduplicated)
704-
local all_scopes=$(echo "$current_scopes,$required_scopes" | tr ',' '\n' | sort -u | tr '\n' ',' | sed 's/,$//')
700+
# Join missing scopes with commas (no spaces)
701+
local missing_scopes_str=$(IFS=,; echo "${missing_scopes[*]}")
705702
706-
# Refresh with all scopes
707-
gh auth refresh --hostname github.com --scopes "$all_scopes"
703+
# Use --scopes flag which is ADDITIVE (adds to existing scopes)
704+
gh auth refresh --hostname github.com --scopes "$missing_scopes_str"
708705
709706
if [ $? -ne 0 ]; then
710707
echo ""
@@ -716,7 +713,7 @@
716713
echo "✓ Token refreshed successfully!"
717714
else
718715
echo "✓ Token already has all required PhpStorm scopes"
719-
echo " Current scopes: $current_scopes"
716+
echo " Current scopes: $(echo "$current_scopes" | tr ',' ', ')"
720717
fi
721718
722719
echo ""

0 commit comments

Comments
 (0)