Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import GitHub
- import Dates
- ############
- # Settings #
- ############
- BACKPORT_LABEL = "backport pending 1.0"
- # where the release branch started
- START_COMMIT = "1dd2f8b397e60c10fdacd85fc62"
- GITHUB_AUTH = ENV["GITHUB_AUTH"]
- # stop looking after encounting PRs opened before this date
- LIMIT_DATE = Dates.Date("2018-08-01")
- # refresh PRs from github
- REFRESH_PRS = false
- ########################################
- # Git executable convenience functions #
- ########################################
- function cherry_picked_commits(start_commit::String)
- commits = String[]
- logg = String(read(`git log $start_commit..HEAD`))
- for match in eachmatch(r"\(cherry picked from commit (.*?)\)", logg)
- push!(commits, match.captures[1])
- end
- return commits
- end
- function try_cherry_pick(hash::String)
- if !success(`git cherry-pick -x $hash`)
- success(`git cherry-pick --abort`)
- return false
- end
- return true
- end
- branch() = String(chomp(String(read(`git rev-parse --abbrev-ref HEAD`))))
- ##################
- # Main functions #
- ##################
- if !@isdefined(__myauth)
- const __myauth = Ref{GitHub.Authorization}()
- end
- function getauth()
- if !isassigned(__myauth)
- __myauth[] = GitHub.authenticate(GITHUB_AUTH)
- end
- return __myauth[]
- end
- function collect_label_prs(backport_label::String)
- # What are good parameters...?
- myparams = Dict("state" => "all", "per_page" => 20, "page" => 1);
- label_prs = []
- i = 1
- print("Collecting PRs...")
- first = true
- local page_data
- while true
- print(".")
- prs, page_data = GitHub.pull_requests("JuliaLang/julia";
- page_limit = 1, auth=getauth(),
- (first ? (params = myparams,) : (start_page = page_data["next"],))...)
- first = false
- for pr in prs
- for label in pr.labels
- if label["name"] == backport_label
- push!(label_prs, pr)
- end
- end
- if pr.created_at < LIMIT_DATE
- return label_prs
- end
- end
- haskey(page_data, "next") || break
- end
- println()
- return label_prs
- end
- if !@isdefined(label_prs)
- const label_prs = Ref{Vector}()
- end
- function run(refresh_prs = false)
- if !isassigned(label_prs) || refresh_prs
- label_prs[] = collect_label_prs(BACKPORT_LABEL)
- end
- already_backported_commits = cherry_picked_commits(START_COMMIT)
- release_branch = branch()
- open_prs = []
- closed_prs = []
- already_backported = []
- backport_candidates = []
- for pr in label_prs[]
- if pr.state != "closed"
- push!(open_prs, pr)
- else
- if pr.merged_at === nothing
- push!(closed_prs, pr)
- elseif pr.merge_commit_sha in already_backported_commits
- push!(already_backported, pr)
- else
- push!(backport_candidates, pr)
- end
- end
- end
- sort!(backport_candidates; by = x -> x.merged_at)
- failed_backports = []
- successful_backports = []
- for pr in backport_candidates
- if try_cherry_pick(pr.merge_commit_sha)
- push!(successful_backports, pr)
- else
- push!(failed_backports, pr)
- end
- end
- # Actions to take:
- if !isempty(closed_prs)
- println("The following PRs are closed but has a backport label, remove the label")
- for pr in closed_prs
- println(" #$(pr.number) - $(pr.html_url)")
- end
- end
- if !isempty(already_backported)
- println("The following PRs are already backported, remove the label")
- for pr in already_backported
- println(" #$(pr.number) - $(pr.html_url)")
- end
- end
- if !isempty(failed_backports)
- println("The following PRs failed to backport cleanly, manually backport")
- for pr in failed_backports
- println(" #$(pr.number) - $(pr.html_url) - $(pr.merge_commit_sha)")
- end
- end
- if !isempty(successful_backports)
- println("The following PRs where backported to this branch")
- for pr in successful_backports
- println(" #$(pr.number) - $(pr.html_url)")
- end
- println("Push the updated branch")
- end
- println("Here is a GitHub comment to paste:")
- if !isempty(successful_backports)
- println("Backported PRs:")
- for pr in successful_backports
- println("- [x] #$(pr.number)")
- end
- end
- if !isempty(failed_backports)
- println()
- println("Needs manual backport:")
- for pr in failed_backports
- println("- [ ] #$(pr.number)")
- end
- end
- end
- run(REFRESH_PRS)
Add Comment
Please, Sign In to add comment