Open source, AI, and CodiumAI's PR-Agent

thecodersden

Rajdip Bhattacharya

Posted on December 9, 2023

Open source, AI, and CodiumAI's PR-Agent

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)

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 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!

PR-Agent by CodiumAI

The PR-Agent 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 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

PR-Agent marketplace

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

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

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

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

Describing the scope of the PR. (/describe)

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
Enter fullscreen mode Exit fullscreen mode

If you have set up everything correctly, Codium-AI will "see" your comment (literally). Notice the "eyes" reaction:
/describe comment

Now, the tool will do the following things:

  • Add a comment stating it has updated the PR description and title
    /describe update notification

  • Add the PR description and change the PR title
    /describe updated PR description and title

Tagging our PR (/generate_labels)

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

PR Agent gives us the ability to generate meaningful labels of our PRs. Here's how you do it:

/generate_labels
Enter fullscreen mode Exit fullscreen mode

/generate label output

/generate label output

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)

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
Enter fullscreen mode Exit fullscreen mode

/review output

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)

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
Enter fullscreen mode Exit fullscreen mode

/improve 1

/improve 2

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>
Enter fullscreen mode Exit fullscreen mode

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

/ask output

/ask output

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

Scope for adding documentation (/add_docs)

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
Enter fullscreen mode Exit fullscreen mode

/add_docs 1

/add_docs 2

/add_docs 3

Image description

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

docs

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)

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

[version]

- things added
- things fixed
- things removed
- anything else
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

update_changelog

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 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

All the custom configurations are mentioned in the individual tool guides

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 guide to see what all things you can change. We will insert these configs into our .pr_agent.toml file:

[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"
Enter fullscreen mode Exit fullscreen mode

Let's break this down.

[config]
enable_custom_labels = true
Enter fullscreen mode Exit fullscreen mode

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:

[custom_labels."Assign this label"]
description = "When this condition satisfies"
Enter fullscreen mode Exit fullscreen mode
[custom_labels."Link Update"]
description = "Whenever any link in the code is updated"
Enter fullscreen mode Exit fullscreen mode

This block tells the PR Agent to assign the Link Update label whenever any link in the code is updated.

[custom_labels."Code Update"]
description = "Whenever any code is updated"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

custom generate_labels

custom generate_labels

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!

💖 💪 🙅 🚩
thecodersden
Rajdip Bhattacharya

Posted on December 9, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related