A Helper Agent That Became My Friend
Published: 2023-12-04 13:08:35

The title seems pretty intuitive and clickbait, right? But I promise that it isn't!


As an open-source contributor, I struggle to check out all the GitHub notifications I get each hour. Sometimes, it burns me out to check them on a daily basis, and I also get a lot of notifications from people complaining about not checking their issues or pull requests.

Pull requests are one of the most important things in our lives if we are developers. Pull requests can contain a fix of a bug that you have been struggling to resolve for a very long time. Pull requests can contain new enhancement features, documentation, and a lot of things that can not be described in sentences.


As for me, I find pull requests need more attention than the issues themselves. Why is that? Let me clarify.


Pull requests indeed require significant attention in the software development process. Here are a few reasons why they might demand more focus than issues:

1. **Complexity**: Pull requests often involve changes across multiple files or even entire systems. They may include multiple solutions or features, making them more intricate than individual issues.

2. **Code Quality and Review**: Pull requests necessitate thorough code reviews to ensure the quality, correctness, and adherence to coding standards. This involves analyzing the changes made, checking for bugs, and ensuring that the code integrates smoothly with the existing codebase.

3. **Integration and Testing**: Pull requests need to be integrated into the main codebase seamlessly. This involves running various tests to ensure that the changes do not introduce new bugs or regressions.

4. **Collaboration and Discussion**: Pull Requests are often the focus of collaboration between team members. Discussions, suggestions, and improvements might occur within the PR itself, making it a focal point for teamwork and iteration.

5. **Impact on the Project**: Unlike issues, which often describe problems or feature requests, pull requests directly implement changes. Their impact on the project can be more profound, making careful attention essential.

6. **Merge Process**: Before merging a pull request, it's essential to consider the implications it might have on the codebase and the project as a whole. Handling conflicts and ensuring a smooth merge is critical.

While issues define problems or tasks, pull requests represent the proposed solutions or changes to address those issues. Hence, they demand more attention due to their direct influence on the codebase and the project's progress.

As we need to do most of the tasks manually in the pull requests, it takes a lot of time and effort to check them. 

What if we had an Artificial Intelligence agent that does the job for us? That sounds interesting, in fact too amazing and too good to be true, right?

