Skip to content

GitLab Duo examples

The following use cases describe practical examples with GitLab Duo. Learn how to start with software development and refactor existing source code. Dive into debugging problems with root cause analysis, solve security vulnerabilities, and use all stages of the DevSecOps lifecycle.

Use GitLab Duo to solve development challenges

Start with a C# application

In this example, open your C# application and start to explore how to use GitLab Duo AI-powered features for more efficiency.

Watch these steps in action in GitLab Duo Coffee Chat: Get started with C#

The challenge is to create a CLI tool for querying the GitLab REST API.

  • Ask GitLab Duo Chat how to start a new C# project and learn how to use the dotNET CLI:

    How can I get started creating an empty C# console application in VSCode?
  • Use Code Suggestions to generate a REST API client with a new code comment:

    // Connect to a REST API and print the response
  • The generated source code might need an explanation: Use the code task /explain to get an insight how the REST API calls work.

After successfully generating the source code from a Code Suggestions comment, CI/CD configuration is needed.

  • Chat can help with best practices for a .gitignore file for C#:

    Please show a .gitignore and .gitlab-ci.yml configuration for a C# project.
  • If your CI/CD job fails, Root Cause Analysis can help understand the problem. Alternatively, you can copy the error message into GitLab Duo Chat, and ask for help:

    Please explain the CI/CD error: The current .NET SDK does not support targeting
    .NET 8.0
  • To create tests later, ask GitLab Duo to use the code task /refactor to refactor the selected code into a function.

  • Chat can also explain programming language specific keywords and functions, or C# compiler errors.

    Can you explain async and await in C# with practical examples?
    
    explain error CS0122: 'Program' is inaccessible due to its protection level
  • Generate tests by using the /tests code task.

The next question is where to put the generated tests in C# solutions. As a beginner, you might not know that the application and test projects need to exist on the same solutions level to avoid import problems.

  • GitLab Duo Chat can help by asking and refining the prompt questions:

    In C# and VS Code, how can I add a reference to a project from a test project?
    
    Please provide the XML configuration which I can add to a C# .csproj file to add a
    reference to another project in the existing solution?
  • Sometimes, you must refine the prompt to get better results. The prompt /refactor into the public class creates a proposal for code that can be accessed from the test project later.

    /refactor into the public class
  • You can also use the /refactor code task to ask Chat how to execute tests in the .gitlab-ci.yml file.

    /refactor add a job to run tests (the test project)

Resources:

Refactor a C++ application with SQLite

In this example, existing source code with a single main function exists. It repeats code, and cannot be tested.

Watch these steps in action in GitLab Duo Coffee Chat: C++, SQLite and CMake

Refactoring the source code into reusable and testable functions is a great first step.

  1. Open VS Code or the Web IDE with GitLab Duo enabled.

  2. Select the source code, and ask GitLab Duo Chat to refactor it into functions, using a refined prompt:

    /refactor into functions

    This refactoring step might not work for the entire selected source code.

  3. Split the refactoring strategy into functional blocks. For example, iterate on all insert, update, and delete operations in the database.

  4. The next step is to generate tests for the newly created functions. Select the source code again. You can use the code task /tests with specific prompt instructions for the test framework:

    /tests using the CTest test framework
  5. If your application uses the Boost.Test framework instead, refine the prompt:

    /tests using the Boost.Test framework

Resources:

Refactor C++ functions into object-oriented code

In this example, existing source code has been wrapped into functions. To support more database types in the future, the code needs to be refactored into classes and object inheritance.

Watch these steps in action in GitLab Duo Coffee Chat: Refactor C++ functions into OOP classes

Start working on the class

  • Ask GitLab Duo Chat how to implement an object-oriented pattern for a base database class and inherit it in a SQLite class:

    Explain a generic database implementation using a base class, and SQLite specific class using C++. Provide source examples and steps to follow.
  • The learning curve includes asking GitLab Duo Chat about pure virtual functions and virtual function overrides in the implementation class.

    What is a pure virtual function, and what is required for the developer inheriting from that class?
  • Code tasks can help refactoring the code. Select the functions in the C++ header file, and use a refined prompt:

    /refactor into class with public functions, and private path/db attributes. Inherit from the base class DB
    
    /refactor into a base class with pure virtual functions called DB. Remote the SQLite specific parts.
  • GitLab Duo Chat also guides with constructor overloading, object initialization, and optimized memory management with shared pointers.

    How to add a function implementation to a class in a cpp file?
    
    How to pass values to class attributes through the class constructor call?

