Commit 6318c7e7 by Tấn Trần Thanh

multiple thread request to github

parent 4164867a
Pipeline #1606 failed with stages
in 0 seconds
......@@ -17,6 +17,7 @@ module WorkflowReport
def build_report(year, month, project_ids)
github = Github.new oauth_token: $workflow_report_config['github_token']
error_links = []
github_links = { prs: [], issues: [] }
result = TABLE_HEADER.length.times.map { [] }
raw_tasks = WorkflowReportIssue.raw_tasks_records(year, month, project_ids)
return { workflow_report: [], error_links: [] } if raw_tasks.empty?
......@@ -60,12 +61,12 @@ module WorkflowReport
else
(11..13).each { |i| result[i] << '' }
end
pull_request = ''
jp_request = ''
issues.each do |issue|
if issue.tracker_id == USER_STORY_TRACKER_ID
pull_request = issue.custom_values.find_by(custom_field_id: PR_FIELD_ID)&.value
pr_links = issue.custom_values.find_by(custom_field_id: PR_FIELD_ID)&.value
jp_request = issue.custom_values.find_by(custom_field_id: JP_REQUEST_FIELD_ID)&.value
github_links[:prs].push({ row: result[0].length - 1, links: pr_links, root_id: root_id }) if pr_links.present?
github_links[:issues].push({ row: result[0].length - 1, links: jp_request, root_id: root_id }) if jp_request.present?
end
process = get_process(issue.subject.gsub(/[^[:print:]]/, ''))
......@@ -96,53 +97,24 @@ module WorkflowReport
result[BUG_COLS + index] << find_max_value_by_custom_field_id(issues, custom_id)
end
end
if jp_request.strip.blank?
result[32] << ''
result[33] << ''
else
jp_request = jp_request.strip
result[32] << jp_request
begin
jp_request_arr = URI(jp_request).path.split('/').reject(&:blank?)
if jp_request_arr.length >= 4 && jp_request_arr[3].to_i.positive?
issue_detail = github.issues.find user: jp_request_arr[0], repo: jp_request_arr[1], number: jp_request_arr[3]
result[33].push(issue_detail.success? ? issue_detail.comments : '')
else
result[33].push('') if result[33][result[0].length - 1].nil?
end
threads = github_links.map do |key, values|
Thread.new do
if key == :prs
values.each_with_index do |prs, index|
find_detail_pr(github, prs[:links], error_links, result, prs[:row], prs[:root_id])
sleep(0.1) if (index % 5).zero? && index.positive?
end
rescue StandardError => e
error_links << "##{root_id} Error occurred during the build with the ISSUE link:\r\n - #{jp_request} \r\n - #{e.message.gsub(/(?<=access_token=).*/, '123')}"
result[33].push('') if result[33][result[0].length - 1].nil?
end
end
# pr detail
pr = { pr_comments: 0, pr_review_comments: 0, pr_commits: 0, pr_changed_files: 0, pr_additions: 0, pr_deletions: 0 }
if pull_request.present?
prs = pull_request.split("\r\n").compact
prs.each do |link|
begin
pr_link_arr = URI(link.strip).path.split('/').reject!(&:empty?)
next unless pr_link_arr.length >= 4
pr_detail = github.pull_requests.find user: pr_link_arr[0], repo: pr_link_arr[1], number: pr_link_arr[3]
if pr_detail.success?
pr[:pr_comments] += pr_detail.comments
pr[:pr_review_comments] += pr_detail.review_comments
pr[:pr_commits] += pr_detail.commits
pr[:pr_additions] += pr_detail.additions
pr[:pr_deletions] += pr_detail.deletions
pr[:pr_changed_files] += pr_detail.changed_files
end
rescue StandardError => e
error_links << "##{root_id} Error occurred during the build with the PR link:\r\n - #{link} \r\n - #{e.message.gsub(/(?<=access_token=).*/, '123')}"
else
values.each_with_index do |issues, index|
find_detail_issue(github, result, error_links, issues[:links], issues[:row], issues[:root_id])
sleep(0.1) if (index % 5).zero? && index.positive?
end
end
end
pr.each_with_index do |(key, _detail), index|
result[PR_CMT_COL + index] << pr[key]
end
end
threads.each(&:join)
{ workflow_report: result, error_links: error_links }
end
......@@ -165,17 +137,17 @@ module WorkflowReport
end
else
if name.include? "Requirement"
name = "1. " + name
name = "1. #{name}"
elsif name.include? "Design"
name = "2. " + name
name = "2. #{name}"
elsif ["Coding", "Code review"].include? name
name = "3. Coding"
elsif ["Testing", "Create Test case"].include? name
name = "4. Testing"
elsif name.include? "Bug fixing"
name = "5. " + name
name = "5. #{name}"
elsif name.include? "Release"
name = "6. " + name
name = "6. #{name}"
end
end
end
......@@ -205,5 +177,56 @@ module WorkflowReport
[max_value, max_value_in_record.to_i].max
end
def find_detail_pr(github, pull_request, error_links, result, row, root_id)
pr = { pr_comments: 0, pr_review_comments: 0, pr_commits: 0, pr_changed_files: 0, pr_additions: 0, pr_deletions: 0 }
if pull_request.present?
prs = pull_request.split("\r\n").compact
prs.each do |link|
begin
pr_link_arr = URI(link.strip).path.split('/').reject!(&:empty?)
next unless pr_link_arr.length >= 4
pr_detail = github.pull_requests.find user: pr_link_arr[0], repo: pr_link_arr[1], number: pr_link_arr[3]
if pr_detail.success?
pr[:pr_comments] += pr_detail.comments
pr[:pr_review_comments] += pr_detail.review_comments
pr[:pr_commits] += pr_detail.commits
pr[:pr_additions] += pr_detail.additions
pr[:pr_deletions] += pr_detail.deletions
pr[:pr_changed_files] += pr_detail.changed_files
end
rescue StandardError => e
error_links << "##{root_id} Error occurred during the build with the PR link:\r\n - #{link} \r\n - #{e.message.gsub(/(?<=access_token=).*/, '123')}"
end
end
end
pr.each_with_index do |(key, _detail), index|
result[PR_CMT_COL + index][row] = pr[key]
end
end
def find_detail_issue(github, result, error_links, jp_request, row, root_id)
if jp_request.strip.blank?
result[32][row] = ''
result[33][row] = ''
else
jp_request = jp_request.strip
result[32][row] = jp_request
begin
jp_request_arr = URI(jp_request).path.split('/').reject(&:blank?)
if jp_request_arr.length >= 4 && jp_request_arr[3].to_i.positive?
issue_detail = github.issues.find user: jp_request_arr[0], repo: jp_request_arr[1], number: jp_request_arr[3]
result[33][row] = issue_detail.success? ? issue_detail.comments : ''
else
result[33][row] = '' if result[33][row].nil?
end
rescue StandardError => e
error_links << "##{root_id} Error occurred during the build with the ISSUE link:\r\n - #{jp_request} \r\n - #{e.message.gsub(/(?<=access_token=).*/, '123')}"
result[33][row] = '' if result[33][row].nil?
end
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment