codeclimate-services 1.9.3 → 1.9.4

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
  SHA1:
3
- metadata.gz: ed9a5c4b56c617ffde12f689f9f42633735c6d93
4
- data.tar.gz: 623c26772ad31e593ab6f75f15f29145a065bc24
3
+ metadata.gz: a5909cb3641c879372c0c98ea0e1535d948a1273
4
+ data.tar.gz: e89f483cd5935228006a8069ad637c363d04e654
5
5
  SHA512:
6
- metadata.gz: 9f159c9c3527889b527fdf1d2172adb0743e83ddb937e69496b7d1b639d405bffd3365aea93a8e1abf527102a73c2a7f5092c5cd3c059194641ca910789dc964
7
- data.tar.gz: 0a51db3bc18c327aa409b4fb3f2b1d42c20156c35366fd1469e5aa3cfab583a56d098e8159a218d88779261657017ae2ff2f4bbcf31695ac19517eee9693cf0e
6
+ metadata.gz: a08685ea16c0972db7bbf463e092fa4d7b92e17c9637c2a01261383932206d77de6801b393bb435ae6812bab37dd4e853cb6db5c71e5d1f4011dc00090b21f4d
7
+ data.tar.gz: 9981e7535f62faf0eb3e3bd5879e0242777cbaaed86e08151f5d8a820bd3009518927b4ded6990a6e5ab240f50b1a973414316fa0fef49ef0745930ceb564440
@@ -1,6 +1,7 @@
1
1
  class CC::Service::Asana < CC::Service
2
2
  class Config < CC::Service::Config
3
- attribute :api_key, Axiom::Types::String, label: "API key"
3
+ attribute :personal_access_token, Axiom::Types::String, label: "Personal Access Token"
4
+ attribute :api_key, Axiom::Types::String, label: "API key (Deprecated)"
4
5
 
5
6
  attribute :workspace_id, Axiom::Types::String, label: "Workspace ID"
6
7
 
@@ -10,8 +11,14 @@ class CC::Service::Asana < CC::Service
10
11
  attribute :assignee, Axiom::Types::String, label: "Assignee",
11
12
  description: "Assignee email address (optional)"
12
13
 
13
- validates :api_key, presence: true
14
+ validate :authorization_provided
14
15
  validates :workspace_id, presence: true
16
+
17
+ def authorization_provided
18
+ if api_key.blank? && personal_access_token.blank?
19
+ errors.add(:personal_access_token, "can't be blank")
20
+ end
21
+ end
15
22
  end
16
23
 
17
24
  ENDPOINT = "https://app.asana.com/api/1.0/tasks".freeze
@@ -82,6 +89,10 @@ class CC::Service::Asana < CC::Service
82
89
  end
83
90
 
84
91
  def authenticate_http
85
- http.basic_auth(config.api_key, "")
92
+ if config.personal_access_token.present?
93
+ http.headers["Authorization"] = "Bearer #{config.personal_access_token}"
94
+ else
95
+ http.basic_auth(config.api_key, "")
96
+ end
86
97
  end
87
98
  end
@@ -1,5 +1,5 @@
1
1
  module CC
2
2
  module Services
3
- VERSION = "1.9.3".freeze
3
+ VERSION = "1.9.4".freeze
4
4
  end
5
5
  end
@@ -1,63 +1,96 @@
1
1
  describe CC::Service::Asana, type: :service do
2
- it "quality" do
3
- assert_asana_receives(
4
- event(:quality, to: "D", from: "C"),
5
- "Refactor User from a D on Code Climate - https://codeclimate.com/repos/1/feed",
6
- )
7
- end
2
+ it "requires an authorization key or token, and nudges users toward personal_access_token" do
3
+ config = CC::Service::Asana::Config.new(workspace_id: '1234')
4
+ expect(config).to_not be_valid
5
+ expect(config.errors[:personal_access_token]).to eq ["can't be blank"]
8
6
 
9
- it "vulnerability" do
10
- assert_asana_receives(
11
- event(:vulnerability, vulnerabilities: [{
12
- "warning_type" => "critical",
13
- "location" => "app/user.rb line 120",
14
- }]),
15
- "New critical issue found in app/user.rb line 120 - https://codeclimate.com/repos/1/feed",
16
- )
17
- end
7
+ config.api_key = "foo"
8
+ expect(config).to be_valid
18
9
 
19
- it "issue" do
20
- payload = {
21
- issue: {
22
- "check_name" => "Style/LongLine",
23
- "description" => "Line is too long [1000/80]",
24
- },
25
- constant_name: "foo.rb",
26
- details_url: "http://example.com/repos/id/foo.rb#issue_123",
27
- }
28
-
29
- assert_asana_receives(
30
- event(:issue, payload),
31
- "Fix \"Style/LongLine\" issue in foo.rb",
32
- "Line is too long [1000/80]\n\nhttp://example.com/repos/id/foo.rb#issue_123",
33
- )
10
+ config.api_key = nil
11
+ config.personal_access_token = "bar"
12
+ expect(config).to be_valid
34
13
  end
35
14
 
