Permalink
Browse files

Add branch setting to YouTrack service

Add a new setting to allow users to define a single branch to watch for
YouTrack commands. If a branch is set, only commits to that branch will
trigger posts to YouTrack. If the branch is not set then all commits
will be processed regardless of branch (current behavior).
  • Loading branch information...
1 parent 42706ed commit 3e1f730308ffca63cea57f03ab1157899b3376bb @davekonopka davekonopka committed Nov 25, 2012
Showing with 43 additions and 3 deletions.
  1. +3 −0 docs/youtrack
  2. +11 −2 services/you_track.rb
  3. +29 −1 test/youtrack_test.rb
View
@@ -21,3 +21,6 @@
- Administrator's account (e.g. 'root' user) credentials to access your YouTrack server.
- Name of a user group in YouTrack, in which YouTrack will search for committer's account.
+
+ - Branch name to track. If a branch is provided, only commits to that branch will trigger
+ YouTrack commands and commits to others will be ignored. If the branch is left empty, commits on any branch will trigger commands.
View
@@ -1,14 +1,23 @@
class Service::YouTrack < Service
- string :base_url, :committers, :username
+ string :base_url, :committers, :username, :branch
password :password
- white_list :base_url, :username, :committers
+ white_list :base_url, :username, :committers, :branch
def receive_push
+ # If branch is defined by user setting, process commands only if commits
+ # are on that branch. If branch is not defined, process regardless of branch.
+ return unless active_branch?
+
http.ssl[:verify] = false
http.url_prefix = data['base_url']
payload['commits'].each { |c| process_commit(c) }
end
+ def active_branch?
+ ref = payload['ref'].to_s
+ data['branch'].to_s.empty? or data['branch'].to_s == ref.split('/').last
+ end
+
def login
@logged_in ||= begin
api_key = data['api_key']
View
@@ -5,7 +5,7 @@ def setup
@stubs = Faraday::Adapter::Test::Stubs.new
end
- def test_push
+ def valid_process_stubs
@stubs.post "/abc/rest/user/login" do |env|
assert_equal 'yt.com', env[:url].host
assert_equal 'u', env[:params]["login"]
@@ -35,6 +35,10 @@ def test_push
assert_equal 'mojombo', env[:params]['runAs']
[200, {}, '']
end
+ end
+
+ def test_push
+ valid_process_stubs
hash = payload
hash['commits'].first['message'].sub! /Case#1/, '#case-1 zomg omg'
@@ -44,6 +48,30 @@ def test_push
svc.receive_push
end
+ def test_branch_match
+ valid_process_stubs
+
+ hash = payload
+ hash['commits'].first['message'].sub! /Case#1/, '#case-1 zomg omg'
+ hash['ref'] = 'refs/heads/master'
+
+ svc = service({'base_url' => 'http://yt.com/abc', 'committers' => 'c',
+ 'username' => 'u', 'password' => 'p', 'branch' => 'master'}, hash)
+ svc.receive_push
+
+ @stubs.verify_stubbed_calls
+ end
+
+ def test_branch_mismatch
+ payload = { 'ref' => 'refs/heads/master' }
+
+ svc = service({'base_url' => '', 'branch' => 'other'}, payload)
+
+ # Missing payload settings would lead to an exception on processing. Processing
+ # should never happen with mismatched branches.
+ assert_nothing_raised { svc.receive_push }
+ end
+
def service(*args)
super Service::YouTrack, *args
end

0 comments on commit 3e1f730

Please sign in to comment.