loadrunner 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 072a2884c588a7518e9599b185cd42e45a97bfee3a6982bd833f72f0ed8655aa
4
- data.tar.gz: b02abbe4a6661866a619dd669db72a9d5223be98db8add50406d6a6bb4fc812a
3
+ metadata.gz: bd9af2dc326ef685b9146a1b8a298abf9ed016561863165fd08e7aa5e4b5e79a
4
+ data.tar.gz: 75fc0ebe16b294f51f0f6aa324170cb9e653315a26b0be8af42f98ee90c2a014
5
5
  SHA512:
6
- metadata.gz: 45f170d7b2738a4191da675451781ce8941a36c73e8ed3da059ea25de26898c51889503b7f43d62dfb68828e0309b0f6d2852d85e706d767c075bf834f29dfd4
7
- data.tar.gz: 0f0b3e40db633939a8b6c3113396baf2ae63d0c86101d98fff041aff9e158575b120c09dba74fbdcd2885064d4b9edd163b5d057f19a586bfb4f8e7913f4c3de
6
+ metadata.gz: b85c8c7f3bf7d681c6e0ebc7adec6a968a78317723c47492782859068414cfe632cd25b75cf26e5f1c69e336c8b3b779718830ace0f98c8784bf235de1592323
7
+ data.tar.gz: d577629537efad165e299e9d9b0853239d89369f7660f455c34d54f78b468f57f5edad85f831c654ed8affe5e4765740db7a7855b323d12b2f709ff6e523a78b
data/README.md CHANGED
@@ -1,14 +1,13 @@
1
- LoadRunner - GitHub Webhook Server and Event Simulator
1
+ Loadrunner - GitHub Webhook Server and Event Simulator
2
2
  ======================================================
3
3
 
