Skip to content

Commit 24e3337

Browse files
committed
fix: fixed repo.sh using shellcheck and added test in CI check
1 parent 4f28bff commit 24e3337

3 files changed

Lines changed: 316 additions & 102 deletions

File tree

.github/workflows/cli-tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ jobs:
7070

7171
- name: CLI tests
7272
run: pytest -s ./tests/*.py
73+
74+
- name: Repo setup-remotes tests
75+
run: |
76+
chmod +x ./tests/test_repo.sh
77+
./tests/test_repo.sh

repo.sh

Lines changed: 107 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -311,156 +311,151 @@ status ()
311311

312312
# Define repositories that exist in both edx and openedx organizations
313313
# These are the ones that need remote setup for forked repositories
314-
declare -A FORKED_REPOS
315314
FORKED_REPOS=(
316-
["course-discovery"]="openedx"
317-
["credentials"]="openedx"
318-
["cs_comments_service"]="openedx"
319-
["ecommerce"]="edx"
320-
["edx-notes-api"]="openedx"
321-
["edx-platform"]="edx"
322-
["xqueue"]="openedx"
323-
["edx-analytics-dashboard"]="edx"
324-
["frontend-app-gradebook"]="edx"
325-
["frontend-app-learner-dashboard"]="edx"
326-
["frontend-app-learner-record"]="edx"
327-
["frontend-app-skills"]="edx"
328-
["frontend-app-learning"]="edx"
329-
["frontend-app-ora"]="edx"
330-
["frontend-app-ora-grading"]="edx"
331-
["frontend-app-exams-dashboard"]="edx"
332-
["frontend-app-learner-portal-programs"]="edx"
333-
["frontend-app-program-console"]="edx"
334-
["frontend-app-communications"]="edx"
335-
["frontend-app-discussions"]="edx"
336-
["frontend-app-profile"]="edx"
337-
["frontend-app-enterprise-public-catalog"]="edx"
338-
["frontend-app-publisher"]="edx"
339-
["frontend-app-support-tools"]="edx"
340-
["frontend-app-admin-portal"]="edx"
341-
["frontend-app-learner-portal-enterprise"]="edx"
342-
["frontend-app-enterprise-checkout"]="edx"
343-
["frontend-app-authoring"]="edx"
344-
["frontend-app-instruct"]="edx"
345-
["frontend-app-catalog"]="edx"
346-
["openedx-translations"]="edx"
347-
["frontend-app-payment"]="edx"
348-
["edx-analytics-data-api"]="edx"
349-
["enterprise-catalog"]="openedx"
350-
["portal-designer"]="edx"
351-
["license-manager"]="openedx"
352-
["codejail-service"]="openedx"
353-
["enterprise-access"]="openedx"
354-
["frontend-app-authn"]="openedx"
355-
["frontend-app-course-authoring"]="openedx"
356-
["registrar"]="edx"
357-
["frontend-app-account"]="openedx"
358-
["enterprise-subsidy"]="openedx"
359-
["edx-exams"]="edx"
315+
"course-discovery"
316+
"credentials"
317+
"cs_comments_service"
318+
"ecommerce"
319+
"edx-notes-api"
320+
"edx-platform"
321+
"xqueue"
322+
"edx-analytics-dashboard"
323+
"frontend-app-gradebook"
324+
"frontend-app-learner-dashboard"
325+
"frontend-app-learner-record"
326+
"frontend-app-skills"
327+
"frontend-app-learning"
328+
"frontend-app-ora"
329+
"frontend-app-ora-grading"
330+
"frontend-app-exams-dashboard"
331+
"frontend-app-learner-portal-programs"
332+
"frontend-app-program-console"
333+
"frontend-app-communications"
334+
"frontend-app-discussions"
335+
"frontend-app-profile"
336+
"frontend-app-enterprise-public-catalog"
337+
"frontend-app-publisher"
338+
"frontend-app-support-tools"
339+
"frontend-app-admin-portal"
340+
"frontend-app-learner-portal-enterprise"
341+
"frontend-app-enterprise-checkout"
342+
"frontend-app-authoring"
343+
"frontend-app-instruct"
344+
"frontend-app-catalog"
345+
"openedx-translations"
346+
"frontend-app-payment"
347+
"edx-analytics-data-api"
348+
"enterprise-catalog"
349+
"portal-designer"
350+
"license-manager"
351+
"codejail-service"
352+
"enterprise-access"
353+
"frontend-app-authn"
354+
"frontend-app-course-authoring"
355+
"registrar"
356+
"frontend-app-account"
357+
"enterprise-subsidy"
358+
"edx-exams"
360359
)
361360

362361
setup_forked_repo_remotes ()
363362
{
364363
local repo_name=$1
365-
local expected_primary_org=${FORKED_REPOS[$repo_name]}
366364
local edx_remote_exists
367365
local openedx_remote_exists
368366
local origin_exists
369-
local origin_url=""
370-
local origin_org=""
367+
local existing_url=""
368+
local existing_org=""
371369
local other_org
372370
local other_remote_exists
373371
local other_url
374-
372+
375373
# Check if we're in a git repository
376374
if [ ! -d ".git" ]; then
377375
echo "ERROR: $repo_name is not a git repository"
378376
return 1
379377
fi
380-
378+
381379
# Check if both remotes already exist (idempotency check)
382380
edx_remote_exists=$(git remote | grep "^edx$" || true)
383381
openedx_remote_exists=$(git remote | grep "^openedx$" || true)
384382
origin_exists=$(git remote | grep "^origin$" || true)
385-
383+
386384
if [ -n "$edx_remote_exists" ] && [ -n "$openedx_remote_exists" ] && [ -z "$origin_exists" ]; then
387385
echo "Both edx and openedx remotes already exist in $repo_name. No changes needed."
388386
return 0
389387
fi
390-
388+
391389
echo "Setting up remotes for forked repository: $repo_name"
392-
393-
# Try to get origin URL first
394-
origin_url=$(git remote get-url origin 2>/dev/null || true)
395-
396-
if [ -n "$origin_url" ]; then
397-
# Origin exists, determine its organization
398-
if [[ $origin_url =~ github\.com[:/]edx/ ]]; then
399-
origin_org="edx"
400-
elif [[ $origin_url =~ github\.com[:/]openedx/ ]]; then
401-
origin_org="openedx"
390+
391+
# First, try to find an existing remote and its URL
392+
if [ -n "$origin_exists" ]; then
393+
# We have an 'origin' remote - determine its organization
394+
existing_url=$(git remote get-url origin 2>/dev/null || true)
395+
396+
if [[ $existing_url =~ github\.com[:/]edx/ ]]; then
397+
existing_org="edx"
398+
elif [[ $existing_url =~ github\.com[:/]openedx/ ]]; then
399+
existing_org="openedx"
402400
else
403-
echo "ERROR: Unexpected origin URL in $repo_name: $origin_url"
401+
echo "ERROR: Unexpected origin URL in $repo_name: $existing_url"
404402
echo "Expected URL to be from either edx or openedx organization"
405403
return 1
406404
fi
407-
405+
408406
# Rename origin to the correct organization name if not already done
409-
if [ -z "$(git remote | grep "^${origin_org}$")" ]; then
410-
echo "Renaming origin to '$origin_org' in $repo_name"
411-
git remote rename origin "$origin_org"
412-
if [ $? -ne 0 ]; then
413-
echo "ERROR: Failed to rename origin to $origin_org in $repo_name"
407+
if ! git remote | grep -q "^${existing_org}$"; then
408+
echo "Renaming origin to '$existing_org' in $repo_name"
409+
if ! git remote rename origin "$existing_org"; then
410+
echo "ERROR: Failed to rename origin to $existing_org in $repo_name"
414411
return 1
415412
fi
416413
else
417-
echo "Remote '$origin_org' already exists, removing origin"
414+
echo "Remote '$existing_org' already exists, removing origin"
418415
git remote remove origin 2>/dev/null || true
419416
fi
417+
elif [ -n "$edx_remote_exists" ]; then
418+
# No origin, but we have an 'edx' remote - use it as reference
419+
existing_url=$(git remote get-url edx)
420+
existing_org="edx"
421+
elif [ -n "$openedx_remote_exists" ]; then
422+
# No origin or edx, but we have an 'openedx' remote - use it as reference
423+
existing_url=$(git remote get-url openedx)
424+
existing_org="openedx"
420425
else
421-
# No origin, check if either edx or openedx remote exists to determine URL format
422-
if [ -n "$edx_remote_exists" ]; then
423-
origin_url=$(git remote get-url edx)
424-
origin_org="edx"
425-
elif [ -n "$openedx_remote_exists" ]; then
426-
origin_url=$(git remote get-url openedx)
427-
origin_org="openedx"
428-
else
429-
echo "ERROR: No origin remote and no edx/openedx remotes found in $repo_name"
430-
return 1
431-
fi
426+
echo "ERROR: No remotes found in $repo_name"
427+
return 1
432428
fi
433-
429+
434430
# Determine the other organization and add its remote if missing
435-
if [ "$origin_org" = "edx" ]; then
431+
if [ "$existing_org" = "edx" ]; then
436432
other_org="openedx"
437433
else
438434
other_org="edx"
439435
fi
440-
436+
441437
# Check if the other remote exists
442438
other_remote_exists=$(git remote | grep "^${other_org}$" || true)
443-
439+
444440
if [ -z "$other_remote_exists" ]; then
445441
# Construct the URL for the other organization
446-
if [[ $origin_url =~ ^git@ ]]; then
442+
if [[ $existing_url =~ ^git@ ]]; then
447443
# SSH URL format
448444
other_url="git@github.com:${other_org}/${repo_name}.git"
449445
else
450446
# HTTPS URL format
451447
other_url="https://github.com/${other_org}/${repo_name}.git"
452448
fi
453-
449+
454450
echo "Adding $other_org remote: $other_url"
455-
git remote add "$other_org" "$other_url"
456-
if [ $? -ne 0 ]; then
451+
if ! git remote add "$other_org" "$other_url"; then
457452
echo "ERROR: Failed to add $other_org remote in $repo_name"
458453
return 1
459454
fi
460455
else
461456
echo "Remote '$other_org' already exists in $repo_name"
462457
fi
463-
458+
464459
echo "Successfully configured remotes for $repo_name"
465460
return 0
466461
}
@@ -472,10 +467,12 @@ setup_all_forked_repo_remotes ()
472467
local skipped_repos=()
473468
local repo
474469
local name
475-
470+
local is_forked
471+
local forked_repo
472+
476473
echo "Setting up remotes for all forked repositories..."
477474
echo "========================================"
478-
475+
479476
for repo in "${repos[@]}" "${non_release_repos[@]}"
480477
do
481478
# Extract repo name from URL
@@ -484,21 +481,29 @@ setup_all_forked_repo_remotes ()
484481
continue
485482
fi
486483
name="${BASH_REMATCH[1]}"
487-
484+
488485
# Check if directory exists
489486
if [ ! -d "$name" ]; then
490487
echo "Repository $name is not cloned. Skipping."
491488
skipped_repos+=("$name")
492489
continue
493490
fi
494-
491+
495492
# Check if this repo is configured as a forked repo
496-
if [ -z "${FORKED_REPOS[$name]}" ]; then
493+
is_forked=false
494+
for forked_repo in "${FORKED_REPOS[@]}"; do
495+
if [[ "$forked_repo" == "$name" ]]; then
496+
is_forked=true
497+
break
498+
fi
499+
done
500+
501+
if [[ "$is_forked" == false ]]; then
497502
echo "Repository $name is not configured as a forked repo. Skipping."
498503
skipped_repos+=("$name")
499504
continue
500505
fi
501-
506+
502507
# Change to repo directory and setup remotes
503508
cd "$name"
504509
if setup_forked_repo_remotes "$name"; then
@@ -509,32 +514,32 @@ setup_all_forked_repo_remotes ()
509514
cd "$DEVSTACK_WORKSPACE"
510515
echo ""
511516
done
512-
517+
513518
# Print summary report
514519
echo "========================================"
515520
echo "Remote Setup Summary:"
516521
echo "========================================"
517-
522+
518523
if [ ${#successful_repos[@]} -gt 0 ]; then
519524
echo "✓ Successfully configured remotes for ${#successful_repos[@]} repositories:"
520525
printf " - %s\n" "${successful_repos[@]}"
521526
echo ""
522527
fi
523-
528+
524529
if [ ${#failed_repos[@]} -gt 0 ]; then
525530
echo "✗ Failed to configure remotes for ${#failed_repos[@]} repositories:"
526531
printf " - %s\n" "${failed_repos[@]}"
527532
echo ""
528533
fi
529-
534+
530535
if [ ${#skipped_repos[@]} -gt 0 ]; then
531536
echo "◦ Skipped ${#skipped_repos[@]} repositories (not cloned or not forked):"
532537
printf " - %s\n" "${skipped_repos[@]}"
533538
echo ""
534539
fi
535-
540+
536541
echo "Total repositories processed: $((${#successful_repos[@]} + ${#failed_repos[@]} + ${#skipped_repos[@]}))"
537-
542+
538543
if [ ${#failed_repos[@]} -gt 0 ]; then
539544
return 1
540545
else

0 commit comments

Comments
 (0)