GitLab Duo Principles - Hands-On Lab: Code Generation with GitLab Duo Chat

This Hands-On Guide walks you through using GitLab Duo to generate and explain code.

Estimated time to complete: 15 minutes

Objectives

To get started with GitLab Duo, we will test out some of the basic code generation features. Code generation allows you to prompt GitLab Duo for code that solves a specific programming problem.

Task A. Generating Some Simple Code

In this task, we are going to generate a basic ‘hello world’ program in Go using GitLab Duo. This task will help you get familiar with the process of creating code in a project with GitLab Duo.

  1. Navigate to your GitLab Duo Principles project.

  2. Select Edit > Web IDE.

    Note: In this lab we are using the Web IDE, but you can also use Duo Chat and other Duo tools in your local IDE. See available extensions.

  3. In the Web IDE, locate the GitLab Duo Chat icon. This icon will be the last item in the left side bar, showing the GitLab icon.

  4. In the chat text input, enter the text How do I create a hello world Go program?

  5. Duo chat should respond with code similar to this:

    package main
    
    import "fmt"
    
    func main() {
      fmt.Println("hello world")
    }
    

    Note: Any AI generator will be non-deterministic in nature. This means that when you prompt an AI, there is a possibility that your answer may differ from someone else’s answer. If you ever receive an output that appears significantly different from the lab guide, please use the lab guide snippets to ensure you can follow along with other examples.

  6. Copy the code generated by GitLab Duo.

  7. Select the explorer icon in the left sidebar to return to your code.

  8. Next to your project name, select the New File icon. The new file should be stored in the root level of your repository.

  9. Name the file main.go.

  10. Inside the file, paste the AI generated code.

You have now created a simple hello world program in Go! In the next section, you will learn how to explain AI generated code.

Task B. Explaining AI Code

Being able to generate code is only one part of the software development process. In many cases, you also want to be able to understand code that has been written. Whether the code is generated by coworkers or AI, GitLab Duo can explain any code snippet you provide.

  1. In the Web IDE, highlight all of the code in your main.go file.

  2. Select the GitLab Duo icon from the left sidebar.

  3. With the code still highlighted, type /explain into the chat prompt text box.

  4. You will see an explanation of all of the code you have highlighted as a result of the prompt.

Task C. Commit your Code

Now that you have code generated and you understand the code, you can commit the code into your project.

  1. Select Source Control in the left sidebar.

  2. Enter any commit message, then select Commit and push to 'main'

  3. If you receive a prompt stating You are committing your changes to the default branch, select Continue.

  4. Select Go to Project to return to your GitLab repository.

Task D. Add a .gitlab-ci.yml File

The last step of this lab is to create a .gitlab-ci.yml file to build our Go application.

  1. In the project repository, at the root level, create a new file in the main branch by clicking (+) > This directory > New file.

  2. In the Filename field, type .gitlab-ci.yml.

  3. Copy the YAML below into the file.

    stages:
      - build
    
    default:
      image: golang:latest
    
    build app:
      stage: build
      script:
        - go run main.go
    

    This .gitlab-ci.yml file has one stage build with a job build app that runs as part of the stage. This job runs our main.go app.

    Again, feel free to highlight the YAML and use /explain in GitLab Duo chat to explain the CI pipeline!

  4. In the Commit message, type any appropriate message.

  5. Ensure the Target Branch field is set to main.

  6. Select Commit changes.

  7. To view the pipeline, select Build > Pipelines to see the pipeline running.

  8. Once the pipeline has successfully completed running, click on the build app job to view the output of your main.go app. The build app job can be found under your Stages section in the build stage.

Lab Guide Complete

You have completed this lab exercise. You can view the other lab guides for this course.

Suggestions?

If you’d like to suggest changes to the lab, please submit them via merge request.