Guest User

Untitled

a guest
Jan 21st, 2018
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. require 'json'
  2.  
  3. # run from git repo. Make sure to pull first
  4. # EXAMPLE
  5. # cd MY_LIGHTNING_GLOBAL_GIT_FOLDER
  6. # ~/cherrychecker.rb
  7. # ~/cherrychecker.rb email=mmakarov@salesforce.com
  8. # ~/cherrychecker.rb --slack
  9. # ~/cherrychecker.rb compare=212-patch
  10.  
  11.  
  12. # Getting a hash from params passed
  13. args_hash = {}
  14. ARGV.each { |a| key, value = a.split('='); args_hash[key] = value }
  15.  
  16. # Vars
  17. SLACK_URL = ENV['SLACK_URL']
  18. PR_REGEX = /#([0-9]+)/
  19. LOG = 'git log --pretty=format:"%h||%an||%ae||%s"'
  20. FIELDS = [:hash, :name, :email, :message]
  21. AMOUNT = args_hash['amount'] ? args_hash['amount'].to_i : 20
  22. USER_EMAIL = args_hash['email'] ? "| grep '#{args_hash['email']}'" : ''
  23. PATCH_BRANCH_NAME = args_hash['compare'] || '212-patch'
  24. prs = {}
  25.  
  26. def get_commits_array(branch)
  27. prs = []
  28. ref_prs = []
  29. history = `#{LOG} #{branch} #{USER_EMAIL}`.split("\n")
  30. records = history.map do |h|
  31. obj = {}
  32. records = h.split("||")
  33. parsed_prs = records.last.scan(PR_REGEX)
  34. if parsed_prs.length > 0
  35. obj[:pr] = parsed_prs.last
  36. prs << parsed_prs.last
  37. end
  38. if parsed_prs.length == 2
  39. obj[:ref_pr] = parsed_prs.first
  40. ref_prs << parsed_prs.first
  41. end
  42. FIELDS.each_with_index do |f, i|
  43. obj[f] = records[i]
  44. end
  45. obj
  46. end
  47. {
  48. records: records,
  49. prs: prs,
  50. ref_prs: ref_prs,
  51. }
  52. end
  53.  
  54. master = get_commits_array('master')
  55. patch = get_commits_array(PATCH_BRANCH_NAME)
  56. missing_prs = patch[:prs] - master[:ref_prs]
  57. missing_prs = missing_prs - master[:prs]
  58. last_20_numbers = missing_prs.first(AMOUNT)
  59. last_20 = last_20_numbers.map do |pr|
  60. patch[:records].select {|r| r[:pr] == pr}.first
  61. end
  62.  
  63. # Posting to slack if slack param's present
  64. if args_hash['--slack']
  65. last_20_json = last_20.map do |r|
  66. {
  67. "title": "#{r[:hash]} #{r[:pr]} #{r[:message]}",
  68. "value": "by #{r[:name]}",
  69. "short": false,
  70. }
  71. end
  72. message = [
  73. {
  74. "pretext": "Last 20 PRs that got merge to 212-patch and were not cherry picked to master",
  75. "fields": last_20_json,
  76. }
  77. ]
  78.  
  79. puts message.to_json
  80.  
  81. puts `curl -X POST --data-urlencode 'payload={"channel": "#cherry-picks", "username": "TEST: Mr. IO", "text": "TEST", "attachments": #{message.to_json}, "icon_emoji": ":philosoraptor:"}' #{SLACK_URL}`
  82. else
  83. # if no slack param, outputting
  84. last_20.each_with_index do |r, i|
  85. puts "#{i + 1}) *** #{r[:hash]} *** #{r[:email]} #{r[:name]} #{r[:pr]}"
  86. puts "#{r[:message]}"
  87. end
  88. end
Add Comment
Please, Sign In to add comment