Skip to content

Commit 3d1a4f3

Browse files
committed
feat: Extend work report command to include Confluence integration and user-specific data retrieval
1 parent e4e8d0a commit 3d1a4f3

File tree

1 file changed

+81
-15
lines changed

1 file changed

+81
-15
lines changed

community/work_report/command.py

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@
66

77
from lib.chatmark.step import Step
88

9+
# TODO: 需要替换为实际的值
10+
USERS = ["xxx"]
11+
912
GITLAB_TOKEN = "xxx"
1013
GITLAB_URL = "xx"
1114
GITLAB_HEADERS = {"Authorization": f"Bearer {GITLAB_TOKEN}"}
12-
GITLAB_USERS = ["xxx"]
1315

1416
JIRA_TOKEN = "xxx"
1517
JIRA_USERNAME = "xxx"
1618
JIRA_URL = "xxx"
17-
JIRA_DEV_USER = "xxx"
1819
JIRA_API_VERSION = "2"
1920

2021
OPENAI_API_URL = "xxx"
2122
OPENAI_API_KEY = "xxx"
2223
LLM_MODEL = "xxx"
2324

25+
CONFLUENCE_URL = "xxx"
26+
CONFLUENCE_USERNAME = "xxx"
27+
CONFLUENCE_TOKEN = "xxx"
28+
2429
START_TIME = datetime.datetime.strptime("2025-06-01 00:00:00", "%Y-%m-%d %H:%M:%S")
2530
END_TIME = datetime.datetime.strptime("2025-06-11 23:59:59", "%Y-%m-%d %H:%M:%S")
2631

@@ -258,7 +263,7 @@ def get_commits(projects, author_email):
258263
return commits
259264

260265

261-
def get_jira_projects():
266+
def get_jira_projects(user):
262267
# 获取当前用户信息
263268
# myself = requests.get(
264269
# f"{JIRA_URL}/rest/api/{JIRA_API_VERSION}/myself",
@@ -267,7 +272,7 @@ def get_jira_projects():
267272

268273
# 获取与当前用户相关的项目
269274
url = f"{JIRA_URL}/rest/api/{JIRA_API_VERSION}/search"
270-
jql = f"assignee = '{JIRA_DEV_USER}' OR watcher = '{JIRA_DEV_USER}'"
275+
jql = f"assignee = '{user}' OR watcher = '{user}'"
271276

