Guest User

Untitled

a guest
Aug 19th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. import GitHub
  2. import Dates
  3.  
  4. ############
  5. # Settings #
  6. ############
  7. BACKPORT_LABEL = "backport pending 1.0"
  8. # where the release branch started
  9. START_COMMIT = "1dd2f8b397e60c10fdacd85fc62"
  10. GITHUB_AUTH = ENV["GITHUB_AUTH"]
  11. # stop looking after encounting PRs opened before this date
  12. LIMIT_DATE = Dates.Date("2018-08-01")
  13. # refresh PRs from github
  14. REFRESH_PRS = false
  15.  
  16. ########################################
  17. # Git executable convenience functions #
  18. ########################################
  19. function cherry_picked_commits(start_commit::String)
  20. commits = String[]
  21. logg = String(read(`git log $start_commit..HEAD`))
  22. for match in eachmatch(r"\(cherry picked from commit (.*?)\)", logg)
  23. push!(commits, match.captures[1])
  24. end
  25. return commits
  26. end
  27.  
  28. function try_cherry_pick(hash::String)
  29. if !success(`git cherry-pick -x $hash`)
  30. success(`git cherry-pick --abort`)
  31. return false
  32. end
  33. return true
  34. end
  35.  
  36. branch() = String(chomp(String(read(`git rev-parse --abbrev-ref HEAD`))))
  37.  
  38.  
  39. ##################
  40. # Main functions #
  41. ##################
  42. if !@isdefined(__myauth)
  43. const __myauth = Ref{GitHub.Authorization}()
  44. end
  45. function getauth()
  46. if !isassigned(__myauth)
  47. __myauth[] = GitHub.authenticate(GITHUB_AUTH)
  48. end
  49. return __myauth[]
  50. end
  51.  
  52. function collect_label_prs(backport_label::String)
  53. # What are good parameters...?
  54. myparams = Dict("state" => "all", "per_page" => 20, "page" => 1);
  55. label_prs = []
  56. i = 1
  57. print("Collecting PRs...")
  58. first = true
  59. local page_data
  60. while true
  61. print(".")
  62. prs, page_data = GitHub.pull_requests("JuliaLang/julia";
  63. page_limit = 1, auth=getauth(),
  64. (first ? (params = myparams,) : (start_page = page_data["next"],))...)
  65. first = false
  66. for pr in prs
  67. for label in pr.labels
  68. if label["name"] == backport_label
  69. push!(label_prs, pr)
  70. end
  71. end
  72. if pr.created_at < LIMIT_DATE
  73. return label_prs
  74. end
  75. end
  76. haskey(page_data, "next") || break
  77. end
  78. println()
  79. return label_prs
  80. end
  81.  
  82. if !@isdefined(label_prs)
  83. const label_prs = Ref{Vector}()
  84. end
  85.  
  86. function run(refresh_prs = false)
  87. if !isassigned(label_prs) || refresh_prs
  88. label_prs[] = collect_label_prs(BACKPORT_LABEL)
  89. end
  90. already_backported_commits = cherry_picked_commits(START_COMMIT)
  91. release_branch = branch()
  92. open_prs = []
  93. closed_prs = []
  94. already_backported = []
  95. backport_candidates = []
  96. for pr in label_prs[]
  97. if pr.state != "closed"
  98. push!(open_prs, pr)
  99. else
  100. if pr.merged_at === nothing
  101. push!(closed_prs, pr)
  102. elseif pr.merge_commit_sha in already_backported_commits
  103. push!(already_backported, pr)
  104. else
  105. push!(backport_candidates, pr)
  106. end
  107. end
  108. end
  109.  
  110. sort!(backport_candidates; by = x -> x.merged_at)
  111.  
  112. failed_backports = []
  113. successful_backports = []
  114. for pr in backport_candidates
  115. if try_cherry_pick(pr.merge_commit_sha)
  116. push!(successful_backports, pr)
  117. else
  118. push!(failed_backports, pr)
  119. end
  120. end
  121.  
  122. # Actions to take:
  123. if !isempty(closed_prs)
  124. println("The following PRs are closed but has a backport label, remove the label")
  125. for pr in closed_prs
  126. println(" #$(pr.number) - $(pr.html_url)")
  127. end
  128. end
  129.  
  130. if !isempty(already_backported)
  131. println("The following PRs are already backported, remove the label")
  132. for pr in already_backported
  133. println(" #$(pr.number) - $(pr.html_url)")
  134. end
  135. end
  136.  
  137. if !isempty(failed_backports)
  138. println("The following PRs failed to backport cleanly, manually backport")
  139. for pr in failed_backports
  140. println(" #$(pr.number) - $(pr.html_url) - $(pr.merge_commit_sha)")
  141. end
  142. end
  143.  
  144. if !isempty(successful_backports)
  145. println("The following PRs where backported to this branch")
  146. for pr in successful_backports
  147. println(" #$(pr.number) - $(pr.html_url)")
  148. end
  149. println("Push the updated branch")
  150. end
  151.  
  152. println("Here is a GitHub comment to paste:")
  153.  
  154. if !isempty(successful_backports)
  155. println("Backported PRs:")
  156. for pr in successful_backports
  157. println("- [x] #$(pr.number)")
  158. end
  159. end
  160.  
  161. if !isempty(failed_backports)
  162. println()
  163. println("Needs manual backport:")
  164. for pr in failed_backports
  165. println("- [ ] #$(pr.number)")
  166. end
  167. end
  168. end
  169.  
  170. run(REFRESH_PRS)
Add Comment
Please, Sign In to add comment