DevSecOps with GitLab Duo - Hands-On Lab: Using GitLab Duo to Write New Code

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

Estimated time to complete: 30 minutes

Objectives

GitLab Duo extends beyond just code generation. GitLab Duo can support you through any stage of the DevSecOps lifecycle! In this lab, you will see how GitLab Duo can work with issues and merge requests inside of a project.

Task A. Using Code Suggestions to Write a New Function

  1. Navigate to your GitLab Duo Principles Project.

  2. Select Edit > Web IDE.

  3. Select the main.go file.

  4. Between the main function and import block, type the following comment:

    // write a function called random_gitlab that pulls a random word from a list and returns it as a string.
    
  5. Press enter and note the small Tanuki icon that appears in the code margin. Wait until code appears. Once you see code, press Tab.

  6. Call your new function in the main() function as part of the new myFigure object.

  7. After doing this, your code should look similar to this:

    package main
    
    import (
        "github.com/common-nighthawk/go-figure"
    )
    
    // write a function called random_gitlab that pulls a random word from a list and returns it as a string
    
    var words = []string{"gitlab", "repository", "commit", "branch", "merge", "issue", "pipeline"}
    
    func random_gitlab() string {
        return words[rand.Intn(len(words))]
    }
    
    func main() {
        myFigure := figure.NewFigure(random_gitlab(), "", true)
        myFigure.Print()
    }
    

    By nature, the prompt we entered will generate different code for most users, since we asked for random words from a list. If you see different words in the words := definition, that is ok.

  8. The function added by GitLab Duo requires the math/rand package. Below the go-figure import, start typing math. Note the Tanuki icon showing code generation in progress.

  9. Once you see math/rand appear, press Tab. Your code should now look similar to this:

    package main
    
    import (
        "github.com/common-nighthawk/go-figure"
        "math/rand"
    )
    
    // write a function called random_gitlab that pulls a random word from a list and returns it as a string
    
    var words = []string{"gitlab", "repository", "commit", "branch", "merge", "issue", "pipeline"}
    
    func random_gitlab() string {
        return words[rand.Intn(len(words))]
    }
    
    func main() {
        myFigure := figure.NewFigure(random_gitlab(), "", true)
        myFigure.Print()
    }
    

Task B. Generating Code Tests

The code generated by the GitLab Duo is likely correct, however it is still best practice to test the code for errors before using it in any production environment. Luckily, GitLab Duo can also help us with the test generation process!

  1. Highlight all of the code in the func random_gitlab() string function.

  2. Select the Tanuki icon in the left sidebar.

  3. Type the prompt /tests. You will receive some tests similar to below:

    func TestRandomGitlab(t *testing.T) {
    
        // Test that it returns one of the words in the slice
        word := random_gitlab()
        words := []string{"hello", "world", "random", "gitlab"}
        contains := false
        for _, w := range words {
            if w == word {
                contains = true
                break
            }
        }
        if !contains {
            t.Errorf("returned word %s not in expected list", word)
        }
    
        // Test that it returns different words if called multiple times
        word1 := random_gitlab()
        word2 := random_gitlab()
        if word1 == word2 {
            t.Error("returned same word twice in a row")
        }
    }
    

    As always, feel free to use /explain to understand what this code is doing.

  4. To add the tests to your project, create a new file named gitlabrandom_test.go.

  5. At the top of the file include the following:

    package main
    
    import "testing"
    
  6. Paste the generated tests.

  7. Select Source Control, then select Commit to main.

  8. Select Go to Project to return to your project.

Task C. Run tests in a CI/CD pipeline

  1. Navigate to Build > Pipeline editor.

  2. In the stages section, add a new stage called test.

  3. Under the build app job create a new job called test.

  4. Add the go test command to run the tests. Your .gitlab-ci.yml file will look like the following:

    stages:
        - build
        - test
    
    default:
        image: golang:latest
    
    build app:
        stage: build
        script:
            - go get github.com/common-nighthawk/go-figure
            - go run main.go
    
    test:
        stage: test
        script:
            - go get github.com/common-nighthawk/go-figure
            - go test ./...
    

You should now have a pipeline that passed the tests. If a job failed, feel free to use Root Cause Analysis to troubleshoot any errors.

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.

Last modified June 27, 2024: Fix various vale errors (46417d02)