36
- it "successful post" do
37
- http_stubs.post "/api/1.0/tasks" do |_env|
38
- [200, {}, '{"data":{"id":"2"}}']
15
+ shared_examples "Asana integration" do |authorization|
16
+ it "creates a ticket for quality changes" do
17
+ assert_asana_receives(
18
+ event(:quality, to: "D", from: "C"),
19
+ "Refactor User from a D on Code Climate - https://codeclimate.com/repos/1/feed",
20
+ authorization,
21
+ )
39
22
  end
40
23
 
41
- response = receive_event
24
+ it "creates a ticket for vulnerability changes" do
25
+ assert_asana_receives(
26
+ event(:vulnerability, vulnerabilities: [{
27
+ "warning_type" => "critical",
28
+ "location" => "app/user.rb line 120",
29
+ }]),
30
+ "New critical issue found in app/user.rb line 120 - https://codeclimate.com/repos/1/feed",
31
+ authorization,
32
+ )
33
+ end
42
34
 
43
- expect(response[:id]).to eq("2")
44
- expect(response[:url]).to eq("https://app.asana.com/0/1/2")
45
- end
35
+ it "creates a ticket for a new issue" do
36
+ payload = {
37
+ issue: {
38
+ "check_name" => "Style/LongLine",
39
+ "description" => "Line is too long [1000/80]",
40
+ },
41
+ constant_name: "foo.rb",
42
+ details_url: "http://example.com/repos/id/foo.rb#issue_123",
43
+ }
46
44
 
47
- it "receive test" do
48
- http_stubs.post "/api/1.0/tasks" do |_env|
49
- [200, {}, '{"data":{"id":"4"}}']
45
+ assert_asana_receives(
46
+ event(:issue, payload),
47
+ "Fix \"Style/LongLine\" issue in foo.rb",
48
+ authorization,
49
+ "Line is too long [1000/80]\n\nhttp://example.com/repos/id/foo.rb#issue_123",
50
+ )
50
51
  end
51
52
 
52
- response = receive_event(name: "test")
53
+ it "can make a successful POST request" do
54
+ http_stubs.post "/api/1.0/tasks" do |_env|
55
+ [200, {}, '{"data":{"id":"2"}}']
56
+ end
57
+
58
+ response = receive_event(authorization)
53
59
 
54
- expect(response[:message]).to eq("Ticket <a href='https://app.asana.com/0/1/4'>4</a> created.")
60
+ expect(response[:id]).to eq("2")
61
+ expect(response[:url]).to eq("https://app.asana.com/0/1/2")
62
+ end
63
+
64
+ it "can make a test request" do
65
+ http_stubs.post "/api/1.0/tasks" do |_env|
66
+ [200, {}, '{"data":{"id":"4"}}']
67
+ end
68
+
69
+ response = receive_event(authorization, name: "test")
70
+
71
+ expect(response[:message]).to eq("Ticket <a href='https://app.asana.com/0/1/4'>4</a> created.")
72
+ end
55
73
  end
56
74
 
75
+ it_behaves_like "Asana integration", :api_key
76
+ it_behaves_like "Asana integration", :personal_access_token
77
+ it_behaves_like "Asana integration", :both
78
+
57
79
  private
58
80
 
59
- def assert_asana_receives(event_data, name, notes = "")
81
+ def assert_asana_receives(event_data, name, authorization, notes = "")
60
82
  http_stubs.post "/api/1.0/tasks" do |env|
83
+ case authorization
84
+ when :api_key
85
+ expect(env[:request_headers]["Authorization"]).to include("Basic")
86
+ when :personal_access_token
87
+ expect(env[:request_headers]["Authorization"]).to eq("Bearer def456")
88
+ when :both
89
+ # prefer the personal access token
90
+ expect(env[:request_headers]["Authorization"]).to eq("Bearer def456")
91
+ else
92
+ raise ArgumentError
93
+ end
61
94
  body = JSON.parse(env[:body])
62
95
  data = body["data"]
63
96
 
@@ -70,13 +103,24 @@ describe CC::Service::Asana, type: :service do
70
103
  [200, {}, '{"data":{"id":4}}']
71
104
  end
72
105
 
73
- receive_event(event_data)
106
+ receive_event(authorization, event_data)
74
107
  end
75
108
 
76
- def receive_event(event_data = nil)
109
+ def receive_event(authorization, event_data = nil)
110
+ service_configuration = { workspace_id: "1", project_id: "2", assignee: "jim@asana.com" }
111
+ case authorization
112
+ when :api_key
113
+ service_configuration[:api_key] = "abc123"
114
+ when :personal_access_token
115
+ service_configuration[:personal_access_token] = "def456"
116
+ when :both
117
+ service_configuration[:api_key] = "abc123"
118
+ service_configuration[:personal_access_token] = "def456"
119
+ else raise ArgumentError
120
+ end
77
121
  service_receive(
78
122
  CC::Service::Asana,
79
- { api_key: "abc123", workspace_id: "1", project_id: "2", assignee: "jim@asana.com" },
123
+ service_configuration,
80
124
  event_data || event(:quality, to: "D", from: "C"),
81
125
  )
82
126
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codeclimate-services
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.3
4
+ version: 1.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Helmkamp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-06 00:00:00.000000000 Z
11
+ date: 2016-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday