|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require "bundler/inline" |
| 4 | +gemfile do |
| 5 | + source "https://rubygems.org" |
| 6 | + gem "colorize" |
| 7 | + gem "octokit" |
| 8 | + gem "optimist" |
| 9 | +end |
| 10 | + |
| 11 | +opts = Optimist.options do |
| 12 | + opt :dry_run, "Execute without making changes", :default => false |
| 13 | +end |
| 14 | + |
| 15 | +def close_pull_request(repo, number, dry_run:, **_) |
| 16 | + if dry_run |
| 17 | + puts "** dry_run: github.close_pull_request(#{repo.inspect}, #{number.inspect})".light_black |
| 18 | + else |
| 19 | + github.close_pull_request(repo, number) |
| 20 | + end |
| 21 | +end |
| 22 | + |
| 23 | +def delete_branch(repo, branch, dry_run:, **_) |
| 24 | + if dry_run |
| 25 | + puts "** dry_run: github.delete_branch(#{repo.inspect}, #{branch.inspect})".light_black |
| 26 | + else |
| 27 | + github.delete_branch(repo, branch) |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +def github |
| 32 | + @github ||= Octokit::Client.new( |
| 33 | + :access_token => ENV.fetch("GITHUB_API_TOKEN"), |
| 34 | + :auto_paginate => true |
| 35 | + ) |
| 36 | +end |
| 37 | + |
| 38 | +CRT_REPO = "ManageIQ/manageiq-cross_repo-tests".freeze |
| 39 | + |
| 40 | +github.pull_requests(CRT_REPO, :state => "open").each do |crt_pr| |
| 41 | + target = crt_pr.title.split(" ").last |
| 42 | + next unless target.match?(%r{^ManageIQ/[^#]+#\d+$}) |
| 43 | + |
| 44 | + repo, number = target.split("#") |
| 45 | + pr = github.pull_request(repo, number) |
| 46 | + |
| 47 | + crt_pr_slug = "#{CRT_REPO}##{crt_pr.number}" |
| 48 | + pr_slug = "#{repo}##{number}" |
| 49 | + |
| 50 | + if pr.state == "closed" |
| 51 | + puts "Closing #{crt_pr_slug} since #{pr_slug} is closed" |
| 52 | + close_pull_request(CRT_REPO, crt_pr.number, **opts) |
| 53 | + puts "Deleting branch #{crt_pr.head.ref}" |
| 54 | + delete_branch(CRT_REPO, crt_pr.head.ref, **opts) |
| 55 | + else |
| 56 | + puts "Skipping #{crt_pr_slug} since #{pr_slug} is open" |
| 57 | + end |
| 58 | +end |
0 commit comments