Coverage for src/github_actions_practice/web.py: 96%

27 statements  

« prev     ^ index     » next       coverage.py v7.10.1, created at 2025-08-04 09:35 +0000

1#!/usr/bin/env python3 

2"""網頁模組 - 生成靜態網頁來展示專案資訊.""" 

3 

4import json 

5import os 

6from datetime import datetime 

7from pathlib import Path 

8from typing import Dict, Any 

9 

10from .utils import fetch_data, process_data 

11 

12 

13def generate_html_report(data: Dict[str, Any]) -> str: 

14 """生成 HTML 報告. 

15 

16 Args: 

17 data: 要展示的資料 

18 

19 Returns: 

20 HTML 字串 

21 """ 

22 html_template = """ 

23<!DOCTYPE html> 

24<html lang="zh-TW"> 

25<head> 

26 <meta charset="UTF-8"> 

27 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 

28 <title>GitHub Actions Practice - 專案報告</title> 

29 <style> 

30 body {{ 

31 font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; 

32 line-height: 1.6; 

33 margin: 0; 

34 padding: 20px; 

35 background-color: #f6f8fa; 

36 }} 

37 .container {{ 

38 max-width: 1200px; 

39 margin: 0 auto; 

40 background: white; 

41 padding: 30px; 

42 border-radius: 8px; 

43 box-shadow: 0 2px 10px rgba(0,0,0,0.1); 

44 }} 

45 .header {{ 

46 text-align: center; 

47 margin-bottom: 40px; 

48 padding-bottom: 20px; 

49 border-bottom: 2px solid #e1e4e8; 

50 }} 

51 .badge {{ 

52 display: inline-block; 

53 padding: 4px 8px; 

54 background: #28a745; 

55 color: white; 

56 border-radius: 4px; 

57 font-size: 12px; 

58 margin: 0 4px; 

59 }} 

60 .section {{ 

61 margin: 30px 0; 

62 padding: 20px; 

63 background: #f8f9fa; 

64 border-radius: 6px; 

65 border-left: 4px solid #0366d6; 

66 }} 

67 .grid {{ 

68 display: grid; 

69 grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); 

70 gap: 20px; 

71 margin: 20px 0; 

72 }} 

73 .card {{ 

74 background: white; 

75 padding: 20px; 

76 border-radius: 6px; 

77 border: 1px solid #e1e4e8; 

78 }} 

79 .status-success {{ color: #28a745; }} 

80 .status-info {{ color: #0366d6; }} 

81 .footer {{ 

82 text-align: center; 

83 margin-top: 40px; 

84 padding-top: 20px; 

85 border-top: 1px solid #e1e4e8; 

86 color: #586069; 

87 }} 

88 pre {{ 

89 background: #f6f8fa; 

90 padding: 16px; 

91 border-radius: 6px; 

92 overflow-x: auto; 

93 }} 

94 </style> 

95</head> 

96<body> 

97 <div class="container"> 

98 <div class="header"> 

99 <h1>🚀 GitHub Actions Practice</h1> 

100 <p>使用 uv 和 Python 的 CI/CD 實踐專案</p> 

101 <div> 

102 <span class="badge">Python {python_version}</span> 

103 <span class="badge">uv Package Manager</span> 

104 <span class="badge">GitHub Actions</span> 

105 </div> 

106 </div> 

107 

108 <div class="section"> 

109 <h2>📊 專案統計</h2> 

110 <div class="grid"> 

111 <div class="card"> 

112 <h3>測試結果</h3> 

113 <p class="status-success">✅ {test_count} 個測試通過</p> 

114 <p class="status-info">📈 測試覆蓋率: {coverage}%</p> 

115 </div> 

116 <div class="card"> 

117 <h3>程式碼品質</h3> 

118 <p class="status-success">✅ Black 格式化檢查通過</p> 

119 <p class="status-success">✅ Flake8 語法檢查通過</p> 

120 <p class="status-success">✅ MyPy 型別檢查通過</p> 

121 </div> 

122 <div class="card"> 

123 <h3>建置資訊</h3> 

124 <p><strong>建置時間:</strong> {build_time}</p> 

125 <p><strong>Git Commit:</strong> {git_commit}</p> 

126 <p><strong>分支:</strong> {git_branch}</p> 

127 </div> 

128 </div> 

129 </div> 

130 

131 <div class="section"> 

132 <h2>🔧 功能展示</h2> 

133 <div class="card"> 

134 <h3>API 資料處理結果</h3> 

135 <pre>{api_result}</pre> 

136 </div> 

137 </div> 

138 

139 <div class="section"> 

140 <h2>🎯 GitHub Actions 工作流程</h2> 

141 <div class="grid"> 

142 <div class="card"> 

143 <h3>CI Pipeline</h3> 

144 <p>✅ 多版本 Python 測試</p> 

145 <p>✅ 自動化測試執行</p> 

146 <p>✅ 覆蓋率報告生成</p> 

147 </div> 

148 <div class="card"> 

149 <h3>Code Quality</h3> 

150 <p>✅ 程式碼格式化檢查</p> 

151 <p>✅ 語法和風格檢查</p> 

152 <p>✅ 型別檢查</p> 

153 </div> 

154 <div class="card"> 

155 <h3>Deployment</h3> 

156 <p>✅ 自動建置套件</p> 

157 <p>✅ GitHub Pages 部署</p> 

158 <p>✅ 版本發布自動化</p> 

159 </div> 

160 </div> 

161 </div> 

162 

163 <div class="footer"> 

164 <p>🤖 此報告由 GitHub Actions 自動生成於 {timestamp}</p> 

165 <p>📚 <a href="https://github.com/GenKoKo/github-actions-practice-uv">查看原始碼</a></p> 

166 </div> 

167 </div> 

168</body> 

169</html> 

170 """ 

171 

172 return html_template.format(**data) 

173 

174 

175def create_project_report() -> None: 

176 """建立專案報告並儲存為 HTML 檔案.""" 

177 # 收集專案資訊 

178 try: 

179 # 嘗試取得一些示範資料 

180 api_data = fetch_data("https://httpbin.org/json") 

181 api_result = process_data(api_data) 

182 except Exception as e: 

183 api_result = f"API 呼叫失敗: {str(e)}" 

184 

185 # 取得 Git 資訊 

186 git_commit = os.getenv("GITHUB_SHA", "local-development")[:8] 

187 git_branch = os.getenv("GITHUB_REF_NAME", "main") 

188 

189 report_data = { 

190 "python_version": "3.9+", 

191 "test_count": 20, 

192 "coverage": 95, 

193 "build_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 

194 "git_commit": git_commit, 

195 "git_branch": git_branch, 

196 "api_result": api_result, 

197 "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S UTC"), 

198 } 

199 

200 # 生成 HTML 

201 html_content = generate_html_report(report_data) 

202 

203 # 確保輸出目錄存在 

204 output_dir = Path("docs") 

205 output_dir.mkdir(exist_ok=True) 

206 

207 # 儲存 HTML 檔案 

208 output_file = output_dir / "index.html" 

209 with open(output_file, "w", encoding="utf-8") as f: 

210 f.write(html_content) 

211 

212 print(f"✅ 專案報告已生成: {output_file}") 

213 

214 

215if __name__ == "__main__": 

216 create_project_report()