-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch-maid.rb
executable file
·115 lines (101 loc) · 3.15 KB
/
branch-maid.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env ruby
require 'json'
require 'logger'
require 'net/http'
require 'optparse'
require 'uri'
def https_request(uri, request)
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
http.request(request)
end
end
def http_get(api_url, endpoint, token)
uri = URI.join(api_url, endpoint)
request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "token #{token}"
https_request(uri, request)
end
def parse_options
options = {
dry_run: false,
github_api: 'https://api.github.com/',
log_level: Logger::WARN
}
option_parser = OptionParser.new do |parser|
parser.banner = 'Usage: branch-maid.rb [options]'
parser.on('-g', '--github-api URL', 'Default is https://api.github.com') do |url|
url << '/' unless url.end_with?('/')
options[:github_api] = url
end
parser.on('-n', '--dry-run') do
options[:dry_run] = true
end
parser.on('-t', '--token TOKEN', 'Github API token') do |token|
options[:token] = token
end
parser.on('-v', '--verbose') do
options[:log_level] = Logger::INFO
end
end
begin
option_parser.parse!
mandatory = [:token]
missing = mandatory.select { |param| options[param].nil? }
unless missing.empty?
puts "Required options: #{missing.join(', ')}"
puts option_parser
exit
end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
puts $ERROR_INFO.to_s
puts option_parser
exit
end
options
end
def pull_requests(github_api, owner, repo, branch, token, logger)
response = http_get(
github_api,
"repos/#{owner}/#{repo}/pulls?state=closed&sort=updated&direction=desc&head=#{owner}:#{branch}",
token).body
logger.debug("PR details for #{branch}: #{response}")
JSON.parse(response)
end
def merged?(response)
return false if response.empty?
return true if response[0]['merged_at']
false
end
def main
logger = Logger.new(STDOUT)
remote = `git remote get-url origin`
owner = %r{(?<=:)(.*)(?=\/)}.match(remote)
repo = %r{(?<=\/)(.*)(?=\.git$)}.match(remote)
branches = `git branch | grep -v '*'`.split
options = parse_options
logger.level = options[:log_level]
merged_branches = []
branches.each do |branch|
response = pull_requests(options[:github_api], owner, repo, branch, options[:token], logger)
merged = merged?(response)
if merged
merged_branches.push(branch)
logger.info("#{branch}: merged")
elsif response.empty?
logger.info("#{branch}: no closed pull request found")
else
logger.info("#{branch}: not merged")
end
end
merged_branches.each do |branch|
if options[:dry_run]
puts branch
else
puts `git branch -D #{branch}`
end
end
if options[:dry_run] && !merged_branches.empty?
puts "\nRerun without '-n' or '--dry-run' to delete the above branches"
end
end
main if __FILE__ == $PROGRAM_NAME