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

Related tags

Git go-gitlab
Overview

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
  • Events
  • Feature Flags
  • Geo Nodes
  • 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
  • Merge Request Approvals
  • Merge Requests
  • Namespaces
  • Notes (comments)
  • Notification Settings
  • Open Source License Templates
  • Pages Domains
  • Personal Access Tokens
  • Pipeline Schedules
  • Pipeline Triggers
  • Pipelines
  • Project Access Requests
  • Project Badges
  • Project Clusters
  • Project Import/export
  • Project Members
  • Project Milestones
  • Project Snippets
  • Project-Level Variables
  • Projects (including setting Webhooks)
  • Protected Branches
  • Protected Environments
  • Protected Tags
  • Repositories
  • Repository Files
  • Runners
  • Search
  • Services
  • Settings
  • Sidekiq Metrics
  • System Hooks
  • Tags
  • Todos
  • 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 := &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 ([email protected])

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

Comments
  • Add GenericPackagesServices (#1100)

    Add GenericPackagesServices (#1100)

    This PR continues the work in #1101, rebasing it to master, updating its interface (returning download URL), and some real-world testing (fixed the URL endpoint and request type).

    opened by neomantra 14
  • Support for avatar option in create/edit project

    Support for avatar option in create/edit project

    https://docs.gitlab.com/ce/api/projects.html#edit-project -> Currently the CreateProjectOptions does not contain an entry to send along an avatar, even thought it is supported in the API. Would be great if we could upload an avatar :smile_cat:

    Maybe we can also directly support the other new params ci_config_path and tag_list?

    enhancement 
    opened by Bouwdie 14
  • Support the v4 API

    Support the v4 API

    Lets track this in an issue. The GitLab API v3 is officially deprecated and will be supported until at least 9.3. Given the current release schedule (once per month), that means it will be unsupported 3 months from now.

    I would like to be able to start consuming v4 functionality so I might start work on a v4 branch.

    I think all current documentation links need to be updated to the v3 documentation as currently the links all point to the v4 documentation which causes confusion. Separately some effort should be made to start supporting v4 specific functionality (probably in a feature branch). Once both those things are completed, v3 could be spun out to a separate branch and v4 can be merged to master.

    Below are the changes made between V3 and V4. A tickbox indicates that work has been completed on that change.

    Completed

    • [x] Rename Build Triggers to be Pipeline Triggers API !9713. Fixed in #152.
      • POST /projects/:id/trigger/builds to POST /projects/:id/trigger/pipeline
      • Require description when creating a new trigger POST /projects/:id/triggers
    • [x] Removed GET /projects/:search (use: GET /projects?search=x) !8877. Fixed in #161.
    • [x] iid filter has been removed from GET /projects/:id/issues !8967. Fixed in #161.
    • [x] GET /projects/:id/merge_requests?iid[]=x&iid[]=y array filter has been renamed to iids !8793. Fixed in #161.
    • [x] Endpoints under GET /projects/merge_request/:id have been removed (use: GET /projects/merge_requests/:id) !8793. Fixed in #161.
    • [x] Project snippets do not return deprecated field expires_at !8723. Fixed in #161.
    • [x] Endpoints under GET /projects/:id/keys have been removed (use GET /projects/:id/deploy_keys) !8716. Fixed in #161.
    • [x] Update v3 documentation to point to archived v3 docs. Fixed in #160.
    • [x] Return basic info about pipeline in GET /projects/:id/pipelines !8875. Fixed in #163
    • [x] Renamed all build references to job !9463. Fixed in #166.
    • [x] Use visibility as string parameter everywhere !9337. Fixed in #167.
    • [x] Project filters are no longer available as GET /projects/foo, but as GET /projects?foo=true instead !8962. Fixed in #170.
      • GET /projects/visible & GET /projects/all are consolidated into GET /projects and can be used with or without authorization
      • GET /projects/owned moved to GET /projects?owned=true
      • GET /projects/starred moved to GET /projects?starred=true
    • [x] GET /projects returns all projects visible to current user, even if the user is not a member !9674. Fixed in #170.
      • To get projects the user is a member of, use GET /projects?membership=true
    • [x] Return HTTP status code 400 for all validation errors when creating or updating a member instead of sometimes 422 error. !9523. Doesn't require changes.
    • [x] Status 409 returned for POST /projects/:id/members when a member already exists !9093. Doesn't require changes.
    • [x] Remove GET /groups/owned. Use GET /groups?owned=true instead !9505. Fixed in commit 62634b8.
    • [x] Moved POST /projects/fork/:id to POST /projects/:id/fork !8940. Fixed in commit 577e19d.
    • [x] Renamed the merge_when_build_succeeds parameter to merge_when_pipeline_succeeds on the following endpoints: !9335
      • PUT /projects/:id/merge_requests/:merge_request_id/merge
      • POST /projects/:id/merge_requests/:merge_request_id/cancel_merge_when_pipeline_succeeds
      • POST /projects
      • POST /projects/user/:user_id
      • PUT /projects/:id
    • [x] Update endpoints for repository files !9637. Fixed in commit a0b1dcb.
      • Moved GET /projects/:id/repository/files?file_path=:file_path to GET /projects/:id/repository/files/:file_path (:file_path should be URL-encoded)
      • GET /projects/:id/repository/blobs/:sha now returns JSON attributes for the blob identified by :sha, instead of finding the commit identified by :sha and returning the raw content of the blob in that commit identified by the required ?filepath=:filepath
      • Moved GET /projects/:id/repository/commits/:sha/blob?file_path=:file_path and GET /projects/:id/repository/blobs/:sha?file_path=:file_path to GET /projects/:id/repository/files/:file_path/raw?ref=:sha
      • GET /projects/:id/repository/tree parameter ref_name has been renamed to ref for consistency
    • [x] Removed the following deprecated Templates endpoints (these are still accessible with /templates prefix) !8853
      • /licences
      • /licences/:key
      • /gitignores
      • /gitlab_ci_ymls
      • /dockerfiles
      • /gitignores/:key
      • /gitlab_ci_ymls/:key
      • /dockerfiles/:key
    • [x] Moved DELETE /todos to POST /todos/mark_as_done and DELETE /todos/:todo_id to POST /todos/:todo_id/mark_as_done !9410
    • [x] Return pagination headers for all endpoints that return an array !8606
    • [x] Removed DELETE /projects/:id/deploy_keys/:key_id/disable. Use DELETE /projects/:id/deploy_keys/:key_id instead !9366
    • [x] Moved PUT /users/:id/(block|unblock) to POST /users/:id/(block|unblock) !9371
    • [x] Make subscription API more RESTful. Use POST /projects/:id/:subscribable_type/:subscribable_id/subscribe to subscribe and POST /projects/:id/:subscribable_type/:subscribable_id/unsubscribe to unsubscribe from a resource. !9325
    • [x] Labels filter on GET /projects/:id/issues and GET /issues now matches only issues containing all labels (i.e.: Logical AND, not OR) !8849
    • [x] Renamed param branch_name to branch on the following endpoints !8936
      • POST /projects/:id/repository/branches
      • POST /projects/:id/repository/commits
      • POST/PUT/DELETE :id/repository/files
    • [x] Renamed branch_name to branch on DELETE /projects/:id/repository/branches/:branch response !8936
    • [x] Remove public param from create and edit actions of projects !8736
    • [x] Remove subscribed field from responses returning list of issues or merge requests. Fetch individual issues or merge requests to obtain the value of subscribed !9661
    • [x] Notes do not return deprecated field upvote and downvote !9384
    • [x] Return 202 with JSON body on async removals on V4 API (DELETE /projects/:id/repository/merged_branches and DELETE /projects/:id) !9449
    • [x] GET /projects/:id/milestones?iid[]=x&iid[]=y array filter has been renamed to iids !9096
    • [x] Drop GET /projects/:id/repository/commits/:sha/jobs !9463
    • [x] Simplify project payload exposed on Environment endpoints !9675
    • [x] API uses merge request IIDs (internal ID, as in the web UI) rather than IDs. This affects the merge requests, award emoji, todos, and time tracking APIs. !9530
    • [x] API uses issue IIDs (internal ID, as in the web UI) rather than IDs. This affects the issues, award emoji, todos, and time tracking APIs. !9530
    • [x] Change initial page from 0 to 1 on GET /projects/:id/repository/commits (like on the rest of the API) [!9679] (https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/9679)
    • [x] Return correct Link header data for GET /projects/:id/repository/commits [!9679] (https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/9679)
    opened by johanbrandhorst 13
  • Duplicated variables in ProjectVariablesService.UpdateVariable

    Duplicated variables in ProjectVariablesService.UpdateVariable

    func (s *ProjectVariablesService) UpdateVariable(pid interface{}, key string, opt *UpdateVariableOptions, options ...OptionFunc) (*ProjectVariable, *Response, error) {
    	project, err := parseID(pid)
    	if err != nil {
    		return nil, nil, err
    	}
    	u := fmt.Sprintf("projects/%s/variables/%s",
    		url.QueryEscape(project),
    		url.QueryEscape(key),
    	)
    

    You are accepting 2 "key" variables. In function and as a parameter of UpdateVariableOptions. Can we use just one instead of two? Does it make sense or am I missing something?

    opened by killmeplz 12
  • Add support for new project import/export API

    Add support for new project import/export API

    GitLab recently released an API to import/export projects.

    Proposal would be to add support to go-gitlab for this: https://docs.gitlab.com/ee/api/project_import_export.html

    enhancement 
    opened by olearycrew 12
  • WIP Terraform endpoint support

    WIP Terraform endpoint support

    Hello there, this is a WIP. I'd like to get early feedback on the general direction of this work. I tried to be consistent with the existing codebase. I intent to add more tests, links to docs, etc and actually test against a real GitLab instance once the support is available (it's being worked on).

    opened by ash2k 11
  • add OnFinished to gitlab.Client

    add OnFinished to gitlab.Client

    In this PR, a hook function OnFinished is added in gitlab client. It will be invoked at the end of each HTTP request, whether the result succeeds or fails. This feature will be very useful, which allows users to do some extra thing with the response and error. By the way, our team really needs this feature urgently. We want to monitor the request for gitlab server, and alarm when the call failure rate is greater than the threshold.

    opened by lzhseu 10
  • Create thread instead of comment

    Create thread instead of comment

    When adding a note to a MR I can also make this a thread which is showing as something that needs to be resolved before the MR will go through. I can not find this in the Gitlab API documentation...? Any idea how that would be used? Screenshot 2021-09-09 at 13 40 20

    enhancement help wanted 
    opened by kniec 10
  • Add support for custom rate limiter

    Add support for custom rate limiter

    Sort of a feature request, use cases may include private gitlab instance not doing the rate limiting itself and not returning rate limit headers and / or when someone wishes to limit load to lower than what's automatically configured.

    Can make a pull request to add it, wanted some thoughts and considerations that I'm not thinking about :)

    Thanks.

    opened by 2785 10
  • Gitlab introduced soft delete of projects and groups

    Gitlab introduced soft delete of projects and groups

    Filing this ticket not as a bug report, more as an awareness.

    The test suites for the Gitlab Terraform & Pulumi providers became instable since Gitlab 12.6. After some investigation, it seems that soft-delete of Projects was introduced in that version by changing the behaviour of DELETE /projects/{id}.

    More context here: https://github.com/terraform-providers/terraform-provider-gitlab/issues/263 which links to even more gitlab tickets.

    So be aware that a delete is not necessarily a hard delete.

    waiting 
    opened by ringods 10
  • golang.org/x/oauth2

    golang.org/x/oauth2

    Is it possible to reference github's class library and use golang's class library to cause us to fail to download in China,I cannot go get golang.org/x/oauth2

    opened by relax-admin 10
  • add a new method that allow to get an issue by id instead of iid

    add a new method that allow to get an issue by id instead of iid

    in issues.go there's no method that wrap https://docs.gitlab.com/ee/api/issues.html#single-issue . It's not possible to Get an issue by id (whole instance id and not project id aka iid).

    This PR add this method and to keep BC, I've name it GetIssueByID but a imho a better name would be GetIssue and we could replace the actual GetIssue by GetProjectIssue, but anyway, both possibilities are fine. Added tests too.

    opened by ahuret 0
  • Update the protected branches API

    Update the protected branches API

    I marked the two functions BranchesService.ProtectBranch() & BranchesService.UnprotectBranch() as deprecated, since they are replaced by the protected branches API.

    In the ProtectedBranchesService I added some missing options, and renamed the RequireCodeOwnerApprovals() function to UpdateProtectedBranch() which has some additional options. (I kept the RequireCodeOwnerApprovals() function and marked it as deprecated.)

    opened by FantasyTeddy 1
  • cannot unmarshal object into Go struct field GroupPackage.tags of type string

    cannot unmarshal object into Go struct field GroupPackage.tags of type string

    with the following code running towards gitlab.com:

    import (
    	"fmt"
    	"github.com/xanzy/go-gitlab"
    	"log"
    	"os"
    )
    
    
    client, err := gitlab.NewClient(os.Getenv("GITLAB_READ_API_TOKEN"))
    
    if err != nil {
            log.Fatalf("Failed to create client: %v", err)
    }
    
    var listOptions = gitlab.ListOptions{
    	Page:    1,
    	PerPage: 100,
    }
    var groupOptions = gitlab.ListGroupPackagesOptions{
    	ListOptions:      listOptions,
    	ExcludeSubGroups: gitlab.Bool(false),
    	PackageType:      gitlab.String("nuget"),
    }
    
    packages, response, err := client.Packages.ListGroupPackages("YOUR_GROUP_ID", &groupOptions)
    

    I get the error:

    Failed call to group endpoint with error: 'json: cannot unmarshal object into Go struct field GroupPackage.tags of type string'
    

    I guess this is due to that our nuget packages contains tags. I've used curl to get the response and found one with many tags, hopefully that is some data to test on:

    command to get data:

    curl --silent --header "PRIVATE-TOKEN: $GITLAB_READ_API_TOKEN" "https://gitlab.com/api/v4/groups/YOUR_GROUP_ID_HERE/packages?exclude_subgroups=false&package_type=nuget&per_page=100&page=1" | jq '.[].tags'
    

    and relevant data from me with IDs replaces with 1 and 2, for having valid data.

    ...
    [
      {
        "id": 1,
        "package_id": 2,
        "name": "convention",
        "created_at": "2021-05-07T11:47:23.118Z",
        "updated_at": "2021-05-07T11:47:23.118Z"
      },
      {
        "id": 1,
        "package_id": 2,
        "name": "package",
        "created_at": "2021-05-07T11:47:23.118Z",
        "updated_at": "2021-05-07T11:47:23.118Z"
      },
      {
        "id": 1,
        "package_id": 2,
        "name": "build",
        "created_at": "2021-05-07T11:47:23.118Z",
        "updated_at": "2021-05-07T11:47:23.118Z"
      },
      {
        "id": 1,
        "package_id": 2,
        "name": "Output",
        "created_at": "2021-05-07T11:47:23.118Z",
        "updated_at": "2021-05-07T11:47:23.118Z"
      },
      {
        "id": 1,
        "package_id": 2,
        "name": "Copy",
        "created_at": "2021-05-07T11:47:23.118Z",
        "updated_at": "2021-05-07T11:47:23.118Z"
      },
      {
        "id": 1,
        "package_id": 2,
        "name": "Targets",
        "created_at": "2021-05-07T11:47:23.118Z",
        "updated_at": "2021-05-07T11:47:23.118Z"
      },
      {
        "id": 1,
        "package_id": 2,
        "name": "Nuget",
        "created_at": "2021-05-07T11:47:23.118Z",
        "updated_at": "2021-05-07T11:47:23.118Z"
      }
    ]
    ...
    

    It also seems like response.TotalPages is always 0.

    bug help wanted 
    opened by Quanalogy 1
Owner
Xanzy
Xanzy
Tfcmt-gitlab is a CLI command to parse and notify Terraform execution results

Tfcmt-gitlab is a CLI command to parse and notify Terraform execution results. This command supports GitLab as a CI and notification platform.

null 16 Dec 23, 2022
A really smart bot that connects with your email, discord, telegram and gitlab

SmarttyBot The smartest multiplatform bot SmarttyBot is a multiplatform bot that uses natural language to execute functions using discord, telegram, e

Omar 6 Jun 2, 2022
Gitlab include draftsman in golang

Gitlab include draftsman WIP Утилита, которая рисует graph include'ов из gitlab-ci Как работает пока: Считывает из app.env файла(который должен быть в

Roman Mironov 3 Oct 31, 2021
A Git RPC service for handling all the git calls made by GitLab

Quick Links: Roadmap | Want to Contribute? | GitLab Gitaly Issues | GitLab Gitaly Merge Requests | Gitaly is a Git RPC service for handling all the gi

null 1 Nov 13, 2021
:octocat: lazyhub - Terminal UI Client for GitHub using gocui.

lazyhub lazyhub - Terminal UI Client for GitHub using gocui. Demo Features ?? Check the trending repositories on GitHub today ?? Search repositories ?

ryo-ma 170 Dec 14, 2022
A Simple and Comprehensive Vulnerability Scanner for Container Images, Git Repositories and Filesystems. Suitable for CI

A Simple and Comprehensive Vulnerability Scanner for Containers and other Artifacts, Suitable for CI. Abstract Trivy (tri pronounced like trigger, vy

Aqua Security 15.6k Jan 9, 2023
A simple tool to help apply changes across many GitHub repositories simultaneously

A simple tool to help apply changes across many GitHub repositories simultaneously

Skyscanner 346 Dec 22, 2022
Simple git hooks written in go that installs globally to your machine

Go-hooks Simple git hooks written in go that installs globally to your machine Install curl -fsSL

Vadim Makerov 2 Oct 19, 2022
🥄A simple generator for semantic git messages.

?? Tablespoon EXPERIMENTAL PREVIEW A simple generator for semantic git messages. Installation | Contributing Tablespoon is a simple generator which ca

Matt 6 Jul 22, 2022
A simple cli tool for switching git user easily inspired by Git-User-Switch

gitsu A simple cli tool for switching git user easily inspired by Git-User-Switch Installation Binary releases are here. Homebrew brew install matsuyo

Masaya Watanabe 207 Dec 31, 2022
Go-commitlinter - simple commit message linter

go-commitlinter go-commitlinter is simple commit message linter. Quick Start go

Masahiro331 15 Oct 8, 2022
Suppress commit to master and development, merge branch to master and development

git-extension masterと名前のつくブランチをマージするのは禁止 masterとdevelopmentブランチに直接commitやmergeするのは禁止 masterブランチを親に新規ブランチを作成するのは禁止 どうしてもmasterやdevelopmentブランチに操作をしたい時は

y-oga 1 Nov 8, 2021
A tool to monitor git repositories and automatically pull & push changes

git-o-matic A tool to monitor git repositories and automatically pull & push changes Installation Packages & Binaries Arch Linux: gitomatic Binaries f

Christian Muehlhaeuser 1k Dec 20, 2022
chglog is a changelog management library and tool

chglog chglog is a changelog management library and tool Why While there are other tool out there that will create a changelog output as part of their

GoReleaser 184 Nov 16, 2022
Fast and powerful Git hooks manager for any type of projects.

Lefthook The fastest polyglot Git hooks manager out there Fast and powerful Git hooks manager for Node.js, Ruby or any other type of projects. Fast. I

Abroskin Alexander 2.6k Jan 4, 2023
go mod vendor lets you check in your dependencies to git, but that's both bloaty (for developers) and tedious (remembering to update it).

go-mod-archiver Afraid of being unable to build historical versions of your Go program? go mod vendor lets you check in your dependencies to git, but

Tailscale 86 Dec 1, 2022
A single Git repository that holds two microservices (Python and GO)

A single Git repository that holds two microservices (Python and GO)

null 0 Nov 19, 2021
Installs git repos onto your system and keeps them up-to-date

Gitfile Installs git repos onto your system and keeps them up-to-date. It's a lightweight package manager for things that haven't been published to a

Brad Urani 18 Jan 16, 2021
A GitLab API client enabling Go programs to interact with GitLab in a simple and uniform way

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

Xanzy 1.9k Dec 28, 2022
A GitLab API client enabling Go programs to interact with GitLab in a simple and uniform way

TEMPORARY REPOSITORY This repo will exist only temporarily waiting for the PR has been merged.

null 0 Nov 5, 2021