Class: Lita::Handlers::GithubCommits

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

Constant Summary collapse

REDIS_KEY_PREFIX =
"GH_COMMTIS:"
SHA_ABBREV_LENGTH =

note the regex below needs to match this constant

7

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_config(config) ⇒ Object

todo: change this to lita4 config docs.lita.io/releases/4/ config :repos, type: Hash, default: {} config :remember_commits_for, type: Integer, default: 1 config :github_webhook_secret, type: String, default: nil



15
16
17
18
19
# File 'lib/lita/handlers/github_commits.rb', line 15

def self.default_config(config)
  config.repos = {}
  config.remember_commits_for = 1
  config.github_webhook_secret = nil
end

.install_commandsObject



28
29
30
31
32
# File 'lib/lita/handlers/github_commits.rb', line 28

def self.install_commands
  route(/commit\/([a-f0-9]{7,})\s?/i, :check_for_commit, command: false,
        help: { "...commit/<SHA1>..." => "Displays the details of commit SHA1 if known (requires at least #{SHA_ABBREV_LENGTH} digits of the SHA)."}
  )
end

.install_routesObject



21
22
23
# File 'lib/lita/handlers/github_commits.rb', line 21

def self.install_routes()
  http.post "/github-commits", :receive
end

Instance Method Details

#check_for_commit(response) ⇒ Object



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

def check_for_commit(response)
  sha = abbrev_sha(response.match_data[1])
  if sha.nil? || sha.empty?
    #this shouldn't match regex
    response.reply("[GitHub] I need at least #{SHA_ABBREV_LENGTH} characters of the commit SHA") if response.message.command?
  elsif sha.size <= 6 && response.message.command?
    #this shouldn't match regex
    response.reply("[GitHub] Can you be more precise?")
  elsif  (commit=redis.get(REDIS_KEY_PREFIX + sha))
    response.reply(render_template("commit_details", commit: parse_payload(commit)))
  elsif response.message.command?
    response.reply("[GitHub] Sorry Boss, I can't find that commit")
  #else
  #  response.reply("I got nothing to say about #{sha}.")
  end
end

#receive(request, response) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lita/handlers/github_commits.rb', line 52

def receive(request, response)
  event_type = request.env['HTTP_X_GITHUB_EVENT'] || 'unknown'
  Lita.logger.debug("Received GitHub #{event_type} event") rescue ""
  if !valid_signature(request)
    response.status = 404
  elsif event_type == "push"
    payload = parse_payload(request.body) or return
    store_commits(payload)
    repo = get_repo(payload)
    notify_rooms(repo, payload)
  elsif event_type == "ping"
    response.status = 200
    response.write "Working!"
  else
    response.status = 404
  end
end