Read more

GitLab: Rails Console Tasks

Marius Schuller
March 27, 2019Software engineer at makandra GmbH

Sometimes you might need to do some task in GitLab which would be tedious if you'd have to do it via the Browser.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show snapshot

There is also an API Show snapshot which you could maybe use!

You can connect to the GitLab internal Rails with executing gitlab-rails console. It might be slow at times, especially if you use a lot of Tab for complex structures, so be aware of this.

The following code listings are expected to be entered inside the gitlab-rails console.

Remove GitLab Labels from already closed Issues

p = Project.find_by_id(1)

labels_to_remove = [3, 4, 1, 84, 86] # Scheduled, Active, This Week, One Week, Two Weeks
# for every issue
p.issues.each do |issue|
  # we edit only closed issues
  if issue.closed?
    # remove our unwanted labels
    issue.label_ids -= labels_to_remove
    if issue.valid?
      # and save the issue

Hint: The Issue ID we know is to be found as iid in GitLab.

First we need to find our project. In our case, this is the makandra-ops/puppet project, which as it happens, was the first project in this GitLab:

p = Project.first

Now we have a filter and we're able to see only issues that concern us. You can take a look in all supported methods of the issue Object without having to wait for tab completion by using this line:

i = p.issues.find_by_iid(4042)
puts i.public_methods.sort

Maybe you want to save this list to be able to search for stuff in it.

Here we want to remove labels from issues they no longer need. These are the Labels Scheduled, Active, This Week, One Week, Two Weeks with the respective label ids 3, 4, 1, 84, 86 which leads to the above code snipped.

Posted by Marius Schuller to makandra Operations (2019-03-27 12:39)