LoginSignup
3
3

More than 5 years have passed since last update.

GitLab API | gitlab gem で Issue を別のプロジェクトにコピーする

Posted at

GitLab API | gitlab gem で Issue を別のプロジェクトにコピーする

概要

gitlab gem で Issue を別のプロジェクトにコピーします。

サンプル

require 'gitlab'

Gitlab.configure do |config|
  config.endpoint       = 'http://some_path/api/v3' 
  config.private_token  = 'your token'
end

def find_project_id_from_project_name(name)
  Gitlab.projects(per_page: 100).select { |e|e.name == name }.first.id
end

def copy_issues(from_project_id, to_project_id)
  Gitlab.issues(from_project_id, per_page: 100).each do |issue|
    assignee_id = issue.assignee ? issue.assignee.id : nil
    milestone_id  = issue.milestone_id  ? issue.milestone_id.id : nil
    Gitlab.create_issue(to_project_id, issue.title,
      description: issue.description,
      assignee_id: assignee_id,
      milestone_id: milestone_id,
      labels: issue.labels)

    # closeされたIssueの場合はステータスをclosedに変更する
    to_issue = Gitlab.issues(to_project_id, per_page: 100).select { |e|e.title == issue.title }.first
    if to_issue && issue.state == 'closed'
      Gitlab.edit_issue(to_project_id, to_issue.id, state_event: 'close')
    end
  end
end

from_project_id = find_project_id_from_project_name('from_project')
to_project_id = find_project_id_from_project_name('to_project')
copy_issues(from_project_id, to_project_id)

参照

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3