Convert examples/ directory to testable examples for pkg.go.dev integration
Summary
I propose to convert most of the examples from examples/ directory into testable examples. This would automatically display runnable, verified examples on https://pkg.go.dev/gitlab.com/gitlab-org/api/client-go#pkg-examples.
Benefits
- pkg.go.dev would show collapsible, copy-paste-ready examples
-
go test ./...would run and verify them, catching API changes or breakage.
Example of converting applicationsExample
- Add
example_application_test.go.
Contents
package gitlab_test
import (
"log"
gitlab "gitlab.com/gitlab-org/api/client-go"
)
func ExampleApplicationsService() {
git, err := gitlab.NewClient("yourtokengoeshere")
if err != nil {
log.Fatal(err)
}
// Create an application
opts := &gitlab.CreateApplicationOptions{
Name: gitlab.Ptr("Travis"),
RedirectURI: gitlab.Ptr("http://example.org"),
Scopes: gitlab.Ptr("api"),
}
created, _, err := git.Applications.CreateApplication(opts)
if err != nil {
log.Fatal(err)
}
log.Printf("Last created application : %v", created)
// List all applications
applications, _, err := git.Applications.ListApplications(&gitlab.ListApplicationsOptions{})
if err != nil {
log.Fatal(err)
}
for _, app := range applications {
log.Printf("Found app : %v", app)
}
// Delete an application
resp, err := git.Applications.DeleteApplication(created.ID)
if err != nil {
log.Fatal(err)
}
log.Printf("Status code response : %d", resp.StatusCode)
}
- Remove
examples/applications.go - Run
pkgsitelocally and open http://localhost:8080/gitlab.com/gitlab-org/api/client-go#pkg-examples
http://localhost:8080/gitlab.com/gitlab-org/api/client-go#example-ApplicationsService:
Edited by 🤖 GitLab Bot 🤖