4
4
  [![Gem Version](https://badge.fury.io/rb/loadrunner.svg)](https://badge.fury.io/rb/loadrunner)
5
5
  [![Build Status](https://travis-ci.com/DannyBen/loadrunner.svg?branch=master)](https://travis-ci.com/DannyBen/loadrunner)
6
6
  [![Maintainability](https://api.codeclimate.com/v1/badges/f1aae46eaf6365ea2ec7/maintainability)](https://codeclimate.com/github/DannyBen/loadrunner/maintainability)
7
- [![Test Coverage](https://api.codeclimate.com/v1/badges/f1aae46eaf6365ea2ec7/test_coverage)](https://codeclimate.com/github/DannyBen/loadrunner/test_coverage)
8
7
 
9
8
  ---
10
9
 
11
- LoadRunner is a multi-purpose utility for working with GitHub webhooks and
10
+ Loadrunner is a multi-purpose utility for working with GitHub webhooks and
12
11
  statuses.
13
12
 
14
13
  It provides these features:
@@ -35,19 +34,18 @@ $ gem install loadrunner
35
34
  Getting Started
36
35
  --------------------------------------------------
37
36
 
38
- 1. Download the [handlers](handlers) directory from this repository, as an
39
- exmaple. This directory contains several handler examples.
37
+ 1. Download the [hooks](hooks) directory from this repository, as an
38
+ exmaple. This directory contains several hook examples.
40
39
  2. Make sure that all files within that folder are executables.
41
- 3. Start the server (from the `handlers` **parent** directory):
40
+ 3. Start the server (from the `hooks` **parent** directory):
42
41
  `loadrunner server`
43
42
  4. In another terminal, send a sample webhook event:
44
- `loadrunner event localhost:3000/payload myrepo push master
43
+ `loadrunner event localhost:3000 myrepo push master
45
44
 
46
45
  The server should respond with a detailed JSON response, specifying what
47
- handlers were executed (`executed_handlers`) and what handlers *could have
48
- been* executed, if they were defined in the handlers folder
49
- (`matching_handlers`).
50
- ```
46
+ hooks were executed (`executed_hooks`) and what hooks *could have
47
+ been* executed, if they were defined in the hooks folder
48
+ (`matching_hooks`).
51
49
 
52
50
 
53
51
  For more options, see the [documentation][1] or run
@@ -58,43 +56,86 @@ $ loadrunner --help
58
56
 
59
57
 
60
58
 
61
- Building Handlers
59
+ Building Hooks
62
60
  --------------------------------------------------
63
61
 
64
- When running the server, it will look for handlers (executable scripts) in
65
- the `./handlers` directory, using one of these format:
62
+ When running the server, it will look for hooks (executable scripts) in
63
+ the `./hooks` directory, using one of these format:
66
64
 
67
- handlers/global
68
- handlers/<repo name>/global
69
- handlers/<repo name>/<event type>
70
- handlers/<repo name>/<event type>@branch=<branch name>
71
- handlers/<repo name>/<event type>@tag=<branch name>
65
+ hooks/global
66
+ hooks/<repo name>/global
67
+ hooks/<repo name>/<event type>
68
+ hooks/<repo name>/<event type>@branch=<branch name>
69
+ hooks/<repo name>/<event type>@tag=<branch name>
72
70
 
73
71
  For example:
74
72
 
75
- handlers/global
76
- handlers/myrepo/global
77
- handlers/myrepo/push
78
- handlers/myrepo/push@branch=master
79
- handlers/myrepo/push@tag=release
73
+ hooks/global
74
+ hooks/myrepo/global
75
+ hooks/myrepo/push
76
+ hooks/myrepo/push@branch=master
77
+ hooks/myrepo/push@tag=release
80
78
 
81
- When none of the handlers are found, LoadRunner will respond with a list of
82
- handlers it was looking for, so you can use this response to figure out what
79
+ When none of the hooks are found, Loadrunner will respond with a list of
80
+ hooks it was looking for, so you can use this response to figure out what
83
81
  it needs.
84
82
 
85
- The handlers can be written in any language, and should simply be
86
- executables.
83
+ The hooks can be written in any language, and should simply be executables.
87
84
 
88
85
  ### Environment Variables
89
86
 
90
- These environment variables are available to your handlers:
87
+ These environment variables are available to your hooks:
91
88
 
92
89
  - `LOADRUNNER_REPO`
93
90
  - `LOADRUNNER_EVENT`
94
91
  - `LOADRUNNER_BRANCH`
92
+ - `LOADRUNNER_COMMIT`
95
93
  - `LOADRUNNER_REF`
96
94
  - `LOADRUNNER_TAG`
97
- - `LOADRUNNER_PAYLOAD` - the entire JSON string as received from GitHub, or the client.
95
+
96
+
97
+
98
+ Using as a Rack or Sinatra mount
99
+ --------------------------------------------------
100
+
101
+ If you wish to mount the Loadrunner server under another Rack or Sinatra
102
+ application, use the `Loadrunner::Server` as the handler.
103
+
104
+
105
+ ```ruby
106
+ # config.ru
107
+ require "loadrunner/server"
108
+
109
+ map "/github" do
110
+ run Loadrunner::Server
111
+ end
112
+
113
+ run YourOwnApp
114
+ ```
115
+
116
+
117
+
118
+ Sending Pull Request status from Ruby code
119
+ --------------------------------------------------
120
+
121
+ You may use the `Loadrunner::Status` class to update the status of a
122
+ GitHub pull request.
123
+
124
+ First, make sure that your GitHub API access token is set in the environment
125
+ variable `GITHUB_ACCESS_TOKEN`.
126
+
127
+ ```ruby
128
+ require 'loadrunner/status'
129
+
130
+ response = Loadrunner::Status.update repo: 'user/repo',
131
+ sha: 'commit sha string',
132
+ state: :pending, # :pending :success :failure :error
133
+ context: "My Hooks Server",
134
+ description: "Jobs have not started yet",
135
+ url: "http://example.com"
136
+ ```
137
+
138
+ Only `repo`, `sha` and `state` are required, the rest arguments are optional.
98
139
 
99
140
 
100
141
 
@@ -103,16 +144,20 @@ Running with Docker
103
144
 
104
145
  You can run both the server and the client using Docker.
105
146
 
106
- # Server
107
- $ docker run -p3000:3000 dannyben/loadrunner server
147
+ ```shell
148
+ # Server
149
+ $ docker run -p3000:3000 dannyben/loadrunner server
108
150
 
109
- # Client
110
- $ docker run dannyben/loadrunner event http://webhook.server.com/payload repo push
151
+ # Client
152
+ $ docker run dannyben/loadrunner event http://webhook.server.com repo push
153
+ ```
111
154
 
112
155
  If you wish to connect the client to the server you are running through Docker,
113
156
  you can do something like this:
114
157
 
115
- $ docker run --network host dannyben/loadrunner event http://localhost:3000/payload repo push
158
+ ```shell
159
+ $ docker run --network host dannyben/loadrunner event http://localhost:3000 repo push
160
+ ```
116
161
 
117
162
  See also: The [docker-compose file](docker-compose.yml).
118
163
 
@@ -1,9 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'loadrunner'
4
+ require 'loadrunner/command_line'
4
5
 
5
6
  begin
6
- LoadRunner::CommandLine.execute ARGV
7
+ Loadrunner::CommandLine.execute ARGV
7
8
  rescue => e
8
9
  STDERR.puts "#{e.class} - #{e.message}"
9
10
  end
@@ -1,15 +1,5 @@
1
- require 'load_runner/version'
2
-
3
- require 'load_runner/signature_helper'
4
- require 'load_runner/server_helper'
5
- require 'load_runner/git_hub_api'
6
-
7
- require 'load_runner/runner'
8
- require 'load_runner/server_base'
9
- require 'load_runner/server'
10
-
11
- require 'load_runner/client'
12
-
13
- require 'load_runner/command_line'
1
+ require 'loadrunner/server'
2
+ require 'loadrunner/client'
3
+ require 'loadrunner/command_line'
14
4
 
15
5
  require 'byebug' if ENV['BYEBUG']
@@ -1,7 +1,7 @@
1
1
  require 'httparty'
2
2
  require 'uri'
3
3
 
4
- module LoadRunner
4
+ module Loadrunner
5
5
 
6
6
  # Send simulated GitHub events to any webhook server
7
7
  class Client
@@ -12,7 +12,7 @@ module LoadRunner
12
12
  @secret_token = opts[:secret_token]
13
13
  @encoding = opts[:encoding] || :json
14
14
 
15
- base_url = opts[:base_url] || 'http://localhost:3000/payload'
15
+ base_url = opts[:base_url] || 'http://localhost:3000'
16
16
  base_url = "http://#{base_url}" unless base_url =~ /^http/
17
17
 
18
18
  url_parts = URI.parse base_url
@@ -42,7 +42,7 @@ module LoadRunner
42
42
  self.class.post host_path, body: @payload, headers: headers(event)
43
43
  end
44
44
 
45
- private
45
+ private
46
46
 
47
47
  def headers(event=:push)
48
48
  {}.tap do |header|
@@ -1,7 +1,11 @@
1
1
  require 'super_docopt'
2
2
  require 'colsole'
3
+ require 'loadrunner/client'
4
+ require 'loadrunner/server'
5
+ require 'loadrunner/status'
6
+ require 'loadrunner/version'
3
7
 
4
- module LoadRunner
8
+ module Loadrunner
5
9
 
6
10
  # Handles the command line interface
7
11
  class CommandLine < SuperDocopt::Base
@@ -33,20 +37,17 @@ module LoadRunner
33
37
  end
34
38
 
35
39
  def status
36
- api = GitHubAPI.new
37
- opts = {
38
- state: args['STATE'],
39
- target_url: args['--url'],
40
- context: args['--context'],
41
- description: args['--desc']
42
- }
43
-
44
- response = api.status args['REPO'], args['SHA'], opts
40
+ response = Status.update repo: args['REPO'],
41
+ sha: args['SHA'],
42
+ state: args['STATE'],
43
+ context: args['--context'],
44
+ description: args['--desc'],
45
+ url: args['--url']
45
46
 
46
47
  show response
47
48
  end
48
49
 
49
- private
50
+ private
50
51
 
51
52
  def client_opts
52
53
  {
@@ -1,4 +1,4 @@
1
- LoadRunner
1
+ loadrunner
2
2
 
3
3
  Usage:
4
4
  loadrunner server [--port N --bind IP]
@@ -27,7 +27,7 @@ Parameters:
27
27
  URL
28
28
  The URL of the webhook endpoint. This path should responds
29
29
  to POST requests. If you are sending an event to a loadrunner server,
30
- send it to the /payload endpoint (e.g. localhost:3000/payload).
30
+ send it to the root endpoint (e.g. localhost:3000).
31
31
 
32
32
  REPO
33
33
  The name of the repository. This can be either the short name
@@ -50,7 +50,7 @@ Parameters:
50
50
  Commit SHA string.
51
51
 
52
52
  STATE
53
- One of 'success', 'pending', 'error', 'failure'
53
+ One of 'success', 'pending', 'error', 'failure'.
54
54
 
55
55
  Options:
56
56
  --port N
@@ -80,13 +80,13 @@ Environment Variables:
80
80
 
81
81
  Examples:
82
82
  # Simulate push events
83
- loadrunner event localhost:3000/payload myrepo push master
84
- loadrunner event localhost:3000/payload myrepo push branch=master
85
- loadrunner event localhost:3000/payload myrepo push tag=staging --form
86
- loadrunner event localhost:3000/payload myrepo push refs/tags/staging
83
+ loadrunner event localhost:3000 myrepo push master
84
+ loadrunner event localhost:3000 myrepo push branch=master
85
+ loadrunner event localhost:3000 myrepo push tag=staging --form
86
+ loadrunner event localhost:3000 myrepo push refs/tags/staging
87
87
 
88
88
  # Send a payload file
89
- loadrunner payload localhost:3000/payload push payload.json
89
+ loadrunner payload localhost:3000 push payload.json
90
90
 
91
91
  # Start the server
92
92
  loadrunner server
@@ -1,8 +1,8 @@
1
1
  require 'httparty'
2
2
 
3
- module LoadRunner
3
+ module Loadrunner
4
4
  # Communicate with GitHub
5
- class GitHubAPI
5
+ class GithubAPI
6
6
  include HTTParty
7
7
 
8
8
  base_uri 'https://api.github.com'
@@ -17,7 +17,7 @@ module LoadRunner
17
17
  message = {
18
18
  body: {
19
19
  state: (opts[:state] ? opts[:state].to_s : 'pending'),
20
- context: (opts[:context] || 'LoadRunner'),
20
+ context: (opts[:context] || 'Loadrunner'),
21
21
  description: opts[:description],
22
22
  target_url: opts[:target_url]
23
23
  }.to_json
@@ -25,7 +25,7 @@ module LoadRunner
25
25
  self.class.post "/repos/#{repo}/statuses/#{sha}", message.merge(request_options)
26
26
  end
27
27
 
28
- private
28
+ private
29
29
 
30
30
  def request_options
31
31
  {
@@ -36,7 +36,7 @@ module LoadRunner
36
36
  def headers
37
37
  {
38
38
  "Authorization" => "token #{secret_token}",
39
- "User-Agent" => "LoadRunner"
39
+ "User-Agent" => "Loadrunner"
40
40
  }
41
41
  end
42
42
 
@@ -0,0 +1,79 @@
1
+ module Loadrunner
2
+
3
+ # Executes event hooks
4
+ class Runner
5
+ attr_reader :opts
6
+ attr_accessor :response, :hooks_dir
7
+
8
+ def initialize(opts)
9
+ @hooks_dir = 'hooks'
10
+ @opts = opts
11
+ end
12
+
13
+ # Execute all matching hooks based on the input payload. This method
14
+ # populates the `#response` object, and returns true on success.
15
+ def execute
16
+ set_environment_vars
17
+
18
+ @response = opts.dup
19
+ hooks = locate_hooks
20
+ @response[:matching_hooks] = matching_hooks
21
+
22
+ if hooks.empty?
23
+ @response[:error] = "Could not find any hook to process this request. Please implement one of the 'matching_hooks'."
24
+ return false
25
+ else
26
+ execute_all hooks
27
+ @response[:executed_hooks] = hooks
28
+ return true
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ # Find all hooks that fit the payload meta data.
35
+ def locate_hooks
36
+ hooks = []
37
+
38
+ matching_hooks.each do |hook|
39
+ hooks << hook if File.exist? hook
40
+ end
41
+
42
+ hooks
43
+ end
44
+
45
+ # Execute all hooks.
46
+ def execute_all(hooks)
47
+ hooks.each do |hook|
48
+ run_bg hook
49
+ end
50
+ end
51
+
52
+ # Run a command in the background.
53
+ def run_bg(cmd)
54
+ job = fork { exec cmd }
55
+ Process.detach job
56
+ end
57
+
58
+ # Set all payload meta data as environment variables so that the
59
+ # hook can use them.
60
+ def set_environment_vars
61
+ opts.each { |key, value| ENV["LOADRUNNER_#{key.to_s.upcase}"] = value }
62
+ end
63
+
64
+ def matching_hooks
65
+ base = "#{hooks_dir}/#{opts[:repo]}/#{opts[:event]}"
66
+ hooks = [
67
+ "#{hooks_dir}/global",
68
+ "#{hooks_dir}/#{opts[:repo]}/global",
69
+ "#{base}"
70
+ ]
71
+
72
+ hooks.tap do |h|
73
+ h << "#{base}@branch=#{opts[:branch]}" if opts[:branch]
74
+ h << "#{base}@tag=#{opts[:tag]}" if opts[:tag]
75
+ end
76
+ end
77
+
78
+ end
79
+ end
@@ -1,4 +1,9 @@
1
- module LoadRunner
1
+ require 'loadrunner/server_helper'
2
+ require 'loadrunner/server_base'
3
+ require 'loadrunner/signature_helper'
4
+ require 'loadrunner/runner'
5
+
6
+ module Loadrunner
2
7
 
3
8
  # The Sinatra server
4
9
  class Server < ServerBase
@@ -6,10 +11,10 @@ module LoadRunner
6
11
  include SignatureHelper
7
12
 
8
13
  get '/' do
9
- "OK"
14
+ "loadrunner ready"
10
15
  end
11
16
 
12
- post '/payload' do
17
+ post '/' do
13
18
  request.body.rewind
14
19
  payload_body = request.body.read
15
20
 
@@ -23,15 +28,17 @@ module LoadRunner
23
28
  else
24
29
  json_string = URI.decode_www_form(payload_body).to_h["payload"]
25
30
  end
26
- payload = ActiveSupport::HashWithIndifferentAccess.new JSON.parse json_string
31
+ payload = JSON.parse json_string
27
32
 
28
33
  opts = {}
29
- opts[:repo] = payload.dig(:repository, :name)
34
+ opts[:repo] = payload.dig('repository', 'name')
35
+ opts[:commit] = payload['after']
30
36
  opts[:event] = request.env['HTTP_X_GITHUB_EVENT']
31
- opts[:ref] = payload[:ref]
32
- opts[:branch] = payload[:ref] =~ /refs\/heads/ ? payload[:ref].sub('refs/heads/', '') : nil
33
- opts[:tag] = payload[:ref] =~ /refs\/tags/ ? payload[:ref].sub('refs/tags/', '') : nil
34
- opts[:payload] = json_string
37
+ opts[:ref] = payload['ref']
38
+ opts[:branch] = payload['ref'] =~ /refs\/heads/ ? payload['ref'].sub('refs/heads/', '') : nil
39
+ opts[:tag] = payload['ref'] =~ /refs\/tags/ ? payload['ref'].sub('refs/tags/', '') : nil
40
+
41
+ File.write "last_payload.json", json_string if ENV['DEBUG']
35
42
 
36
43
  runner = Runner.new opts
37
44
  success = runner.execute
@@ -1,9 +1,8 @@
1
1
  require 'sinatra/base'
2
2
  require "sinatra/reloader"
3
3
  require "sinatra/json"
4
- require 'active_support/core_ext/hash/indifferent_access'
5
4
 
6
- module LoadRunner
5
+ module Loadrunner
7
6
 
8
7
  # The base class for the sinatra server.
9
8
  # Initialize what we can here, but since there are values that will
@@ -1,4 +1,4 @@
1
- module LoadRunner
1
+ module Loadrunner
2
2
  module ServerHelper
3
3
  def halt_messages
4
4
  {
@@ -1,4 +1,4 @@
1
- module LoadRunner
1
+ module Loadrunner
2
2
  module SignatureHelper
3
3
  def verify_signature(payload_body, signature)
4
4
  return :no_client if secret_token and !signature
@@ -0,0 +1,21 @@
1
+ require 'loadrunner/github_api'
2
+
3
+ module Loadrunner
4
+ # Update GitHub pull request status
5
+ class Status
6
+ class << self
7
+ def update(repo:, sha:, state:, context: nil, description: nil, url: nil)
8
+ api = GithubAPI.new
9
+
10
+ opts = {
11
+ state: state,
12
+ target_url: url,
13
+ context: context,
14
+ description: description
15
+ }
16
+
17
+ api.status repo, sha, opts
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Loadrunner
2
+ VERSION = "0.4.3"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loadrunner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-22 00:00:00.000000000 Z
11
+ date: 2019-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: super_docopt
@@ -72,42 +72,28 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '3.9'
75
+ version: '4.3'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '3.9'
83
- - !ruby/object:Gem::Dependency
84
- name: activesupport
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '5.1'
90
- type: :runtime
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '5.1'
82
+ version: '4.3'
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: colsole
99
85
  requirement: !ruby/object:Gem::Requirement
100
86
  requirements:
101
87
  - - "~>"
102
88
  - !ruby/object:Gem::Version
103
- version: '0.5'
89
+ version: '0.6'
104
90
  type: :runtime
105
91
  prerelease: false
106
92
  version_requirements: !ruby/object:Gem::Requirement
107
93
  requirements:
108
94
  - - "~>"
109
95
  - !ruby/object:Gem::Version
110
- version: '0.5'
96
+ version: '0.6'
111
97
  description: Run your GitHub webhook server and Send simulated github events
112
98
  email: db@dannyben.com
113
99
  executables:
@@ -117,17 +103,18 @@ extra_rdoc_files: []
117
103
  files:
118
104
  - README.md
119
105
  - bin/loadrunner
120
- - lib/load_runner/client.rb
121
- - lib/load_runner/command_line.rb
122
- - lib/load_runner/docopt.txt
123
- - lib/load_runner/git_hub_api.rb
124
- - lib/load_runner/runner.rb
125
- - lib/load_runner/server.rb
126
- - lib/load_runner/server_base.rb
127
- - lib/load_runner/server_helper.rb
128
- - lib/load_runner/signature_helper.rb
129
- - lib/load_runner/version.rb
130
106
  - lib/loadrunner.rb
107
+ - lib/loadrunner/client.rb
108
+ - lib/loadrunner/command_line.rb
109
+ - lib/loadrunner/docopt.txt
110
+ - lib/loadrunner/github_api.rb
111
+ - lib/loadrunner/runner.rb
112
+ - lib/loadrunner/server.rb
113
+ - lib/loadrunner/server_base.rb
114
+ - lib/loadrunner/server_helper.rb
115
+ - lib/loadrunner/signature_helper.rb
116
+ - lib/loadrunner/status.rb
117
+ - lib/loadrunner/version.rb
131
118
  homepage: https://github.com/DannyBen/loadrunner
132
119
  licenses:
133
120
  - MIT
@@ -1,79 +0,0 @@
1
- module LoadRunner
2
-
3
- # Executes event handlers
4
- class Runner
5
- attr_reader :opts
6
- attr_accessor :response, :handlers_dir
7
-
8
- def initialize(opts)
9
- @handlers_dir = 'handlers'
10
- @opts = opts
11
- end
12
-
13
- # Execute all matching handlers based on the input payload. This method
14
- # populates the `#response` object, and returns true on success.
15
- def execute
16
- set_environment_vars
17
-
18
- @response = opts.dup
19
- handlers = locate_handlers
20
- @response[:matching_handlers] = matching_handlers
21
-
22
- if handlers.empty?
23
- @response[:error] = "Could not find any handler to process this webhook. Please implement one of the 'matching_handlers'."
24
- return false
25
- else
26
- execute_all handlers
27
- @response[:executed_handlers] = handlers
28
- return true
29
- end
30
- end
31
-
32
- private
33
-
34
- # Find all handlers that fit the payload meta data.
35
- def locate_handlers
36
- handlers = []
37
-
38
- matching_handlers.each do |handler|
39
- handlers << handler if File.exist? handler
40
- end
41
-
42
- handlers
43
- end
44
-
45
- # Execute all handlers.
46
- def execute_all(handlers)
47
- handlers.each do |handler|
48
- run_bg handler
49
- end
50
- end
51
-
52
- # Run a command in the background.
53
- def run_bg(cmd)
54
- job = fork { exec cmd }
55
- Process.detach job
56
- end
57
-
58
- # Set all payload meta data as environment variables so that the
59
- # handler can use them.
60
- def set_environment_vars
61
- opts.each { |key, value| ENV["LOADRUNNER_#{key.to_s.upcase}"] = value }
62
- end
63
-
64
- def matching_handlers
65
- base = "#{handlers_dir}/#{opts[:repo]}/#{opts[:event]}"
66
- handlers = [
67
- "#{handlers_dir}/global",
68
- "#{handlers_dir}/#{opts[:repo]}/global",
69
- "#{base}"
70
- ]
71
-
72
- handlers.tap do |h|
73
- h << "#{base}@branch=#{opts[:branch]}" if opts[:branch]
74
- h << "#{base}@tag=#{opts[:tag]}" if opts[:tag]
75
- end
76
- end
77
-
78
- end
79
- end
@@ -1,3 +0,0 @@
1
- module LoadRunner
2
- VERSION = "0.4.2"
3
- end