272277
response = requests.get(
273278
url,
@@ -296,7 +301,7 @@ def get_jira_projects():
296301
return {"values": list(projects.values())}
297302

298303

299-
def get_jira_issues(projects):
304+
def get_jira_issues(projects, user):
300305
"""获取JIRA项目统计信息"""
301306
url = f"{JIRA_URL}/rest/api/{JIRA_API_VERSION}/search"
302307
result = {}
@@ -343,7 +348,7 @@ def get_jira_issues(projects):
343348
"jql": f"""project = {project_id} AND
344349
updated >= '{START_TIME.strftime("%Y-%m-%d %H:%M")}' AND
345350
updated <= '{END_TIME.strftime("%Y-%m-%d %H:%M")}' AND
346-
assignee = '{JIRA_USERNAME}'""",
351+
assignee = '{user}'""",
347352
"startAt": 0,
348353
"maxResults": 1000,
349354
"expand": "changelog",
@@ -362,7 +367,7 @@ def get_jira_issues(projects):
362367
filtered_histories = []
363368
for history in issue["changelog"].get("histories", []):
364369
author = history.get("author", {})
365-
if "emailAddress" in author and author["emailAddress"] == JIRA_USERNAME:
370+
if "emailAddress" in author and author["emailAddress"] == user:
366371
author.pop("avatarUrls", None)
367372
filtered_histories.append(history)
368373
issue["changelog"]["histories"] = filtered_histories
@@ -390,13 +395,15 @@ def get_jira_issues(projects):
390395
return result, user_updated_issues
391396

392397

393-
def gen_report(relations):
398+
def gen_report(relations, confluence_pages):
394399
prompt = f"""
395400
你是一个经验丰富的助手,请根据以下数据,分析我的工作情况,并给出我的工作总结。
396401
数据如下:
397402
- 数据启始结束时间: {str(START_TIME)} - {str(END_TIME)}
398403
- 用户相关数据:
399404
{json.dumps(relations, indent=2, ensure_ascii=False)}
405+
- confluence 页面数据:
406+
{json.dumps(confluence_pages, indent=2, ensure_ascii=False)}
400407
401408
输出有以下要求:
402409
1. 如果 issue 没有和提交相互关联,需要给出警告
@@ -405,6 +412,7 @@ def gen_report(relations):
405412
4. 日报需要以 markdown 的形式输出
406413
5. 日报需要以中文输出
407414
6. 不要输入额外的解释,直接输出日报
415+
7. 日报中包含confluence的数据,比如新增了哪些页面,修改了哪些页面
408416
409417
具体输出格式内容如下:
410418
```report
@@ -596,6 +604,57 @@ def mr_commits_to_issue(project_id, mr, commits, jira_issues):
596604
return issues_new
597605

598606

607+
def get_confluence_current_user(account_id):
608+
url = f"https://{CONFLUENCE_URL}/wiki/rest/api/user"
609+
response = requests.get(
610+
url,
611+
auth=HTTPBasicAuth(CONFLUENCE_USERNAME, CONFLUENCE_TOKEN),
612+
headers={"Accept": "application/json"},
613+
params={"accountId": account_id},
614+
)
615+
data = response.json()
616+
return data
617+
618+
619+
def get_confluence_page_versions(page_id):
620+
url = f"https://{CONFLUENCE_URL}/wiki/api/v2/pages/{page_id}/versions"
621+
response = requests.get(
622+
url,
623+
auth=HTTPBasicAuth(CONFLUENCE_USERNAME, CONFLUENCE_TOKEN),
624+
headers={"Accept": "application/json"},
625+
)
626+
data = response.json()
627+
return data["results"]
628+
629+
630+
def get_confluence_pages(email):
631+
url = f"https://{CONFLUENCE_URL}/wiki/api/v2/pages"
632+
response = requests.get(
633+
url,
634+
auth=HTTPBasicAuth(CONFLUENCE_USERNAME, CONFLUENCE_TOKEN),
635+
headers={"Accept": "application/json"},
636+
params={"sort": "-modified-date", "limit": 250},
637+
)
638+
data = response.json()
639+
results = data["results"]
640+
ret = []
641+
for result in results:
642+
ownerId = result["ownerId"]
643+
user = get_confluence_current_user(ownerId)
644+
if user["email"] == email:
645+
versions = get_confluence_page_versions(result["id"])
646+
versions_ = []
647+
for version in versions:
648+
created_at = datetime.datetime.fromisoformat(version["createdAt"])
649+
created_at = created_at.replace(tzinfo=None)
650+
if START_TIME <= created_at <= END_TIME:
651+
versions_.append(version)
652+
if versions_:
653+
result["versions"] = versions_
654+
ret.append(result)
655+
return ret
656+
657+
599658
def get_relation(merge_requests, commits, jira_issues):
600659
ret = []
601660

@@ -810,9 +869,9 @@ def update_description_by_commits(relations):
810869

811870
def main():
812871
print("------", flush=True)
813-
for GITLAB_USER in GITLAB_USERS:
814-
with Step(f"正在生成用户{GITLAB_USER}的报告..."):
815-
user_id, projects = get_projects_by_email(GITLAB_USER)
872+
for user in USERS:
873+
with Step(f"正在生成用户{user}的报告..."):
874+
user_id, projects = get_projects_by_email(user)
816875
print(projects)
817876
print("获取到的GITLAB项目数量:", len(projects))
818877
if not projects:
@@ -826,25 +885,32 @@ def main():
826885
json.dumps(merge_requests, indent=2, ensure_ascii=False),
827886
flush=True,
828887
)
829-
commits = get_commits(projects, GITLAB_USER)
888+
commits = get_commits(projects, user)
830889
print(
831890
"获取到的提交数量:", sum(len(project["commits"]) for project in commits), flush=True
832891
)
833892
print("提交详情:", json.dumps(commits, indent=2, ensure_ascii=False), flush=True)
834-
jira_projects = get_jira_projects()
893+
jira_projects = get_jira_projects(user)
835894
print("获取到的JIRA项目数量:", len(jira_projects["values"]), flush=True)
836-
jira_issues, user_updated_issues = get_jira_issues(jira_projects["values"])
895+
jira_issues, user_updated_issues = get_jira_issues(jira_projects["values"], user)
837896
print(
838897
"获取到的JIRA ISSUE数量:",
839898
sum(issue["user_updated_issues_count"] for issue in jira_issues.values()),
840899
flush=True,
841900
)
901+
confluence_pages = get_confluence_pages(user)
902+
print("获取到的confluence页面数量:", len(confluence_pages), flush=True)
903+
print(
904+
"confluence页面详情:",
905+
json.dumps(confluence_pages, indent=2, ensure_ascii=False),
906+
flush=True,
907+
)
842908
relations = get_relation(merge_requests, commits, user_updated_issues)
843909
update_description_by_commits(relations)
844910
print("获取到的关联关系数量:", len(relations), flush=True)
845911
print("关联关系详情:", json.dumps(relations, indent=2, ensure_ascii=False), flush=True)
846912
print("生成日报...", flush=True)
847-
result = gen_report(relations)
913+
result = gen_report(relations, confluence_pages)
848914
with open("result.md", "a") as f:
849915
f.write(result)
850916
print("日报已生成,保存在 result.md 文件中。")

0 commit comments

Comments
 (0)