From cd71532dd7f76648de4e7eb83783f22d9d169acf Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 3 Sep 2024 10:13:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E7=A6=85=E9=81=93=E8=84=9A=E6=9C=AC=E6=97=A0=E6=B3=95=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E8=BF=AD=E4=BB=A3=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/import_issues_from_chandao.rake | 265 +++++++++++++--------- 1 file changed, 154 insertions(+), 111 deletions(-) diff --git a/lib/tasks/import_issues_from_chandao.rake b/lib/tasks/import_issues_from_chandao.rake index de890b717..c4a787a57 100644 --- a/lib/tasks/import_issues_from_chandao.rake +++ b/lib/tasks/import_issues_from_chandao.rake @@ -1,133 +1,176 @@ desc "导入禅道数据" namespace :import_from_chandao do + + def get_sprint_data(name, pm_project_id, org) + assigner = org.team_users.joins(:team).where(teams: {authorize: "owner"}).take&.user + url = URI("#{EduSetting.get("pms_server_url")}/api/pms/#{org.login}/pmsProjectSprint/findOrCreate") + + http = Net::HTTP.new(url.host, url.port); + request = Net::HTTP::Post.new(url) + request["Cookie"] = "autologin_trustie=#{Token.get_or_create_permanent_login_token(assigner, 'autologin')}" + request["Content-Type"] = "application/json" + request.body = JSON.dump({ + "pmsProjectId": pm_project_id, + "sprintAssigneeId": assigner.id, + "sprintName": name + }) + + response = http.request(request) + puts response.read_body + return JSON.parse(response.read_body)['data'] + rescue + return nil + end + desc "bug数据" # 执行示例 bundle exec rake "import_from_chandao:bugs[企业内部工时管理系统.csv, 3]" # RAILS_ENV=production bundle exec rake "import_from_chandao:bugs[企业内部工时管理系统.csv, 3]" - task :bugs, [:name, :pm_project_id] => :environment do |t, args| - def trans_status(str) - h={ - "激活" => 1, - "已解决" => 3, - "已关闭" => 5 - } - h[str] - end - name = args.name - CSV.foreach("#{Rails.root}/#{args.name}", headers: true) do | row | - randd_field_hash = row.to_hash - issue = Issue.new(issue_classify: "issue") - author = User.like(randd_field_hash['由谁创建']).take || User.where(admin: true).first - issue.author_id = author&.id - assigner = randd_field_hash['指派给'].present? ? User.like(randd_field_hash['指派给']).take : nil - if assigner.present? - issue.assigners << assigner - end - issue.project_issues_index = randd_field_hash['Bug编号'].to_i - issue.status_id = trans_status(randd_field_hash['Bug状态']) || IssueStatus.first.id - issue.tracker_id = Tracker.first.id - issue.priority_id = randd_field_hash['优先级'].to_i - issue.subject = randd_field_hash['Bug标题'] - issue.description = randd_field_hash['重现步骤'] - issue.created_on = randd_field_hash['创建日期'].to_time rescue nil - issue.updated_on = randd_field_hash['修改日期'].to_time rescue issue.created_on - issue.due_date = randd_field_hash['截止日期'].to_time rescue nil - issue.project_id = 0 - issue.pm_project_id = args.pm_project_id - issue.pm_issue_type = 3 - issue.save! - requirement_issue = Issue.find_by(project_issues_index: randd_field_hash['相关需求'].split('(#')[1].split(')')[0], pm_project_id: args.pm_project_id, pm_issue_type: 1) rescue nil - if requirement_issue.present? - requirement_issue.pm_links.find_or_create_by(be_linkable_type: 'Issue', be_linkable_id: issue.id) - end - task_issue = Issue.find_by(project_issues_index: randd_field_hash['相关任务'].split('(')[1].split(')')[0], pm_project_id: args.pm_project_id, pm_issue_type: 2) rescue nil - if task_issue.present? - task_issue.pm_links.find_or_create_by(be_linkable_type: 'Issue', be_linkable_id: issue.id) + task :bugs, [:name, :pm_project_id, :org_login] => :environment do |t, args| + org = Organization.find_by(login: args.org_login) + if org.present? + def trans_status(str) + h={ + "激活" => 1, + "已解决" => 3, + "已关闭" => 5 + } + h[str] + end + name = args.name + CSV.foreach("#{Rails.root}/#{args.name}", headers: true) do | row | + randd_field_hash = row.to_hash + issue = Issue.new(issue_classify: "issue") + author = User.like(randd_field_hash['由谁创建']).take || User.where(admin: true).first + issue.author_id = author&.id + assigner = randd_field_hash['指派给'].present? ? User.like(randd_field_hash['指派给']).take : nil + if assigner.present? + issue.assigners << assigner + end + issue.project_issues_index = randd_field_hash['Bug编号'].to_i + issue.status_id = trans_status(randd_field_hash['Bug状态']) || IssueStatus.first.id + issue.tracker_id = Tracker.first.id + issue.priority_id = randd_field_hash['优先级'].to_i + issue.subject = randd_field_hash['Bug标题'] + issue.description = randd_field_hash['重现步骤'] + issue.created_on = randd_field_hash['创建日期'].to_time rescue nil + issue.updated_on = randd_field_hash['修改日期'].to_time rescue issue.created_on + issue.due_date = randd_field_hash['截止日期'].to_time rescue nil + issue.project_id = 0 + issue.pm_project_id = args.pm_project_id + issue.pm_issue_type = 3 + sprint_name = randd_field_hash['所属迭代'].split('(#')[0] + sprint = get_sprint_data(sprint_name, args.pm_project_id, org) + issue.pm_sprint_id = sprint['id'] if sprint.present? + issue.save! + requirement_issue = Issue.find_by(project_issues_index: randd_field_hash['相关需求'].split('(#')[1].split(')')[0], pm_project_id: args.pm_project_id, pm_issue_type: 1) rescue nil + if requirement_issue.present? + requirement_issue.pm_links.find_or_create_by(be_linkable_type: 'Issue', be_linkable_id: issue.id) + end + task_issue = Issue.find_by(project_issues_index: randd_field_hash['相关任务'].split('(')[1].split(')')[0], pm_project_id: args.pm_project_id, pm_issue_type: 2) rescue nil + if task_issue.present? + task_issue.pm_links.find_or_create_by(be_linkable_type: 'Issue', be_linkable_id: issue.id) + end end + else + puts "组织不存在" end end # 执行示例 bundle exec rake "import_from_chandao:tasks[复杂智能软件项目-所有任务.csv, 365]" # RAILS_ENV=production bundle exec rake "import_from_chandao:tasks[复杂智能软件项目-所有任务.csv, 365]" - task :tasks, [:name, :pm_project_id] => :environment do |t, args| - def trans_status(str) - h={ - "未开始" => 1, - "进行中" => 2, - "已完成" => 3, - "已关闭" => 5 - } - h[str] - end - - name = args.name - pm_project_id = args.pm_project_id - CSV.foreach("#{Rails.root}/#{name}", headers: true) do | row | - randd_field_hash = row.to_hash - issue = Issue.new(issue_classify: "issue") - author = User.like(randd_field_hash['由谁创建']).take || User.where(admin: true).first - issue.author_id = author&.id - assigner = randd_field_hash['指派给'].present? ? User.like(randd_field_hash['指派给']).take : nil - if assigner.present? - issue.assigners << assigner - end - issue.project_issues_index = randd_field_hash['编号'].to_i - issue.status_id = trans_status(randd_field_hash['任务状态']) || IssueStatus.first.id - issue.tracker_id = Tracker.first.id - issue.priority_id = randd_field_hash['优先级'].to_i - issue.subject = randd_field_hash['任务名称'] - issue.description = randd_field_hash['任务描述'] - issue.created_on = randd_field_hash['创建日期'].to_time rescue nil - issue.updated_on = randd_field_hash['最后修改日期'].to_time rescue issue.created_on - issue.time_scale = randd_field_hash['最初预计'].to_i - issue.start_date = randd_field_hash['预计开始'].to_time rescue nil - issue.due_date = randd_field_hash['截止日期'].to_time rescue nil - issue.project_id = 0 - issue.pm_project_id = pm_project_id - issue.pm_issue_type = 2 - issue.save! - requirement_issue = Issue.find_by(project_issues_index: randd_field_hash['相关需求'].split('(#')[1].split(')')[0], pm_project_id: pm_project_id, pm_issue_type: 1) rescue nil - if requirement_issue.present? - requirement_issue.pm_links.find_or_create_by(be_linkable_type: 'Issue', be_linkable_id: issue.id) + task :tasks, [:name, :pm_project_id, :org_login] => :environment do |t, args| + org = Organization.find_by(login: args.org_login) + if org.present? + def trans_status(str) + h={ + "未开始" => 1, + "进行中" => 2, + "已完成" => 3, + "已关闭" => 5 + } + h[str] + end + + name = args.name + pm_project_id = args.pm_project_id + CSV.foreach("#{Rails.root}/#{name}", headers: true) do | row | + randd_field_hash = row.to_hash + issue = Issue.new(issue_classify: "issue") + author = User.like(randd_field_hash['由谁创建']).take || User.where(admin: true).first + issue.author_id = author&.id + assigner = randd_field_hash['指派给'].present? ? User.like(randd_field_hash['指派给']).take : nil + if assigner.present? + issue.assigners << assigner + end + issue.project_issues_index = randd_field_hash['编号'].to_i + issue.status_id = trans_status(randd_field_hash['任务状态']) || IssueStatus.first.id + issue.tracker_id = Tracker.first.id + issue.priority_id = randd_field_hash['优先级'].to_i + issue.subject = randd_field_hash['任务名称'] + issue.description = randd_field_hash['任务描述'] + issue.created_on = randd_field_hash['创建日期'].to_time rescue nil + issue.updated_on = randd_field_hash['最后修改日期'].to_time rescue issue.created_on + issue.time_scale = randd_field_hash['最初预计'].to_i + issue.start_date = randd_field_hash['预计开始'].to_time rescue nil + issue.due_date = randd_field_hash['截止日期'].to_time rescue nil + issue.project_id = 0 + issue.pm_project_id = pm_project_id + issue.pm_issue_type = 2 + sprint_name = randd_field_hash['所属迭代'].split('(#')[0] + sprint = get_sprint_data(sprint_name, args.pm_project_id, org) + issue.pm_sprint_id = sprint['id'] if sprint.present? + issue.save! + requirement_issue = Issue.find_by(project_issues_index: randd_field_hash['相关需求'].split('(#')[1].split(')')[0], pm_project_id: pm_project_id, pm_issue_type: 1) rescue nil + if requirement_issue.present? + requirement_issue.pm_links.find_or_create_by(be_linkable_type: 'Issue', be_linkable_id: issue.id) + end end + else + puts '组织不存在' end end # 执行示例 bundle exec rake "import_from_chandao:requirements[企业网站第二期.csv, 3]" # RAILS_ENV=production bundle exec rake "import_from_chandao:requirements[企业网站第二期.csv, 3]" - task :requirements, [:name, :pm_project_id] => :environment do |t, args| - def trans_status(str) - h={ - "草稿" => 1, - "激活" => 1, - "已关闭" => 5 - } - h[str] - end - name = args.name - pm_project_id = args.pm_project_id - CSV.foreach("#{Rails.root}/#{name}", headers: true) do | row | - randd_field_hash = row.to_hash - issue = Issue.new(issue_classify: "issue") - author = User.like(randd_field_hash['由谁创建']).take || User.where(admin: true).first - issue.author_id = author&.id - assigner = randd_field_hash['指派给'].present? ? User.like(randd_field_hash['指派给']).take : nil - if assigner.present? - issue.assigners << assigner + task :requirements, [:name, :pm_project_id, :org_login] => :environment do |t, args| + org = Organization.find_by(login: args.org_login) + if org.present? + def trans_status(str) + h={ + "草稿" => 1, + "激活" => 1, + "已关闭" => 5 + } + h[str] + end + name = args.name + pm_project_id = args.pm_project_id + CSV.foreach("#{Rails.root}/#{name}", headers: true) do | row | + randd_field_hash = row.to_hash + issue = Issue.new(issue_classify: "issue") + author = User.like(randd_field_hash['由谁创建']).take || User.where(admin: true).first + issue.author_id = author&.id + assigner = randd_field_hash['指派给'].present? ? User.like(randd_field_hash['指派给']).take : nil + if assigner.present? + issue.assigners << assigner + end + issue.project_issues_index = randd_field_hash['编号'].to_i + issue.status_id = trans_status(randd_field_hash['当前状态']) || IssueStatus.first.id + issue.tracker_id = Tracker.first.id + issue.priority_id = randd_field_hash['优先级'].to_i + issue.subject = randd_field_hash['需求名称'] + issue.description = randd_field_hash['需求描述'] + issue.created_on = randd_field_hash['创建日期'].to_time + issue.updated_on = randd_field_hash['最后修改日期'].to_time rescue randd_field_hash['创建日期'].to_time + issue.time_scale = randd_field_hash['预计工时'].to_i + issue.project_id = 0 + issue.pm_project_id = pm_project_id + issue.pm_issue_type = 1 + issue.save! end - issue.project_issues_index = randd_field_hash['编号'].to_i - issue.status_id = trans_status(randd_field_hash['当前状态']) || IssueStatus.first.id - issue.tracker_id = Tracker.first.id - issue.priority_id = randd_field_hash['优先级'].to_i - issue.subject = randd_field_hash['需求名称'] - issue.description = randd_field_hash['需求描述'] - issue.created_on = randd_field_hash['创建日期'].to_time - issue.updated_on = randd_field_hash['最后修改日期'].to_time rescue randd_field_hash['创建日期'].to_time - issue.time_scale = randd_field_hash['预计工时'].to_i - issue.project_id = 0 - issue.pm_project_id = pm_project_id - issue.pm_issue_type = 1 - issue.save! + else + puts '组织不存在' end end end \ No newline at end of file