2016-07-18 16:45:49 +08:00
|
|
|
|
namespace :gitlab do
|
|
|
|
|
desc "sync gitlab's commit acts to trustie"
|
|
|
|
|
task :forge_acts => :environment do
|
2016-07-20 17:05:55 +08:00
|
|
|
|
g = Gitlab.client
|
2016-07-21 16:08:13 +08:00
|
|
|
|
projects = Project.find_by_sql("select * from projects where gpid is not null and id not in (2,847,931,942)")
|
2016-07-20 17:05:55 +08:00
|
|
|
|
projects.each do |project|
|
|
|
|
|
begin
|
2016-07-18 16:45:49 +08:00
|
|
|
|
g_project = g.project(project.gpid)
|
|
|
|
|
# 获取默认分支
|
|
|
|
|
g_default_branch = g_project.default_branch.nil? ? "master" : g_project.default_branch
|
|
|
|
|
# 总的提交次数
|
|
|
|
|
commit_count = g.user_static(project.gpid, :rev => g_default_branch).count
|
|
|
|
|
pages = commit_count / 20 + 1
|
2016-07-19 15:59:52 +08:00
|
|
|
|
puts "#{pages}"
|
2016-07-20 17:05:55 +08:00
|
|
|
|
puts "project id is #{project.id}"
|
2016-07-21 16:08:13 +08:00
|
|
|
|
# api获取每次只能获取20次提交,所以需要通过取得page值来获取每页的提交动态
|
2016-07-19 15:59:52 +08:00
|
|
|
|
(0..pages).each do |page|
|
|
|
|
|
commits = g.commits(project.gpid, :ref_name => g_default_branch, :page => page)
|
2016-07-18 16:45:49 +08:00
|
|
|
|
commits.each do |commit|
|
2016-07-21 17:19:45 +08:00
|
|
|
|
Commit.create(:project_id => project.id, :repository_id => project.gpid, :version => commit.id, :committer => commit.author_email, :comments => Redmine::CodesetUtil.to_utf8(commit.title, 'UTF-8'), :committed_on => commit.created_at)
|
2016-07-18 16:45:49 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2016-07-20 17:05:55 +08:00
|
|
|
|
rescue Exception => e
|
2016-07-21 16:08:13 +08:00
|
|
|
|
# puts "Some wrong with project #{project.id}"
|
|
|
|
|
# Project.where(:id => project.id).first.update_column(:gpid, nil)
|
|
|
|
|
# Repository.where(:project_id => project.id).first.destroy
|
|
|
|
|
# try
|
2016-07-20 17:05:55 +08:00
|
|
|
|
puts e
|
2016-07-18 16:45:49 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|