Update get_pr_labels method to support label updates and prevent unnecessary label republishing

This commit is contained in:
mrT23
2024-03-12 17:02:45 +02:00
parent 8fb75c16af
commit 31a8f5302a
11 changed files with 31 additions and 17 deletions

View File

@ -165,7 +165,7 @@ class AzureDevopsProvider(GitProvider):
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,

View File

@ -404,5 +404,5 @@ class BitbucketProvider(GitProvider):
pass
# bitbucket does not support labels
def get_pr_labels(self):
def get_pr_labels(self, update=False):
pass

View File

@ -347,7 +347,7 @@ class BitbucketServerProvider(GitProvider):
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):

View File

@ -216,7 +216,7 @@ class CodeCommitProvider(GitProvider):
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):

View File

@ -208,7 +208,7 @@ class GerritProvider(GitProvider):
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')

View File

@ -164,7 +164,7 @@ class GitProvider(ABC):
pass
@abstractmethod
def get_pr_labels(self):
def get_pr_labels(self, update=False):
pass
def get_repo_labels(self):

View File

@ -650,9 +650,16 @@ class GithubProvider(GitProvider):
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 []

View File

@ -419,7 +419,7 @@ class GitLabProvider(GitProvider):
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):

View File

@ -176,5 +176,5 @@ class LocalGitProvider(GitProvider):
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')

View File

@ -118,11 +118,15 @@ class PRDescription:
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:

View File

@ -369,17 +369,20 @@ class PRReviewer:
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}")