-
Notifications
You must be signed in to change notification settings - Fork 1
/
rebase_prs_and_development.rb
executable file
·79 lines (59 loc) · 1.85 KB
/
rebase_prs_and_development.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
#!/usr/bin/env ruby
require 'json'
require 'faraday'
CONNECTION = Faraday.new(url: 'https://api.github.com/repos/redmine-patch-meetup/redmine-dev-mirror') do |conn|
conn.response :raise_error
conn.adapter Faraday.default_adapter
end
TOKEN = File.read('.token')
def rebase_local_development_branch
system 'git checkout develop'
system 'git rebase master'
end
def get_repo_resource(resource)
response = CONNECTION.get resource
JSON.parse response.body
end
def post_repo_resource(resource, body)
response = CONNECTION.post resource,
body.to_json,
"Content-Type" => "application/json",
"Authorization" => "token #{TOKEN}"
JSON.parse response.body
end
REBASE_NEEDED_LABEL = 'manual rebase needed'
def has_rebase_needed_label?(pr)
pr['labels'].any? { |label| label['name'] == REBASE_NEEDED_LABEL }
end
def add_rebase_needed_label(pr)
post_repo_resource("issues/#{pr['number']}/labels", { 'labels': [REBASE_NEEDED_LABEL] })
end
def rebase_onto_new_development_branch(pr)
branch = pr['head']['ref']
base_sha = pr['base']['sha']
system "git checkout #{branch}"
system "git rebase #{base_sha} --onto develop && git push -f"
end
def rebase_pull_requests
get_repo_resource('pulls').each do |pr|
if has_rebase_needed_label?(pr)
puts "Skip rebase of \"#{pr['title']}\" - Rebase needed"
next
end
rebase_successful = rebase_onto_new_development_branch pr
next if rebase_successful
puts "Rebase of \"#{pr['title']}\" failed - Adding label"
add_rebase_needed_label(pr)
end
end
def push_new_development_branch
system 'git checkout develop'
system 'git push -f origin develop'
end
def main
Dir.chdir('redmine-dev-mirror')
rebase_local_development_branch
rebase_pull_requests
push_new_development_branch
end
main if __FILE__ == $0