Class: Amp::LighthouseHook

Inherits:
Lighthouse.class
  • Object
show all
Defined in:
lib/amp/extensions/lighthouse.rb

Overview

LighthouseHook

Makes creating lighthouse-committing hooks extremely easy. It is a delegate to Lighthouse (it requires rubygems)

end

end

Examples:

Amp::LighthouseHook.add_hooks(:commit) do |hook|

hook.token   = 'abcdefghiljklmnopqrstuvxyzabcdefghiljklmnopqrstuvxyz'
hook. = 'youraccount'
hook.project = 'yourprojectname'

hook :commit do |opts|

h = Amp::LighthouseHook.make do |hook|
  hook.token   = 'abcdefghiljklmnopqrstuvxyzabcdefghiljklmnopqrstuvxyz'
  hook. = 'youraccount'
  hook.project = 'yourprojectname'
end
h.call opts

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ LighthouseHook

Initializes a new LighthouseHook. Delegates all unknown methods to the Lighthouse singleton class, yields itself to the (required!) block, and loads the requested project.

Yields:

  • (_self)

Yield Parameters:



97
98
99
100
101
# File 'lib/amp/extensions/lighthouse.rb', line 97

def initialize
  super(Lighthouse)
  yield self
  load_project
end

Instance Attribute Details

#projectObject

Returns the value of attribute project.



91
92
93
# File 'lib/amp/extensions/lighthouse.rb', line 91

def project
  @project
end

Class Method Details

.add_hook(*events) {|hook| ... } ⇒ Object

Takes a block to configure a hook that can submit changesets to a Lighthouse project. The arguments are a list of symbols, which are the events that are automatically hooked into. This provides a terser, though slightly less explicit and less flexible syntax than that used with LighthouseHook.make().

You must provide either a username/password, or an API token to the hook when configuring it. Otherwise, submitting changesets will fail.

Examples:

Creating a commit hook

Amp::LighthouseHook.add_hooks(:commit) do |hook|
  hook.token   = 'abcdefghiljklmnopqrstuvxyzabcdefghiljklmnopqrstuvxyz'
  hook. = 'youraccount'
  hook.project = 'yourprojectname'
end

Parameters:

  • events (Array<Symbol>)

    each argument is an event that is hooked into, such as :commit, :incoming, etc. Currently, only :commit is supported.

Yields:

  • the hook, configurable exactly as the Lighthouse gem is configured. Also, you must specify #project= to set the project to send changesets to.

Yield Parameters:

  • hook

    the new hook object you must configure



75
76
77
78
79
80
81
82
83
# File 'lib/amp/extensions/lighthouse.rb', line 75

def self.add_hook(*events, &block)
  h = self.make(&block)
  
  events.each do |evt|
    Amp::Hook.new(evt) do |opts|
      h[opts]
    end
  end
end

.add_hooks(*args, &block) ⇒ Object

See Also:

  • {add_hook}


87
88
89
# File 'lib/amp/extensions/lighthouse.rb', line 87

def self.add_hooks(*args, &block)
  add_hook(*args, &block)
end

.make {|hook| ... } ⇒ Proc

Takes a block to configure a hook that can submit changesets to a Lighthouse project. The result is a proc, which when run with an Amp::Hook’s options, will submit the changeset.

You must provide either a username/password, or an API token to the hook when configuring it. Otherwise, submitting changesets will fail.

Examples:

Creating a commit hook

hook :commit do |opts|
  h = Amp::LighthouseHook.make do |hook|
    hook.token   = 'abcdefghiljklmnopqrstuvxyzabcdefghiljklmnopqrstuvxyz'
    hook. = 'youraccount'
    hook.project = 'yourprojectname'
  end
  h.call(opts)
end

Yields:

  • the hook, configurable exactly as the Lighthouse gem is configured. Also, you must specify #project= to set the project to send changesets to.

Yield Parameters:

  • hook

    the new hook object you must configure

Returns:

  • (Proc)

    a proc that takes a hook’s options, and when called, will submit the new changeset(s)



50
51
52
# File 'lib/amp/extensions/lighthouse.rb', line 50

def self.make(&block)
  new(&block).block
end

Instance Method Details

#blockProc

Creates a proc that - when executed, with a hook’s options - will send a changeset to Lighthouse.

Returns:

  • (Proc)

    a proc that will send a changeset to lighthouse



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/amp/extensions/lighthouse.rb', line 116

def block
  proc do |opts|
    cs = Lighthouse::Changeset.new(:project_id => @project.id)
    
    ##
    # Each file must be sent as an array: ["A", file] for an added file,
    # ["M", file] for a modified file, ["D", file] for a removed file.
    # Thus, all changes are an array of these arrays, pairing each changed
    # file with a letter.
    temp_arr = []
    opts[:added].each    {|file| temp_arr << ["A", file]} if opts[:added].any?
    opts[:modified].each {|file| temp_arr << ["M", file]} if opts[:modified].any?
    opts[:removed].each  {|file| temp_arr << ["D", file]} if opts[:removed].any?
    cs.changes    = temp_arr.to_yaml
    
    cs.user       = opts[:user]
    cs.updated_at = opts[:date]
    cs.body       = opts[:text]
    cs.revision   = opts[:revision]
    cs.title      = "#{opts[:user]} committed revision #{opts[:revision]}"
    
    result = cs.save
    
    unless result
      Amp::UI::err cs.errors.errors.inspect
    end
  end
end