Skip to content

Commit 1315fa6

Browse files
authored
Fix: decouple inline and table suggestion filtering logic
- **Refactor `run` method in `PRCodeSuggestions`**: - Pre-filter suggestions based on `suggestions_score_threshold` early in the process to prevent publishing low-score suggestions in any mode. - Separate inline and table publishing logic for better clarity and control. - Ensure dual publishing mode correctly displays all valid suggestions inline while filtering the summary table based on `dual_publishing_score_threshold`. - Fix potential issue where the progress bar or "no suggestions" message could be mishandled when no high-score suggestions exist for the table. - **Update `push_inline_code_suggestions`**: - Re-verify score threshold within the function to ensure robust filtering (though pre-filtering in `run` handles this primarily now).
1 parent ab808fd commit 1315fa6

File tree

1 file changed

+68
-45
lines changed

1 file changed

+68
-45
lines changed

pr_agent/tools/pr_code_suggestions.py

Lines changed: 68 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -123,59 +123,82 @@ async def run(self):
123123
await self.publish_no_suggestions()
124124
return
125125

126+
# Filter data by suggestions_score_threshold
127+
score_threshold = max(1, int(get_settings().pr_code_suggestions.suggestions_score_threshold))
128+
data['code_suggestions'] = [
129+
d for d in data['code_suggestions']
130+
if int(d.get('score', 0)) >= score_threshold
131+
]
132+
133+
if not data['code_suggestions']:
134+
await self.publish_no_suggestions()
135+
return
136+
126137
# publish the suggestions
127138
if get_settings().config.publish_output:
128139
# If a temporary comment was published, remove it
129140
self.git_provider.remove_initial_comment()
130141

131-
# Publish table summarized suggestions
132-
if ((not get_settings().pr_code_suggestions.commitable_code_suggestions) and
133-
self.git_provider.is_supported("gfm_markdown")):
134-
135-
# generate summarized suggestions
136-
pr_body = self.generate_summarized_suggestions(data)
137-
get_logger().debug(f"PR output", artifact=pr_body)
138-
139-
# require self-review
140-
if get_settings().pr_code_suggestions.demand_code_suggestions_self_review:
141-
pr_body = await self.add_self_review_text(pr_body)
142-
143-
# add usage guide
144-
if (get_settings().pr_code_suggestions.enable_chat_text and get_settings().config.is_auto_command
145-
and isinstance(self.git_provider, GithubProvider)):
146-
pr_body += "\n\n>💡 Need additional feedback ? start a [PR chat](https://chromewebstore.google.com/detail/ephlnjeghhogofkifjloamocljapahnl) \n\n"
147-
if get_settings().pr_code_suggestions.enable_help_text:
148-
pr_body += "<hr>\n\n<details> <summary><strong>💡 Tool usage guide:</strong></summary><hr> \n\n"
149-
pr_body += HelpMessage.get_improve_usage_guide()
150-
pr_body += "\n</details>\n"
151-
152-
# Output the relevant configurations if enabled
153-
if get_settings().get('config', {}).get('output_relevant_configurations', False):
154-
pr_body += show_relevant_configurations(relevant_section='pr_code_suggestions')
155-
156-
# publish the PR comment
157-
if get_settings().pr_code_suggestions.persistent_comment: # true by default
158-
self.publish_persistent_comment_with_history(self.git_provider,
159-
pr_body,
160-
initial_header="## PR Code Suggestions ✨",
161-
update_header=True,
162-
name="suggestions",
163-
final_update_message=False,
164-
max_previous_comments=get_settings().pr_code_suggestions.max_history_len,
165-
progress_response=self.progress_response)
166-
else:
167-
if self.progress_response:
168-
self.git_provider.edit_comment(self.progress_response, body=pr_body)
169-
else:
170-
self.git_provider.publish_comment(pr_body)
142+
# Logic for Inline Suggestions
143+
dual_threshold = int(get_settings().pr_code_suggestions.dual_publishing_score_threshold)
144+
if get_settings().pr_code_suggestions.commitable_code_suggestions or dual_threshold > 0:
145+
await self.push_inline_code_suggestions(data)
171146

147+
# Logic for Table Suggestions
148+
if self.git_provider.is_supported("gfm_markdown") and \
149+
(not get_settings().pr_code_suggestions.commitable_code_suggestions or dual_threshold > 0):
150+
151+
data_for_table = data
172152
# dual publishing mode
173-
if int(get_settings().pr_code_suggestions.dual_publishing_score_threshold) > 0:
174-
await self.dual_publishing(data)
153+
if dual_threshold > 0:
154+
data_for_table = {'code_suggestions': []}
155+
for suggestion in data['code_suggestions']:
156+
if int(suggestion.get('score', 0)) >= dual_threshold:
157+
data_for_table['code_suggestions'].append(suggestion)
158+
159+
if data_for_table['code_suggestions']:
160+
# generate summarized suggestions
161+
pr_body = self.generate_summarized_suggestions(data_for_table)
162+
get_logger().debug(f"PR output", artifact=pr_body)
163+
164+
# require self-review
165+
if get_settings().pr_code_suggestions.demand_code_suggestions_self_review:
166+
pr_body = await self.add_self_review_text(pr_body)
167+
168+
# add usage guide
169+
if (get_settings().pr_code_suggestions.enable_chat_text and get_settings().config.is_auto_command
170+
and isinstance(self.git_provider, GithubProvider)):
171+
pr_body += "\n\n>💡 Need additional feedback ? start a [PR chat](https://chromewebstore.google.com/detail/ephlnjeghhogofkifjloamocljapahnl) \n\n"
172+
if get_settings().pr_code_suggestions.enable_help_text:
173+
pr_body += "<hr>\n\n<details> <summary><strong>💡 Tool usage guide:</strong></summary><hr> \n\n"
174+
pr_body += HelpMessage.get_improve_usage_guide()
175+
pr_body += "\n</details>\n"
176+
177+
# Output the relevant configurations if enabled
178+
if get_settings().get('config', {}).get('output_relevant_configurations', False):
179+
pr_body += show_relevant_configurations(relevant_section='pr_code_suggestions')
180+
181+
# publish the PR comment
182+
if get_settings().pr_code_suggestions.persistent_comment: # true by default
183+
self.publish_persistent_comment_with_history(self.git_provider,
184+
pr_body,
185+
initial_header="## PR Code Suggestions ✨",
186+
update_header=True,
187+
name="suggestions",
188+
final_update_message=False,
189+
max_previous_comments=get_settings().pr_code_suggestions.max_history_len,
190+
progress_response=self.progress_response)
191+
else:
192+
if self.progress_response:
193+
self.git_provider.edit_comment(self.progress_response, body=pr_body)
194+
else:
195+
self.git_provider.publish_comment(pr_body)
196+
else:
197+
if self.progress_response:
198+
self.git_provider.remove_comment(self.progress_response)
175199
else:
176-
await self.push_inline_code_suggestions(data)
177-
if self.progress_response:
178-
self.git_provider.remove_comment(self.progress_response)
200+
if self.progress_response and not (get_settings().pr_code_suggestions.commitable_code_suggestions or dual_threshold > 0):
201+
self.git_provider.remove_comment(self.progress_response)
179202
else:
180203
get_logger().info('Code suggestions generated for PR, but not published since publish_output is False.')
181204
pr_body = self.generate_summarized_suggestions(data)

0 commit comments

Comments
 (0)