diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d594a89..6164830b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 2023-08-01 + +### Enhanced +- Introduced the ability to retrieve commit messages from pull requests across different git providers. +- Implemented commit messages retrieval for GitHub and GitLab providers. +- Updated the PR description template to include a section for commit messages if they exist. + ## 2023-07-30 ### Enhanced diff --git a/pr_agent/git_providers/bitbucket_provider.py b/pr_agent/git_providers/bitbucket_provider.py index 8f660c57..2f3ec2c2 100644 --- a/pr_agent/git_providers/bitbucket_provider.py +++ b/pr_agent/git_providers/bitbucket_provider.py @@ -120,3 +120,6 @@ class BitbucketProvider: def _get_pr_file_content(self, remote_link: str): return "" + + def get_commit_messages(self): + return "" # not implemented yet diff --git a/pr_agent/git_providers/github_provider.py b/pr_agent/git_providers/github_provider.py index fa8b1168..ae3eaeba 100644 --- a/pr_agent/git_providers/github_provider.py +++ b/pr_agent/git_providers/github_provider.py @@ -344,4 +344,19 @@ class GithubProvider(GitProvider): return [label.name for label in self.pr.labels] except Exception as e: logging.exception(f"Failed to get labels, error: {e}") - return [] \ No newline at end of file + return [] + + def get_commit_messages(self) -> str: + """ + Retrieves the commit messages of a pull request. + + Returns: + str: A string containing the commit messages of the pull request. + """ + try: + commit_list = self.pr.get_commits() + commit_messages = [commit.commit.message for commit in commit_list] + commit_messages_str = "\n".join([f"{i + 1}. {message}" for i, message in enumerate(commit_messages)]) + except: + commit_messages_str = "" + return commit_messages_str diff --git a/pr_agent/git_providers/gitlab_provider.py b/pr_agent/git_providers/gitlab_provider.py index fc7e9a73..10363ec1 100644 --- a/pr_agent/git_providers/gitlab_provider.py +++ b/pr_agent/git_providers/gitlab_provider.py @@ -297,3 +297,17 @@ class GitLabProvider(GitProvider): def get_labels(self): return self.mr.labels + + def get_commit_messages(self) -> str: + """ + Retrieves the commit messages of a pull request. + + Returns: + str: A string containing the commit messages of the pull request. + """ + try: + commit_messages_list = [commit['message'] for commit in self.mr.commits()._list] + commit_messages_str = "\n".join([f"{i + 1}. {message}" for i, message in enumerate(commit_messages_list)]) + except: + commit_messages_str = "" + return commit_messages_str diff --git a/pr_agent/settings/pr_description_prompts.toml b/pr_agent/settings/pr_description_prompts.toml index 16b6bfde..95a12681 100644 --- a/pr_agent/settings/pr_description_prompts.toml +++ b/pr_agent/settings/pr_description_prompts.toml @@ -36,8 +36,14 @@ Don't repeat the prompt in the answer, and avoid outputting the 'type' and 'desc user="""PR Info: Branch: '{{branch}}' {%- if language %} + Main language: {{language}} {%- endif %} +{%- if commit_messages_str %} + +Commit messages: +{{commit_messages_str}} +{%- endif %} The PR Git Diff: diff --git a/pr_agent/tools/pr_description.py b/pr_agent/tools/pr_description.py index 3262ee10..aaf14227 100644 --- a/pr_agent/tools/pr_description.py +++ b/pr_agent/tools/pr_description.py @@ -27,7 +27,8 @@ class PRDescription: self.main_pr_language = get_main_pr_language( self.git_provider.get_languages(), self.git_provider.get_files() ) - + commit_messages_str = self.git_provider.get_commit_messages() + # Initialize the AI handler self.ai_handler = AiHandler() @@ -39,6 +40,7 @@ class PRDescription: "language": self.main_pr_language, "diff": "", # empty diff for initial calculation "extra_instructions": get_settings().pr_description.extra_instructions, + "commit_messages_str": commit_messages_str } # Initialize the token handler