Last active
December 19, 2015 06:39
-
-
Save jenikm/5912680 to your computer and use it in GitHub Desktop.
Create pull requests, respond to code reviews, and reassign pull requests directly from commit messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
DEFAULT_ASSIGN_TO = "efrankfurt" | |
REPO = "BayRu/bayru_rails" | |
def get_branch | |
branch = `git branch`.split("\n").map(&:strip).select{|c| c =~ /^\*/}.first.gsub(/[\*\s]*/, '') | |
end | |
def determine_pull_request(client) | |
branch = get_branch | |
client.pull_requests("#{REPO}", "open").select{|p| p.head.ref == branch}.first | |
end | |
def get_client | |
Octokit.netrc = true | |
client = Octokit::Client.new | |
end | |
def get_needed_requires | |
require 'rubygems' | |
require 'octokit' | |
require 'open-uri' | |
end | |
def open_pull(client, message) | |
assign_to = message.match(/pull:(.*)/)[1] | |
if !assign_to.to_s.strip.empty? | |
assign_to.strip! | |
else | |
assign_to = DEFAULT_ASSIGN_TO | |
end | |
branch = get_branch | |
acunote_id = branch.gsub(/\D/, '') | |
acunote_session_id = Netrc.read["bayru.acunote.com"].first | |
url = "https://bayru.acunote.com/projects/20529/tasks/#{acunote_id}" | |
task_page = open(url, "Cookie" => "_acunote_session_id=#{acunote_session_id}").read.gsub("\n", "") | |
task_title = task_page.scan(/task_title_in_header.*#\d+:(.*?)<\/span>/).first.to_s.strip | |
title = "#{ acunote_id } - #{ task_title }" | |
result = client.create_pull_request("#{REPO}", "release", branch, title, url) | |
Octokit.update_issue("#{REPO}", result.number, title, url, {:assignee => assign_to}) | |
puts "pull request open: 'https://github.com/#{REPO}/pull/#{ result.number }'" | |
end | |
def code_review(client, message) | |
response_message = message.match(/rev:([^\|]*)/)[1].to_s.strip | |
if !response_message.empty? | |
response_message << ". " | |
end | |
pull_request = determine_pull_request(client) | |
if pull_request.nil? | |
puts "PUll request does not exist for current branch" | |
return | |
end | |
#Returns comments sorted AS IN UI | |
comments = client.pull_request_comments("#{REPO}", pull_request.number) #.sort_by(&:created_at) | |
#Get rid of out-dated comments | |
comments = comments.select(&:position) | |
comments_grouped = comments.group_by{|c| c.path + c.position.to_s}.to_a.each{ |k,v| v.sort!{|x,y| x.created_at <=> y.created_at} } | |
comments_grouped.sort!{ |x,y| x[1].first.created_at <=> y[1].first.created_at } | |
not_answered = comments_grouped.select{|k,v| v.last.user.login != client.login} | |
unreplied_number = not_answered.first | |
#Write repsonse message DONE and last commit | |
commit_id = message.match(/\S+/)[0] | |
message = "#{response_message}done - #{ commit_id }" | |
result = client.create_pull_request_comment_reply("#{REPO}", pull_request.number, message, unreplied_number.last.last.id) | |
puts "Comment Message: #{ message } AT: 'https://github.com/#{REPO}/pull/#{pull_request.number}#discussion_r#{result.id}'" | |
if not_answered.length == 1 | |
assign_to = comments.select{|c| c.user.login != client.login }.map(&:user).group_by(&:login).to_a.sort_by{|t| t[1].length}.first | |
if assign_to | |
assign_to = assign_to[0] | |
else | |
assign_to = DEFAULT_ASSIGN_TO | |
end | |
puts "You are finished! Reassigning it to: #{ assign_to }" | |
reassign_pull(client, nil, pull_request, assign_to) | |
end | |
end | |
def reassign_pull(client, message, pull_request=nil, assign_to=nil) | |
if pull_request.nil? | |
pull_request = determine_pull_request(client) | |
if pull_request.nil? | |
puts "PUll request does not exist for current branch" | |
return | |
end | |
end | |
assign_to ||= message.match(/done:([^\|]*)/)[1].to_s.strip | |
if assign_to.empty? | |
assign_to = DEFAULT_ASSIGN_TO | |
end | |
Octokit.add_comment("#{REPO}", pull_request.number, "@#{assign_to} - done") | |
Octokit.update_issue("#{REPO}", pull_request.number, pull_request.title, pull_request.body, {:assignee => assign_to}) | |
puts "Pull request reassigned to: #{assign_to}" | |
end | |
message = `git log --pretty=oneline -1` | |
if message =~ /pull:|rev:|done:/ | |
get_needed_requires | |
client = get_client | |
if message =~ /pull:/ | |
open_pull client, message | |
end | |
if message =~ /rev:/ | |
code_review client, message | |
end | |
if message =~ /done:/ | |
reassign_pull client, message | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gem install octokit
create file: ~/.netrc WITH:
machine api.github.com
login 'your login'
password 'your password'
machine bayru.acunote.com
login 'your acunote session id (can be taken from chrome session cookies)'