Class: Lita::Handlers::Pullrequests

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/pullrequests.rb

Constant Summary collapse

SCHEDULER =
Rufus::Scheduler.new
REDIS_KEY =
"pullrequests-cron"

Instance Method Summary collapse

Instance Method Details

#fetch_pull_requestsObject



52
53
54
55
56
57
58
59
# File 'lib/lita/handlers/pullrequests.rb', line 52

def fetch_pull_requests
  config.repos.map do |repo|
    url    = "https://api.github.com/repos/#{repo}/issues"
    req    = http.get(url, access_token: config.access_token)
    issues = MultiJson.load(req.body)
    issues.select { |issue| issue["pull_request"] }
  end.flatten
end

#formatted_pull_request_summaryObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/lita/handlers/pullrequests.rb', line 106

def formatted_pull_request_summary
  {
    ":heavy_exclamation_mark: *Pull Requests that need review*:\n"     => pulls_that_need_reviews,
    ":thought_balloon: *Pull Requests that need QA*:\n"                => pulls_that_need_qa,
    ":white_check_mark: *Pull Requests that are ready for merging*:\n" => pulls_that_need_merging
  }.map do |heading, pulls|
    heading + if pulls.any?
      pulls.map do |pr|
        "- _#{pr['title']}_ - #{pr['user']['login']} \n    #{pr['pull_request']['html_url']}"
      end.join("\n\n")
    else
      "_None!_\n"
    end
  end.join("\n\n")
end

#get_random_pr(chat) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lita/handlers/pullrequests.rb', line 61

def get_random_pr(chat)
  pulls_by_repo = pulls_that_need_reviews.group_by { |pr| pr["url"].split("/")[-3] }
  repo  = chat.matches[0][0]

  if config.repos.map { |r| r.split("/")[1] }.include?(repo)
    pulls = pulls_by_repo[repo]
    if pulls
      pr = pulls.sample
      title, user, url = pr["title"], pr["user"]["login"], pr["pull_request"]["html_url"]
      chat.reply "_#{title}_ - #{user} \n    #{url}"
    else
      chat.reply "No pull requests need a review right now!"
    end
  else
    chat.reply "I'm not configured for a repo with that name."
  end
end

#list_all_pull_requests(chat) ⇒ Object



122
123
124
# File 'lib/lita/handlers/pullrequests.rb', line 122

def list_all_pull_requests(chat)
  chat.reply(formatted_pull_request_summary)
end

#pulls_that_need_mergingObject



88
89
90
91
92
93
94
95
# File 'lib/lita/handlers/pullrequests.rb', line 88

def pulls_that_need_merging
  pulls = fetch_pull_requests
  merge_label = config.merge_label || "Ready To Merge"

  pulls.select do |pr|
    pr["labels"] && pr["labels"].any? { |label| label["name"] == config.merge_label }
  end
end

#pulls_that_need_qaObject



97
98
99
100
101
102
103
104
# File 'lib/lita/handlers/pullrequests.rb', line 97

def pulls_that_need_qa
  pulls = fetch_pull_requests
  merge_label = config.qa_label || "QA Needed"

  pulls.select do |pr|
    pr["labels"] && pr["labels"].any? { |label| label["name"] == config.qa_label }
  end
end

#pulls_that_need_reviewsObject



79
80
81
82
83
84
85
86
# File 'lib/lita/handlers/pullrequests.rb', line 79

def pulls_that_need_reviews
  pulls = fetch_pull_requests
  review_label = config.review_label || "Needs Review"

  pulls.select do |pr|
    pr["labels"] && pr["labels"].any? { |label| label["name"] == config.review_label }
  end
end

#remember_reminder(payload) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lita/handlers/pullrequests.rb', line 37

def remember_reminder(payload)
  reminder = redis.hgetall(REDIS_KEY)["reminder-info"]
  if reminder
    info = MultiJson.load(reminder)
    job  = SCHEDULER.cron info["cron_expression"] do |job|
      target = Source.new(user: info["u_id"], room: info["room"])
      robot.send_messages(target, formatted_pull_request_summary)
    end

    log.info "Created cron job: #{info['cron_expression']}."
  else
    log.info "no reminder found."
  end
end

#remove_reminder(chat) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/lita/handlers/pullrequests.rb', line 153

def remove_reminder(chat)
  reminder = redis.hgetall(REDIS_KEY)["reminder-info"]
  if reminder
    info = MultiJson.load(reminder)
    SCHEDULER.unschedule(info["j_id"])
    redis.hdel(REDIS_KEY, "reminder-info")
    chat.reply "okay, I turned off your reminder."
  else
    chat.reply "Your reminder is not set."
  end
end

#set_reminder(chat) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lita/handlers/pullrequests.rb', line 126

def set_reminder(chat)
  input = chat.matches[0][1].split(" ")
  cron_expression = input[0..4].join(" ")
  job = SCHEDULER.cron cron_expression do |job|
    list_all_pull_requests(chat)
  end

  redis.hset(REDIS_KEY, "reminder-info", {
    :cron_expression => cron_expression,
    :j_id => job,
    :u_id => chat.message.source.user.id,
    :room => chat.message.source.room
  }.to_json)

  chat.reply("I will post a pull request summary according to this cron: #{cron_expression}")
end

#show_reminder(chat) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/lita/handlers/pullrequests.rb', line 143

def show_reminder(chat)
  reminder = redis.hgetall(REDIS_KEY)["reminder-info"]
  if reminder
    info = MultiJson.load(reminder)
    chat.reply "I will remind you in channel #{info["room"]} at #{info["cron_expression"]}"
  else
    chat.reply "Your reminder is not set."
  end
end