DevSecOps with GitLab Duo - Hands-On Lab: Using GitLab Duo to Write New Code
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
-
Navigate to your GitLab Duo Principles Project.
-
Select Edit > Web IDE.
-
Select the
main.go
file. -
Between the
main
function andimport
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.
-
Press enter and note the small Tanuki icon that appears in the code margin. Wait until code appears. Once you see code, press Tab.
-
Call your new function in the
main()
function as part of the newmyFigure
object. -
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. -
The function added by GitLab Duo requires the
math/rand
package. Below thego-figure
import, start typingmath
. Note the Tanuki icon showing code generation in progress. -
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!
-
Highlight all of the code in the
func random_gitlab() string
function. -
Select the Tanuki icon in the left sidebar.
-
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. -
To add the tests to your project, create a new file named
gitlabrandom_test.go
. -
At the top of the file include the following:
package main import "testing"
-
Paste the generated tests.
-
Select Source Control, then select Commit to main.
-
Select Go to Project to return to your project.
Task C. Run tests in a CI/CD pipeline
-
Navigate to Build > Pipeline editor.
-
In the
stages
section, add a new stage calledtest
. -
Under the
build app
job create a new job calledtest
. -
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.
46417d02
)