Skip to content

Commit

Permalink
Update get_pr_labels method to support label updates and prevent unne…
Browse files Browse the repository at this point in the history
…cessary label republishing
  • Loading branch information
mrT23 committed Mar 12, 2024
1 parent 8fb75c1 commit 31a8f53
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pr_agent/git_providers/azuredevops_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def publish_labels(self, pr_types):
except Exception as e:
get_logger().exception(f"Failed to publish labels, error: {e}")

def get_pr_labels(self):
def get_pr_labels(self, update=False):
try:
labels = self.azure_devops_client.get_pull_request_labels(
project=self.workspace_slug,
Expand Down
2 changes: 1 addition & 1 deletion pr_agent/git_providers/bitbucket_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,5 +404,5 @@ def publish_labels(self, pr_types: list):
pass

# bitbucket does not support labels
def get_pr_labels(self):
def get_pr_labels(self, update=False):
pass
2 changes: 1 addition & 1 deletion pr_agent/git_providers/bitbucket_server_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def publish_labels(self, pr_types: list):
pass

# bitbucket does not support labels
def get_pr_labels(self):
def get_pr_labels(self, update=False):
pass

def _get_pr_comments_url(self):
Expand Down
2 changes: 1 addition & 1 deletion pr_agent/git_providers/codecommit_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def publish_code_suggestions(self, code_suggestions: list) -> bool:
def publish_labels(self, labels):
return [""] # not implemented yet

def get_pr_labels(self):
def get_pr_labels(self, update=False):
return [""] # not implemented yet

def remove_initial_comment(self):
Expand Down
2 changes: 1 addition & 1 deletion pr_agent/git_providers/gerrit_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def get_issue_comments(self):
Comment = namedtuple('Comment', ['body'])
return Comments([Comment(c['message']) for c in reversed(comments)])

def get_pr_labels(self):
def get_pr_labels(self, update=False):
raise NotImplementedError(
'Getting labels is not implemented for the gerrit provider')

Expand Down
2 changes: 1 addition & 1 deletion pr_agent/git_providers/git_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def publish_labels(self, labels):
pass

@abstractmethod
def get_pr_labels(self):
def get_pr_labels(self, update=False):
pass

def get_repo_labels(self):
Expand Down
11 changes: 9 additions & 2 deletions pr_agent/git_providers/github_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,16 @@ def publish_labels(self, pr_types):
except Exception as e:
get_logger().exception(f"Failed to publish labels, error: {e}")

def get_pr_labels(self):
def get_pr_labels(self, update=False):
try:
return [label.name for label in self.pr.labels]
if not update:
labels =self.pr.labels
return [label.name for label in labels]
else: # obtain the latest labels. Maybe they changed while the AI was running
headers, labels = self.pr._requester.requestJsonAndCheck(
"GET", f"{self.pr.issue_url}/labels")
return [label['name'] for label in labels]

except Exception as e:
get_logger().exception(f"Failed to get labels, error: {e}")
return []
Expand Down
2 changes: 1 addition & 1 deletion pr_agent/git_providers/gitlab_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def publish_labels(self, pr_types):
def publish_inline_comments(self, comments: list[dict]):
pass

def get_pr_labels(self):
def get_pr_labels(self, update=False):
return self.mr.labels

def get_repo_labels(self):
Expand Down
2 changes: 1 addition & 1 deletion pr_agent/git_providers/local_git_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ def get_pr_title(self):
def get_issue_comments(self):
raise NotImplementedError('Getting issue comments is not implemented for the local git provider')

def get_pr_labels(self):
def get_pr_labels(self, update=False):
raise NotImplementedError('Getting labels is not implemented for the local git provider')
10 changes: 7 additions & 3 deletions pr_agent/tools/pr_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,15 @@ async def run(self):
if get_settings().config.publish_output:
# publish labels
if get_settings().pr_description.publish_labels and self.git_provider.is_supported("get_labels"):
original_labels = self.git_provider.get_pr_labels()
original_labels = self.git_provider.get_pr_labels(update=True)
get_logger().debug(f"original labels", artifact=original_labels)
user_labels = get_user_labels(original_labels)
get_logger().debug(f"published labels:\n{pr_labels + user_labels}")
self.git_provider.publish_labels(pr_labels + user_labels)
new_labels = pr_labels + user_labels
get_logger().debug(f"published labels", artifact=new_labels)
if new_labels != original_labels:
self.git_provider.publish_labels(new_labels)
else:
get_logger().debug(f"Labels are the same, not updating")

# publish description
if get_settings().pr_description.publish_description_as_comment:
Expand Down
11 changes: 7 additions & 4 deletions pr_agent/tools/pr_reviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,17 +369,20 @@ def set_review_labels(self, data):
if security_concerns_bool:
review_labels.append('Possible security concern')

current_labels = self.git_provider.get_pr_labels()
current_labels = self.git_provider.get_pr_labels(update=True)
get_logger().debug(f"Current labels:\n{current_labels}")
if current_labels:
current_labels_filtered = [label for label in current_labels if
not label.lower().startswith('review effort [1-5]:') and not label.lower().startswith(
'possible security concern')]
else:
current_labels_filtered = []
if current_labels or review_labels:
get_logger().debug(f"Current labels:\n{current_labels}")
new_labels = review_labels + current_labels_filtered
if (current_labels or review_labels) and new_labels != current_labels:
get_logger().info(f"Setting review labels:\n{review_labels + current_labels_filtered}")
self.git_provider.publish_labels(review_labels + current_labels_filtered)
self.git_provider.publish_labels(new_labels)
else:
get_logger().info(f"Review labels are already set:\n{review_labels + current_labels_filtered}")
except Exception as e:
get_logger().error(f"Failed to set review labels, error: {e}")

Expand Down

0 comments on commit 31a8f53

Please sign in to comment.