Skip to content

Commit 85beee7

Browse files
authored
Merge pull request #18 from link-foundation/issue-17-5aba117748c0
feat: use gh CLI as default backend with API fallback
2 parents d38cccf + ca552c3 commit 85beee7

File tree

7 files changed

+852
-133
lines changed

7 files changed

+852
-133
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'gh-load-pull-request': minor
3+
---
4+
5+
Use gh CLI as default backend with API fallback
6+
7+
- By default, the tool now uses the `gh` CLI to fetch PR data when available
8+
- Automatically falls back to GitHub REST API (Octokit) if gh CLI is not installed or not authenticated
9+
- Added `--force-api` flag to force using the GitHub API instead of gh CLI
10+
- Added `--force-gh` flag to force using gh CLI (fails if not available)
11+
- Added new CI/CD job to test both gh CLI and API modes
12+
- Improved verbose logging to show which backend is being used

.github/workflows/release.yml

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,63 @@ jobs:
113113

114114
- name: Run tests
115115
run: bun test
116+
env:
117+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
118+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
119+
120+
# Test both gh CLI and API modes explicitly
121+
test-modes:
122+
name: Test Modes (gh CLI and API)
123+
runs-on: ubuntu-latest
124+
needs: [changeset-check]
125+
if: always() && (github.event_name == 'push' || needs.changeset-check.result == 'success')
126+
steps:
127+
- uses: actions/checkout@v4
128+
129+
- name: Setup Bun
130+
uses: oven-sh/setup-bun@v2
131+
with:
132+
bun-version: latest
133+
134+
- name: Install dependencies
135+
run: bun install
136+
137+
- name: Test gh CLI mode
138+
run: |
139+
# Test with gh CLI (should work since GH_TOKEN is set)
140+
bun run src/gh-load-pull-request.mjs link-foundation/gh-load-pull-request#2 --force-gh --format json > gh-output.json
141+
echo "gh CLI mode test passed"
142+
env:
143+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
144+
145+
- name: Test API mode
146+
run: |
147+
# Test with API mode
148+
bun run src/gh-load-pull-request.mjs link-foundation/gh-load-pull-request#2 --force-api --format json > api-output.json
149+
echo "API mode test passed"
150+
env:
151+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
152+
153+
- name: Compare outputs
154+
run: |
155+
# Parse and compare key fields
156+
GH_NUMBER=$(cat gh-output.json | bun -e "console.log(JSON.parse(await Bun.stdin.text()).pullRequest.number)")
157+
API_NUMBER=$(cat api-output.json | bun -e "console.log(JSON.parse(await Bun.stdin.text()).pullRequest.number)")
158+
159+
if [ "$GH_NUMBER" != "$API_NUMBER" ]; then
160+
echo "PR number mismatch: gh=$GH_NUMBER api=$API_NUMBER"
161+
exit 1
162+
fi
163+
164+
echo "Both modes produce consistent output for PR #$GH_NUMBER"
116165
117166
# Release - only runs on main after tests pass (for push events)
118167
release:
119168
name: Release
120-
needs: [lint, test]
169+
needs: [lint, test, test-modes]
121170
# Use always() to ensure this job runs even if changeset-check was skipped
122171
# This is needed because lint/test jobs have a transitive dependency on changeset-check
123-
if: always() && github.ref == 'refs/heads/main' && github.event_name == 'push' && needs.lint.result == 'success' && needs.test.result == 'success'
172+
if: always() && github.ref == 'refs/heads/main' && github.event_name == 'push' && needs.lint.result == 'success' && needs.test.result == 'success' && needs.test-modes.result == 'success'
124173
runs-on: ubuntu-latest
125174
# Permissions required for npm OIDC trusted publishing
126175
permissions:

README.md

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,45 @@ chmod +x gh-load-pull-request.mjs
7373
Usage: gh-load-pull-request <pr-url> [options]
7474
7575
Options:
76-
-t, --token GitHub personal access token (optional for public PRs)
77-
-o, --output Output file path (default: stdout)
78-
-h, --help Show help
79-
--version Show version number
76+
-t, --token GitHub personal access token (optional for public PRs)
77+
-o, --output Output directory (creates pr-<number>/ subfolder)
78+
--format Output format: markdown, json (default: markdown)
79+
--download-images Download embedded images (default: true)
80+
--include-reviews Include PR reviews (default: true)
81+
--force-api Force using GitHub API instead of gh CLI
82+
--force-gh Force using gh CLI, fail if not available
83+
-v, --verbose Enable verbose logging
84+
-h, --help Show help
85+
--version Show version number
86+
```
87+
88+
## Backend Modes
89+
90+
The tool supports two backend modes for fetching PR data:
91+
92+
### 1. gh CLI Mode (Default)
93+
94+
By default, the tool uses the [GitHub CLI](https://cli.github.com/) (`gh`) to fetch PR data. This is the recommended mode as it:
95+
96+
- Uses your existing `gh` authentication
97+
- Doesn't require managing tokens separately
98+
- Works seamlessly with GitHub Enterprise
99+
100+
### 2. API Mode (Fallback)
101+
102+
If `gh` CLI is not available or not authenticated, the tool automatically falls back to using the GitHub REST API via Octokit. You can also force this mode with `--force-api`.
103+
104+
### Controlling Backend Mode
105+
106+
```bash
107+
# Use default mode (gh CLI with API fallback)
108+
gh-load-pull-request owner/repo#123
109+
110+
# Force gh CLI mode (fails if gh is not available)
111+
gh-load-pull-request owner/repo#123 --force-gh
112+
113+
# Force API mode (useful for testing or when gh has issues)
114+
gh-load-pull-request owner/repo#123 --force-api
80115
```
81116

82117
## Input Formats
@@ -152,14 +187,23 @@ gh-load-pull-request https://github.com/facebook/react/pull/28000
152187
gh-load-pull-request facebook/react#28000
153188

154189
# Save to file
155-
gh-load-pull-request facebook/react#28000 -o react-pr-28000.md
190+
gh-load-pull-request facebook/react#28000 -o ./output
156191

157192
# Download private PR using gh CLI auth
158193
gh-load-pull-request myorg/private-repo#42
159194

160195
# Download with explicit token
161196
gh-load-pull-request myorg/repo#123 --token ghp_your_token_here
162197

198+
# Force using GitHub API instead of gh CLI
199+
gh-load-pull-request owner/repo#123 --force-api
200+
201+
# Output as JSON
202+
gh-load-pull-request owner/repo#123 --format json
203+
204+
# Verbose mode for debugging
205+
gh-load-pull-request owner/repo#123 -v
206+
163207
# Pipe to other tools (e.g., AI for review)
164208
gh-load-pull-request owner/repo#123 | claude-analyze
165209
```

0 commit comments

Comments
 (0)