Find better answers

  • The following question did not provide enough context.

    Should I use virtual override instead of just override?
  • Instead, try to add more context to get better answers.

    When implementing a pure virtual function in an inherited class, should I use virtual function override, or just function override? Context is C++.
  • A relatively complex question involves how to instantiate an object from the newly created class, and call specific functions.

    How to instantiate an object from a class in C++, call the constructor with the SQLite DB path and call the functions. Prefer pointers.
  • The result can be helpful, but needed refinements for shared pointers and required source code headers.

    How to instantiate an object from a class in C++, call the constructor with the SQLite DB path and call the functions. Prefer shared pointers. Explain which header includes are necessary.
  • Code Suggestions help generate the correct syntax for std::shared_ptr pointer arithmetic and help improve the code quality.

    // Define the SQLite path in a variable, default value database.db
    
    // Create a shared pointer for the SQLite class
    
    // Open a database connection using OpenConnection

Refactor your code

  • After refactoring the source code, compiler errors may occur. Ask Chat to explain them.

    Explain the error: `db` is a private member of `SQLiteDB`
  • A specific SQL query string should be refactored into a multi-line string for more efficient editing.

    std::string sql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL)";
  • Select the source code, and use the /refactor code task:

    /refactor into a stringstream with multiple lines
  • You can also refactor utility functions into a class with static functions in C++ and then ask Chat how to call them.

    /refactor into a class providing static functions
    
    How to call the static functions in the class?

After refactoring the source code, the foundation for more database types is built, and overall code quality improved.

Resources:

Explain and resolve vulnerabilities

In this example, detected security vulnerabilities in C should be fixed with the help from GitLab Duo.

Watch the GitLab Duo Coffee Chat: Vulnerability Resolution Challenge #1

This source code snippet introduces a security vulnerability with a buffer overflow:

    strcpy(region, "Hello GitLab Duo Vulnerability Resolution challenge");

    printf("Contents of region: %s\n", region);

SAST security scanners can detect and report the problem. Use Vulnerability Explanation to understand the problem. Vulnerability resolution helps to generate an MR. If the suggested changes do not fit requirements, or would otherwise lead to problems, you can use Code Suggestions and Chat to refine. For example:

  1. Open VS Code or the Web IDE with GitLab Duo enabled, and add a comment with instructions:

        // Avoid potential buffer overflows
    
        // Possible AI-generated code below
        strncpy(region, "Hello GitLab Duo Vulnerability Resolution challenge", pagesize);
        region[pagesize-1] = '\0';
        printf("Contents of region: %s\n", region);
  2. Delete the suggested code, and use a different comment to use an alternative method.

        // Avoid potential buffer overflows using snprintf()
    
        // Possible AI-generated code below
        snprintf(region, pagesize, "Hello GitLab Duo Vulnerability Resolution challenge");
    
        printf("Contents of region: %s\n", region);
  3. In addition, use GitLab Duo Chat to ask questions. The /refactor code task can generate different suggestions. If you prefer a specific algorithm or function, refine the prompt:

    /refactor using snprintf

Resources:

Answer questions about GitLab

In this example, the challenge is exploring the GitLab Duo Chat Beta to solve problems.

Watch the recording here: GitLab Duo Coffee Chat: Solve problems with GitLab Duo Chat Beta Challenge

  • You can use GitLab Duo Chat to explain CI/CD errors.

    Explain this CI/CD error: build.sh: line 14: go command not found
  • What happens when you are impatient, and input just one or two words?

    labels
    
    issue labels

    GitLab Duo Chat asks for more context.

  • Refine your question into a full sentence, describing the problem and asking for a solution.

    Explain labels in GitLab. Provide an example for efficient usage.

Resources:

Use GitLab Duo to contribute to GitLab

GitLab Duo usage focuses on contributing to the GitLab codebase, and how customers can contribute more efficiently.

