-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathpost-mr-note.py
166 lines (137 loc) · 6.34 KB
/
post-mr-note.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
import sys, os, urllib3, argparse
# Silence the irritating insecure warnings.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Add the location of python-gitlab to the path so we can import it
repo_top = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
python_gitlab_dir = os.path.join(repo_top, "external/python-gitlab")
sys.path.append(python_gitlab_dir)
import gitlab
def getArgs():
"""
Parse the command line arguments.
Usage: python post-mr-note.py [PROJECT-TOKEN] [PROJECT] [MR-IID]
Sample usage: python post-mr-note.py [PROJECT-TOKEN] documentation/latex-sample-chip-errata-repo 19
"""
parser = argparse.ArgumentParser(
description='Post note to existing MR.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument( "authkey", type=str, help="Project or personal access token for authentication with GitLab. Create one at https://gitlab.com/profile/personal_access_tokens" )
parser.add_argument( "project", type=str, help="Path to GitLab project in the form <namespace>/<project>")
parser.add_argument( "mr_iid", type=str, help="Merge request number to post the message")
parser.add_argument( "--url", help="Gitlab URL.")
return parser, parser.parse_args()
class PythonGitlabNotes():
"""
Collect links to artifacts saved by other jobs in logs folder.
Post the links as a note to an existing merge request.
"""
def __init__(self, url, authkey, project, mr_iid):
# Parse command line arguments
self.url = url
self.url_api = url + '/api/v4'
self.authkey = authkey
self.project_name = project
self.mr_iid = mr_iid
# Create python-gitlab server instance
server = gitlab.Gitlab(self.url, myargs.authkey, api_version=4, ssl_verify=False)
# Get an instance of the project and store it off
self.project = server.projects.get(self.project_name)
def collect_data(self):
"""
Read document URLs from logs and organize them by language and chip series.
"""
series_links_html = {}
series_links_pdf = {}
try:
with open("logs/doc-url.txt", "r") as file:
for line in file:
if line.startswith('[document preview]'):
tokens = line.split(']')
if len(tokens) < 3:
continue
desc_url = tokens[2].strip()
lang_chip_info = tokens[1].strip('[]')
lang_chip = lang_chip_info.split('_')
if len(lang_chip) == 2:
language = 'en'
chip_series = lang_chip[1]
elif len(lang_chip) == 3:
language = 'zh_CN'
chip_series = lang_chip[2]
else:
continue # Skip malformed lines
# Construct new PDF URL
pdf_url = desc_url.replace(
"index.html",
f"esp-dev-kits-{language}-master-{chip_series}.pdf"
)
# Store links
if 'pdf' in pdf_url:
series_links_pdf.setdefault(chip_series, {})[language] = pdf_url
if 'html' in desc_url:
series_links_html.setdefault(chip_series, {})[language] = desc_url
except FileNotFoundError:
print("Error: logs/doc-url.txt not found.")
sys.exit(1)
self.series_links_html = series_links_html
self.series_links_pdf = series_links_pdf
# Debugging line
print("HTML Links:", series_links_html)
print("PDF Links:", series_links_pdf)
def prepare_note(self):
"""
Prepare a note with links to be posted in .md format.
"""
note = "Documentation preview:\n\n"
product_names = {
'esp32': 'ESP32', 'esp32s2': 'ESP32-S2', 'esp32s3': 'ESP32-S3',
'esp32c3': 'ESP32-C3', 'esp32c6': 'ESP32-C6', 'esp32h2': 'ESP32-H2',
'esp32c2': 'ESP32-C2', 'esp32p4': 'ESP32-P4', 'esp32c5': 'ESP32-C5',
'esp32c61': 'ESP32-C61', 'other': 'Other'
}
# Process both HTML and PDF links
for chip_series, language_links_html in self.series_links_html.items():
product_name = product_names.get(chip_series, "Unknown")
note += f"- **{product_name}**\n"
# Append HTML link if available
if 'zh_CN' in language_links_html:
note += f"\t * HTML: [esp-dev-kits 文档]({language_links_html['zh_CN']})/"
else:
note += f"esp-dev-kits 文档/"
if 'en' in language_links_html:
note += f"[esp-dev-kits Documentation]({language_links_html['en']}) \n"
else:
note += "esp-dev-kits Documentation"
# Check if there are PDF links for the same chip series
if chip_series in self.series_links_pdf:
language_links_pdf = self.series_links_pdf[chip_series]
if 'zh_CN' in language_links_pdf:
note += f"\t * PDF: [esp-dev-kits 文档]({language_links_pdf['zh_CN']})/"
else:
note += f"esp-dev-kits 文档/"
if 'en' in language_links_pdf:
note += f"[esp-dev-kits Documentation]({language_links_pdf['en']}) \n"
else:
note += "esp-dev-kits Documentation"
note += "\n"
# Store the note in the instance variable
self.note = note
# Print the note for debugging
print(note)
def post_note(self):
"""
Post the note to the merge request.
"""
# Get the MR
mr = self.project.mergerequests.get(self.mr_iid)
# Post the note to the MR
mr.notes.create({'body': self.note})
def run(self):
self.collect_data()
self.prepare_note()
self.post_note()
if __name__ == '__main__':
myParser, myargs = getArgs()
sys.exit(PythonGitlabNotes(url=myargs.url, authkey=myargs.authkey,
project=myargs.project, mr_iid=myargs.mr_iid).run())