1 Star 0 Fork 0

QuecPython/GiteeToExcel

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
GiteeToExcel.py 15.93 KB
一键复制 编辑 原始数据 按行查看 历史
chenchi 提交于 2024-06-06 17:32 . Initial commit.
# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# encoding: utf-8
import sys
import os
import pandas as pd
import requests
from datetime import datetime
from openpyxl import Workbook
from openpyxl.styles import Font, Border, Side, Alignment, PatternFill
class GiteeRequest(object):
def __init__(self):
self.access_token = '6ba2ad6046c08ed81ed0ff4538e420da'
self.owner = 'qpy-doc-center'
self.repo = 'teedoc_with_qpydoc'
self.issue_info_list = list()
self.issue_data = list()
def get_all_Issues(self):
all_issues_url_open = f"https://gitee.com/api/v5/repos/{self.owner}/{self.repo}/issues?access_token={self.access_token}&state={'open'}"
all_issues_url_progressing = f"https://gitee.com/api/v5/repos/{self.owner}/{self.repo}/issues?access_token={self.access_token}&state={'progressing'}"
url_list = [all_issues_url_open, all_issues_url_progressing]
issue_all_list = []
for url_info in url_list:
response_all = requests.get(url_info)
issue_all = response_all.json()
issue_all_list.extend(issue_all)
return issue_all_list
def get_Issue_Pr(self, issue_all):
for issue_info in issue_all:
# 获取 Issue 的详细信息
issue_info_dict = {
"Issue_id": issue_info.get("number"),
"Issue_title": issue_info.get("title"),
"Pr": []
}
issue_url = f"https://gitee.com/api/v5/repos/{self.owner}/issues/{issue_info.get('number')}/pull_requests?access_token={self.access_token}&repo={self.repo}"
print("issue url :", issue_url)
# 查询当前Issue下的所有Pr
response_details = requests.get(issue_url)
issue_details = response_details.json()
# Pr遍历结果返回
message_list = [] # 提交信息
examine_list = [] # 审查人员与审查状态
test_list = [] # 测试人员
pr_id_list = [] # pr_id
table_info_list = []
for details in issue_details:
# 获取Pr id
pr_id_list.append(details.get("number"))
# 获取Pr 审查人员及审查状态
assignees = details.get("assignees")
for assignees_i in assignees:
examine_list.append([{
"Pr_examine_name": assignees_i.get("name"),
"Pr_examine_state": assignees_i.get("accept")
}])
# 获取Pr 测试人员信息
testers = details.get("testers")
for testers_i in testers:
test_list.append([{
"Pr_test_name": testers_i.get("name"),
"Pr_test_state": testers_i.get("accept")
}])
# 请求获取Pr提交描述信息
pr_commit_url = details.get("commits_url") + f"?access_token={self.access_token}"
response_commit = requests.get(pr_commit_url)
commit_details = response_commit.json()
# 遍历结果返回
for commit_info in commit_details:
# 提取message信息
message_list.append([commit_info.get("commit").get("message").split("#")[0]])
table_info = {
"Pr_id": pr_id_list,
"Pr_description": message_list,
"Pr_examine": examine_list,
"Pr_test": test_list
}
table_info_list.append(table_info)
issue_info_dict["Pr"] = table_info_list
self.issue_info_list.append(issue_info_dict)
def output_excel_data(self):
for idx, v in enumerate(self.issue_info_list,start=1):
Issue_title = v.get("Issue_title")
Pr_all = v.get("Pr")
PR_id_list = []
Pr_description = []
Pr_examine_name = []
Pr_examine_state = []
Pr_test_name = []
Pr_test_state = []
for pr_v in Pr_all:
PR_id_list = pr_v.get("Pr_id")
Pr_description = pr_v.get("Pr_description")
Pr_examine = pr_v.get("Pr_examine")
Pr_test = pr_v.get("Pr_test")
Pr_examine_name = [[v[0].get("Pr_examine_name")] for v in Pr_examine]
Pr_examine_state = [["已完成" if v[0].get("Pr_examine_state") else "进行中"] for v in Pr_examine]
Pr_test_name = [[v[0].get("Pr_test_name")] for v in Pr_test]
Pr_test_state = [["已完成" if v[0].get("Pr_test_state") else "进行中"] for v in Pr_test]
r_data = {
'序号': [idx],
'Issue': [Issue_title],
'PR': PR_id_list,
'PR描述': Pr_description,
'审查人员': Pr_examine_name,
'审查状态': Pr_examine_state,
'测试人员': Pr_test_name,
'验证版本': [[""]],
'验证状态': Pr_test_state,
'备注': [[""]]
}
self.issue_data.append(r_data)
return self.issue_data
class ExcelManager(object):
def __init__(self, loc_file_path=None):
self.loc_file_path = loc_file_path
# 定义标题和对应的列宽
self.titles = [
('序号', 5),
('Issue', 30),
('PR', 8),
('PR描述', 50),
('审查人员', 15),
('审查状态', 15),
('测试人员', 15),
('验证版本', 40),
('验证状态', 15),
('备注', 35)
]
# 添加标题行
self.row = 1
self.current_row = 2
# 创建一个新的Excel工作簿和工作表
self.wb = Workbook()
self.ws = self.wb.active
self.ws.title = "Gitee任务"
# 设置标题行的字体、背景和边框样式
self.header_font = Font(bold=True, color='000000') # 黑色字体
self.fill = PatternFill(start_color='BDD7EE', end_color='BDD7EE', fill_type='solid') # 淡蓝色背景
self.border = Border(left=Side(style='thin'), right=Side(style='thin'),top=Side(style='thin'), bottom=Side(style='thin'))
self.alignment = Alignment(horizontal='center', vertical='center')
# 设置字体和边框
self.font = Font(name='等线', size=10)
self.border = Border(left=Side(style='thin'),right=Side(style='thin'),top=Side(style='thin'),bottom=Side(style='thin'))
self.alignment_center = Alignment(horizontal='center', vertical='center')
self.alignment_top = Alignment(wrap_text=True, vertical='top')
self.alignment_left_center = Alignment(horizontal='left', vertical='center')
self.alignment_no_wrap_center = Alignment(horizontal='center', vertical='center', wrap_text=False)
def create_title(self):
for col, (title, width) in enumerate(self.titles, start=1):
self.ws.column_dimensions[self.ws.cell(row=self.row, column=col).column_letter].width = width
cell = self.ws.cell(row=self.row, column=col, value=title)
cell.font = self.header_font
cell.fill = self.fill
cell.border = self.border
cell.alignment = self.alignment
def create_excel(self, r_data):
if self.loc_file_path is not None:
loacl_data = self.read_excel(self.loc_file_path)
all_data = self.update_or_add(loacl_data, r_data)
else:
all_data = r_data
self.create_title()
for item in all_data:
issue_row_start = self.current_row
for pr_index, pr in enumerate(item['PR']):
pr_row_start = self.current_row
pr_descriptions = item['PR描述'][pr_index]
for desc in pr_descriptions:
self.ws.cell(row=self.current_row, column=1, value=item['序号'][0]).alignment = self.alignment_center
self.ws.cell(row=self.current_row, column=2, value=item['Issue'][0]).alignment = self.alignment_left_center
self.ws.cell(row=self.current_row, column=3, value=pr).alignment = self.alignment_center
self.ws.cell(row=self.current_row, column=4, value=desc).alignment = Alignment(wrap_text=True, vertical='top')
self.current_row += 1
# 合并PR及其他字段的单元格
self.ws.merge_cells(start_row=pr_row_start, start_column=3, end_row=self.current_row - 1, end_column=3)
self.ws.cell(row=pr_row_start, column=5, value="/".join(item['审查人员'][pr_index])).alignment = self.alignment_center
self.ws.cell(row=pr_row_start, column=6, value="/".join(item['审查状态'][pr_index])).alignment = self.alignment_center
self.ws.cell(row=pr_row_start, column=7, value="/".join(item['测试人员'][pr_index])).alignment = self.alignment_center
if pr_index < len(item['验证版本']):
if not item['验证版本'][pr_index]:
msg = ""
else:
msg = item['验证版本'][pr_index][0]
self.ws.cell(row=pr_row_start, column=8, value=msg).alignment = self.alignment_center
self.ws.cell(row=pr_row_start, column=9, value="/".join(item['验证状态'][pr_index])).alignment = self.alignment_center
if pr_index < len(item['备注']):
if not item['备注'][pr_index]:
msg = ""
else:
msg = item['备注'][pr_index][0]
self.ws.cell(row=pr_row_start, column=10, value=msg).alignment = self.alignment_center
self.ws.merge_cells(start_row=pr_row_start, start_column=5, end_row=self.current_row - 1, end_column=5)
self.ws.merge_cells(start_row=pr_row_start, start_column=6, end_row=self.current_row - 1, end_column=6)
self.ws.merge_cells(start_row=pr_row_start, start_column=7, end_row=self.current_row - 1, end_column=7)
self.ws.merge_cells(start_row=pr_row_start, start_column=8, end_row=self.current_row - 1, end_column=8)
self.ws.merge_cells(start_row=pr_row_start, start_column=9, end_row=self.current_row - 1, end_column=9)
self.ws.merge_cells(start_row=pr_row_start, start_column=10, end_row=self.current_row - 1, end_column=10)
# 合并序号和Issue单元格
self.ws.merge_cells(start_row=issue_row_start, start_column=1, end_row=self.current_row - 1, end_column=1)
self.ws.merge_cells(start_row=issue_row_start, start_column=2, end_row=self.current_row - 1, end_column=2)
# 为所有单元格添加边框
for row in self.ws.iter_rows(min_row=1, max_row=self.ws.max_row, min_col=1, max_col=len(self.titles)):
for cell in row:
cell.border = self.border
now = datetime.now()
output_path = '{}\ST_Gitee_{}.xlsx'.format(os.getcwd(), now.strftime('%Y%m%d%H%M%S'))
self.wb.save(output_path)
print("Success , out excel path : ", output_path)
def read_excel(self, file_path):
df = pd.read_excel(file_path)
# 前向填充空白字段
df['Issue'].ffill(inplace=True)
df['PR'].ffill(inplace=True)
# 处理可能的数据类型问题(将float转换为int)
df['PR'] = df['PR'].astype(int)
# 按Issue分组
grouped = df.groupby('Issue')
data = []
for issue, group in grouped:
pr_groups = group.groupby('PR')
prs = []
pr_descriptions = []
reviewers = []
review_statuses = []
testers = []
verification_versions = []
verification_statuses = []
notes = []
for pr, pr_group in pr_groups:
prs.append(pr)
# 使用自定义函数去除PR描述中的重复项,保持原始顺序
descriptions = pr_group['PR描述'].dropna().tolist()
unique_descriptions = self.remove_duplicates_keep_order(descriptions)
pr_descriptions.append(unique_descriptions)
reviewers.append(list(set(pr_group['审查人员'].dropna().str.split(',').sum())))
review_statuses.append(list(set(pr_group['审查状态'].dropna().str.split(',').sum())))
testers.append(list(set(pr_group['测试人员'].dropna().str.split(',').sum())))
verification_versions.append(list(set(pr_group['验证版本'].dropna().astype(str))))
verification_statuses.append(list(set(pr_group['验证状态'].dropna().str.split(',').sum())))
notes.append(list(set(pr_group['备注'].dropna().astype(str))))
entry = {
'Issue': [issue],
'PR': prs,
'PR描述': pr_descriptions,
'审查人员': reviewers,
'审查状态': review_statuses,
'测试人员': testers,
'验证版本': verification_versions,
'验证状态': verification_statuses,
'备注': notes
}
data.append(entry)
return data
def update_or_add(self, data1, data2):
"""更新或添加data1到data2"""
for entry1 in data1:
match_found = False
for entry2 in data2:
# 检查Issue相同且PR的交集非空
if entry1['Issue'] == entry2['Issue'] and not set(entry1['PR']).isdisjoint(entry2['PR']):
match_found = True
# 更新data2中的条目,除了验证版本和备注
entry1['PR描述'] = entry2['PR描述']
entry1['审查人员'] = entry2['审查人员']
entry1['审查状态'] = entry2['审查状态']
entry1['测试人员'] = entry2['测试人员']
entry1['验证版本'] = entry2['验证版本']
entry1['验证状态'] = entry2['验证状态']
entry1['备注'] = entry2['备注']
break
if not match_found:
# 如果没有匹配,添加新记录到data2,设置适当的序号
last_seq = data2[-1]['序号'][0] if data2 else 0
new_entry = entry1.copy()
new_entry['序号'] = [last_seq + 1]
data2.append(new_entry)
return data2
def remove_duplicates_keep_order(self, seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
def main():
print(sys.argv)
git_obj = GiteeRequest()
issue_all = git_obj.get_all_Issues()
git_obj.get_Issue_Pr(issue_all)
data = git_obj.output_excel_data()
if len(sys.argv) == 2:
loc_file_path = sys.argv[1]
else:
loc_file_path = None
excel_obj = ExcelManager(loc_file_path=loc_file_path)
excel_obj.create_excel(data)
main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/quecpython/GiteeToExcel.git
git@gitee.com:quecpython/GiteeToExcel.git
quecpython
GiteeToExcel
GiteeToExcel
master

搜索帮助