Quick Start Example

To run the code in this vignette you’ll need to have a GitLab account and you need to generate a personal access token (PAT). See the GitLab documentation on how to generate PATs. In the Scopes section you only need to tick the api box.

R code using {gitlabr} to perform some easy, common GitLab actions can look like this:


# Store your token in .Renviron and restart your session
# Add: GITLAB_COM_TOKEN=YourTokenHere
# You can verify it worked

# connect as a fixed user to a gitlab instance
my_gitlab <- gl_connection(gitlab_url = "https://gitlab.com",
                           private_token = Sys.getenv("GITLAB_COM_TOKEN"))
# Set the connection for the session
# a function is returned
# its first argument is the request (name or function), optionally followed by parameters

gl_list_projects(page = 1)  # Returns all projects on GitLab, so we limit to just the first page of results.

# It's unlikely that you'll want to use {gitlabr} to interact with all the projects on GitLab, so a better approach is to define the project you want to work on. This is done by finding the the project ID on GitLab.com (it is listed right below the project name on the repo front page).
# Here we use the [project "repo.rtask"](https://gitlab.com/statnmap/repo.rtask)
my_project <- 20384533
gl_list_files(project = my_project)

# create a new issue
new_feature_issue <- gl_new_issue(title = "Implement new feature",
                                  project = my_project)

# statnmap user ID
my_id <- 4809823

# assign issue to me
gl_assign_issue(assignee_id = example_user$id,
                issue_id = new_feature_issue$iid,
                project = my_project)

gl_list_issues(state = "opened",
               project = my_project)

# close issue
gl_close_issue(issue_id = new_feature_issue$iid, 
               project = my_project)$state

Central features of {gitlabr}

  • {gitlabr} provides a high and a low level interface to the GitLab API at the same time:
    • Common queries are wrapped in special convenience functions that can be used without any knowledge of the GitLab API itself (convenience functions are listed in a dedicated section on {gitlabr}’s pkgdown site).
    • Still, the package can be used to access the complete GitLab API – learn how to use its full power in the section “API calls”.
  • The output of every call to a {gitlabr} function is a tibble to integrate seamless into dplyr’s data manipulation mindset (often called the “tidyverse”)
  • Pagination is wrapped for the user, but can be controlled via parameters page and per_page if necessary.
  • To allow programming in your favorite style, everything you can do with {gitlabr} you can do using any of a set of general idioms – get to know them in the section “Different ways to do it”.
  • You can write your own convenience wrappers on top of the {gitlabr} logic following only one principle as described in the section “Writing custom GitLab request functions”.

API calls

This section describes how R function calls are translated into HTTP requests to the GitLab API ({gitlabr}’s “low level interface”). For a documentation using {gitlabr} without knowledge of the GitLab API ({gitlabr}’s “high level interface”), see the “Quick Start Example” above or refer to the individual function documentation in the Reference section of {gitlabr}’s pkgdown site.

Currently ({gitlabr} >= 1.1.6) GitLab API v4 is supported. Support for Gitlab API v3 (for GitLab version < 9.0) is still included via flag parameters, but is no longer maintained. For details see the section “API version” of the documentation of gl_connection.

The core function of the low level interface is gitlab, with the help of which arbitrary calls to the GitLab API can be formulated. It takes as required arguments the request location as a character vector, API endpoint URL and HTTP verb and passes additional arguments as query parameters (keeping their names) on to the API request.

gitlab(c("projects", 12, "issues"), 
       api_root = "https://gitlab.points-of-interest.cc/api/v4",
       private_token = "XXX", # authentication for API
       verb = httr::GET,  # defaults to GET, but POST, PUT, DELETE can be used likewise
       state = "active") # additional parameters (...) for the query

translates to

GET https://gitlab.points-of-interest.cc/api/v4/projects/12/issues?state=active&private_token=XXX

This way, any request documented in the GitLab API documentation can be issued from {gitlabr}.

The high level interface consists of a number of functions that each have additional arguments from which the request location is constructed, while all other arguments are simply passed on to gitlab(). For example:

gl_edit_issue(project = "test-project", 12, description = "Really cool new feature",
           api_root = "...", private_token = "XXX")

does nothing but

         4,  # numeric id of test-project is found by search
       description = "Really cool new feature",
       api_root = "...",
       private_token = "XXX",
       verb = httr::PUT)

and hence translates to

PUT .../projects/4/issues/12?private_token=XXX?description=Really%20cool%20new%20feature

To spare you the repetitive task of specifying the API root and key in every call, you can use gitlab_connection as described below.

