Open source, AI, and CodiumAI's PR-Agent
Tags: ai, opensource, github, development
Published: 2023-12-09 06:10:14

Hola geeks!

Development, undoubtably, is a vast ocean. We can barely define a starting point and definitely no ending point. Like we all know, "Necessity is the mother of all inventions", our journey this far is no exception either. It all started with Sir Charles Babbage inventing the first computer, followed by a massive breakthrough when Sir Alan Turing developed the **Turingery** during WWII to decipher the **Enigma** code. This decade and the last have seen the creation of the most prolific systems and pieces of software that lay the foundation of modern day computing and software development. Throughout this entire journey, we have always been, and are, fascinated by one technology in particular - **Artificial Intelligence**. 

In our day-to-day lives, we interact with multiple software that employ AI to ease our work. Not only that, developers have put in efforts to bring AI to our loved IDEs for our ease of development! 

Undeniably, most of the development happen on **GitHub**. Every developer in his/her life has come across GitHub to look for a solution or look at the code of some of the greatest **open-source** repositories. GitHub says that there are a whooping **94 million** developers who are actively contributing and developing in the open-source industry!

This brings us to an important concept - a **PR**

## So what's a PR?

For any developer out there, who is a contributor to any **OSS**, certainly know about [**pull requests (PRs)**](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#:~:text=Pull%20requests%20let%20you%20tell,merged%20into%20the%20base%20branch.)

In case you heard this term for the first time (or perhaps you want a refresher), whenever we **contribute** any changes to an open source project, we do it via a pull request (PR). For doing this, you first write your code in a separate branch, then you head over to the repository to create a PR. This is done with the intention of giving the maintainers a glance into the changes you made and test the correctness/feasibility of your changes. 

This brings us to a gradual conclusion that, for our PRs to be accepted, we need the maintainers to have a full picture of the changes we made. As humans, we are bound to make mistakes, the top ones being:
- Inaccurate/incomplete description about the changes you made
- Bugs/errors/potential security issues in your code base
- And most importantly, the lack of energy to write a concise PR.

These errors are mostly fixable, but there's someone (some"thing") that could make our lives a lot easier: **CodiumAI's PR-Agent**

## A quick glance into CodiumAI

[CodiumAI](https://www.codium.ai/) is an AI based product that helps developers in doing their stuff. Their plugins sit inside your IDEs, helping you out in your code by doing a significant heavy lifting. They provide automated tests for your code, that not only makes your code more resilient, but also aids in ramping up your efficiency and productivity. And the best part is, **CodiumAI is entirely [open-sourced](https://github.com/Codium-ai)!**

## PR-Agent by CodiumAI

The [PR-Agent](https://www.codium.ai/products/git-plugin/) is an AI-based Git plugin developed by CodiumAI. It helps us in our open-source journey by accomplishing the following:
- **Auto-Description:** Automatically generating the description about the entire PR that includes name, summary and code walkthrough.
- ** PR Review:** Automated smart feedbacks about possible errors or bugs in our code
- **Question Answering:** Helps us in querying the different aspects of the PR.
- **Code Suggestion:** Suggestions about possible improvement scope for our code to polish our PR.

## Finally, the hands-on!

Skills are pointless until and unless you practice using them for your own good! And the fact that we are learning about an absolute game changer, this blog would be incomplete without a hands-on section.

We would take a wild ride doing the following:
- Installing PR-Agent
- Creating a pull request
- Creating a detailed description of what the PR is about.
- Tagging our PR
- We will ask the PR-Agent to give us a review on our PR (oh, how badly I strive to get a review!)
- Of course, we are bound to make mistakes. We will detect the errors in our code.
- We will take a glance into how PR-Agent helps us in fixing the error. 
- Making further improvements by adding documentation.
- Generating a CHANGELOG

You can follow this [article](https://github.com/Codium-ai/pr-agent/blob/main/docs/TOOLS_GUIDE.md) throughout the blog to have a reference. 

**NOTE: Custom configs to be covered in the later half!** 

### Installing PR-Agent
Here is the nice part about PR-Agent: It enables you to use the tool without even any installation. All you need to do is `@CodiumAI-Agent <command>` (commands to be discussed later), and it will do its magic! 

But there are some drawbacks as well:
- The first being, you have to put a mention tag (@CodiumAI-Agent) in front of every command.
- Not all commands will work with the mention tag. 
That being said, it's safe to say that you get a sort of "trial" of the tool without installing it. 

Now, to further extend its capabilities, an installation is necessary. For doing that, head over to this [link](https://github.com/marketplace/codiumai-pr-agent)

![PR-Agent marketplace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h08otl64jv4hcs3737zc.png)

One important thing, before you proceed, make sure you are logged on to GitHub.

As you can see, it's free for individual developers! Next, scroll down and hit on **Install it for free**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ia51fcr0jt0bnpyw39h0.png)

This will take you to a billing page, where you have to enter your billing details. Rest assured, you won't have to add any banking details, nor will you be charged a single penny. 

Once everything is successful, you would be redirected to the integration page of your GitHub profile.

![GitHub integrations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00tgbvzuf2i908cly1nv.png)

In case you want to visit this page later on for some reason, you can do it by visiting **Profile Icon** -> **Settings** -> **Integrations** -> **Applications** -> **CodiumAI PR-Agent**

### Creating the PR
There are numerous ways to get started with this. I won't be digging into creating the PR, but here's an article that will help you to follow along: https://docs.github.com/en/desktop/working-with-your-remote-repository-on-github-or-github-enterprise/creating-an-issue-or-pull-request-from-github-desktop

For the context, I have created a PR to my own portfolio repository, where I have intended to add some code to include my Twitter account in my portfolio. Again, the code base doesn't matter, since the PR-Agent works with any and every code base.

Here is a glimpse of the initial PR:

![PR](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s57yh7ecevau3xa1fjt1.png)

It looks pretty empty (and useless). Let's give it the shine it deserves.

### Describing the scope of the PR. ([/describe](https://github.com/Codium-ai/pr-agent/blob/main/docs/DESCRIBE.md))
As you saw in the above image, our PR looks pretty barren. Creating PRs have a sole purpose of integrating the changes we made into the existing code base. For that to happen, we want our reviewers to have a clear understanding of what the PR does. Thankfully, our AI buddy makes it a breeze of an effort! Let's see how.

Head over to the comment section, and enter:
```
/describe
```

If you have set up everything correctly, Codium-AI will "see" your comment (literally). Notice the "eyes" reaction:
![/describe comment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8t3we77khdvk2u6xsy55.png)

Now, the tool will do the following things:
- Add a comment stating it has updated the PR description and title
![/describe update notification](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8noqqzfasb82lb7h3gst.png)

- Add the PR description and change the PR title
![/describe updated PR description and title](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gmawr8rglpz3l3p8i6k.png)

### Tagging our PR ([/generate_labels](https://github.com/Codium-ai/pr-agent/blob/main/docs/GENERATE_CUSTOM_LABELS.md))

GitHub (and every other platform) provides us with the ability to search issues and pull requests using tags. Tags help us categorize the issues. Here is a glimpse of the PR-Agent issues in GitHub.
![codium ai pr agent issues](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7z9wcny2mvb4wrn9ptxk.png)

PR Agent gives us the ability to generate meaningful labels of our PRs. Here's how you do it:
```
/generate_labels
```
![/generate label output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/molhqzvyt6wficgqfgju.png)

![/generate label output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/663nefqtilnkm5kfk0zm.png)

As you can see, the label **enhancement** was inferred from the changes we made.

This doesn't end here, as we can inform the AI to use custom labels for pre-defined changes. More on that later.

### Feedbacks are important ([/review](https://github.com/Codium-ai/pr-agent/blob/main/docs/REVIEW.md))

Often times you will find yourself struggling to get a concise feedback on your work. Feedbacks enable us to further improve on our code and make it much more efficient. Along with it, you also get to know about the compatibility of the new features that you introduced with the existing code. 

However challenging this might sound, fear not, because our AI partner has got our backs!

All you need to do is, type in:
```
/review
```

![/review output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7y8kb2ri0zc644d89bp7.png)


As you can see, the review cover a great details. It has even got the **Estimated effort to review [1-5]** scale that comes in super handy to the maintainers.

Now that we got a brief about the internals of the code, let's see if our code works as expected or not.

### Looking for scopes of improvement ([/improve](https://github.com/Codium-ai/pr-agent/blob/main/docs/IMPROVE.md))
As humans, we tend to make mistakes. The PR-Agent gives us a handle `/improve` to scan for possible errors / bugs.

We run this, (and all of the commands to follow) in the same way we ran the previous command:
```
/improve
```

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

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

As you can see, we do have issues in our codebase! I have mistakenly (deliberately) used the wrong URL for my twitter account.

Now, let's see how we can fix them.

### Got questions? All you need to do is `/ask`!

Although we got the error, we still don't know what's causing it, or what we should do to fix it. In such scenarios, we can use the `ask` command. The usage of this command is similar to the previous one, just that it takes a question as the parameter:
```
/ask <your question>
```

So, let's ask our AI buddy "how should we fix this?"

![/ask output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5op8bpp2g55bumrbptf.png)

![/ask output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3pp7do2u8mbtg4j0vp1p.png)

And we see a neatly formatted and concise message, explaining us what we should do to fix our issue.

Now we can fix the errors, and can make a push.

![/ask fixed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/447exhiqi3is009yr27v.png)

### Scope for adding documentation ([/add_docs](https://github.com/Codium-ai/pr-agent/blob/main/docs/ADD_DOCUMENTATION.md))
Documenting our code, although might seem boring, but is an extremely important task in software development. Generally, any software that gets written, has a bulk of documentation associated with it. 

Often times, while implementing some fix or adding some feature, we miss out on docs. PR Agent helps us in finding those spots where we can add documentation.

We do this by:
```
/add_docs
```
![/add_docs 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlftoqol0luzgtmy7z7z.png)

![/add_docs 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gx46c88x9u1kxdxgmre.png)

![/add_docs 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xf68d1ulghjganshl0en.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rn1suytj8pkpqfh0mosh.png)

As you can see, the tool gives us a bunch of things that can be documented.

Now, I am that lazy developer who never writes the docs. So I'll as my AI buddy to do it for me :D

![ask for docs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i840rlgf8j72wejmz8sl.png)

![docs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwum73mwwq45i7nag96o.png)

And as you can see, it gave me the things I need to change, all I need to do is copy and paste.

### Generating a CHANGELOG ([/update_changelog](https://github.com/Codium-ai/pr-agent/blob/main/docs/UPDATE_CHANGELOG.md))
Every project has a file called `CHANGELOG.md`. In case you are wondering what that is, it is a ledger that keeps track of all the changes that were made in a specific version. It mosly contains these things in this format

```toml
[version]

- things added
- things fixed
- things removed
- anything else
```

Changelog is an important component that helps us in visibility, hence it becomes extremely important to manage it properly. 

PR Agent helps us in this by generating a CHANGELOG for all the changes made in the particular PR. We do it by:
```
/update_changelog
```

![update_changelog](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk39azd1s4l97hm9pjjo.png)

We do have a small change that we made, and hence the log file is small, but this does demonstrate us the capability of the tool in assessing our file!

## Custom configuration - going a step further

We have already crossed two big steps:
- Using the commands via @CodiumAI-Agent mention
- Using the commands via local installation

While we feel we might have everything that we need, this approach might not fit bigger projects. Such projects have a more "focused" way of tracking their changes, and might want the PR-Agent to be customized as per their wishes. 

But fear not, because we do have complete authority over how and what CodiumAI PR Agent does!

The local installation comes with a pre-defined configuration file (`.pr_agent.toml`) that hosts all the settings that PR Agent will use. [This](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#working-with-github-app) article explains you how you can get a customized PR Agent at your fingertips.

Since the article covers a lot, and the configurations can seem overwhelming at the beginning, I will try to break it down to you by demonstrating how to generate custom labels based on the changes you made (now we're getting at it!).

One important note, you might have to close your existing PR before your do this. PR Agent expects you have your configurations in place before you make the PR. (Gah... sorry for breaking it to you this late). 

### Customize your AI buddy

We will get started by creating a `.pr_agent.toml` file in the root directory of the project. Note that this should be the **main** branch of your project.
![project root](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o712fm06c2y3x39yfa72.png)

All the custom configurations are mentioned in the individual [tool guides](https://github.com/Codium-ai/pr-agent/blob/main/docs/TOOLS_GUIDE.md)

We want to do the following:
  1. Label our PR as **Link Update** if any link is updated in our code
  2. Label our PR as **Code Update** if any code changes.

Head over to the [custom labels](https://github.com/Codium-ai/pr-agent/blob/main/docs/GENERATE_CUSTOM_LABELS.md) guide to see what all things you can change. We will insert these configs into our `.pr_agent.toml` file:

```toml
[config]
enable_custom_labels = true

[custom_labels."Link Update"]
description = "Whenever any link in the code is updated"

[custom_labels."Code Update"]
description = "Whenever any code is updated"
```

Let's break this down.

```toml
[config]
enable_custom_labels = true
```
This tells the PR Agent that we want to use custom labels. What this means to the tool is, besides using its own rules for evaluating our PR labels, it will also look throught the `.pr_agent.toml` file to find user-defined rules for generating labels. User-defined labels have higher priority, which mean, given 2 rules - user-defined and pre-defined, PR Agent will use our rule for generating the labels.

The next two blocks have a simple format:
```toml
[custom_labels."Assign this label"]
description = "When this condition satisfies"
```

```toml
[custom_labels."Link Update"]
description = "Whenever any link in the code is updated"
```
This block tells the PR Agent to assign the **Link Update** label whenever any link in the code is updated.

```toml
[custom_labels."Code Update"]
description = "Whenever any code is updated"
```
This block tells the PR Agent to assign the **Code Update** label whenever any code is updated.

Once you have committed the changes to the main branch, you can open up another PR to test these settings.

Let's give it a run:
```
/generate_labels
```

![custom generate_labels](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpp49oyw1goiyo73gh65.png)

![custom generate_labels](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hguy1qk69zdfl7t12bgb.png)

As you can see, we were successful in generating custom labels for us.

## Closing thoughts

PR-Agent is certainly a game changer in the world of open source (and also closed source). It is an all-in-one package for developers and maintainers, allowing us to lift the weight of manual review and documentation off our shoulders. Doing this gives our productivity an extra polish, thereby leading us to develop with speed and accuracy.

The fact that the plugin is open-source and free of cost is a cherry on the top, setting no bounds for developers.

But why stop in using the tool? Why not contribute to the cause? [Jump right in!](https://github.com/Codium-ai/pr-agent)