@@ -247,7 +247,7 @@ function handle_custom_action() {
247247
248248---
249249
250- * Auto-generated by [ WP Code Check] ( https://github .com/Hypercart-Dev-Tools/WP-Code-Check ) v1.2.2*
250+ * 🤖 AI Supercharged Code Review by [ WP Code Check] ( https://wpcodecheck .com ) v1.2.2*
251251```
252252
253253---
@@ -330,10 +330,12 @@ gh issue list --repo "$GITHUB_REPO" --search "in:title [WP Code Check] Scan Repo
330330- Add GitHub integration fields to ` _TEMPLATE.txt `
331331- Update ` _AI_INSTRUCTIONS.md ` with Phase 3 workflow
332332
333- ### ** Step 2: Add Repo Detection**
334- - Create ` detect-github-repo.sh ` helper
335- - Parse git remote URL
336- - Validate access with ` gh ` CLI
333+ ### ** Step 2: Add Repo Detection & Template Enhancement**
334+ - ** Update ` _TEMPLATE.txt ` ** - Add GitHub integration section
335+ - ** Update existing templates** - Add ` GITHUB_REPO= ` field (optional, auto-detected if blank)
336+ - ** Create ` detect-github-repo.sh ` helper** - Auto-detect from git remote if not in template
337+ - ** Parse git remote URL** - Extract owner/repo from various formats
338+ - ** Validate access with ` gh ` CLI** - Check permissions before creating issues
337339
338340### ** Step 3: Parent Issue Template Generator**
339341- Create ` generate-parent-issue-body.sh `
@@ -540,3 +542,364 @@ Instead of creating 5-10 individual issues per scan (noisy), create **ONE parent
5405426 . Document workflow in README
541543
542544** No action taken yet** - awaiting your approval to proceed! 🎯
545+
546+ ---
547+
548+ ## 📋 Appendix A: Template Updates & Fallback Strategy
549+
550+ ### ** Question 1: Do we need to update existing templates?**
551+
552+ ** Answer:** No, existing templates will continue to work. GitHub integration is ** opt-in** and ** auto-detected** .
553+
554+ ### ** Question 2: What's the fallback if template doesn't have GITHUB_REPO?**
555+
556+ ** Answer:** Multi-layer fallback strategy with auto-detection.
557+
558+ ---
559+
560+ ## 🔧 Template Enhancement Strategy
561+
562+ ### ** 1. Update ` _TEMPLATE.txt ` (Reference Template)**
563+
564+ Add new optional section:
565+
566+ ``` bash
567+ # ============================================================
568+ # GITHUB INTEGRATION (Optional - Phase 3)
569+ # ============================================================
570+
571+ # GitHub repository (owner/repo format)
572+ # If blank, will auto-detect from git remote in PROJECT_PATH
573+ # Example: GITHUB_REPO=Hypercart-Dev-Tools/Server-Monitor-MKII
574+ # GITHUB_REPO=
575+
576+ # Auto-create GitHub issue after scan (requires gh CLI)
577+ # Default: false (manual review required)
578+ # GITHUB_AUTO_ISSUE=false
579+
580+ # Issue labels (comma-separated)
581+ # Default: code-quality,wp-code-check
582+ # GITHUB_ISSUE_LABELS=code-quality,wp-code-check
583+
584+ # Issue assignee (@username or leave blank)
585+ # Default: @me (current gh CLI user)
586+ # GITHUB_ASSIGNEE=@me
587+
588+ # Milestone (must exist in repo)
589+ # Default: none
590+ # GITHUB_MILESTONE=
591+
592+ # Allow multiple issues per day (default: false)
593+ # If false, will add comment to existing issue instead of creating new one
594+ # GITHUB_ALLOW_MULTIPLE_DAILY_SCANS=false
595+ ```
596+
597+ ### ** 2. Existing Templates - Backward Compatibility**
598+
599+ ** No changes required!** Existing templates like ` hypercart-server-monitor-mkii.txt ` will:
600+ - ✅ Continue to work exactly as before
601+ - ✅ Auto-detect GitHub repo if ` GITHUB_REPO ` is not set
602+ - ✅ Skip GitHub integration if ` GITHUB_AUTO_ISSUE ` is not set to ` true `
603+
604+ ** Optional enhancement:** Users can manually add ` GITHUB_REPO= ` field if they want to override auto-detection.
605+
606+ ---
607+
608+ ## 🔍 Fallback Detection Strategy
609+
610+ ### ** Layer 1: Template Configuration (Explicit)**
611+
612+ ``` bash
613+ # User explicitly sets in template
614+ GITHUB_REPO=Hypercart-Dev-Tools/Server-Monitor-MKII
615+ ```
616+
617+ ** Priority:** Highest (user knows best)
618+
619+ ### ** Layer 2: Git Remote Auto-Detection (Smart)**
620+
621+ If ` GITHUB_REPO ` is blank or not set:
622+
623+ ``` bash
624+ # detect-github-repo.sh logic:
625+
626+ # 1. Check if PROJECT_PATH is a git repository
627+ cd " $PROJECT_PATH " || exit 1
628+ if ! git rev-parse --git-dir > /dev/null 2>&1 ; then
629+ echo " Not a git repository" >&2
630+ exit 1
631+ fi
632+
633+ # 2. Get remote URL
634+ REMOTE_URL=$( git remote get-url origin 2> /dev/null)
635+
636+ # 3. Parse owner/repo from various formats:
637+ # - https://github.com/owner/repo.git
638+ # - git@github.com:owner/repo.git
639+ # - https://github.com/owner/repo
640+ # - git://github.com/owner/repo.git
641+
642+ GITHUB_REPO=$( echo " $REMOTE_URL " | sed -E ' s#.*github\.com[:/]([^/]+/[^/]+)(\.git)?$#\1#' )
643+
644+ # 4. Validate format (owner/repo)
645+ if [[ ! " $GITHUB_REPO " =~ ^[a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+$ ]]; then
646+ echo " Invalid GitHub repo format: $GITHUB_REPO " >&2
647+ exit 1
648+ fi
649+
650+ echo " $GITHUB_REPO "
651+ ```
652+
653+ ** Priority:** Medium (reliable for GitHub-hosted projects)
654+
655+ ### ** Layer 3: Manual Specification (Fallback)**
656+
657+ If auto-detection fails:
658+
659+ ``` bash
660+ # Scanner prompts user (if --create-github-issue flag is used):
661+ echo " ⚠️ Could not auto-detect GitHub repository"
662+ echo " Please specify repository in template or use --github-repo flag:"
663+ echo " "
664+ echo " ./dist/bin/run my-plugin --create-github-issue --github-repo owner/repo"
665+ echo " "
666+ echo " Or add to template:"
667+ echo " GITHUB_REPO=owner/repo"
668+ exit 1
669+ ```
670+
671+ ** Priority:** Lowest (requires user intervention)
672+
673+ ### ** Layer 4: Skip GitHub Integration (Safe Default)**
674+
675+ If all detection fails and user didn't explicitly request GitHub integration:
676+
677+ ``` bash
678+ # Silently skip GitHub integration
679+ # Scan completes normally, no issue created
680+ # User can manually create issue from JSON log later
681+ ```
682+
683+ ** Priority:** Safest (no errors, no spam)
684+
685+ ---
686+
687+ ## 🎯 Detection Flow Diagram
688+
689+ ```
690+ ┌─────────────────────────────────────┐
691+ │ User runs scan with template │
692+ └─────────────┬───────────────────────┘
693+ │
694+ ▼
695+ ┌───────────────────┐
696+ │ GITHUB_AUTO_ISSUE │
697+ │ = true? │
698+ └───────┬───────────┘
699+ │
700+ ┌─────┴─────┐
701+ │ │
702+ NO YES
703+ │ │
704+ │ ▼
705+ │ ┌───────────────────┐
706+ │ │ GITHUB_REPO set │
707+ │ │ in template? │
708+ │ └───────┬───────────┘
709+ │ │
710+ │ ┌─────┴─────┐
711+ │ │ │
712+ │ YES NO
713+ │ │ │
714+ │ │ ▼
715+ │ │ ┌───────────────────┐
716+ │ │ │ Auto-detect from │
717+ │ │ │ git remote │
718+ │ │ └───────┬───────────┘
719+ │ │ │
720+ │ │ ┌─────┴─────┐
721+ │ │ │ │
722+ │ │ SUCCESS FAIL
723+ │ │ │ │
724+ │ └─────┤ │
725+ │ │ ▼
726+ │ │ ┌───────────────────┐
727+ │ │ │ Prompt user or │
728+ │ │ │ skip integration │
729+ │ │ └───────────────────┘
730+ │ │
731+ │ ▼
732+ │ ┌───────────────────┐
733+ │ │ Validate gh CLI │
734+ │ │ access │
735+ │ └───────┬───────────┘
736+ │ │
737+ │ ┌─────┴─────┐
738+ │ │ │
739+ │ SUCCESS FAIL
740+ │ │ │
741+ │ │ ▼
742+ │ │ ┌───────────────────┐
743+ │ │ │ Error: gh CLI not │
744+ │ │ │ authenticated │
745+ │ │ └───────────────────┘
746+ │ │
747+ │ ▼
748+ │ ┌───────────────────┐
749+ │ │ Create parent │
750+ │ │ GitHub issue │
751+ │ └───────────────────┘
752+ │
753+ ▼
754+ ┌─────────────────────────────────────┐
755+ │ Scan complete (no GitHub issue) │
756+ └─────────────────────────────────────┘
757+ ```
758+
759+ ---
760+
761+ ## 🛠️ Implementation Details
762+
763+ ### ** File: ` dist/bin/lib/detect-github-repo.sh ` **
764+
765+ ``` bash
766+ #! /usr/bin/env bash
767+ # Detect GitHub repository from git remote or template configuration
768+ # Usage: detect_github_repo <project_path> [template_repo]
769+ # Returns: owner/repo format or exits with error
770+
771+ detect_github_repo () {
772+ local project_path=" $1 "
773+ local template_repo=" ${2:- } "
774+
775+ # Layer 1: Use template value if provided
776+ if [ -n " $template_repo " ]; then
777+ echo " $template_repo "
778+ return 0
779+ fi
780+
781+ # Layer 2: Auto-detect from git remote
782+ if [ ! -d " $project_path " ]; then
783+ echo " Error: Project path does not exist: $project_path " >&2
784+ return 1
785+ fi
786+
787+ cd " $project_path " || return 1
788+
789+ # Check if git repository
790+ if ! git rev-parse --git-dir > /dev/null 2>&1 ; then
791+ echo " Error: Not a git repository: $project_path " >&2
792+ return 1
793+ fi
794+
795+ # Get remote URL (try origin first, then any remote)
796+ local remote_url
797+ remote_url=$( git remote get-url origin 2> /dev/null)
798+ if [ -z " $remote_url " ]; then
799+ remote_url=$( git remote get-url " $( git remote | head -1) " 2> /dev/null)
800+ fi
801+
802+ if [ -z " $remote_url " ]; then
803+ echo " Error: No git remote found" >&2
804+ return 1
805+ fi
806+
807+ # Parse GitHub repo from various URL formats
808+ local github_repo
809+ github_repo=$( echo " $remote_url " | sed -E ' s#.*github\.com[:/]([^/]+/[^/]+)(\.git)?$#\1#' )
810+
811+ # Validate format
812+ if [[ ! " $github_repo " =~ ^[a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+$ ]]; then
813+ echo " Error: Could not parse GitHub repo from: $remote_url " >&2
814+ return 1
815+ fi
816+
817+ echo " $github_repo "
818+ return 0
819+ }
820+
821+ # If sourced, export function; if executed, run it
822+ if [ " ${BASH_SOURCE[0]} " = " ${0} " ]; then
823+ detect_github_repo " $@ "
824+ fi
825+ ```
826+
827+ ### ** Integration in ` check-performance.sh ` **
828+
829+ Add after template loading (around line 304):
830+
831+ ``` bash
832+ # Load template variables
833+ source " $TEMPLATE_FILE "
834+
835+ # Apply template variables
836+ if [ -n " ${PROJECT_PATH:- } " ]; then
837+ PATHS=" $PROJECT_PATH "
838+ fi
839+
840+ # NEW: Detect GitHub repository if GitHub integration is enabled
841+ if [ " ${GITHUB_AUTO_ISSUE:- false} " = " true" ]; then
842+ # Source the detection helper
843+ source " $LIB_DIR /detect-github-repo.sh"
844+
845+ # Detect or use template value
846+ DETECTED_GITHUB_REPO=$( detect_github_repo " $PATHS " " ${GITHUB_REPO:- } " )
847+
848+ if [ $? -eq 0 ]; then
849+ GITHUB_REPO=" $DETECTED_GITHUB_REPO "
850+ echo " ✓ GitHub repository detected: $GITHUB_REPO "
851+ else
852+ echo " ⚠️ Could not detect GitHub repository"
853+ echo " Add GITHUB_REPO=owner/repo to template or use --github-repo flag"
854+ exit 1
855+ fi
856+
857+ # Validate gh CLI access
858+ if ! command -v gh & > /dev/null; then
859+ echo " ⚠️ GitHub CLI (gh) not found"
860+ echo " Install: https://cli.github.com/"
861+ exit 1
862+ fi
863+
864+ if ! gh auth status & > /dev/null; then
865+ echo " ⚠️ GitHub CLI not authenticated"
866+ echo " Run: gh auth login"
867+ exit 1
868+ fi
869+
870+ # Check repository access
871+ if ! gh repo view " $GITHUB_REPO " & > /dev/null; then
872+ echo " ⚠️ Cannot access repository: $GITHUB_REPO "
873+ echo " Check repository name and permissions"
874+ exit 1
875+ fi
876+ fi
877+ ```
878+
879+ ---
880+
881+ ## ✅ Summary: Template Update Strategy
882+
883+ ### ** Existing Templates**
884+ - ✅ ** No changes required** - backward compatible
885+ - ✅ ** Auto-detection works** - if project is in git repo with GitHub remote
886+ - ✅ ** Opt-in only** - GitHub integration disabled by default
887+
888+ ### ** New Templates**
889+ - ✅ ** Include GitHub section** - from updated ` _TEMPLATE.txt `
890+ - ✅ ** Pre-filled if auto-detected** - AI agent can detect during template completion
891+ - ✅ ** Optional field** - can be left blank for auto-detection
892+
893+ ### ** Fallback Strategy**
894+ 1 . ** Template value** (if set) → Use it
895+ 2 . ** Git remote** (if available) → Auto-detect
896+ 3 . ** User prompt** (if --create-github-issue flag used) → Ask for it
897+ 4 . ** Skip integration** (if no flag) → Silent fallback
898+
899+ ### ** Migration Path**
900+ - ** Phase 1:** Update ` _TEMPLATE.txt ` with new section
901+ - ** Phase 2:** Existing templates work as-is (no migration needed)
902+ - ** Phase 3:** Users can optionally add ` GITHUB_REPO= ` to templates over time
903+ - ** Phase 4:** AI agent auto-completes new templates with detected repo
904+
905+ ** Zero breaking changes, maximum flexibility!** 🎉
0 commit comments