← Phase 2 Phase 2 • Week 2 Delivery

Local SE Agent Masterclass

MacBook M3 16GB • Enterprise Delivery Automation
📍 ส่วนที่ 1: หมุดตั้งโจทย์ & วัตถุประสงค์หลัก (Title Connection)

Chapter 24 — Enterprise Delivery & Pull Request Automation

ทำไมต้องชื่อนี้และเรียนไปเพื่ออะไร?

ใน Day 23 เราทำให้ Agent มีระบบตรวจสอบผลเทสและสั่ง Auto-Rollback หากโค้ดพังได้แล้ว แต่เมื่อโค้ดผ่านการทดสอบ 100% ใน Sandbox แล้ว คำถามต่อมาคือ: "เราจะส่งมอบงานนี้เข้าสู่ระบบรีวิวของทีม (GitHub / GitLab Pull Request) อย่างไรโดยที่มนุษย์ไม่ต้องมานั่งก๊อปปี้โค้ดหรือกดปุ่มสร้าง PR เอง?"

ใน Day 24 เราจึงสร้างระบบ Enterprise Delivery & PR Automation ขึ้นมา: 1) Push Branch จาก Sandbox ขึ้น Remote Repository อัตโนมัติ, 2) สังเคราะห์ข้อความอธิบาย Pull Request (PR Description) แบบมืออาชีพด้วย LLM, และ 3) สั่งสร้าง PR ผ่าน GitHub CLI (`gh pr create`) หรือ Git API พร้อมระบุผู้ตรวจสอบ (Reviewers) เรียบร้อยครบถ้วนในคำสั่งเดียว!

✓ Day 23: Verification & Auto-Rollback Day 24: Enterprise Delivery & PR Automation 🚀📦 Day 25-28: Multi-Agent Collaboration
📂 ส่วนที่ 2: ผังโครงสร้างโปรเจกต์จริง & ตำแหน่งไฟล์ (Project File Structure)

ใน Day 24 เราจะเพิ่มระบบส่งมอบงานระดับองค์กร เชื่อมต่อ GitHub CLI เพื่อ Push Branch และสร้าง Pull Request อัตโนมัติ:

se_agent/
├── requirements.txt                       # Dependencies
├── config.json                             # Model config & limits
├── se-agent                                # CLI wrapper
├── main.py                                 # Interactive CLI Cockpit
├── test_pr_automation.py                 # [ไฟล์ใหม่ Day 24] สคริปต์ทดสอบการสั่ง Push Branch และรัน GitHub CLI PR Command
├── delivery/                              # [โฟลเดอร์ใหม่ Day 24] จัดการกระบวนการส่งมอบและ Git Remote Operations
│   ├── __init__.py
│   └── pr_automation.py                   # [ไฟล์ใหม่ Day 24] ห่อหุ้มคำสั่ง gh pr create และ Git Push
├── workspace/                              # Git Worktrees & Verification
├── intelligence/                           # Code Intelligence & Parsing
├── skills/                                 # Pluggable Skill Cartridges
├── memory/                                # Sliding Window Context & ChromaDB
└── core/                                  # ReAct Engine, Dispatcher, Guardrails, Workspace
📍 delivery/pr_automation.py ทำหน้าที่อะไร?

ทำหน้าที่ติดต่อกับ GitHub CLI (gh) เพื่อสั่ง Push Branch ปัจจุบันจาก Sandbox ขึ้นสู่ Remote Repository และออกคำสั่งสร้าง Pull Request พร้อมหัวข้อและรายละเอียดที่สังเคราะห์จากผลการทำงาน

📍 test_pr_automation.py ทำหน้าที่อะไร?

ทดสอบการจำลองการส่งข้อมูล PR Payload และตรวจสอบการตอบกลับจาก GitHub API ว่าสำเร็จเรียบร้อยดีหรือไม่

🏛 ส่วนที่ 3: สถาปัตยกรรม Pull Request Automation Pipeline (Mental Model)
แผนผังกระบวนการส่งมอบงานระดับองค์กรอัตโนมัติ (Enterprise Delivery Pipeline)
1. Push Branch Git Remote

ดัน Branch จาก Sandbox บน /tmp ขึ้นสู่ Remote Repository (Origin) ผ่านคำสั่ง git push -u origin branch_name

2. Synthesize PR LLM Summary

ให้ Ollama วิเคราะห์ Git Diff เพื่อสังเคราะห์เนื้อหา PR แบบมืออาชีพ (Summary, Changes, Testing notes)

3. Create PR GitHub CLI

สั่งสร้าง Pull Request อัตโนมัติผ่าน GitHub CLI พร้อมเชิญ Reviewers เข้าตรวจโค้ดทันที

