Andreas Vöst
6 months
Andreas Vöst
8 months
Marc Dierig
11 months
Claus-Theodor Riegg
4 years
Andreas Vöst
4 years
Marius Schuller
5 years
Claus-Theodor Riegg
6 years
Claus-Theodor Riegg
7 years
Claus-Theodor Riegg
7 years
Claus-Theodor Riegg
7 years
Claus-Theodor Riegg
8 years

GitLab: Rails Console Tasks

Posted . Visible to the public.

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

There is also an API Show archive.org 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
      issue.save
    end
  end
end

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.

Marius Schuller
Last edit
Marius Schuller
License
Source code in this card is licensed under the MIT License.