/go-gitlab

GitLab Go SDK

Primary LanguageGoApache License 2.0Apache-2.0

go-gitlab

A GitLab API client enabling Go programs to interact with GitLab in a simple and uniform way

Build Status Sourcegraph GoDoc Go Report Card

NOTE

Release v0.6.0 (released on 25-08-2017) no longer supports the older V3 GitLab API. If you need V3 support, please use the f-api-v3 branch. This release contains some backwards incompatible changes that were needed to fully support the V4 GitLab API.

Coverage

This API client package covers most of the existing GitLab API calls and is updated regularly to add new and/or missing endpoints. Currently, the following services are supported:

  • Applications
  • Award Emojis
  • Branches
  • Broadcast Messages
  • Commits
  • Container Registry
  • Custom Attributes
  • Deploy Keys
  • Deployments
  • Discussions (threaded comments)
  • Environments
  • Epic Issues
  • Epics
  • Error Tracking
  • Events
  • Feature Flags
  • Geo Nodes
  • Generic Packages
  • GitLab CI Config Templates
  • Gitignores Templates
  • Group Access Requests
  • Group Issue Boards
  • Group Members
  • Group Milestones
  • Group Wikis
  • Group-Level Variables
  • Groups
  • Instance Clusters
  • Invites
  • Issue Boards
  • Issues
  • Jobs
  • Keys
  • Labels
  • License
  • Markdown
  • Merge Request Approvals
  • Merge Requests
  • Namespaces
  • Notes (comments)
  • Notification Settings
  • Open Source License Templates
  • Packages
  • Pages
  • Pages Domains
  • Personal Access Tokens
  • Pipeline Schedules
  • Pipeline Triggers
  • Pipelines
  • Plan limits
  • Project Access Requests
  • Project Badges
  • Project Clusters
  • Project Import/export
  • Project Members
  • Project Milestones
  • Project Repository Storage Moves
  • Project Snippets
  • Project Vulnerabilities
  • Project-Level Variables
  • Projects (including setting Webhooks)
  • Protected Branches
  • Protected Environments
  • Protected Tags
  • Repositories
  • Repository Files
  • Repository Submodules
  • Runners
  • Search
  • Services
  • Settings
  • Sidekiq Metrics
  • System Hooks
  • Tags
  • Todos
  • Topics
  • Users
  • Validate CI Configuration
  • Version
  • Wikis

Usage

import "github.com/xanzy/go-gitlab"

Construct a new GitLab client, then use the various services on the client to access different parts of the GitLab API. For example, to list all users:

git, err := gitlab.NewClient("yourtokengoeshere")
if err != nil {
  log.Fatalf("Failed to create client: %v", err)
}
users, _, err := git.Users.ListUsers(&gitlab.ListUsersOptions{})

There are a few With... option functions that can be used to customize the API client. For example, to set a custom base URL:

git, err := gitlab.NewClient("yourtokengoeshere", gitlab.WithBaseURL("https://git.mydomain.com/api/v4"))
if err != nil {
  log.Fatalf("Failed to create client: %v", err)
}
users, _, err := git.Users.ListUsers(&gitlab.ListUsersOptions{})

Some API methods have optional parameters that can be passed. For example, to list all projects for user "svanharmelen":

git := gitlab.NewClient("yourtokengoeshere")
opt := &gitlab.ListProjectsOptions{Search: gitlab.String("svanharmelen")}
projects, _, err := git.Projects.ListProjects(opt)

Examples

The examples directory contains a couple for clear examples, of which one is partially listed here as well:

package main

import (
	"log"

	"github.com/xanzy/go-gitlab"
)

func main() {
	git, err := gitlab.NewClient("yourtokengoeshere")
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}

	// Create new project
	p := &gitlab.CreateProjectOptions{
		Name:                 gitlab.String("My Project"),
		Description:          gitlab.String("Just a test project to play with"),
		MergeRequestsEnabled: gitlab.Bool(true),
		SnippetsEnabled:      gitlab.Bool(true),
		Visibility:           gitlab.Visibility(gitlab.PublicVisibility),
	}
	project, _, err := git.Projects.CreateProject(p)
	if err != nil {
		log.Fatal(err)
	}

	// Add a new snippet
	s := &gitlab.CreateProjectSnippetOptions{
		Title:           gitlab.String("Dummy Snippet"),
		FileName:        gitlab.String("snippet.go"),
		Content:         gitlab.String("package main...."),
		Visibility:      gitlab.Visibility(gitlab.PublicVisibility),
	}
	_, _, err = git.ProjectSnippets.CreateSnippet(project.ID, s)
	if err != nil {
		log.Fatal(err)
	}
}

For complete usage of go-gitlab, see the full package docs.

ToDo

  • The biggest thing this package still needs is tests 😞

Issues

Author

Sander van Harmelen (sander@vanharmelen.nl)

Contributing

Contributions are always welcome. For more information, check out the contributing guide

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0