Skip to content

Commit

Permalink
feat(base-branch): add base branch option to cli (#18)
Browse files Browse the repository at this point in the history
* feat(base-branch): add base branch option to cli

Now, with this option we are able to add base branch that we want to
compare with our current branch

* docs(readme): add readme todo
  • Loading branch information
alissonperez committed Mar 13, 2024
1 parent a90db71 commit c4fd5b3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ openai = "*"
pygithub = "*"
prompt-toolkit = "*"
inquirerpy = "*"
fire = "*"

[dev-packages]
flake8 = "*"
Expand Down
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Before getting started, make sure you have the following installed:
- Python 3.7 or higher
- [Pipenv](https://pipenv.pypa.io/en/latest/)

## Installation and usage
## Installation

You can install and use GPT-PR in one of two ways. Choose the option that best suits your needs.

Expand All @@ -45,7 +45,7 @@ pip install -U gpt-pr
3. Inside the Git repository you are working on, ensure you have pushed your branch to origin, then run:

```bash
gpt-pr
gpt-pr --help
```

### Option 2: Cloning the code
Expand All @@ -68,7 +68,31 @@ After exporting api keys as environment variables ([Authentication & API Keys](#
```bash
PYTHONPATH=~/workplace/gpt-pr/gpt-pr \
PIPENV_PIPFILE=~/workplace/gpt-pr/Pipfile \
pipenv run python ~/workplace/gpt-pr/gptpr/main.py
pipenv run python ~/workplace/gpt-pr/gptpr/main.py --help
```

## Usage

### Generating Github Pull Requests

To create a Pull request from your current branch commits to merge with `main` branch, just run:

```
gpt-pr
```

If you would like to compare with other base branch that is not `main`, just use `-b` param:

```
gpt-pr -b my-other-branch
```

### Usage help

To show help commands:

```
gpt-pr -h
```

## Authentication & API Keys
Expand Down Expand Up @@ -115,4 +139,5 @@ Please follow our [CONTRIBUTING](./CONTRIBUTING.md) guide.
- [x] Improve execution method, possibly through a shell script or at least an alias in bash rc files.
- Change to use with pip installation and console_scripts entry point.
- [x] Fetch GitHub PR templates from the current project.
- [ ] Add configuration to set which LLM and model should be used (OpenAI GPT, Mistral, etc...)
- [ ] Add unit tests.
30 changes: 20 additions & 10 deletions gptpr/gitutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BranchInfo:
repo: str
branch: str
commits: list
main_commits: list
highlight_commits: list
diff: str


Expand All @@ -31,7 +31,7 @@ def desc(self):
return f'{self.file_path} (+{(self.lines_added)} -{self.lines_removed})'


def get_branch_info():
def get_branch_info(base_branch):
# Get current directory
current_dir = os.getcwd()

Expand All @@ -53,30 +53,40 @@ def get_branch_info():
else:
raise Exception('Not currently on any branch.')

if not _branch_exists(repo, base_branch):
raise Exception(f'Base branch {base_branch} does not exist.')

owner, repo_name = _get_remote_info(repo)

commits = _get_diff_messages_against_main_branch(repo, current_branch.name)
commits = _get_diff_messages_against_base_branch(repo, current_branch.name, base_branch)
commits = _get_valid_commits(commits)

if not commits:
print('No commit changes detected.')
return None

main_commits = _get_main_commits(commits)
highlight_commits = _get_highlight_commits(commits)

return BranchInfo(
owner=owner,
repo=repo_name,
branch=current_branch.name,
commits=commits,
main_commits=main_commits,
highlight_commits=highlight_commits,
diff=_get_diff_changes(repo, current_branch.name)
)


def _get_diff_messages_against_main_branch(repo, branch):
def _branch_exists(repo, branch_name):
if branch_name in repo.branches:
return True

return False


def _get_diff_messages_against_base_branch(repo, branch, base_branch):
# Get commit messages that are in the current branch but not in the main branch
commits_diff = list(repo.iter_commits(f'main..{branch}'))
commits_diff = list(repo.iter_commits(f'{base_branch}..{branch}'))

return [commit.message.strip('\n') for commit in commits_diff]

Expand All @@ -97,16 +107,16 @@ def _get_valid_commits(commits):


# use inquirer to select main commits
def _get_main_commits(commits):
def _get_highlight_commits(commits):
options = [Choice(value=commit, name=commit) for commit in commits]

main_commits = inquirer.checkbox(
highlight_commits = inquirer.checkbox(
message='Pick commits to highlight in description (optional)\':',
choices=options,
instruction="(Press <space> to select, <enter> to confirm)",
).execute()

return main_commits
return highlight_commits


def _get_remote_info(repo):
Expand Down
12 changes: 10 additions & 2 deletions gptpr/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import fire
from InquirerPy import inquirer

from gptpr.gitutil import get_branch_info
from gptpr.gh import create_pr
from gptpr.prdata import get_pr_data


def main():
branch_info = get_branch_info()
def run(base_branch='main'):
'''
Create Pull Requests from current branch with base branch (default 'main' branch)
'''
branch_info = get_branch_info(base_branch)

if not branch_info:
exit(0)
Expand All @@ -29,5 +33,9 @@ def main():
create_pr(pr_data)


def main():
fire.Fire(run)


if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions gptpr/prdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def get_pr_data(branch_info):
{'role': 'system', 'content': system_content},
]

if len(branch_info.main_commits) > 0:
messages.append({'role': 'user', 'content': 'main commits: ' + '\n'.join(branch_info.main_commits)})
if len(branch_info.highlight_commits) > 0:
messages.append({'role': 'user', 'content': 'main commits: ' + '\n'.join(branch_info.highlight_commits)})
messages.append({'role': 'user', 'content': 'secondary commits: ' + '\n'.join(branch_info.commits)})
else:
messages.append({'role': 'user', 'content': 'git commits: ' + '\n'.join(branch_info.commits)})
Expand Down

0 comments on commit c4fd5b3

Please sign in to comment.