Well, luckily for me, I have already found an interesting tool that does exactly what I struggle to do in the pull requests. That is none other than the [PR Agent of Codium AI](https://www.codium.ai/products/git-plugin/).


![Website for the Git Plugin of CodiumAI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyuouo0uw27ugzgvnsk4.png)

Now, I know that a lot of you are thinking about the existing GitHub copilot. Don't get me wrong, I was there too. I was an avid Copilot user, but after checking out CodiumAI, I have become their fan, to be honest. The detailed comparison can take a lot of pages here, but I am going to make it simple so that you can get to know the differences at a glance.

| GitHub Next (Copilot for Pull Request)  | PR Agent of Codium AI  |
|---|---|
| Closed Source  | Open Source   |
| Limited amount of commands   |  Supports many commands |
|  Supports only GitHub | Supports all Git platforms  |
| Supports GitHub only | Supports GitHub and IDEs |

## How To Add Codium AI's PR Agent Into Your GitHub Repository

There are many ways to add the PR agent to your new or existing GitHub repository, but I find it useful to do that as a GitHub action. If you are interested in checking the other available installation methods, then you can simply head over to their [official documentation](https://github.com/Codium-ai/pr-agent/blob/main/INSTALL.md) where they described each of them really well.

I will specifically show the hands-on procedure of how you can do that too.

- You have to create a `.github` directory in your repository's root directory. In that `.github` directory/folder, you can create another sub-directory/folder named `workflows` which is useful especially when you are planning to use multiple workflows in a repository.
For the sake of this article, I am creating a brand new repository named **test-pr-agent** on GitHub.
![Creating a new GitHub repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wk2m8alor3hfxwbf3f2l.png)
![Creating a new GitHub repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38mbdqlsbngln24xikv3.png)
- The new repository has been created.
![A new GitHub repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3q8cl1ebesbfx5i83u4v.png)
- For all of the later tasks, you can directly use your browser and make the changes, or you can also open the VSCode dev edition on your browser/codespace. You can also make the changes locally after cloning the repository. I like to work on local machines, therefore, I will clone the repository and make the changes.
- For cloning my repository, I would simply copy the `SSH` URL.
![Clone using SSH](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gt2fi549jr7b65y77lwi.png)
- Now I will open my terminal. You can use any terminal you want. Then you can also change the directory if you want to clone the repository in a different location. For example, I am changing my current directory to the Desktop as I want to clone the repository in my Desktop directory.
![Changing the current directory to the Desktop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yumar94enm7rtkndnmd1.png)
- Make sure that you already have configured your local machine for GitHub usage, like installing Git if you are on Windows, adding the SSH keys, and so on. To clone the repository, I can simply use the `git clone SSH-URL` command. For me, it is `git clone git@github.com:FahimFBA/test-pr-agent.git`
![Cloning the repository in the local machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cf87kq2kijzcb3uviaiw.png)
- As I already have the VS Code installed on my local machine, I can simply open the VS Code in that cloned repository's directory.
![Opening VS Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmn8kiec9610h84pbogb.png)
- For now, I am creating a new `yml` file under `.github/workflows/`. I am naming the file as `pr_agent.yml`. You can give it other names if you want, but you can not keep space between the words in the file name. After this, the file is residing within `.github/workflows/pr_agent.yml`. 
- Now I can copy the relevant lines of code from the official documentation.
```yml
on:
  pull_request:
  issue_comment:
jobs:
  pr_agent_job:
    runs-on: ubuntu-latest
    permissions:
      issues: write
      pull-requests: write
      contents: write
    name: Run pr agent on every pull request, respond to user comments
    steps:
      - name: PR Agent action step
        id: pragent
        uses: Codium-ai/pr-agent@main
        env:
          OPENAI_KEY: ${{ secrets.OPENAI_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
- Now I can simply commit the changes and push to the origin.
- Maybe you can already understand that we need to enable the GitHub action to write settings on the repository. We also need to generate our OpenAI Key and add that as a repository secret in the GitHub repository.
- Every GitHub repository has a specified GitHub token, and that is generally used as `GITHUB_TOKEN` in the code. But we need to enable the write access from that specific repository settings. To do that, simply head over to the GitHub repository in your browser, and go to the **Settings** tab.
![Go to repository settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzt0zu8flnb3z4an50kl.png) <br>
- Click on the **Actions** menu and then click on the **General** submenu.
![GitHub action settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kifw6dgbvwout9wy7jjk.png)
- Make sure that you have selected **Allow all actions and reusable workflows** within the Actions permission section.
![Actions permission](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4e585dgjiyqt3ji952p.png)
- You can keep the **Fork pull request workflows from outside collaborators** section as it was. I keep it in `Require approval for first-time contributors`.
![Fork pull request workflows from outside collaborators section](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ia5mhit2avmk9619s7j3.png)
- Make sure to select `Read and write permissions` within the **Workflow permissions** section.
![Workflow permissions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38rcsdn1cnktszd51bdl.png)
Also, make sure that you have added a tick (✅) mark in the `Allow GitHub Actions to create and approve pull requests` section.
- Click `Save` to save all newly customized settings.
- Now we need the API Key from OpenAI. Simply head over to the [official website](https://platform.openai.com/api-keys) to get the API key. Login to your OpenAI/ChatGPT account.
- We need to generate a new secret API key to use in that repository. Simply click on "Create new secret key".
![Create new secret key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izjv7ape8q5n0zoga3bu.png)
- Give a name to your new secret key so that you can understand afterward which key you used for the PR-agent. 
![Name your secret key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mz6v5rymfw6xnictxyou.png)
For me, let me name it "PR-agent secret key". Then click on "Create secret key".
This will provide you with a new secret key. Make sure to copy it, as we need it right after this. Also, it is a sensitive key, therefore, make sure no one else gets to know this key. For security purposes, I have also tried to hide the key.
![My Secret key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/at2kig4omt0r6to4fa79.png)
Click on "Done".
- Now head over to your repository where you have added the `yml` file to use the PR-agent. Go to the repository settings, Click on "Secrets and variables" and lastly click on "Actions".
![repo actions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51ujiyushkd663w43wck.png)
- Click on "New repository secret".
![repo secret](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/toaacjo0hml77jcodxqc.png)
- Now paste your OpenAI Secret key in the Secret box and give it a name that you used in the `yml` file. As I used `OPENAI_KEY` as the variable name in the `yml` file, I am going to give the same name here as well.
![my repo secret for the OpenAI key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yml9g7hlj7rqnoflyv4.png)
- Click on "Add secret". Now you are good to go!

## Let us test it out!
Now it is the time to check whether that is working or not. For simplicity, I am creating a new branch locally where I am making some changes. Later I am going to add a new pull request.


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

Now you see that the PR-Agent has already started working in the PR.


![actions on the go](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ugq7w8ee5f2oumxlyok.png)

I can call the CodiumAI-Agent by mentioning it with a `@` and I can pass the pre-defined commands. For now, let's see what it reviews on the new pull request.

I can simply use `@CodiumAI-Agent /review`, and then click on "Comment".

The PR-agent has started working instantly, and I can also check that from the Actions > Workflows tab.


![PR-agent has started working](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmtisniuc3t5dr1h7p1e.png)

Now you see that the PR-agent has responded to my comment with an eye emoji (👀), and it immediately provided me with its review. It also gives feedback, how to use, instructions, etc. data so that others can also use it.

After calling once, the PR-agent would become available in the mentions, and you can call it using the `@`. 

![Call the PR-agent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtl356tmacbiejhek9vb.png)

## Currently available commands for the PR-agent are:

You can tag `@CodiumAI-Agent` in a comment '@CodiumAI-Agent' and add one of the following commands:
`/review`: Request a review of your Pull Request.
`/describe`: Update the PR title and description based on the contents of the PR.
`/improve [--extended]`: Suggest code improvements. Extended mode provides a higher quality feedback.
`/ask <QUESTION>`: Ask a question about the PR.
/update_changelog: Update the changelog based on the PR's contents.
`/add_docs`: Generate docstring for new components introduced in the PR.
`/generate_labels`: Generate labels for the PR based on the PR's contents.
You can also see the [tools guide](https://github.com/Codium-ai/pr-agent/blob/main/docs/TOOLS_GUIDE.md) for more details.

I love asking questions using this!

![A silly conversation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gqo2b1crjdkerqehqo8.png)

## On a serious note

Enough chatting! Now it is the time to test it in action. I am working on a project named [AssistMe](https://github.com/FahimFBA/assist-me/).

Now I am going to integrate PR-agent into this existing repository, and then I am going to check whether it performs well on the older Pull Requests.

Voila! 🎊

It also works with the earlier pull requests.

- Test: review [Old pull request 1](https://github.com/FahimFBA/assist-me/pull/136)
![old PR](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iux5gak8jrkpz20bxl8n.png)


![Code feedback](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1mr1bdtbeabqnt9yv6hk.png)

- Test: add_docs [Old pull request 2](https://github.com/FahimFBA/assist-me/pull/14)


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

- Test: improve [Old pull request 3](https://github.com/FahimFBA/assist-me/pull/71)


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

## Conclusion
Thanks for reading the entire article. Now you see how I scaled up my open source project progress and made myself more productive while CodiumAI's PR-Agent is doing my task for me more efficiently!

It's really a blessing to have this type of AI tool for us. Surprisingly enough, CodiumAI's PR-agent also offers a free tier, and the examples I have shown above, all of them have been done using the free tier account. So, just think how many things can be done if you want to scale it more!

This is it for today. Hope to see you again someday.

Cheers! 🥂

Cover Image Credit: Photo by <a href="https://unsplash.com/@neonbrand?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Kenny Eliason</a> on <a href="https://unsplash.com/photos/man-wearing-white-and-black-plaid-button-up-sports-shirt-pointing-the-silver-macbook-y_6rqStQBYQ?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Unsplash</a>