Class: XMPPDiff

Inherits:
Oxidized::Hook show all
Defined in:
lib/oxidized/hook/xmppdiff.rb

Instance Attribute Summary

Attributes inherited from Oxidized::Hook

#cfg

Instance Method Summary collapse

Methods inherited from Oxidized::Hook

#log

Instance Method Details

#connectObject



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
# File 'lib/oxidized/hook/xmppdiff.rb', line 5

def connect
  @client = Jabber::Client.new(Jabber::JID.new(cfg.jid))

  log "Connecting to XMPP"
  begin
    Timeout.timeout(15) do
      begin
        @client.connect
      rescue StandardError => e
        log "Failed to connect to XMPP: #{e}"
      end
      sleep 1

      log "Authenticating to XMPP"
      @client.auth(cfg.password)
      sleep 1

      log "Connected to XMPP"

      @muc = Jabber::MUC::SimpleMUCClient.new(@client)
      @muc.join(cfg.channel + "/" + cfg.nick)

      log "Joined #{cfg.channel}"
    end
  rescue Timeout::Error
    log "timed out"
    @client = nil
    @muc = nil
  end

  @client.on_exception do
    log "XMPP connection aborted, reconnecting"
    @client = nil
    @muc = nil
    connect
  end
end

#run_hook(ctx) ⇒ Object



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
# File 'lib/oxidized/hook/xmppdiff.rb', line 50

def run_hook(ctx)
  return unless ctx.node
  return unless ctx.event.to_s == "post_store"

  begin
    Timeout.timeout(15) do
      gitoutput = ctx.node.output.new
      diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil

      interesting = diff[:patch].lines.to_a[4..-1].any? do |line|
        ["+", "-"].include?(line[0]) && (not ["#", "!"].include?(line[1]))
      end

      if interesting
        connect if @muc.nil?

        # Maybe connecting failed, so only proceed if we actually joined the MUC
        unless @muc.nil?
          title = "#{ctx.node.name} #{ctx.node.group} #{ctx.node.model.class.name.to_s.downcase}"
          log "Posting diff as snippet to #{cfg.channel}"

          @muc.say(title + "\n\n" + diff[:patch].lines.to_a[4..-1].join)
        end
      end
    end
  rescue Timeout::Error
    log "timed out"
  end
end

#validate_cfg!Object

Raises:

  • (KeyError)


43
44
45
46
47
48
# File 'lib/oxidized/hook/xmppdiff.rb', line 43

def validate_cfg!
  raise KeyError, 'hook.jid is required' unless cfg.has_key?('jid')
  raise KeyError, 'hook.password is required' unless cfg.has_key?('password')
  raise KeyError, 'hook.channel is required' unless cfg.has_key?('channel')
  raise KeyError, 'hook.nick is required' unless cfg.has_key?('nick')
end