| | @@ -2,11 +2,11 @@ |
| 2 | 2 | import re |
| 3 | 3 | |
| 4 | 4 | import markdown as md |
| 5 | 5 | from django.contrib.auth.decorators import login_required |
| 6 | 6 | from django.http import Http404 |
| 7 | | -from django.shortcuts import get_object_or_404, render |
| 7 | +from django.shortcuts import get_object_or_404, redirect, render |
| 8 | 8 | from django.utils.safestring import mark_safe |
| 9 | 9 | |
| 10 | 10 | from projects.models import Project |
| 11 | 11 | |
| 12 | 12 | from .models import FossilRepository |
| | @@ -1049,10 +1049,16 @@ |
| 1049 | 1049 | action = request.POST.get("action", "") |
| 1050 | 1050 | if action == "create": |
| 1051 | 1051 | git_url = request.POST.get("git_remote_url", "").strip() |
| 1052 | 1052 | auth_method = request.POST.get("auth_method", "token") |
| 1053 | 1053 | auth_credential = request.POST.get("auth_credential", "").strip() |
| 1054 | + # Use OAuth token from session if available and no manual credential provided |
| 1055 | + if not auth_credential: |
| 1056 | + if auth_method == "oauth_github" and request.session.get("github_oauth_token"): |
| 1057 | + auth_credential = request.session.pop("github_oauth_token") |
| 1058 | + elif auth_method == "oauth_gitlab" and request.session.get("gitlab_oauth_token"): |
| 1059 | + auth_credential = request.session.pop("gitlab_oauth_token") |
| 1054 | 1060 | sync_mode = request.POST.get("sync_mode", "scheduled") |
| 1055 | 1061 | sync_schedule = request.POST.get("sync_schedule", "*/15 * * * *").strip() |
| 1056 | 1062 | git_branch = request.POST.get("git_branch", "main").strip() |
| 1057 | 1063 | |
| 1058 | 1064 | if git_url: |
| | @@ -1100,17 +1106,91 @@ |
| 1100 | 1106 | project, fossil_repo, reader = _get_repo_and_reader(slug, request, "admin") |
| 1101 | 1107 | |
| 1102 | 1108 | if request.method == "POST": |
| 1103 | 1109 | from fossil.tasks import run_git_sync |
| 1104 | 1110 | |
| 1105 | | - run_git_sync.delay(mirror_id) |
| 1106 | | - from django.contrib import messages |
| 1111 | + try: |
| 1112 | + run_git_sync.delay(mirror_id) |
| 1113 | + from django.contrib import messages |
| 1107 | 1114 | |
| 1108 | | - messages.info(request, "Git sync triggered. Check back shortly for results.") |
| 1115 | + messages.info(request, "Git sync triggered in background.") |
| 1116 | + except Exception: |
| 1117 | + # Celery not available — run synchronously |
| 1118 | + run_git_sync(mirror_id) |
| 1119 | + from django.contrib import messages |
| 1120 | + |
| 1121 | + messages.success(request, "Git sync completed.") |
| 1109 | 1122 | |
| 1110 | 1123 | from django.shortcuts import redirect |
| 1111 | 1124 | |
| 1125 | + return redirect("fossil:git_mirror", slug=slug) |
| 1126 | + |
| 1127 | + |
| 1128 | +# --- OAuth --- |
| 1129 | + |
| 1130 | + |
| 1131 | +@login_required |
| 1132 | +def oauth_github_start(request, slug): |
| 1133 | + """Start GitHub OAuth flow.""" |
| 1134 | + from fossil.oauth import github_authorize_url |
| 1135 | + |
| 1136 | + url = github_authorize_url(request, slug) |
| 1137 | + if not url: |
| 1138 | + from django.contrib import messages |
| 1139 | + |
| 1140 | + messages.error(request, "GitHub OAuth not configured. Set GITHUB_OAUTH_CLIENT_ID in admin settings.") |
| 1141 | + return redirect("fossil:git_mirror", slug=slug) |
| 1142 | + return redirect(url) |
| 1143 | + |
| 1144 | + |
| 1145 | +@login_required |
| 1146 | +def oauth_gitlab_start(request, slug): |
| 1147 | + """Start GitLab OAuth flow.""" |
| 1148 | + from fossil.oauth import gitlab_authorize_url |
| 1149 | + |
| 1150 | + url = gitlab_authorize_url(request, slug) |
| 1151 | + if not url: |
| 1152 | + from django.contrib import messages |
| 1153 | + |
| 1154 | + messages.error(request, "GitLab OAuth not configured. Set GITLAB_OAUTH_CLIENT_ID in admin settings.") |
| 1155 | + return redirect("fossil:git_mirror", slug=slug) |
| 1156 | + return redirect(url) |
| 1157 | + |
| 1158 | + |
| 1159 | +@login_required |
| 1160 | +def oauth_github_callback(request, slug): |
| 1161 | + """Handle GitHub OAuth callback.""" |
| 1162 | + from fossil.oauth import github_exchange_token |
| 1163 | + |
| 1164 | + result = github_exchange_token(request, slug) |
| 1165 | + from django.contrib import messages |
| 1166 | + |
| 1167 | + if result["token"]: |
| 1168 | + # Store token in session for the mirror config form to pick up |
| 1169 | + request.session["github_oauth_token"] = result["token"] |
| 1170 | + request.session["github_oauth_user"] = result.get("username", "") |
| 1171 | + messages.success(request, f"Connected to GitHub as {result.get('username', 'unknown')}. Now configure your mirror.") |
| 1172 | + else: |
| 1173 | + messages.error(request, f"GitHub OAuth failed: {result.get('error', 'Unknown error')}") |
| 1174 | + |
| 1175 | + return redirect("fossil:git_mirror", slug=slug) |
| 1176 | + |
| 1177 | + |
| 1178 | +@login_required |
| 1179 | +def oauth_gitlab_callback(request, slug): |
| 1180 | + """Handle GitLab OAuth callback.""" |
| 1181 | + from fossil.oauth import gitlab_exchange_token |
| 1182 | + |
| 1183 | + result = gitlab_exchange_token(request, slug) |
| 1184 | + from django.contrib import messages |
| 1185 | + |
| 1186 | + if result["token"]: |
| 1187 | + request.session["gitlab_oauth_token"] = result["token"] |
| 1188 | + messages.success(request, "Connected to GitLab. Now configure your mirror.") |
| 1189 | + else: |
| 1190 | + messages.error(request, f"GitLab OAuth failed: {result.get('error', 'Unknown error')}") |
| 1191 | + |
| 1112 | 1192 | return redirect("fossil:git_mirror", slug=slug) |
| 1113 | 1193 | |
| 1114 | 1194 | |
| 1115 | 1195 | # --- Technotes --- |
| 1116 | 1196 | |
| 1117 | 1197 | |