Skip to content

Commit ca55492

Browse files
authored
Merge pull request #206 from lehors/feature/github-pr-submission
Automatic model GitHub PR submission
2 parents 5232788 + 0d2da3e commit ca55492

15 files changed

Lines changed: 1946 additions & 12 deletions

GITHUB_OAUTH_SETUP.md

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
# GitHub OAuth Configuration Guide
2+
3+
This guide explains how to configure the `social_auth_github` module with valid OAuth credentials for the Model Openness Tool.
4+
5+
## Prerequisites
6+
7+
- Drupal site with `social_auth_github` module installed
8+
- GitHub account with admin access to create OAuth Apps
9+
- Access to Drupal admin interface
10+
11+
## Step 1: Create a GitHub OAuth App
12+
13+
1. **Go to GitHub Developer Settings**
14+
- Navigate to: https://github.com/settings/developers
15+
- Or: GitHub → Settings → Developer settings → OAuth Apps
16+
17+
2. **Click "New OAuth App"**
18+
19+
3. **Fill in the Application Details:**
20+
- **Application name**: `Model Openness Tool` (or your preferred name)
21+
- **Homepage URL**: `https://mot.isitopen.ai` (or your site URL)
22+
- **Application description**: `OAuth integration for Model Openness Tool`
23+
- **Authorization callback URL**: `https://mot.isitopen.ai/user/login/github/callback`
24+
- Replace `mot.isitopen.ai` with your actual domain
25+
- For local development: `http://localhost/user/login/github/callback`
26+
- The path `/user/login/github/callback` is standard for social_auth_github
27+
28+
4. **Click "Register application"**
29+
30+
5. **Note Your Credentials:**
31+
- **Client ID**: Copy this value (e.g., `Iv1.a1b2c3d4e5f6g7h8`)
32+
- **Client Secret**: Click "Generate a new client secret" and copy it immediately
33+
- ⚠️ **Important**: Save this secret securely - you won't be able to see it again!
34+
35+
## Step 2: Configure OAuth Scopes
36+
37+
The PR submission feature requires specific GitHub API scopes:
38+
39+
1. **In your GitHub OAuth App settings**, scroll to "Permissions"
40+
2. **Required scopes for PR submission:**
41+
- `repo` - Full control of private repositories (includes public repos)
42+
- This is needed to create forks, branches, commits, and pull requests
43+
- `user:email` - Access user email addresses (automatically included by social_auth_github)
44+
45+
**Note**: The `repo` scope is automatically requested when users authenticate. You don't need to configure this in GitHub - it's set in the Drupal module configuration.
46+
47+
## Step 3: Configure Drupal Module
48+
49+
### Option A: Via Drupal Admin UI
50+
51+
1. **Navigate to Social Auth GitHub Settings:**
52+
- Go to: `/admin/config/social-api/social-auth/github`
53+
- Or: Configuration → Social API → Social Auth → GitHub
54+
55+
2. **Enter Your OAuth Credentials:**
56+
- **Client ID**: Paste the Client ID from GitHub
57+
- **Client Secret**: Paste the Client Secret from GitHub
58+
59+
3. **Configure Scopes:**
60+
- **Scopes**: Enter `repo,user:email`
61+
- The `repo` scope is required for PR submission
62+
- The `user:email` scope is required for user authentication
63+
64+
4. **Save Configuration**
65+
66+
### Option B: Via Drush/Configuration
67+
68+
If you prefer to configure via configuration files:
69+
70+
1. **Export current configuration:**
71+
```bash
72+
drush config:get social_auth_github.settings
73+
```
74+
75+
2. **Edit the configuration:**
76+
```bash
77+
drush config:edit social_auth_github.settings
78+
```
79+
80+
3. **Set the values:**
81+
```yaml
82+
client_id: 'YOUR_CLIENT_ID'
83+
client_secret: 'YOUR_CLIENT_SECRET'
84+
scopes: 'repo,user:email'
85+
```
86+
87+
4. **Import the configuration:**
88+
```bash
89+
drush config:import
90+
```
91+
92+
## Step 4: Test the Configuration
93+
94+
### Test Authentication
95+
96+
1. **Clear Drupal cache:**
97+
```bash
98+
drush cr
99+
```
100+
101+
2. **Test Login:**
102+
- Log out of Drupal
103+
- Go to `/user/login`
104+
- Click "Log in with GitHub" (or navigate to `/user/login/github`)
105+
- You should be redirected to GitHub
106+
- Authorize the application
107+
- You should be redirected back to Drupal and logged in
108+
109+
3. **Verify Token Storage:**
110+
```bash
111+
drush sql:query "SELECT * FROM social_auth WHERE plugin_id = 'social_auth_github' LIMIT 1;"
112+
```
113+
- You should see a record with an encrypted token
114+
115+
### Test PR Submission
116+
117+
1. **Navigate to Model Evaluation:**
118+
- Go to `/model/evaluate`
119+
- Fill out the form with test data
120+
- Click "Evaluate"
121+
122+
2. **Verify PR Button:**
123+
- You should see a "Submit Pull Request" button
124+
- If not logged in, you should see "Login with GitHub to Submit PR"
125+
126+
3. **Test PR Creation:**
127+
- Click "Submit Pull Request"
128+
- Wait for the process to complete
129+
- You should receive a success message with a PR link
130+
131+
## Troubleshooting
132+
133+
### Issue: "Callback URL mismatch"
134+
135+
**Cause**: The callback URL in GitHub doesn't match your Drupal site URL.
136+
137+
**Solution**:
138+
- Verify the callback URL in GitHub matches: `https://YOUR_DOMAIN/user/login/github/callback`
139+
- Check for trailing slashes or http vs https mismatches
140+
141+
### Issue: "Invalid client_id or client_secret"
142+
143+
**Cause**: Incorrect credentials entered in Drupal.
144+
145+
**Solution**:
146+
- Double-check the Client ID and Client Secret in GitHub
147+
- Regenerate the Client Secret if needed
148+
- Re-enter the credentials in Drupal
149+
- Clear Drupal cache: `drush cr`
150+
151+
### Issue: "Insufficient permissions" when creating PR
152+
153+
**Cause**: The OAuth token doesn't have the `repo` scope.
154+
155+
**Solution**:
156+
1. Go to `/admin/config/social-api/social-auth/github`
157+
2. Ensure "Scopes" field contains: `repo,user:email`
158+
3. Save configuration
159+
4. Users need to re-authenticate to get the new scopes
160+
5. Go to GitHub → Settings → Applications → Authorized OAuth Apps
161+
6. Revoke access to the MOT app
162+
7. Log in again through Drupal
163+
164+
### Issue: "Token not found" error
165+
166+
**Cause**: User's OAuth token is not stored or has expired.
167+
168+
**Solution**:
169+
- Log out and log back in with GitHub
170+
- Check the `social_auth` table for the user's record
171+
- Verify the token is encrypted and stored
172+
173+
### Issue: Rate limiting errors
174+
175+
**Cause**: GitHub API rate limits exceeded.
176+
177+
**Solution**:
178+
- Authenticated requests have a limit of 5,000 requests/hour
179+
- Check current rate limit: `curl -H "Authorization: Bearer YOUR_TOKEN" https://api.github.com/rate_limit`
180+
- Wait for the rate limit to reset (shown in the response)
181+
182+
## Security Best Practices
183+
184+
1. **Keep Client Secret Secure:**
185+
- Never commit the Client Secret to version control
186+
- Use environment variables or secure configuration management
187+
- Rotate the secret periodically
188+
189+
2. **Use HTTPS:**
190+
- Always use HTTPS for production sites
191+
- GitHub requires HTTPS for OAuth callbacks in production
192+
193+
3. **Limit Scope Access:**
194+
- Only request the scopes you need
195+
- The `repo` scope is powerful - ensure users understand what they're authorizing
196+
197+
4. **Monitor OAuth App:**
198+
- Regularly check the OAuth app's activity in GitHub
199+
- Review authorized users
200+
- Monitor for suspicious activity
201+
202+
## Environment-Specific Configuration
203+
204+
### Local Development
205+
206+
For local development, you may need a separate OAuth app:
207+
208+
1. Create a new OAuth App in GitHub for development
209+
2. Set callback URL to: `http://localhost/user/login/github/callback`
210+
3. Use different Client ID/Secret for local environment
211+
4. Consider using `.env` files to manage credentials
212+
213+
### Staging Environment
214+
215+
1. Create a separate OAuth App for staging
216+
2. Set callback URL to your staging domain
217+
3. Use separate credentials from production
218+
219+
### Production Environment
220+
221+
1. Use production OAuth App credentials
222+
2. Ensure HTTPS is enabled
223+
3. Monitor logs for authentication issues
224+
4. Set up alerts for OAuth failures
225+
226+
## Additional Resources
227+
228+
- **Drupal Social Auth GitHub Module**: https://www.drupal.org/project/social_auth_github
229+
- **GitHub OAuth Documentation**: https://docs.github.com/en/developers/apps/building-oauth-apps
230+
- **GitHub API Scopes**: https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps
231+
- **Drupal Social API**: https://www.drupal.org/project/social_api
232+
233+
## Support
234+
235+
If you encounter issues:
236+
237+
1. Check Drupal logs: `drush watchdog:show`
238+
2. Check PHP error logs
239+
3. Verify GitHub OAuth app settings
240+
4. Test with a fresh browser session (incognito mode)
241+
5. Review the `social_auth` database table for stored tokens
242+
243+
## Configuration Checklist
244+
245+
- [ ] GitHub OAuth App created
246+
- [ ] Client ID and Client Secret copied
247+
- [ ] Callback URL matches Drupal site
248+
- [ ] Scopes include `repo,user:email`
249+
- [ ] Credentials entered in Drupal
250+
- [ ] Configuration saved
251+
- [ ] Drupal cache cleared
252+
- [ ] Test authentication successful
253+
- [ ] Token stored in database
254+
- [ ] PR submission button visible
255+
- [ ] Test PR creation successful

0 commit comments

Comments
 (0)