Top Related Projects
Quick Overview
xanzy/go-gitlab is a Go client library for accessing the GitLab API. It provides a comprehensive set of functions to interact with GitLab's features programmatically, allowing developers to automate tasks, integrate GitLab into their workflows, and build custom tools around GitLab's functionality.
Pros
- Extensive coverage of GitLab API endpoints
- Well-maintained and regularly updated to support new GitLab features
- Clear and consistent API design, making it easy to use for Go developers
- Comprehensive documentation and examples
Cons
- Requires understanding of GitLab's API structure and concepts
- May need frequent updates to keep up with GitLab's API changes
- Limited built-in error handling, requiring additional error checking in user code
- Some advanced GitLab features may not be fully supported
Code Examples
- Creating a new project:
project, _, err := client.Projects.CreateProject(&gitlab.CreateProjectOptions{
Name: gitlab.String("My Project"),
Description: gitlab.String("A sample project created using go-gitlab"),
Visibility: gitlab.Visibility(gitlab.PublicVisibility),
})
- Listing merge requests:
mrs, _, err := client.MergeRequests.ListProjectMergeRequests(projectID, &gitlab.ListProjectMergeRequestsOptions{
State: gitlab.String("opened"),
OrderBy: gitlab.String("updated_at"),
})
- Creating an issue:
issue, _, err := client.Issues.CreateIssue(projectID, &gitlab.CreateIssueOptions{
Title: gitlab.String("New feature request"),
Description: gitlab.String("We need to implement this new feature"),
Labels: &[]string{"enhancement", "priority"},
})
Getting Started
To start using go-gitlab, first install the package:
go get github.com/xanzy/go-gitlab
Then, create a client and authenticate:
import "github.com/xanzy/go-gitlab"
git, err := gitlab.NewClient("your_gitlab_token", gitlab.WithBaseURL("https://gitlab.com/api/v4"))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Now you can use the client to make API calls
user, _, err := git.Users.CurrentUser()
if err != nil {
log.Fatalf("Failed to get current user: %v", err)
}
fmt.Printf("Hello, %s!\n", user.Name)
Competitor Comparisons
Go library for accessing the GitHub v3 API
Pros of go-github
- More comprehensive API coverage for GitHub features
- Larger community and more frequent updates
- Better documentation and examples
Cons of go-github
- Specific to GitHub, not usable for GitLab
- May include unnecessary features for users only needing basic functionality
Code Comparison
go-github:
client := github.NewClient(nil)
repo, _, err := client.Repositories.Get(context.Background(), "google", "go-github")
go-gitlab:
git, err := gitlab.NewClient("your_token")
project, _, err := git.Projects.GetProject("group/project")
Key Differences
- go-github is tailored for GitHub API, while go-gitlab is specific to GitLab API
- go-github uses a context-based approach, while go-gitlab relies on a token-based authentication
- Both libraries provide similar functionality for their respective platforms, but with platform-specific features and methods
Use Cases
- Choose go-github for GitHub-specific projects and integrations
- Opt for go-gitlab when working exclusively with GitLab repositories and features
- Consider using both libraries if your project interacts with both GitHub and GitLab
Community and Support
- go-github has a larger user base and more contributors
- go-gitlab has a dedicated maintainer and active community, albeit smaller
Both libraries are well-maintained and provide robust solutions for interacting with their respective Git hosting platforms.
Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql).
Pros of githubv4
- Utilizes GitHub's GraphQL API v4, offering more flexibility and efficiency in querying data
- Provides strongly-typed query structures, reducing runtime errors and improving code reliability
- Supports custom GraphQL queries, allowing for more precise and tailored data retrieval
Cons of githubv4
- Limited to GitHub-specific functionality, unlike go-gitlab which covers GitLab features
- Steeper learning curve for developers unfamiliar with GraphQL
- May require more setup and configuration compared to go-gitlab's REST-based approach
Code Comparison
githubv4:
var query struct {
Repository struct {
Issues struct {
Nodes []struct {
Title string
}
} `graphql:"issues(first: 100)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
go-gitlab:
issues, _, err := client.Issues.ListProjectIssues(projectID, &gitlab.ListProjectIssuesOptions{
ListOptions: gitlab.ListOptions{
PerPage: 100,
Page: 1,
},
})
The githubv4 example demonstrates a GraphQL query structure, while go-gitlab uses a more traditional REST-like approach. The GraphQL query allows for more precise data selection, potentially reducing over-fetching, while go-gitlab's method is more straightforward but may retrieve more data than necessary.
A highly extensible Git implementation in pure Go.
Pros of go-git
- Pure Go implementation, allowing for easy cross-platform compatibility
- Supports a wide range of Git operations, including cloning, pushing, and pulling
- Can be used as a library in other Go projects for Git functionality
Cons of go-git
- Focused solely on Git operations, lacking GitLab-specific features
- May require additional work to integrate with GitLab API for repository management
Code Comparison
go-git example:
r, _ := git.PlainClone("/path/to/repo", false, &git.CloneOptions{
URL: "https://github.com/go-git/go-git",
Progress: os.Stdout,
})
go-gitlab example:
git, _ := gitlab.NewClient("your_token", gitlab.WithBaseURL("https://gitlab.com/api/v4"))
project, _, _ := git.Projects.GetProject(projectID, nil)
Summary
go-git is a pure Go implementation of Git, offering a wide range of Git operations and cross-platform compatibility. It's ideal for projects requiring Git functionality without GitLab-specific features. go-gitlab, on the other hand, is tailored for interacting with GitLab's API, providing easy access to GitLab-specific features and repository management. The choice between the two depends on whether you need general Git operations or GitLab-specific functionality in your project.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
go-gitlab
A GitLab API client enabling Go programs to interact with GitLab in a simple and uniform way
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.Ptr("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.Ptr("My Project"),
Description: gitlab.Ptr("Just a test project to play with"),
MergeRequestsAccessLevel: gitlab.Ptr(gitlab.EnabledAccessControl),
SnippetsAccessLevel: gitlab.Ptr(gitlab.EnabledAccessControl),
Visibility: gitlab.Ptr(gitlab.PublicVisibility),
}
project, _, err := git.Projects.CreateProject(p)
if err != nil {
log.Fatal(err)
}
// Add a new snippet
s := &gitlab.CreateProjectSnippetOptions{
Title: gitlab.Ptr("Dummy Snippet"),
FileName: gitlab.Ptr("snippet.go"),
Content: gitlab.Ptr("package main...."),
Visibility: gitlab.Ptr(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 :disappointed:
Issues
- If you have an issue: report it on the issue tracker
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
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot