Read more

GitLab: Rails Console Tasks

Deleted user #4941
March 27, 2019Software engineer

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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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.

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