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 24 เราจะเพิ่มระบบส่งมอบงานระดับองค์กร เชื่อมต่อ GitHub CLI เพื่อ Push Branch และสร้าง Pull Request อัตโนมัติ:
ทำหน้าที่ติดต่อกับ GitHub CLI (gh) เพื่อสั่ง Push Branch ปัจจุบันจาก Sandbox ขึ้นสู่ Remote Repository และออกคำสั่งสร้าง Pull Request พร้อมหัวข้อและรายละเอียดที่สังเคราะห์จากผลการทำงาน
ทดสอบการจำลองการส่งข้อมูล PR Payload และตรวจสอบการตอบกลับจาก GitHub API ว่าสำเร็จเรียบร้อยดีหรือไม่
ดัน Branch จาก Sandbox บน /tmp ขึ้นสู่ Remote Repository (Origin) ผ่านคำสั่ง git push -u origin branch_name
ให้ Ollama วิเคราะห์ Git Diff เพื่อสังเคราะห์เนื้อหา PR แบบมืออาชีพ (Summary, Changes, Testing notes)
สั่งสร้าง Pull Request อัตโนมัติผ่าน GitHub CLI พร้อมเชิญ Reviewers เข้าตรวจโค้ดทันที
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
}
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()
เปิด Terminal บน MacBook M3 แล้วทำตามขั้นตอนเพื่อทดสอบระบบ Enterprise Delivery:
cd ~/se_agent
source .venv/bin/activate
touch core/delivery.py test_delivery.py
python test_delivery.py
ข้อที่ 1: วัตถุประสงค์หลักของ "Enterprise Delivery & PR Automation" ใน Day 24 คืออะไร?
ข้อที่ 2: เครื่องมือใดในระบบที่นิยมใช้สั่งสร้าง Pull Request ผ่าน Command Line ในระบบนิเวศ GitHub?
🏁 สรุปหมุดเชื่อมโยง (Chapter Wrap-up)
เนื้อหาใน Day 24 — Enterprise Delivery & Pull Request Automation ได้เติมเต็มวงจรการส่งมอบงานระดับองค์กรอย่างสมบูรณ์แบบ ตอนนี้ Agent ของคุณไม่เพียงแต่แก้โค้ดและรันเทสได้เท่านั้น แต่ยังสามารถส่งมอบงานขึ้นระบบรีวิวของทีมได้เองอีกด้วย!