1#!/usr/bin/env python3
2# coding: utf-8
3
4"""
5Copyright (c) 2024 Huawei Device Co., Ltd.
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17
18Description: utils for test suite
19"""
20
21import json
22import os
23
24
25def generate_html_table(data):
26    table_rows = ""
27    for project, items in data.items():
28        for item_idx, item in enumerate(items):
29            if item_idx == 0:
30                table_rows = ''.join([table_rows, f"<tr><td rowspan='{len(items)}'>{project}</td>"])
31            else:
32                table_rows = ''.join([table_rows, "<tr>"])
33
34            if item['committer'] is None:
35                table_rows = ''.join([table_rows, f"<td colspan=4 style='color: #FF9671;'>{item['title']}</td>"])
36            else:
37                table_rows = ''.join([table_rows, f"<td>{item['title']}</td>"])
38                table_rows = ''.join([table_rows, f"<td>{item['committer']}</td>"])
39                table_rows = ''.join([table_rows, f"<td>{item['commit_time_str']}</td>"])
40                table_rows = ''.join([table_rows, f"<td><a href='https://gitee.com/{item['pr_link']}'>点击查看</a></td></tr>\n"])
41
42    return table_rows
43
44
45def get_table_style():
46    table_style = f"""
47    <style>
48            .table-wrapper {{
49                width: 1800px;
50                margin: auto;
51            }}
52            .table-container {{
53                height: 200px;
54                font-family: Arial, sans-serif;
55            }}
56            .commit-table {{
57                border-collapse: collapse;
58                border-spacing: 0;
59                width: 100%;
60                border: 1px solid #ddd;
61            }}
62            .commit-table th, td {{
63                padding: 8px;
64                text-align: left;
65                border: 1px solid #ddd;
66            }}
67            .commit-table th {{
68                background-color: #f2f2f2;
69                color: #333;
70                font-weight: bold;
71                border-bottom: 1px solid #ddd;
72            }}
73            .commit-table td {{
74                background-color: #fff;
75                color: #333;
76                vertical-align: middle;
77                border-bottom: 1px solid #ddd;
78            }}
79            .commit-table tr:nth-child(even) {{
80                background-color: #f9f9f9;
81            }}
82            .commit-table a {{
83                color: #0066cc;
84                text-decoration: none;
85            }}
86            caption {{
87                font-size: 1.4em;
88                font-weight: bold;
89                margin-bottom: 18px;
90            }}
91        </style>
92        """
93
94    return table_style
95
96
97def render_html(data):
98    table_rows = generate_html_table(data)
99    table_style = get_table_style()
100    html = f"""
101    <!DOCTYPE html>
102    <html>
103    <head>
104        <meta charset="UTF-8">
105        <title>提交记录</title>
106        {table_style}
107    </head>
108    <body>
109    <div class="table-wrapper">
110        <div class="table-container">
111            <table class="commit-table">
112                <thead>
113                <caption>提交记录</caption>
114                <tr>
115                    <th>仓库</th>
116                    <th>描述</th>
117                    <th>作者</th>
118                    <th>时间</th>
119                    <th>链接</th>
120                </tr>
121                </thead>
122                <tbody>
123                {table_rows}
124                </tbody>
125            </table>
126        </div>
127    </div>
128
129    </body>
130    </html>
131    """
132
133    return html
134
135
136def get_result():
137    data_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data.txt')
138    with open(data_file, 'r', encoding='utf-8') as file:
139        lines = file.readlines()
140
141    data = {}
142    for line in lines:
143        item = json.loads(line)
144
145        repo_name = item['repo_name']
146        if repo_name in data:
147            data[repo_name].append(item)
148        else:
149            data[repo_name] = [item]
150
151    html = render_html(data)
152
153    commit_log_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'commit_log.html')
154    with open(commit_log_file, 'w', encoding='utf-8') as file:
155        file.write(html)
156
157
158if __name__ == '__main__':
159    get_result()