In this new world where most products come with AI built in (with varying degrees of intelligence), GitLab Duo adds powerful generative AI features to the GitLab platform.

Duo provides assistance both within the web-based user interface and when writing code in an IDE like IntelliJ IDEA or Microsoft Visual Studio.

GitLab Duo can assist with both code and non-code items. Here’s a list of the types of things that Duo can do for you.

  • Duo Chat
    • Interact with Duo to get answers for pretty much anything.
  • Code Suggestions
    • Use natural language to describe your requirements and have code generated for you.
  • Code Explanations
    • Get explanations for complex pieces of code in natural language.
  • Test Generation
    • Generate regression tests for pieces of code.
  • Suggested Reviewers
    • Populate merge requests with the most suitable reviewers.

There are also some additional capabilities currently in beta, such as the automation of descriptions for merge requests based on code changes and the fixing of security vulnerabilities identified by SAST (Static Application Security Testing) scanning.

The list of features for Duo continues to grow, which you can keep track of here.

Accessing Duo

Before you can use GitLab Duo, you need to have a Duo Pro or Duo Enterprise license assigned to your account*. It’s important to note that GitLab Duo is an add-on that is separate from your GitLab subscription and that you do not need to buy it for everyone.

You can purchase fewer seats for GitLab Duo than your total for GitLab and then choose who has access. This does mean, however, that you cannot assign a Duo license to a user without access to GitLab in the same instance (for self-managed or top-level groups, for GitLab.com).

*Please note that at the time of writing, there were some Duo features available without a subscription. However, in order to access all of the features, including Code Suggestions, you need a Duo license assigned to your GitLab account.

GitLab Duo on the web interface

Once you have been assigned a Duo license, you will start to notice new buttons on the GitLab web interface that allow you to use Duo to generate information or perform actions for you. These buttons have the GitLab Duo logo on them.

For example, when creating a new merge request, there is a new button (highlighted in yellow below) that allows you to automatically generate a description for your merge requests based on the code changes:

New merge request

The change on my branch was very small but it is pretty succinctly summarised with the description that Duo has generated here.

Another great example for using Duo in the web interface is when looking at GitLab issues. It is quite common for a GitLab issue to have a lot of comments, and it is sometimes hard to see the bigger picture.

Duo can summarize the comments and provide you with a bullet list extracted from the comment section:

Activity

Getting started with Code Suggestions

Code Suggestions are the most exciting features for developers in particular.

When working in an IDE, you need to install an extension to be able to use Code Suggestions as you type. The extension is available for the most popular IDEs such as VS Code, Visual Studio, IntelliJ IDEA, and Neovim.

Once you have installed the GitLab Workflow extension, you will not only get code suggestions but the following features directly from the IDE:

  • Interact with Merge Requests
  • Interact with GitLab Issues
  • Interact with GitLab Duo Chat

After installation, you need to log in to GitLab from the extension in your IDE. You can either use OAuth based login or add a GitLab personal access token (with API scope), whichever suits you best.

You can then start using code suggestions by writing a comment in your code using natural language. For example, in my NodeJS based web server, I wanted to read HTML text from a file and return it as the response from a GET request to my server.

Gitlab

As you can see, Duo is aware from the context that I am writing an ExpressJS web server, and the code it suggests (shown in grey) makes sense within that context. But you can also see that if I accepted that code, I would end up with something that doesn’t make sense.

The reason for that is simple—like with all technologies, you need to know how to use it to get the most out of it. I just wasn’t specific enough, and Code Suggestions are only as good as your questions.

So, let’s look at another example. I want my server to respond to a GET request that takes an email address as input and returns the content of a file that matches it.

The first step is to write the comment and then see what it suggests. As you can see, my description is quite vague, but it still generated what I wanted.

Gitlab_duo

So I accept the suggestion (by pressing the TAB key).

Now, the piece of code is great, but it doesn’t actually validate that the query parameter “email” looks like an email address. So let’s ask Duo to generate me code that validates it with a regular expression.

Gitlab.

Now that’s actually pretty cool. The indentation isn’t quite right, but the code is pretty much there. So again, I’ll accept that piece of code.

As you can see, using natural language in comments, I can ask GitLab Duo to regenerate code for me. It is worth noting that it can take a little bit of trial and error until you get the right code, but persevere and keep writing comments and refining until you get what you want.

Using Code Explanations

Now, the next interesting feature for developers is Code Explanations. Have you ever come across code that you don’t quite understand? For example, in the previous section we generated some that validates that a string looks like an email address.

If you are not familiar with regular expressions, you may find it hard to understand them. So let’s ask Duo to explain it for us.

To get an explanation, you use Duo Chat with the “/explain” command. For example:

/explain const emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

You can either use Duo Chat in the web interface or directly from your IDE. In this case, we are going down the IDE route:

Code explanation

And there you go, it explains in detail what the regular expression does and how it does it.

This works for both single lines as well as whole functions. Let’s try a small one:

Gitlab code

Here it explains what the code does and how it achieves its goal. However, what Code Explanations does not do is simplify code for you. My favorite example is complex Boolean expressions that can be simplified with a little bit of Boolean algebra.

When you throw such complex expressions at GitLab Duo Chat, it correctly explains the expression and under what condition it would be true or false, but it doesn’t take the next step and give you a new expression that is equivalent and much simpler (which is rather sad).

The quality of generative AI

As with most generative AI functionality, the quality of the response depends on two factors:

  1. The question/instruction
  2. The AI model itself

When it comes to Code Suggestions, vague instructions in comments are unlikely to lead to satisfactory results, so we must learn how to write natural-language comments in a way that makes it easy for a generative AI model like GitLab Duo to make good suggestions. And sometimes, like all generative AI, it simply gets it wrong. It goes to show that these models are not infallible, even when provided with good inputs.

Overall, GitLab Duo provides pretty decent Code Suggestions for many scenarios, architectures, and languages. We have mostly used it with Java and JavaScript (including ReactJS front-end code), and the results are very good. However, while impressive at times, it is important to note that GitLab Duo does not replace the programmer or even aim to do so. It is designed to help programmers produce better code faster, and it does this well.

Having said all of that, as good as it is, there is one obvious omission in GitLab Duo that surprised me a bit—I have not been able to get any help with .gitlab-ci.yml files out of Duo. Since Duo is designed for GitLab, you would have thought that it could help me write my CI file as well! Alas, it does not, and I look forward to the day that it can. Watch this space.

Published: Sep 3, 2024

GitLab