The GitLab codebase is large, and requires to understand sometimes complex algorithms or application specific implementations. Review the architecture components to learn more.

Contribute to frontend: Profile Settings

In this example, the challenge was to update the GitLab profile page and improve the social networks settings.

Watch the recording here: GitLab Duo Coffee Chat: Contribute to GitLab using Code Suggestions and Chat

GitLab Duo Chat can be helpful to explain and refactor code, and generate tests. Code Suggestions help complete existing code, and can generate new functions and algorithms in Ruby, Go, or VueJS.

  1. Use the /explain code task to explain selected code sections, and learn how the HAML templates work.
  2. You can refine the code task prompts, and instead ask /explain how HAML rendering works

Alternatively, you can write in the chat prompt directly, for example:

how to populate a select in haml

The refactoring examples involve the following:

  1. /refactor into a HAML dropdown
  2. After inspecting the existing UI form code, refine the prompt to /refactor into a HAML dropdown with a form select

GitLab Duo Chat helped with error debugging, prefixing the error message:

please explain this error: undefined method `icon` for

Code generation prompts

The following examples provide helpful code generation prompts for the supported languages in GitLab Duo. Code generation prompts can be refined using multi-line comments.

The examples are stored in the GitLab Duo Prompts project, maintained by the Developer Relations team.

C++ code generation prompts

Create an application to manage distributed file nodes.

// Create an application to manage distributed file nodes
// Provide an overview the health state of nodes
// Use OOP patterns to define the base file node
// Add specific filesystems inherited from the base file

Create an eBPF program which attaches to XDP kernel events to measure network traffic. Only works on Linux kernels.

// Create an eBPF program which attaches to XDP kernel events
// Count all packets by IP address
// Print a summary
// Include necessary headers

C# code generation prompts

Create a medical analyzer app from different sensors, and store the data in MSSQL.

// Create a medical analyzer app
// Collect data from different sensors
// Store data in MSSQL
// Provide methods to access the sensor data

Go code generation prompts

Create an observability application for Kubernetes which reads and prints the state of containers, pods, and services in the cluster.

// Create a client for Kubernetes observability
// Create a function that
// Read the kubernetes configuration file from the KUBECONFIG env var
// Create kubernetes context, namespace default
// Inspect container, pod, service status and print an overview
// Import necessary packages
// Create main package

Watch the recording here: GitLab Duo Coffee Chat: Code Generation Challenge with Go and Kubernetes Observability

Java code generation prompts

Create a data analytics application, with different data sources for metrics. Provide an API for data queries and aggregation.

// Create a data analytics app
// Parse different input sources and their values
// Store the metrics in a columnar format
// Provide an API to query and aggregate data

JavaScript code generation prompts

Create a paid-time-off application for employees in ReactJS, with a date-time picker.

// Create a Paid Time Off app for users
// Create a date-time picker in ReactJS
// Provide start and end options
// Show public holidays based on the selected country
// Send the request to a server API

PHP code generation prompts

Create an RSS feed fetcher for GitLab releases, allow filtering by title.

// Create a web form to show GitLab releases
// Fetch the RSS feed from https://about.gitlab.com/atom.xml
// Provide filter options for the title

Python code generation prompts

Create a webserver using Flask to manage users using the REST API, store them in SQLite.

# Create a Flask webserver
# Add REST API entrypoints to manage users by ID
# Implement create, update, delete functions
# User data needs to be stored in SQlite, create table if not exists
# Run the server on port 8080, support TLS
# Print required packages for requirements.txt in a comment.
# Use Python 3.10 as default

Ruby code generation prompts

Create a log parser application which stores log data in Elasticsearch.

# Create a Ruby app as log parser
# Provide hooks to replace sensitive strings in log lines
# Format the logs and store them in Elasticsearch

Rust code generation prompts

Create an RSS feed reader app, example from the blog post Learn advanced Rust programming with a little help from AI.

    // Create a function that iterates over the source array
    // and fetches the data using HTTP from the RSS feed items.
    // Store the results in a new hash map.
    // Print the hash map to the terminal.

Resources

Many of the use cases are available as hands-on recordings in the GitLab Duo Coffee Chat YouTube playlist. The GitLab Duo Coffee Chat is a learning series maintained by the Developer Relations team.

Blog resources