AI Powered Automatic PR Body Generator
Tags: githubhack23
Published: 2023-05-17 18:10:15

## What I built 

I built a GitHub Action that automatically generates and updates a pull request body on each push by feeding the diff to the Text Completion endpoint of OpenAI. The generated pull request bodies include a summary of the PR, a list of changes, as well as refactoring suggestions. 

### Category Submission: 

Maintainer Must-Haves

### App Link
https://github.com/marketplace/actions/ai-powered-automatic-pr-body-generation

### Screenshots 

![Ai generated PR body 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wegvsxxklhsfg2lhtpjb.png)

![AI generated PR body 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c6fwrleqebnpnvgx5ip5.png)

### Description 

The GitHub Action takes the diff of the changes made in a pull request and uses OpenAI to generate a summary of the changes. The summary includes information such as the files changed, lines added, and lines removed, as well as suggestions for refactoring the code. The pull request body is then updated with the summary on each push.


### Link to Source Code 

https://github.com/jbrocher/auto-pr-body-generator

### Permissive License 

MIT

## Background (What made you decide to build this particular app? What inspired you?) 

As a  developer, I spend a lot of time writing pull request bodies manually, which can be a tedious task. However it is important to maintainers so they can quickly get a high level overview of a Pull Request. I think automating this process would bring benefits to both the person submitting the PR and the maintainers. With an automated system in place, the PR author can save time, while the maintainers can rest assured that they have all the relevant information they need to prioritize the PR they want to review.

I was also interested in exploring the capabilities of OpenAI and how it could be used to automate tasks in software development. This was a great way to do that! 


### How I built it (How did you utilize GitHub Actions or GitHub Codespaces? Did you learn something new along the way? Pick up a new skill?) 

I used a composite Github Action and the Text Completion endpoint of OpenAI. 

The diff can be of any length, and there is a limit on the number of tokens that can be submitted to OpenAI. To work around this, I split the diff into smaller chunks and  combined and summarized the results by recursively feeding them to the completion endpoint. 

The prompt needed to be engineered to provide more relevant information. I am still working on refining the prompts to improve the relevance of the refactoring suggestions. I'm discovering the world of prompt engineering, which is a lot of fun! 

I'm still working on the project, and have a few next steps in mind: 
 - Add a safeguard to limit the number of tokens processing credit spent by the action for a single run
 - Use more information from the PR to generate better prompts
 - Improve the prompt chaining to provide an accurate big picture for large pull requests
 - Improve the relevance of the refactoring suggestions. It should be possible to incorporates customizable coding style guides.

### Additional Resources/Info
 - [OpenAi Text Completion Endpoint documentation](https://platform.openai.com/docs/guides/completion)
 - [Github Actions Documentation](https://docs.github.com/en/actions)