3
3
import random
4
4
import string
5
5
6
- CHARS = string .ascii_letters + string .digits
7
- ID_LENGTH = 12
8
6
9
7
def generate_id (prefix ):
10
- """Generate a random ID with the given prefix."""
8
+ """
9
+ Generate a random ID with the given prefix.
10
+ """
11
+
12
+ CHARS = string .ascii_letters + string .digits
13
+ ID_LENGTH = 12
11
14
random_part = "" .join (random .choice (CHARS ) for _ in range (ID_LENGTH ))
12
15
return f"{ prefix } _{ random_part } "
13
16
14
17
def extract_form_fields (issue_body : str ) -> dict :
18
+ """
19
+ This function parses issues json into a dict of important fields
20
+ """
15
21
# Mapping from issue body headers to dict keys
22
+ # Changing issues fields name to snake_case (eg. 'Guideline Title' => 'guideline_title')
16
23
header_map = {
17
- "Chapter" : "Chapter " ,
18
- "Guideline Title" : "Guideline Title " ,
19
- "Category" : "Category " ,
20
- "Status" : "Status " ,
21
- "Release Begin" : "Release Begin " ,
22
- "Release End" : "Release End " ,
23
- "FLS Paragraph ID" : "Fls Paragraph Id " ,
24
- "Decidability" : "Decidability " ,
25
- "Scope" : "Scope " ,
26
- "Tags" : "Tags " ,
27
- "Amplification" : "Amplification " ,
28
- "Exception(s)" : "Exception(S) " ,
29
- "Rationale" : "Rationale " ,
30
- "Non-Compliant Example - Prose" : "Non Compliant Example Prose " ,
31
- "Non-Compliant Example - Code" : "Non Compliant Example Code " ,
32
- "Compliant Example - Prose" : "Compliant Example Prose " ,
33
- "Compliant Example - Code" : "Compliant Example Code " ,
24
+ "Chapter" : "chapter " ,
25
+ "Guideline Title" : "guideline_title " ,
26
+ "Category" : "category " ,
27
+ "Status" : "status " ,
28
+ "Release Begin" : "release_begin " ,
29
+ "Release End" : "release_end " ,
30
+ "FLS Paragraph ID" : "fls_id " ,
31
+ "Decidability" : "decidability " ,
32
+ "Scope" : "scope " ,
33
+ "Tags" : "tags " ,
34
+ "Amplification" : "amplification " ,
35
+ "Exception(s)" : "exceptions " ,
36
+ "Rationale" : "rationale " ,
37
+ "Non-Compliant Example - Prose" : "non_compliant_ex_prose " ,
38
+ "Non-Compliant Example - Code" : "non_compliant_ex " ,
39
+ "Compliant Example - Prose" : "compliant_example_prose " ,
40
+ "Compliant Example - Code" : "compliant_example " ,
34
41
}
35
42
36
43
fields = {v : "" for v in header_map .values ()}
@@ -41,12 +48,14 @@ def extract_form_fields(issue_body: str) -> dict:
41
48
42
49
lines .append ("### END" ) # Sentinel to process last field
43
50
51
+ # Look for '###' in every line, ### represent a sections/field in a guideline
44
52
for line in lines :
45
53
header_match = re .match (r'^### (.+)$' , line .strip ())
46
54
if header_match :
47
55
# Save previous field value if any
48
56
if current_key is not None :
49
57
value = "\n " .join (current_value_lines ).strip ()
58
+ # `_No response_` represents an empty field
50
59
if value == "_No response_" :
51
60
value = ""
52
61
if current_key in fields :
@@ -61,6 +70,10 @@ def extract_form_fields(issue_body: str) -> dict:
61
70
return fields
62
71
63
72
def save_guideline_file (content : str , chapter : str ):
73
+ """
74
+ Appends a guideline str to a chapter
75
+
76
+ """
64
77
content = "\n " + content + "\n "
65
78
# os.makedirs(f"src/coding-guidelines/{chapter}", exist_ok=True)
66
79
filename = f"src/coding-guidelines/{ chapter .lower ()} .rst"
@@ -75,8 +88,14 @@ def save_guideline_file(content: str, chapter: str):
75
88
print (f"Saved guideline to { filename } " )
76
89
77
90
def guideline_template (fields : dict ) -> str :
91
+ """
92
+ This function turns a dictionary that contains the guideline fields
93
+ into a proper .rst guideline format
94
+ """
95
+
78
96
79
97
# taken from generate-guideline-templates.py
98
+ # to generate random ids
80
99
guideline_id = generate_id ("gui" )
81
100
rationale_id = generate_id ("rat" )
82
101
non_compliant_example_id = generate_id ("non_compl_ex" )
@@ -86,43 +105,43 @@ def get(key):
86
105
return fields .get (key , "" ).strip ()
87
106
88
107
89
- guideline_text = f""".. guideline:: { get ('Guideline Title ' )}
108
+ guideline_text = f""".. guideline:: { get ('guideline_title ' )}
90
109
:id: { guideline_id }
91
- :category: { get ('Category ' ).lower ()}
92
- :status: { get ('Status ' ).lower ()}
93
- :release: { get ('Release Begin ' ).lower ()}
94
- :fls: { get ('Fls Paragraph Id ' ).lower ()}
95
- :decidability: { get ('Decidability ' ).lower ()}
96
- :scope: { get ('Scope ' ).lower ()}
97
- :tags: { "," .join (get ('Tags ' ).split (" " ))}
110
+ :category: { get ('category ' ).lower ()}
111
+ :status: { get ('status ' ).lower ()}
112
+ :release: { get ('release_begin ' ).lower () } - { get ( 'release_end' )}
113
+ :fls: { get ('fls_id ' ).lower ()}
114
+ :decidability: { get ('decidability ' ).lower ()}
115
+ :scope: { get ('scope ' ).lower ()}
116
+ :tags: { "," .join (get ('tags ' ).split (" " ))}
98
117
99
- { get ('Amplification ' )}
118
+ { get ('amplification ' )}
100
119
101
120
.. rationale::
102
121
:id: { rationale_id }
103
- :status: { get ('Status ' ).lower ()}
122
+ :status: { get ('status ' ).lower ()}
104
123
105
- { get ('Rationale ' )}
124
+ { get ('rationale ' )}
106
125
107
126
.. non_compliant_example::
108
127
:id: { non_compliant_example_id }
109
- :status: { get ('Status ' ).lower ()}
128
+ :status: { get ('status ' ).lower ()}
110
129
111
- { get ('Non Compliant Example Prose ' )}
130
+ { get ('non_compliant_ex_prose ' )}
112
131
113
132
.. code-block:: rust
114
133
115
- { get ('Non Compliant Example Code ' )}
134
+ { get ('non_compliant_example ' )}
116
135
117
136
.. compliant_example::
118
137
:id: { compliant_example_id }
119
- :status: { get ('Status ' ).lower ()}
138
+ :status: { get ('status ' ).lower ()}
120
139
121
- { get ('Compliant Example Prose ' )}
140
+ { get ('compliant_example_prose ' )}
122
141
123
142
.. code-block:: rust
124
143
125
- { get ('Compliant Example Code ' )}
144
+ { get ('compliant_example ' )}
126
145
"""
127
146
128
147
return guideline_text
@@ -139,6 +158,7 @@ def get(key):
139
158
140
159
## locally test with `cat scripts/test_issue_sample.json | python3 scripts/auto-pr-helper.py`
141
160
161
+ # Read json from stdin
142
162
issue_json = sys .stdin .read ()
143
163
issue = json .loads (issue_json )
144
164
@@ -147,7 +167,7 @@ def get(key):
147
167
issue_body = issue ['body' ]
148
168
149
169
fields = extract_form_fields (issue_body )
150
- chapter = fields ["Chapter " ]
170
+ chapter = fields ["chapter " ]
151
171
152
172
153
173
content = guideline_template (fields )
0 commit comments