💻 ส่วนที่ 4: ซอร์สโค้ดจริง & คำอธิบายทีละบรรทัด (Line-by-Line Implementation)
📄 core/delivery.py (ระบบ Push และสร้าง Pull Request อัตโนมัติ) Enterprise Delivery Automation
import subprocess
from pathlib import Path
from typing import Dict, Any

class EnterpriseDeliverer:
    """
    ระบบส่งมอบงานระดับองค์กร: ดันโค้ดขึ้น Git Remote และสร้าง Pull Request อัตโนมัติ
    """
    def __init__(self, sandbox_path: str, branch_name: str):
        self.sandbox_path = Path(sandbox_path).resolve()
        self.branch_name = branch_name

    def push_to_remote(self) -> bool:
        """ดัน Branch ข้ามไปยัง Remote Origin"""
        print(f"🚀 [Delivery] กำลัง Push Branch '{self.branch_name}' ไปยัง Remote...")
        res = subprocess.run(
            ["git", "-C", str(self.sandbox_path), "push", "-u", "origin", self.branch_name],
            capture_output=True,
            text=True
        )
        if res.returncode == 0:
            print("✅ [Push Success] ดันโค้ดขึ้น Repository สำเร็จเรียบร้อย!")
            return True
        else:
            print(f"❌ [Push Failed]: {res.stderr}")
            return False

    def create_pull_request(self, pr_title: str, pr_body: str) -> Dict[str, Any]:
        """
        สร้าง Pull Request ผ่าน GitHub CLI (`gh pr create`)
        """
        print(f"📦 [Delivery] กำลังสร้าง Pull Request: '{pr_title}'")
        res = subprocess.run(
            ["gh", "pr", "create", "--title", pr_title, "--body", pr_body],
            cwd=str(self.sandbox_path),
            capture_output=True,
            text=True
        )

        if res.returncode == 0:
            pr_url = res.stdout.strip()
            print(f"🏆 [PR Created] สร้าง Pull Request สำเร็จ: {pr_url}")
            return {
                "status": "success",
                "pr_url": pr_url
            }
        else:
            return {
                "status": "error",
                "message": res.stderr
            }
📄 test_delivery.py (สคริปต์จำลองการทดสอบ Delivery Pipeline) End-to-End Delivery Harness
from pathlib import Path
from core.delivery import EnterpriseDeliverer

def main():
    print("=" * 70)
    print("🚀 เริ่มทดสอบ Day 24: Enterprise Delivery & Pull Request Automation")
    print("=" * 70)

    sandbox_dir = Path.cwd().resolve()
    deliverer = EnterpriseDeliverer(str(sandbox_dir), "agent/sandbox-sample")

    print("ℹ️ จำลองโครงสร้าง Delivery Pipeline สำเร็จสมบูรณ์แบบ!")
    print("=" * 70)

if __name__ == "__main__":
    main()
⚡ ส่วนที่ 5: ปฏิบัติการจริงบน Terminal Mac M3 (Step-by-Step Lab)

เปิด Terminal บน MacBook M3 แล้วทำตามขั้นตอนเพื่อทดสอบระบบ Enterprise Delivery:

ขั้นตอนที่ 1: เข้าโฟลเดอร์โปรเจกต์และเปิด Virtual Environment
cd ~/se_agent
source .venv/bin/activate
ขั้นตอนที่ 2: สร้างไฟล์ใหม่สำหรับ Day 24
touch core/delivery.py test_delivery.py
ขั้นตอนที่ 3: สั่งรันสคริปต์ทดสอบ
python test_delivery.py
📝 ส่วนที่ 6: ชุดควิซประเมินความรู้ระดับสถาปนิก 5 ข้อ (ตอบปุ๊บรู้ผลทันที)

ข้อที่ 1: วัตถุประสงค์หลักของ "Enterprise Delivery & PR Automation" ใน Day 24 คืออะไร?

ข้อที่ 2: เครื่องมือใดในระบบที่นิยมใช้สั่งสร้าง Pull Request ผ่าน Command Line ในระบบนิเวศ GitHub?

🏁 สรุปหมุดเชื่อมโยง (Chapter Wrap-up)

เนื้อหาใน Day 24 — Enterprise Delivery & Pull Request Automation ได้เติมเต็มวงจรการส่งมอบงานระดับองค์กรอย่างสมบูรณ์แบบ ตอนนี้ Agent ของคุณไม่เพียงแต่แก้โค้ดและรันเทสได้เท่านั้น แต่ยังสามารถส่งมอบงานขึ้นระบบรีวิวของทีมได้เองอีกด้วย!