CodiumAI PR-Agent: AI-powered review for pull requests
Tags: ai
Published: 2024-01-22 09:10:22

# TL;DR

PR-Agent looks promising for quick feedback, answering questions, and providing committable suggestions on pull requests.

# Intro

Have you ever dreamed of pull requests (PRs) that are fixed and merged automatically right after they are created? With no devs and QAs spending time on code review and with no wait time on your side.

No? Me neither.

![CodiumAI pr-agent intro](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19svw8d87fezhkkgntz8.jpeg)

BUT. What I think would be beneficial is a tool that can provide meaningful feedback on PR changes with committable suggestions. And this is exactly what PR-Agent can do.

# PR-Agent overview

PR-Agent is an open-source tool that helps in reviewing pull requests. It analyzes the pull request and provides feedback as well as suggestions on how to improve the code.

PR-Agent supports GitHub, Gitlab, and Bitbucket. You can use the free public [@CodiumAI-Agent](https://github.com/CodiumAI-Agent) on GitHub, install PR-Agent on your premises, or subscribe to PR-Agent Pro which is a hosted version of PR-Agent, provided by CodiumAI.

PR-Agent calls [OpenAI's GPT-4](https://openai.com/gpt-4) under the hood. It also supports other models should you want to use one of them, e.g., GPT-3.5, Anthropic, Cohere, Llama2.

You can find more info on the [official page](https://www.codium.ai/products/git-plugin/) and on [GitHub](https://github.com/Codium-ai/pr-agent).

# PR-Agent commands

Okay, now let's take a look at what PR-Agent can do. I will use the free `@CodiumAI-Agent` available on GitHub.

Running PR-Agent is as simple as tagging it in the PR comment.

I'm going to take my repository for [Advent of Code](https://adventofcode.com/) puzzle solutions and create a [PR](https://github.com/pfilaretov42/advent-of-code/pull/1). All the samples below are run in this PR, so feel free to check it out for more details.

Also, here is the [tools guide](https://github.com/Codium-ai/pr-agent/blob/main/docs/TOOLS_GUIDE.md) with the description of available PR-Agent commands.

All right, let's dive in.

## Review a PR

Provides PR analysis and feedback on the code changes.

Usage:

```
@CodiumAI-Agent /review
```

Sample:

![CodiumAI pr-agent review 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/84m9n8z2svi9h4cww9fv.png)

Code feedback under the cut:

![CodiumAI pr-agent review 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4lxxmhagrh0x4ckwhdmc.png)

The analysis is well-formatted and pretty clear.

It suggests adding error handling for cases like file not found or wrong file format, so it looks like it sees only the context of the PR. It cannot see the bigger picture, i.e. that the file is local to that same repository and I don't need error handling here. If the file is not found, then it means I made a typo and I will just fix the path in the code. But still, it's good to have feedback on this as a reminder.

## Suggest code improvements

Suggests changes that can be committed.

Usage:

```
@CodiumAI-Agent /improve
```

Samples:

![CodiumAI pr-agent improve 0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yyucl65guxharit3m18s.png)

![CodiumAI pr-agent improve 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/of7pfkxifx38xibyeicq.png)

![CodiumAI pr-agent improve 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hw7sp0rymndvxgxwqfk0.png)

These suggestions make sense to me, I think I would apply them.

## Add documentation

Generate documentation for new components introduced in the PR, again as committable changes.

Usage:

```
@CodiumAI-Agent /add_docs
```

Sample:

![CodiumAI pr-agent add docs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwt3g4kfo1dgsxd22l9f.png)

The doc looks verbose a bit. I would not commit it as is, but it can be used as a starting point.

## Describe PR

Update the title and description of the PR based on its content.

Note that `@CodiumAI-Agent` is a "promotional" bot of PR-Agent, which does not have access to edit existing data in my PR. Hence, `/describe` won't work in this case.
		  What we can do here is add a command parameter, so that description is published as a PR comment:  
```
@CodiumAI-Agent /describe --pr_description.publish_description_as_comment=true
```

Sample:

![CodiumAI pr-agent describe 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c6gsayhk500lf8h2vfsi.png)

Changes walkthrough under the cut:

![CodiumAI pr-agent describe 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/labv16bg9x5c8j7h54l4.png)

The generated description is not as verbose as the generated documentation which is good. And yet it is a bit vague in "performs some calculations". But again, it looks nice as a starting point.

## Answer questions

You can ask questions about the PR. For instance:

```
@CodiumAI-Agent /ask How can I change the code to make it more readable?
```

Sample:

![CodiumAI pr-agent ask 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99ng9eq7nufipft90z82.png)

Continuation - example of refactored code:

![CodiumAI pr-agent ask 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pr1fv5j6ld0ddkayloru.png)

The refactored code compiles and produces the same output which is good. And it indeed looks more readable. There is one thing here though that I don't personally like and try to avoid: modification of mutable map passed as a parameter. But again, this is just an example that could be used as a quick help to refactor the code.

## Add changelog updates

Update the changelog based on the PR's contents. By default, PR-Agent generates a comment but then it can push it to the `CHANGELOG.md` file.

Usage:

```
@CodiumAI-Agent /update_changelog
```

Sample:

![CodiumAI pr-agent update changelog](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7wna33m5nue3ze95rer.png)

Nothing special here and yet the markdown formatting is good, the description is clean and concise.

Also note that it inserts the current date in the header, not the date when PR was created.

## List config parameters

There are a lot of configuration parameters you can set in a config file or in the command itself (for instance, see the `/describe` command sample above). To list them all, use the `/config` command:

```
@CodiumAI-Agent /config
```

Sample:

![CodiumAI pr-agent config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wxa72qjmbytk6guq2gt.png)

# Security concerns

Okay, this is great, but what about security? Does the PR-Agent and/or OpenAI store the code they analyze?

In a nutshell, free-tier users' data is used to train AI models. Paid subscribers' data is never used to train models and is stored for 48 hours for troubleshooting purposes only. Also, CodiumAI-hosted PR-Agent uses zero-data-retention OpenAI account.

If you still prefer zero data retention, then there is an option to email CodiumAI support and explicitly ask for this.

If you use a self-hosted PR-Agent with your own OpenAI API key, then it is between you and OpenAI.

For more information see the following blog post: [CodiumAI Security: Our Commitment to Data Privacy and Security](https://www.codium.ai/blog/codiumai-security-our-commitment-to-data-privacy-and-security/).

# Things to improve

After using `@CodiumAI-Agent` on GitHub for some time I found a couple of things that would be nice to have:

- PR-Agent does not answer anything on both wrong and premium (PR-Agent Pro) commands. For instance:
```
@CodiumAI-Agent /test
@CodiumAI-Agent /analyze
```
It would be nice if it added a comment to PR stating that the command is wrong and providing usage info with a list of the correct commands.

- I don't know if it's even possible with comments in PRs but it would be really cool to have an autocompletion for commands available. So that when I'm typing `@CodiumAI-Agent /`, the list of possible commands appears. Then, once I picked the command, the list of possible configuration parameters for this command is presented.

One more feature that I think would be beneficial is integration with static code analysis tools such as SonarQube. You can set your own profile and rules in SonarQube. When you open a PR, SonarQube analyzes changes, and based on issues found PR-Agent could automatically suggest committable changes in the PR to fix issues.

# Conclusion

PR-Agent looks promising for quick feedback and suggestions on PRs. Of course, you cannot blindly accept everything it produces (yet), and review is still needed. However, this looks like a big help in speeding up the PR review and improving code quality.

---

*Dream your code, code your dream.*