diff --git a/docs/workmarket b/docs/workmarket new file mode 100644 index 000000000..08739b589 --- /dev/null +++ b/docs/workmarket @@ -0,0 +1,23 @@ +Work Market +=========== + +Use this service hook to add notes to Work Market assignments. + +Install Notes +------------- + +**API token and secret** - One of your company's API tokens. See [Settings > API](http://www.workmarket.com/mmw/api) + +In order to update an assignment, you must include the Work Market assignment number in the commit message, e.g.: + + Fixed a bug [#1234567890] + +Developer Notes +--------------- + +data + - token + - secret + +payload + - refer to docs/github_payload \ No newline at end of file diff --git a/services/workmarket.rb b/services/workmarket.rb new file mode 100644 index 000000000..8aaa4452b --- /dev/null +++ b/services/workmarket.rb @@ -0,0 +1,47 @@ +class Service::Workmarket < Service + + string :token + password :secret + white_list :token + default_events :push + + def auth_token + http.url_prefix = 'https://www.workmarket.com/api/v1' + http.headers['Accept'] = 'application/json' + + r = http_post 'authorization/request', { + :token => data['token'], + :secret => data['secret'], + :type => 'json' + }.to_query + + response = JSON.parse(r.body) + token = response['response']['access_token'] + + raise_config_error 'Invalid API token or secret' if token.empty? + + token + end + + def receive_push + token = auth_token + + commits.each do |commit| + next if commit['message'] =~ /^x / + + commit['message'].match(/\[#(\d{10})\]/) + + next unless $1 + + work_number = $1 + message = "#{commit['id'][0..6]} #{format_commit_message(commit)}" + + http_post 'assignments/add_note', { + :access_token => token, + :id => work_number, + :content => message, + :is_private => false + }.to_query + end + end +end \ No newline at end of file diff --git a/test/workmarket_test.rb b/test/workmarket_test.rb new file mode 100644 index 000000000..26f49fbb2 --- /dev/null +++ b/test/workmarket_test.rb @@ -0,0 +1,25 @@ +require File.expand_path('../helper', __FILE__) + +class WorkmarketTest < Service::TestCase + def setup + @stubs = Faraday::Adapter::Test::Stubs.new + end + + def test_push + @stubs.post "/api/v1/authorization/request" do |env| + assert_equal 'application/json', env[:request_headers]['Content-Type'] + assert_equal 'www.workmarket.com', env[:url].host + [200, {}, ''] + end + + svc = service( + {'token' => 't', 'secret' => 's'}, + payload + ) + svc.receive_push + end + + def service(*args) + super Service::Workmarket, *args + end +end \ No newline at end of file