1
- import requests
2
- from datetime import datetime , date , timedelta
3
- import os
4
- from logger import LOG
1
+ # src/github_client.py
2
+
3
+ import requests # 导入requests库用于HTTP请求
4
+ from datetime import datetime , date , timedelta # 导入日期处理模块
5
+ import os # 导入os模块用于文件和目录操作
6
+ from logger import LOG # 导入日志模块
5
7
6
8
class GitHubClient :
7
9
def __init__ (self , token ):
8
- self .token = token
9
- self .headers = {'Authorization' : f'token { self .token } ' }
10
+ self .token = token # GitHub API令牌
11
+ self .headers = {'Authorization' : f'token { self .token } ' } # 设置HTTP头部认证信息
10
12
11
13
def fetch_updates (self , repo , since = None , until = None ):
14
+ # 获取指定仓库的更新,可以指定开始和结束日期
12
15
updates = {
13
- 'commits' : self .fetch_commits (repo , since , until ),
14
- 'issues' : self .fetch_issues (repo , since , until ),
15
- 'pull_requests' : self .fetch_pull_requests (repo , since , until )
16
+ 'commits' : self .fetch_commits (repo , since , until ), # 获取提交记录
17
+ 'issues' : self .fetch_issues (repo , since , until ), # 获取问题
18
+ 'pull_requests' : self .fetch_pull_requests (repo , since , until ) # 获取拉取请求
16
19
}
17
20
return updates
18
21
19
22
def fetch_commits (self , repo , since = None , until = None ):
20
- url = f'https://api.github.com/repos/{ repo } /commits'
23
+ url = f'https://api.github.com/repos/{ repo } /commits' # 构建获取提交的API URL
21
24
params = {}
22
25
if since :
23
- params ['since' ] = since
26
+ params ['since' ] = since # 如果指定了开始日期,添加到参数中
24
27
if until :
25
- params ['until' ] = until
28
+ params ['until' ] = until # 如果指定了结束日期,添加到参数中
26
29
27
30
response = requests .get (url , headers = self .headers , params = params )
28
- response .raise_for_status ()
29
- return response .json ()
31
+ response .raise_for_status () # 检查请求是否成功
32
+ return response .json () # 返回JSON格式的数据
30
33
31
34
def fetch_issues (self , repo , since = None , until = None ):
32
- url = f'https://api.github.com/repos/{ repo } /issues'
35
+ url = f'https://api.github.com/repos/{ repo } /issues' # 构建获取问题的API URL
33
36
params = {
34
- 'state' : 'closed' ,
37
+ 'state' : 'closed' , # 仅获取已关闭的问题
35
38
'since' : since ,
36
39
'until' : until
37
40
}
@@ -40,9 +43,9 @@ def fetch_issues(self, repo, since=None, until=None):
40
43
return response .json ()
41
44
42
45
def fetch_pull_requests (self , repo , since = None , until = None ):
43
- url = f'https://api.github.com/repos/{ repo } /pulls'
46
+ url = f'https://api.github.com/repos/{ repo } /pulls' # 构建获取拉取请求的API URL
44
47
params = {
45
- 'state' : 'closed' ,
48
+ 'state' : 'closed' , # 仅获取已合并的拉取请求
46
49
'since' : since ,
47
50
'until' : until
48
51
}
@@ -51,46 +54,46 @@ def fetch_pull_requests(self, repo, since=None, until=None):
51
54
return response .json ()
52
55
53
56
def export_daily_progress (self , repo ):
54
- today = datetime .now ().date ().isoformat ()
55
- updates = self .fetch_updates (repo , since = today )
57
+ today = datetime .now ().date ().isoformat () # 获取今天的日期
58
+ updates = self .fetch_updates (repo , since = today ) # 获取今天的更新数据
56
59
57
- repo_dir = os .path .join ('daily_progress' , repo .replace ("/" , "_" ))
58
- os .makedirs (repo_dir , exist_ok = True )
60
+ repo_dir = os .path .join ('daily_progress' , repo .replace ("/" , "_" )) # 构建存储路径
61
+ os .makedirs (repo_dir , exist_ok = True ) # 确保目录存在
59
62
60
- file_path = os .path .join (repo_dir , f'{ today } .md' )
63
+ file_path = os .path .join (repo_dir , f'{ today } .md' ) # 构建文件路径
61
64
with open (file_path , 'w' ) as file :
62
65
file .write (f"# Daily Progress for { repo } ({ today } )\n \n " )
63
66
file .write ("\n ## Issues Closed Today\n " )
64
- for issue in updates ['issues' ]:
67
+ for issue in updates ['issues' ]: # 写入今天关闭的问题
65
68
file .write (f"- { issue ['title' ]} #{ issue ['number' ]} \n " )
66
69
file .write ("\n ## Pull Requests Merged Today\n " )
67
- for pr in updates ['pull_requests' ]:
70
+ for pr in updates ['pull_requests' ]: # 写入今天合并的拉取请求
68
71
file .write (f"- { pr ['title' ]} #{ pr ['number' ]} \n " )
69
72
70
- LOG .info (f"Exported daily progress to { file_path } " )
73
+ LOG .info (f"Exported daily progress to { file_path } " ) # 记录日志
71
74
return file_path
72
75
73
76
def export_progress_by_date_range (self , repo , days ):
74
- today = date .today ()
75
- since = today - timedelta (days = days )
77
+ today = date .today () # 获取当前日期
78
+ since = today - timedelta (days = days ) # 计算开始日期
76
79
77
- updates = self .fetch_updates (repo , since = since .isoformat (), until = today .isoformat ())
80
+ updates = self .fetch_updates (repo , since = since .isoformat (), until = today .isoformat ()) # 获取指定日期范围内的更新
78
81
79
- repo_dir = os .path .join ('daily_progress' , repo .replace ("/" , "_" ))
80
- os .makedirs (repo_dir , exist_ok = True )
82
+ repo_dir = os .path .join ('daily_progress' , repo .replace ("/" , "_" )) # 构建目录路径
83
+ os .makedirs (repo_dir , exist_ok = True ) # 确保目录存在
81
84
82
- # Updated filename with date range
85
+ # 更新文件名以包含日期范围
83
86
date_str = f"{ since } _to_{ today } "
84
- file_path = os .path .join (repo_dir , f'{ date_str } .md' )
87
+ file_path = os .path .join (repo_dir , f'{ date_str } .md' ) # 构建文件路径
85
88
86
89
with open (file_path , 'w' ) as file :
87
90
file .write (f"# Progress for { repo } ({ since } to { today } )\n \n " )
88
- file .write ("\n ## Issues Closed in the Last {days} Days\n " )
89
- for issue in updates ['issues' ]:
91
+ file .write (f "\n ## Issues Closed in the Last { days } Days\n " )
92
+ for issue in updates ['issues' ]: # 写入在指定日期内关闭的问题
90
93
file .write (f"- { issue ['title' ]} #{ issue ['number' ]} \n " )
91
- file .write ("\n ## Pull Requests Merged in the Last {days} Days\n " )
92
- for pr in updates ['pull_requests' ]:
94
+ file .write (f "\n ## Pull Requests Merged in the Last { days } Days\n " )
95
+ for pr in updates ['pull_requests' ]: # 写入在指定日期内合并的拉取请求
93
96
file .write (f"- { pr ['title' ]} #{ pr ['number' ]} \n " )
94
97
95
- LOG .info (f"Exported time-range progress to { file_path } " )
98
+ LOG .info (f"Exported time-range progress to { file_path } " ) # 记录日志
96
99
return file_path
0 commit comments