{gitlabr} is implemented following the functional programming paradigm. Several of its functions return or accept functions as arguments. This results in huge flexibility in how API requests using {gitlabr} can be formulated in your R code. Three major styles are described below, after introducing the central mechanism of creating more specific API connection functions.

Creating connections

The idea of connections in {gitlabr} is to generate functions with the same signature and capability of the central API call function gitlab(), but with certain parameters set to fixed values (“curried”). This way these more specialized functions represent and provide the connection – for example – to a specific GitLab instance as a specific user. Such specialized functions can be created by the function gitlab_connection() and then used exactly as you would use gitlab():

my_gitlab <- gl_connection("https://test-gitlab.points-of-interest.cc",
                           private_token = readLines("secrets/gitlab_token.txt"))

gitlab_connection can take arbitrary parameters, returning a function that issues API requests with these parameter values set.

As a convenience wrapper to directly connect to a specific project in a GitLab instance, project_connection exists.

For combining so created GitLab connections with the convenience functions to perform common tasks, several possible styles/idioms exist:

function-in-function style

Instead of the query as character vector gitlab() and thus also all connections accept equivalently a function as first argument, that is then called with the additional parameters and using the connection for all API calls:

my_gitlab(gl_new_issue, "Implement new feature", project = my_project)

gl_new_issue() is an example function here, the principle style works for all convenience functions of {gitlabr} listed in the “Convenience function list” below or user-defined functions as described in the section “Writing custom gitlab request functions”.

Some of the convenience perform additional transformation or renaming of parameters. Hence, the parameters given to the exemplary my_gitlab(...) call after the function should be valid according the documentation of the respective function and may differ from names used in the GitLab API itself, although this is the case only in very few cases.

parameter style

Alternatively, gitlab() as well as all convenience wrappers accept a parameter gitlab_con() specifying the function to use for the actual API call. Hence, you can pass a GitLab connection (as returned by gl_connection()) with the R function call:

gl_new_issue("Implement new feature", project = my_project, gitlab_con = my_gitlab)

Again, gl_new_issue() is an example function here, the principle style works for all convenience functions of {gitlabr} listed in the “Convenience function list” below or user-defined functions as described in the section “Writing custom GitLab request functions”.

set style

In order to avoid the repeated specification of gitlab_con() in the parameter style, you can also set a global variable managed by {gitlabr} to use a specific connection function for every call:

gl_new_issue("Implement new feature", project = my_project)

Again, gl_new_issue() is an example function here, the principle style works for all convenience functions of {gitlabr} listed in the “Convenience function list” below or user-defined functions as described in the section “Writing custom GitLab request functions”.

Note that the set style is not purely functional, since set_gitlab_connection() changes a saved global variable affecting the results of all future gitlab() calls. You can reset this variable to the default value using unset_gitlab_connection().

Note: There are more locations and actions that can be accessed through the GitLab API. See the documentation of the GitLab API for this. The next section describes how you can write your own convenience wrappers that work with all styles described in the section “Different ways to do it”.

Writing custom gitlab request functions

It is very easy to write your own convenience wrappers for accessing API endpoints you wish and make sure they fully integrate into {gitlabr} and work conveniently with all connection and call idioms described in this vignette. The only requirement to your function is that it executes an R function call to gitlab() (or another convenience function) to which the ... argument is passed on.

That is, a simple function to block users directly from R is as simple as:

gl_block_user <- function(uid, ...) {
  gitlab(c("users", uid, "block"),  ## for API side documentation see:
         verb = httr::PUT, ## https://doc.gitlab.com/ce/api/users.html#block-user
         ...) ## don't forget the dots to make {gitlabr} features fully available

More hints for more convenience:

  • To be consistent with another important {gitlabr} principle, make sure your function returns a tibble (which it does if you simply pass up the return value of gitlab() or one of the package’s own convenience functions). gitlab() has some heuristics to format the API response to a tibble, if these fail for your specific request, you can pass auto_format = FALSE and format the response manually.
  • To translate project names to numeric ids automatically, you can use {gitlabr}’s internal functions proj_req() translating the request location.
  • To translate user-visible project-wide issue ids to global ids used by the GitLab API, you can use {gitlabr}’s internal function to_issue_id() when constructing the request.

And last but not least, if you’ve written a convenience wrapper for yourself, keep in mind that it might be of help to many others and you can contribute it to {gitlabr} on https://github.com/statnmap/gitlabr.

Using GitLab CI with {gitlabr}

{gitlabr} can also be used to create a .gitlab-ci.yml file to test, build and check an R package using GitLab’s CI software. See the use_gitlab_ci() and related functions for documentation.