zk-jira-ruby 2.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.travis.yml +9 -0
- data/Gemfile +14 -0
- data/Guardfile +14 -0
- data/LICENSE +19 -0
- data/README.md +427 -0
- data/Rakefile +31 -0
- data/example.rb +232 -0
- data/http-basic-example.rb +113 -0
- data/jira-ruby.gemspec +35 -0
- data/lib/jira/base.rb +525 -0
- data/lib/jira/base_factory.rb +46 -0
- data/lib/jira/client.rb +315 -0
- data/lib/jira/has_many_proxy.rb +42 -0
- data/lib/jira/http_client.rb +112 -0
- data/lib/jira/http_error.rb +14 -0
- data/lib/jira/jwt_client.rb +67 -0
- data/lib/jira/oauth_client.rb +114 -0
- data/lib/jira/railtie.rb +10 -0
- data/lib/jira/request_client.rb +31 -0
- data/lib/jira/resource/agile.rb +79 -0
- data/lib/jira/resource/applinks.rb +39 -0
- data/lib/jira/resource/attachment.rb +50 -0
- data/lib/jira/resource/board.rb +91 -0
- data/lib/jira/resource/board_configuration.rb +9 -0
- data/lib/jira/resource/comment.rb +12 -0
- data/lib/jira/resource/component.rb +8 -0
- data/lib/jira/resource/createmeta.rb +44 -0
- data/lib/jira/resource/field.rb +83 -0
- data/lib/jira/resource/filter.rb +15 -0
- data/lib/jira/resource/issue.rb +141 -0
- data/lib/jira/resource/issuelink.rb +20 -0
- data/lib/jira/resource/issuelinktype.rb +14 -0
- data/lib/jira/resource/issuetype.rb +8 -0
- data/lib/jira/resource/priority.rb +8 -0
- data/lib/jira/resource/project.rb +41 -0
- data/lib/jira/resource/rapidview.rb +67 -0
- data/lib/jira/resource/remotelink.rb +26 -0
- data/lib/jira/resource/resolution.rb +8 -0
- data/lib/jira/resource/serverinfo.rb +18 -0
- data/lib/jira/resource/sprint.rb +105 -0
- data/lib/jira/resource/sprint_report.rb +8 -0
- data/lib/jira/resource/status.rb +8 -0
- data/lib/jira/resource/transition.rb +29 -0
- data/lib/jira/resource/user.rb +30 -0
- data/lib/jira/resource/version.rb +66 -0
- data/lib/jira/resource/watcher.rb +35 -0
- data/lib/jira/resource/webhook.rb +37 -0
- data/lib/jira/resource/worklog.rb +14 -0
- data/lib/jira/tasks.rb +0 -0
- data/lib/jira/version.rb +3 -0
- data/lib/jira-ruby.rb +49 -0
- data/lib/tasks/generate.rake +18 -0
- data/spec/integration/attachment_spec.rb +32 -0
- data/spec/integration/comment_spec.rb +52 -0
- data/spec/integration/component_spec.rb +39 -0
- data/spec/integration/field_spec.rb +32 -0
- data/spec/integration/issue_spec.rb +93 -0
- data/spec/integration/issuelinktype_spec.rb +26 -0
- data/spec/integration/issuetype_spec.rb +24 -0
- data/spec/integration/priority_spec.rb +24 -0
- data/spec/integration/project_spec.rb +49 -0
- data/spec/integration/rapidview_spec.rb +74 -0
- data/spec/integration/resolution_spec.rb +26 -0
- data/spec/integration/status_spec.rb +24 -0
- data/spec/integration/transition_spec.rb +49 -0
- data/spec/integration/user_spec.rb +41 -0
- data/spec/integration/version_spec.rb +39 -0
- data/spec/integration/watcher_spec.rb +62 -0
- data/spec/integration/webhook.rb +25 -0
- data/spec/integration/worklog_spec.rb +51 -0
- data/spec/jira/base_factory_spec.rb +45 -0
- data/spec/jira/base_spec.rb +598 -0
- data/spec/jira/client_spec.rb +291 -0
- data/spec/jira/has_many_proxy_spec.rb +46 -0
- data/spec/jira/http_client_spec.rb +328 -0
- data/spec/jira/http_error_spec.rb +24 -0
- data/spec/jira/jwt_uri_builder_spec.rb +59 -0
- data/spec/jira/oauth_client_spec.rb +162 -0
- data/spec/jira/request_client_spec.rb +41 -0
- data/spec/jira/resource/agile_spec.rb +135 -0
- data/spec/jira/resource/attachment_spec.rb +138 -0
- data/spec/jira/resource/board_spec.rb +224 -0
- data/spec/jira/resource/createmeta_spec.rb +258 -0
- data/spec/jira/resource/field_spec.rb +85 -0
- data/spec/jira/resource/filter_spec.rb +97 -0
- data/spec/jira/resource/issue_spec.rb +227 -0
- data/spec/jira/resource/issuelink_spec.rb +14 -0
- data/spec/jira/resource/project_factory_spec.rb +11 -0
- data/spec/jira/resource/project_spec.rb +123 -0
- data/spec/jira/resource/sprint_spec.rb +90 -0
- data/spec/jira/resource/user_factory_spec.rb +31 -0
- data/spec/jira/resource/worklog_spec.rb +22 -0
- data/spec/mock_responses/board/1.json +33 -0
- data/spec/mock_responses/board/1_issues.json +62 -0
- data/spec/mock_responses/component/10000.invalid.put.json +5 -0
- data/spec/mock_responses/component/10000.json +39 -0
- data/spec/mock_responses/component/10000.put.json +39 -0
- data/spec/mock_responses/component.post.json +28 -0
- data/spec/mock_responses/empty_issues.json +8 -0
- data/spec/mock_responses/field/1.json +15 -0
- data/spec/mock_responses/field.json +32 -0
- data/spec/mock_responses/issue/10002/attachments/10000.json +20 -0
- data/spec/mock_responses/issue/10002/comment/10000.json +29 -0
- data/spec/mock_responses/issue/10002/comment/10000.put.json +29 -0
- data/spec/mock_responses/issue/10002/comment.json +65 -0
- data/spec/mock_responses/issue/10002/comment.post.json +29 -0
- data/spec/mock_responses/issue/10002/transitions.json +49 -0
- data/spec/mock_responses/issue/10002/transitions.post.json +1 -0
- data/spec/mock_responses/issue/10002/watchers.json +13 -0
- data/spec/mock_responses/issue/10002/worklog/10000.json +31 -0
- data/spec/mock_responses/issue/10002/worklog/10000.put.json +30 -0
- data/spec/mock_responses/issue/10002/worklog.json +98 -0
- data/spec/mock_responses/issue/10002/worklog.post.json +30 -0
- data/spec/mock_responses/issue/10002.invalid.put.json +6 -0
- data/spec/mock_responses/issue/10002.json +126 -0
- data/spec/mock_responses/issue/10002.put.missing_field_update.json +6 -0
- data/spec/mock_responses/issue.json +1108 -0
- data/spec/mock_responses/issue.post.json +5 -0
- data/spec/mock_responses/issueLinkType/10000.json +7 -0
- data/spec/mock_responses/issueLinkType.json +25 -0
- data/spec/mock_responses/issuetype/5.json +8 -0
- data/spec/mock_responses/issuetype.json +42 -0
- data/spec/mock_responses/jira/rest/webhooks/1.0/webhook/2.json +11 -0
- data/spec/mock_responses/jira/rest/webhooks/1.0/webhook.json +11 -0
- data/spec/mock_responses/priority/1.json +8 -0
- data/spec/mock_responses/priority.json +42 -0
- data/spec/mock_responses/project/SAMPLEPROJECT.issues.json +1108 -0
- data/spec/mock_responses/project/SAMPLEPROJECT.json +84 -0
- data/spec/mock_responses/project.json +12 -0
- data/spec/mock_responses/rapidview/SAMPLEPROJECT.issues.full.json +276 -0
- data/spec/mock_responses/rapidview/SAMPLEPROJECT.issues.json +111 -0
- data/spec/mock_responses/rapidview/SAMPLEPROJECT.json +6 -0
- data/spec/mock_responses/rapidview.json +10 -0
- data/spec/mock_responses/resolution/1.json +7 -0
- data/spec/mock_responses/resolution.json +15 -0
- data/spec/mock_responses/sprint/1_issues.json +125 -0
- data/spec/mock_responses/status/1.json +7 -0
- data/spec/mock_responses/status.json +37 -0
- data/spec/mock_responses/user_username=admin.json +17 -0
- data/spec/mock_responses/version/10000.invalid.put.json +5 -0
- data/spec/mock_responses/version/10000.json +11 -0
- data/spec/mock_responses/version/10000.put.json +7 -0
- data/spec/mock_responses/version.post.json +7 -0
- data/spec/mock_responses/webhook/webhook.json +11 -0
- data/spec/mock_responses/webhook.json +11 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/support/clients_helper.rb +16 -0
- data/spec/support/matchers/have_attributes.rb +11 -0
- data/spec/support/matchers/have_many.rb +9 -0
- data/spec/support/matchers/have_one.rb +5 -0
- data/spec/support/shared_examples/integration.rb +177 -0
- metadata +491 -0
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'rdoc/task'
|
6
|
+
|
7
|
+
Dir.glob('lib/tasks/*.rake').each { |r| import r }
|
8
|
+
|
9
|
+
task default: [:test]
|
10
|
+
|
11
|
+
task test: %i[prepare spec]
|
12
|
+
|
13
|
+
desc 'Prepare and run rspec tests'
|
14
|
+
task :prepare do
|
15
|
+
rsa_key = File.expand_path('rsakey.pem')
|
16
|
+
unless File.exist?(rsa_key)
|
17
|
+
raise 'rsakey.pem does not exist, tests will fail. Run `rake jira:generate_public_cert` first'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Run RSpec tests'
|
22
|
+
# RSpec::Core::RakeTask.new(:spec)
|
23
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
24
|
+
task.rspec_opts = ['--color', '--format', 'doc']
|
25
|
+
end
|
26
|
+
|
27
|
+
Rake::RDocTask.new(:doc) do |rd|
|
28
|
+
rd.main = 'README.rdoc'
|
29
|
+
rd.rdoc_dir = 'doc'
|
30
|
+
rd.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
|
31
|
+
end
|
data/example.rb
ADDED
@@ -0,0 +1,232 @@
|
|
1
|
+
require 'pp'
|
2
|
+
require './lib/zk-jira-ruby'
|
3
|
+
|
4
|
+
CONSUMER_KEY = 'test'
|
5
|
+
SITE = 'https://test.jira.com'
|
6
|
+
|
7
|
+
options = {
|
8
|
+
:private_key_file => "rsakey.pem",
|
9
|
+
:context_path => '',
|
10
|
+
:consumer_key => CONSUMER_KEY,
|
11
|
+
:site => SITE
|
12
|
+
}
|
13
|
+
|
14
|
+
client = JIRA::Client.new(options)
|
15
|
+
|
16
|
+
if ARGV.length == 0
|
17
|
+
# If not passed any command line arguments, open a browser and prompt the
|
18
|
+
# user for the OAuth verifier.
|
19
|
+
request_token = client.request_token
|
20
|
+
puts "Opening #{request_token.authorize_url}"
|
21
|
+
system "open #{request_token.authorize_url}"
|
22
|
+
|
23
|
+
puts "Enter the oauth_verifier: "
|
24
|
+
oauth_verifier = gets.strip
|
25
|
+
|
26
|
+
access_token = client.init_access_token(:oauth_verifier => oauth_verifier)
|
27
|
+
puts "Access token: #{access_token.token} secret: #{access_token.secret}"
|
28
|
+
elsif ARGV.length == 2
|
29
|
+
# Otherwise assume the arguments are a previous access token and secret.
|
30
|
+
access_token = client.set_access_token(ARGV[0], ARGV[1])
|
31
|
+
else
|
32
|
+
# Script must be passed 0 or 2 arguments
|
33
|
+
raise "Usage: #{$0} [ token secret ]"
|
34
|
+
end
|
35
|
+
|
36
|
+
# Show all projects
|
37
|
+
projects = client.Project.all
|
38
|
+
|
39
|
+
projects.each do |project|
|
40
|
+
puts "Project -> key: #{project.key}, name: #{project.name}"
|
41
|
+
end
|
42
|
+
issue = client.Issue.find('SAMPLEPROJECT-1')
|
43
|
+
pp issue
|
44
|
+
|
45
|
+
# # Handling fields by name, rather than by id
|
46
|
+
# # ------------------------------------------
|
47
|
+
# Cache the Field list from the server
|
48
|
+
client.Field.map_fields
|
49
|
+
# This allows use of friendlier names for custom fields
|
50
|
+
# Say that 'Special Field' is customfield_12345
|
51
|
+
# It becomes mapped to Special_Field which is usable as a method call
|
52
|
+
#
|
53
|
+
# Say that there is a second 'Special Field' is customfield_54321
|
54
|
+
# Names are deduplicated so the second 'Special Field' becomes Special_Field_54321
|
55
|
+
#
|
56
|
+
# Names are massaged to get rid of special characters, and spaces
|
57
|
+
# So 'Special & @ Field' becomes Special_____Field - not perfect, but usable
|
58
|
+
old_way = issue.customfield_12345
|
59
|
+
new_way = issue.Special_Field
|
60
|
+
(old_way == new_way) && puts 'much easier'
|
61
|
+
#
|
62
|
+
# You can also specify fields to be returned in the response
|
63
|
+
# This is especially useful in regards to shortening JQL query response times if performance becomes an issue
|
64
|
+
client.Issue.jql(a_normal_jql_search, fields:[:description, :summary, :Special_field, :created])
|
65
|
+
# Or you could always do it the old way - if you can remember the custom field numbers...
|
66
|
+
client.Issue.jql(a_normal_jql_search, fields:[:description, :summary, :customfield_1234, :created])
|
67
|
+
# You can also specify the maximum number of results to be returned in the response, i.e. 500
|
68
|
+
client.Issue.jql(a_normal_jql_search, max_results: 500)
|
69
|
+
|
70
|
+
# # Find a specific project by key
|
71
|
+
# # ------------------------------
|
72
|
+
# project = client.Project.find('SAMPLEPROJECT')
|
73
|
+
# pp project
|
74
|
+
# project.issues.each do |issue|
|
75
|
+
# puts "#{issue.id} - #{issue.fields['summary']}"
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# # List all Issues
|
79
|
+
# # ---------------
|
80
|
+
# client.Issue.all.each do |issue|
|
81
|
+
# puts "#{issue.id} - #{issue.fields['summary']}"
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# # List issues by JQL query
|
85
|
+
# # ------------------------
|
86
|
+
# client.Issue.jql('PROJECT = "SAMPLEPROJECT"', [comments, summary]).each do |issue|
|
87
|
+
# puts "#{issue.id} - #{issue.fields['summary']}"
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
# # Delete an issue
|
91
|
+
# # ---------------
|
92
|
+
# issue = client.Issue.find('SAMPLEPROJECT-2')
|
93
|
+
# if issue.delete
|
94
|
+
# puts "Delete of issue SAMPLEPROJECT-2 sucessful"
|
95
|
+
# else
|
96
|
+
# puts "Delete of issue SAMPLEPROJECT-2 failed"
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# # Create an issue
|
100
|
+
# # ---------------
|
101
|
+
# issue = client.Issue.build
|
102
|
+
# labels = ['label1', 'label2']
|
103
|
+
# issue.save({
|
104
|
+
# "fields" => {
|
105
|
+
# "summary" => "blarg from in example.rb",
|
106
|
+
# "project" => {"key" => "SAMPLEPROJECT"},
|
107
|
+
# "issuetype" => {"id" => "3"},
|
108
|
+
# "labels" => labels,
|
109
|
+
# "priority" => {"id" => "1"}
|
110
|
+
# }
|
111
|
+
# })
|
112
|
+
# issue.fetch
|
113
|
+
# pp issue
|
114
|
+
#
|
115
|
+
# # Update an issue
|
116
|
+
# # ---------------
|
117
|
+
# issue = client.Issue.find("10002")
|
118
|
+
# issue.save({"fields"=>{"summary"=>"EVEN MOOOOOOARRR NINJAAAA!"}})
|
119
|
+
# pp issue
|
120
|
+
#
|
121
|
+
# # Transition an issue
|
122
|
+
# # -------------------
|
123
|
+
# issue_transition = issue.transitions.build
|
124
|
+
# issue_transition.save!('transition' => {'id' => transition_id})
|
125
|
+
#
|
126
|
+
# # Change assignee
|
127
|
+
# # -------------------
|
128
|
+
# issue.save({'fields' => {'assignee' => {'name' => person_name}}})
|
129
|
+
#
|
130
|
+
# # Find a user
|
131
|
+
# # -----------
|
132
|
+
# user = client.User.find('admin')
|
133
|
+
# pp user
|
134
|
+
#
|
135
|
+
# # Get all issue watchers
|
136
|
+
# # ----------------------
|
137
|
+
# issue = client.Issue.find("10002")
|
138
|
+
# watchers = issue.watchers.all
|
139
|
+
# watchers = client.Watcher.all(:issue => issue)
|
140
|
+
# # Get all issue types
|
141
|
+
# # -------------------
|
142
|
+
# issuetypes = client.Issuetype.all
|
143
|
+
# pp issuetypes
|
144
|
+
#
|
145
|
+
# # Get a single issue type
|
146
|
+
# # -----------------------
|
147
|
+
# issuetype = client.Issuetype.find('5')
|
148
|
+
# pp issuetype
|
149
|
+
#
|
150
|
+
# # Get all comments for an issue
|
151
|
+
# # -----------------------------
|
152
|
+
# issue.comments.each do |comment|
|
153
|
+
# pp comment
|
154
|
+
# end
|
155
|
+
#
|
156
|
+
# # Build and Save a comment
|
157
|
+
# # ------------------------
|
158
|
+
# comment = issue.comments.build
|
159
|
+
# comment.save!(:body => "New comment from example script")
|
160
|
+
#
|
161
|
+
# # Delete a comment from the collection
|
162
|
+
# # ------------------------------------
|
163
|
+
# issue.comments.last.delete
|
164
|
+
#
|
165
|
+
# # Update an existing comment
|
166
|
+
# # --------------------------
|
167
|
+
# issue.comments.first.save({"body" => "an updated comment frome example.rb"})
|
168
|
+
|
169
|
+
|
170
|
+
# # Add attachment to Issue
|
171
|
+
# # ------------------------
|
172
|
+
# issue = client.Issue.find('PROJ-1')
|
173
|
+
# attachment = issue.attachments.build
|
174
|
+
# attachment.save('file': '/path/to/file')
|
175
|
+
#
|
176
|
+
|
177
|
+
# List all available link types
|
178
|
+
# ------------------------------
|
179
|
+
pp client.Issuelinktype.all
|
180
|
+
|
181
|
+
# List issue's links
|
182
|
+
# -------------------------
|
183
|
+
issue = client.Issue.find("10002")
|
184
|
+
pp issue.issuelinks
|
185
|
+
|
186
|
+
# Link two issues (on the same Jira instance)
|
187
|
+
# --------------------------------------------
|
188
|
+
link = client.Issuelink.build
|
189
|
+
link.save(
|
190
|
+
{
|
191
|
+
:type => {:name => 'Relates'},
|
192
|
+
:inwardIssue => {:key => 'AL-1'},
|
193
|
+
:outwardIssue => {:key => 'AL-2'}
|
194
|
+
}
|
195
|
+
)
|
196
|
+
|
197
|
+
# List issue's remote links
|
198
|
+
# -------------------------
|
199
|
+
pp issue.remotelink.all
|
200
|
+
|
201
|
+
# Link two remote issues (on the different Jira instance)
|
202
|
+
# In order to add remote links, you have to add
|
203
|
+
# Application Links between two Jira instances first.
|
204
|
+
# More information:
|
205
|
+
# https://developer.atlassian.com/jiradev/jira-platform/guides/other/guide-jira-remote-issue-links/fields-in-remote-issue-links
|
206
|
+
# http://stackoverflow.com/questions/29850252/jira-api-issuelink-connect-two-different-instances
|
207
|
+
# -------------------------------------------------------
|
208
|
+
client_1 = JIRA::Client.new(options)
|
209
|
+
client_2 = JIRA::Client.new(options)
|
210
|
+
|
211
|
+
# you have to search for your app id here, instead of getting the first
|
212
|
+
client_2_app_link = client_2.ApplicationLink.manifest
|
213
|
+
issue_1 = client_1.Issue.find('BB-2')
|
214
|
+
issue_2 = client_2.Issue.find('AA-1')
|
215
|
+
|
216
|
+
remote_link = issue_2.remotelink.build
|
217
|
+
|
218
|
+
remote_link.save(
|
219
|
+
{
|
220
|
+
:globalId => "appId=#{client_2_app_link.id}&issueId=#{issue_1.id}",
|
221
|
+
:application => {
|
222
|
+
:type => 'com.atlassian.jira',
|
223
|
+
:name => client_2_app_link['name']
|
224
|
+
},
|
225
|
+
:relationship => 'relates to',
|
226
|
+
|
227
|
+
:object => {
|
228
|
+
:url => client_1.options[:site] + client_1.options[:context_path] + "/browse/#{issue_1.key}",
|
229
|
+
:title => issue_1.key,
|
230
|
+
}
|
231
|
+
}
|
232
|
+
)
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pp'
|
3
|
+
require 'zk-jira-ruby'
|
4
|
+
|
5
|
+
if ARGV.empty?
|
6
|
+
# If not passed any command line arguments, prompt the
|
7
|
+
# user for the username and password.
|
8
|
+
puts 'Enter the username: '
|
9
|
+
username = gets.strip
|
10
|
+
|
11
|
+
puts 'Enter the password: '
|
12
|
+
password = gets.strip
|
13
|
+
elsif ARGV.length == 2
|
14
|
+
username = ARGV[0]
|
15
|
+
password = ARGV[1]
|
16
|
+
else
|
17
|
+
# Script must be passed 0 or 2 arguments
|
18
|
+
raise "Usage: #{$PROGRAM_NAME} [ username password ]"
|
19
|
+
end
|
20
|
+
|
21
|
+
options = {
|
22
|
+
username: username,
|
23
|
+
password: password,
|
24
|
+
site: 'http://localhost:8080/',
|
25
|
+
context_path: '',
|
26
|
+
auth_type: :basic,
|
27
|
+
use_ssl: false
|
28
|
+
}
|
29
|
+
|
30
|
+
client = JIRA::Client.new(options)
|
31
|
+
|
32
|
+
# Show all projects
|
33
|
+
projects = client.Project.all
|
34
|
+
|
35
|
+
projects.each do |project|
|
36
|
+
puts "Project -> key: #{project.key}, name: #{project.name}"
|
37
|
+
end
|
38
|
+
|
39
|
+
# # Find a specific project by key
|
40
|
+
# # ------------------------------
|
41
|
+
# project = client.Project.find('SAMPLEPROJECT')
|
42
|
+
# pp project
|
43
|
+
# project.issues.each do |issue|
|
44
|
+
# puts "#{issue.id} - #{issue.fields['summary']}"
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# # List all Issues
|
48
|
+
# # ---------------
|
49
|
+
# client.Issue.all.each do |issue|
|
50
|
+
# puts "#{issue.id} - #{issue.fields['summary']}"
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# # List issues by JQL query
|
54
|
+
# # ------------------------
|
55
|
+
# client.Issue.jql('PROJECT = "SAMPLEPROJECT"', {fields: %w(summary status)}).each do |issue|
|
56
|
+
# puts "#{issue.id} - #{issue.fields['summary']}"
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# # Delete an issue
|
60
|
+
# # ---------------
|
61
|
+
# issue = client.Issue.find('SAMPLEPROJECT-2')
|
62
|
+
# if issue.delete
|
63
|
+
# puts "Delete of issue SAMPLEPROJECT-2 sucessful"
|
64
|
+
# else
|
65
|
+
# puts "Delete of issue SAMPLEPROJECT-2 failed"
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# # Create an issue
|
69
|
+
# # ---------------
|
70
|
+
# issue = client.Issue.build
|
71
|
+
# issue.save({"fields"=>{"summary"=>"blarg from in example.rb","project"=>{"id"=>"10001"},"issuetype"=>{"id"=>"3"}}})
|
72
|
+
# issue.fetch
|
73
|
+
# pp issue
|
74
|
+
#
|
75
|
+
# # Update an issue
|
76
|
+
# # ---------------
|
77
|
+
# issue = client.Issue.find("10002")
|
78
|
+
# issue.save({"fields"=>{"summary"=>"EVEN MOOOOOOARRR NINJAAAA!"}})
|
79
|
+
# pp issue
|
80
|
+
#
|
81
|
+
# # Find a user
|
82
|
+
# # -----------
|
83
|
+
# user = client.User.find('admin')
|
84
|
+
# pp user
|
85
|
+
#
|
86
|
+
# # Get all issue types
|
87
|
+
# # -------------------
|
88
|
+
# issuetypes = client.Issuetype.all
|
89
|
+
# pp issuetypes
|
90
|
+
#
|
91
|
+
# # Get a single issue type
|
92
|
+
# # -----------------------
|
93
|
+
# issuetype = client.Issuetype.find('5')
|
94
|
+
# pp issuetype
|
95
|
+
#
|
96
|
+
# # Get all comments for an issue
|
97
|
+
# # -----------------------------
|
98
|
+
# issue.comments.each do |comment|
|
99
|
+
# pp comment
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# # Build and Save a comment
|
103
|
+
# # ------------------------
|
104
|
+
# comment = issue.comments.build
|
105
|
+
# comment.save!(:body => "New comment from example script")
|
106
|
+
#
|
107
|
+
# # Delete a comment from the collection
|
108
|
+
# # ------------------------------------
|
109
|
+
# issue.comments.last.delete
|
110
|
+
#
|
111
|
+
# # Update an existing comment
|
112
|
+
# # --------------------------
|
113
|
+
# issue.comments.first.save({"body" => "an updated comment frome example.rb"})
|
data/jira-ruby.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
2
|
+
require 'jira/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'zk-jira-ruby'
|
6
|
+
s.version = JIRA::VERSION
|
7
|
+
s.authors = ['SUMO Heavy Industries', 'test IO']
|
8
|
+
s.homepage = 'https://github.com/erick-martins/zk-jira'
|
9
|
+
s.summary = 'Ruby Gem for use with the Atlassian JIRA REST API'
|
10
|
+
s.description = 'API for JIRA'
|
11
|
+
s.licenses = ['MIT']
|
12
|
+
s.metadata = { 'source_code_uri' => 'https://github.com/erick-martins/zk-jira' }
|
13
|
+
|
14
|
+
s.required_ruby_version = '>= 1.9.3'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
|
21
|
+
# Runtime Dependencies
|
22
|
+
s.add_runtime_dependency 'activesupport'
|
23
|
+
s.add_runtime_dependency 'atlassian-jwt'
|
24
|
+
s.add_runtime_dependency 'multipart-post'
|
25
|
+
s.add_runtime_dependency 'oauth', '~> 0.5', '>= 0.5.0'
|
26
|
+
|
27
|
+
# Development Dependencies
|
28
|
+
s.add_development_dependency 'guard', '~> 2.13', '>= 2.13.0'
|
29
|
+
s.add_development_dependency 'guard-rspec', '~> 4.6', '>= 4.6.5'
|
30
|
+
s.add_development_dependency 'pry', '~> 0.10', '>= 0.10.3'
|
31
|
+
s.add_development_dependency 'railties'
|
32
|
+
s.add_development_dependency 'rake', '~> 10.3', '>= 10.3.2'
|
33
|
+
s.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0'
|
34
|
+
s.add_development_dependency 'webmock', '~> 1.18', '>= 1.18.0'
|
35
|
+
end
|
data/lib/jira/base.rb
ADDED
@@ -0,0 +1,525 @@
|
|
1
|
+
require 'active_support/core_ext/string'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
module JIRA
|
6
|
+
# This class provides the basic object <-> REST mapping for all JIRA::Resource subclasses,
|
7
|
+
# i.e. the Create, Retrieve, Update, Delete lifecycle methods.
|
8
|
+
#
|
9
|
+
# == Lifecycle methods
|
10
|
+
#
|
11
|
+
# Note that not all lifecycle
|
12
|
+
# methods are available for all resources, for example some resources cannot be updated
|
13
|
+
# or deleted.
|
14
|
+
#
|
15
|
+
# === Retrieving all resources
|
16
|
+
#
|
17
|
+
# client.Resource.all
|
18
|
+
#
|
19
|
+
# === Retrieving a single resource
|
20
|
+
#
|
21
|
+
# client.Resource.find(id)
|
22
|
+
#
|
23
|
+
# === Creating a resource
|
24
|
+
#
|
25
|
+
# resource = client.Resource.build({'name' => '')
|
26
|
+
# resource.save
|
27
|
+
#
|
28
|
+
# === Updating a resource
|
29
|
+
#
|
30
|
+
# resource = client.Resource.find(id)
|
31
|
+
# resource.save('updated_attribute' => 'new value')
|
32
|
+
#
|
33
|
+
# === Deleting a resource
|
34
|
+
#
|
35
|
+
# resource = client.Resource.find(id)
|
36
|
+
# resource.delete
|
37
|
+
#
|
38
|
+
# == Nested resources
|
39
|
+
#
|
40
|
+
# Some resources are not defined in the top level of the URL namespace
|
41
|
+
# within the JIRA API, but are always nested under the context of another
|
42
|
+
# resource. For example, a JIRA::Resource::Comment always belongs to a
|
43
|
+
# JIRA::Resource::Issue.
|
44
|
+
#
|
45
|
+
# These resources must be indexed and built from an instance of the class
|
46
|
+
# they are nested under:
|
47
|
+
#
|
48
|
+
# issue = client.Issue.find(id)
|
49
|
+
# comments = issue.comments
|
50
|
+
# new_comment = issue.comments.build
|
51
|
+
#
|
52
|
+
class Base
|
53
|
+
QUERY_PARAMS_FOR_SINGLE_FETCH = Set.new %i[expand fields]
|
54
|
+
QUERY_PARAMS_FOR_SEARCH = Set.new %i[expand fields startAt maxResults]
|
55
|
+
|
56
|
+
# A reference to the JIRA::Client used to initialize this resource.
|
57
|
+
attr_reader :client
|
58
|
+
|
59
|
+
# Returns true if this instance has been fetched from the server
|
60
|
+
attr_accessor :expanded
|
61
|
+
|
62
|
+
# Returns true if this instance has been deleted from the server
|
63
|
+
attr_accessor :deleted
|
64
|
+
|
65
|
+
# The hash of attributes belonging to this instance. An exact
|
66
|
+
# representation of the JSON returned from the JIRA API
|
67
|
+
attr_accessor :attrs
|
68
|
+
|
69
|
+
alias expanded? expanded
|
70
|
+
alias deleted? deleted
|
71
|
+
|
72
|
+
def initialize(client, options = {})
|
73
|
+
@client = client
|
74
|
+
@attrs = options[:attrs] || {}
|
75
|
+
@expanded = options[:expanded] || false
|
76
|
+
@deleted = false
|
77
|
+
|
78
|
+
# If this class has any belongs_to relationships, a value for
|
79
|
+
# each of them must be passed in to the initializer.
|
80
|
+
self.class.belongs_to_relationships.each do |relation|
|
81
|
+
if options[relation]
|
82
|
+
instance_variable_set("@#{relation}", options[relation])
|
83
|
+
instance_variable_set("@#{relation}_id", options[relation].key_value)
|
84
|
+
elsif options["#{relation}_id".to_sym]
|
85
|
+
instance_variable_set("@#{relation}_id", options["#{relation}_id".to_sym])
|
86
|
+
else
|
87
|
+
raise ArgumentError, "Required option #{relation.inspect} missing" unless options[relation]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# The class methods are never called directly, they are always
|
93
|
+
# invoked from a BaseFactory subclass instance.
|
94
|
+
def self.all(client, options = {})
|
95
|
+
response = client.get(collection_path(client))
|
96
|
+
json = parse_json(response.body)
|
97
|
+
json = json[endpoint_name.pluralize] if collection_attributes_are_nested
|
98
|
+
json.map do |attrs|
|
99
|
+
new(client, { attrs: attrs }.merge(options))
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# Finds and retrieves a resource with the given ID.
|
104
|
+
def self.find(client, key, options = {})
|
105
|
+
instance = new(client, options)
|
106
|
+
instance.attrs[key_attribute.to_s] = key
|
107
|
+
instance.fetch(false, query_params_for_single_fetch(options))
|
108
|
+
instance
|
109
|
+
end
|
110
|
+
|
111
|
+
# Builds a new instance of the resource with the given attributes.
|
112
|
+
# These attributes will be posted to the JIRA Api if save is called.
|
113
|
+
def self.build(client, attrs)
|
114
|
+
new(client, attrs: attrs)
|
115
|
+
end
|
116
|
+
|
117
|
+
# Returns the name of this resource for use in URL components.
|
118
|
+
# E.g.
|
119
|
+
# JIRA::Resource::Issue.endpoint_name
|
120
|
+
# # => issue
|
121
|
+
def self.endpoint_name
|
122
|
+
name.split('::').last.downcase
|
123
|
+
end
|
124
|
+
|
125
|
+
# Returns the full path for a collection of this resource.
|
126
|
+
# E.g.
|
127
|
+
# JIRA::Resource::Issue.collection_path
|
128
|
+
# # => /jira/rest/api/2/issue
|
129
|
+
def self.collection_path(client, prefix = '/')
|
130
|
+
client.options[:rest_base_path] + prefix + endpoint_name
|
131
|
+
end
|
132
|
+
|
133
|
+
# Returns the singular path for the resource with the given key.
|
134
|
+
# E.g.
|
135
|
+
# JIRA::Resource::Issue.singular_path('123')
|
136
|
+
# # => /jira/rest/api/2/issue/123
|
137
|
+
#
|
138
|
+
# If a prefix parameter is provided it will be injected between the base
|
139
|
+
# path and the endpoint.
|
140
|
+
# E.g.
|
141
|
+
# JIRA::Resource::Comment.singular_path('456','/issue/123/')
|
142
|
+
# # => /jira/rest/api/2/issue/123/comment/456
|
143
|
+
def self.singular_path(client, key, prefix = '/')
|
144
|
+
collection_path(client, prefix) + '/' + key
|
145
|
+
end
|
146
|
+
|
147
|
+
# Returns the attribute name of the attribute used for find.
|
148
|
+
# Defaults to :id unless overridden.
|
149
|
+
def self.key_attribute
|
150
|
+
:id
|
151
|
+
end
|
152
|
+
|
153
|
+
def self.parse_json(string) # :nodoc:
|
154
|
+
JSON.parse(string)
|
155
|
+
end
|
156
|
+
|
157
|
+
# Declares that this class contains a singular instance of another resource
|
158
|
+
# within the JSON returned from the JIRA API.
|
159
|
+
#
|
160
|
+
# class Example < JIRA::Base
|
161
|
+
# has_one :child
|
162
|
+
# end
|
163
|
+
#
|
164
|
+
# example = client.Example.find(1)
|
165
|
+
# example.child # Returns a JIRA::Resource::Child
|
166
|
+
#
|
167
|
+
# The following options can be used to override the default behaviour of the
|
168
|
+
# relationship:
|
169
|
+
#
|
170
|
+
# [:attribute_key] The relationship will by default reference a JSON key on the
|
171
|
+
# object with the same name as the relationship.
|
172
|
+
#
|
173
|
+
# has_one :child # => {"id":"123",{"child":{"id":"456"}}}
|
174
|
+
#
|
175
|
+
# Use this option if the key in the JSON is named differently.
|
176
|
+
#
|
177
|
+
# # Respond to resource.child, but return the value of resource.attrs['kid']
|
178
|
+
# has_one :child, :attribute_key => 'kid' # => {"id":"123",{"kid":{"id":"456"}}}
|
179
|
+
#
|
180
|
+
# [:class] The class of the child instance will be inferred from the name of the
|
181
|
+
# relationship. E.g. <tt>has_one :child</tt> will return a <tt>JIRA::Resource::Child</tt>.
|
182
|
+
# Use this option to override the inferred class.
|
183
|
+
#
|
184
|
+
# has_one :child, :class => JIRA::Resource::Kid
|
185
|
+
# [:nested_under] In some cases, the JSON return from JIRA is nested deeply for particular
|
186
|
+
# relationships. This option allows the nesting to be specified.
|
187
|
+
#
|
188
|
+
# # Specify a single depth of nesting.
|
189
|
+
# has_one :child, :nested_under => 'foo'
|
190
|
+
# # => Looks for {"foo":{"child":{}}}
|
191
|
+
# # Specify deeply nested JSON
|
192
|
+
# has_one :child, :nested_under => ['foo', 'bar', 'baz']
|
193
|
+
# # => Looks for {"foo":{"bar":{"baz":{"child":{}}}}}
|
194
|
+
def self.has_one(resource, options = {})
|
195
|
+
attribute_key = options[:attribute_key] || resource.to_s
|
196
|
+
child_class = options[:class] || ('JIRA::Resource::' + resource.to_s.classify).constantize
|
197
|
+
define_method(resource) do
|
198
|
+
attribute = maybe_nested_attribute(attribute_key, options[:nested_under])
|
199
|
+
return nil unless attribute
|
200
|
+
child_class.new(client, attrs: attribute)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# Declares that this class contains a collection of another resource
|
205
|
+
# within the JSON returned from the JIRA API.
|
206
|
+
#
|
207
|
+
# class Example < JIRA::Base
|
208
|
+
# has_many :children
|
209
|
+
# end
|
210
|
+
#
|
211
|
+
# example = client.Example.find(1)
|
212
|
+
# example.children # Returns an instance of Jira::Resource::HasManyProxy,
|
213
|
+
# # which behaves exactly like an array of
|
214
|
+
# # Jira::Resource::Child
|
215
|
+
#
|
216
|
+
# The following options can be used to override the default behaviour of the
|
217
|
+
# relationship:
|
218
|
+
#
|
219
|
+
# [:attribute_key] The relationship will by default reference a JSON key on the
|
220
|
+
# object with the same name as the relationship.
|
221
|
+
#
|
222
|
+
# has_many :children # => {"id":"123",{"children":[{"id":"456"},{"id":"789"}]}}
|
223
|
+
#
|
224
|
+
# Use this option if the key in the JSON is named differently.
|
225
|
+
#
|
226
|
+
# # Respond to resource.children, but return the value of resource.attrs['kids']
|
227
|
+
# has_many :children, :attribute_key => 'kids' # => {"id":"123",{"kids":[{"id":"456"},{"id":"789"}]}}
|
228
|
+
#
|
229
|
+
# [:class] The class of the child instance will be inferred from the name of the
|
230
|
+
# relationship. E.g. <tt>has_many :children</tt> will return an instance
|
231
|
+
# of <tt>JIRA::Resource::HasManyProxy</tt> containing the collection of
|
232
|
+
# <tt>JIRA::Resource::Child</tt>.
|
233
|
+
# Use this option to override the inferred class.
|
234
|
+
#
|
235
|
+
# has_many :children, :class => JIRA::Resource::Kid
|
236
|
+
# [:nested_under] In some cases, the JSON return from JIRA is nested deeply for particular
|
237
|
+
# relationships. This option allows the nesting to be specified.
|
238
|
+
#
|
239
|
+
# # Specify a single depth of nesting.
|
240
|
+
# has_many :children, :nested_under => 'foo'
|
241
|
+
# # => Looks for {"foo":{"children":{}}}
|
242
|
+
# # Specify deeply nested JSON
|
243
|
+
# has_many :children, :nested_under => ['foo', 'bar', 'baz']
|
244
|
+
# # => Looks for {"foo":{"bar":{"baz":{"children":{}}}}}
|
245
|
+
def self.has_many(collection, options = {})
|
246
|
+
attribute_key = options[:attribute_key] || collection.to_s
|
247
|
+
child_class = options[:class] || ('JIRA::Resource::' + collection.to_s.classify).constantize
|
248
|
+
self_class_basename = name.split('::').last.downcase.to_sym
|
249
|
+
define_method(collection) do
|
250
|
+
child_class_options = { self_class_basename => self }
|
251
|
+
attribute = maybe_nested_attribute(attribute_key, options[:nested_under]) || []
|
252
|
+
collection = attribute.map do |child_attributes|
|
253
|
+
child_class.new(client, child_class_options.merge(attrs: child_attributes))
|
254
|
+
end
|
255
|
+
HasManyProxy.new(self, child_class, collection)
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
def self.belongs_to_relationships
|
260
|
+
@belongs_to_relationships ||= []
|
261
|
+
end
|
262
|
+
|
263
|
+
def self.belongs_to(resource)
|
264
|
+
belongs_to_relationships.push(resource)
|
265
|
+
attr_reader resource
|
266
|
+
attr_reader "#{resource}_id"
|
267
|
+
end
|
268
|
+
|
269
|
+
def self.collection_attributes_are_nested
|
270
|
+
@collection_attributes_are_nested ||= false
|
271
|
+
end
|
272
|
+
|
273
|
+
def self.nested_collections(value)
|
274
|
+
@collection_attributes_are_nested = value
|
275
|
+
end
|
276
|
+
|
277
|
+
def id
|
278
|
+
attrs['id']
|
279
|
+
end
|
280
|
+
|
281
|
+
# Returns a symbol for the given instance, for example
|
282
|
+
# JIRA::Resource::Issue returns :issue
|
283
|
+
def to_sym
|
284
|
+
self.class.endpoint_name.to_sym
|
285
|
+
end
|
286
|
+
|
287
|
+
# Checks if method_name is set in the attributes hash
|
288
|
+
# and returns true when found, otherwise proxies the
|
289
|
+
# call to the superclass.
|
290
|
+
def respond_to?(method_name, _include_all = false)
|
291
|
+
if attrs.key?(method_name.to_s)
|
292
|
+
true
|
293
|
+
else
|
294
|
+
super(method_name)
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
# Overrides method_missing to check the attribute hash
|
299
|
+
# for resources matching method_name and proxies the call
|
300
|
+
# to the superclass if no match is found.
|
301
|
+
def method_missing(method_name, *_args)
|
302
|
+
if attrs.key?(method_name.to_s)
|
303
|
+
attrs[method_name.to_s]
|
304
|
+
else
|
305
|
+
super(method_name)
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
# Each resource has a unique key attribute, this method returns the value
|
310
|
+
# of that key for this instance.
|
311
|
+
def key_value
|
312
|
+
@attrs[self.class.key_attribute.to_s]
|
313
|
+
end
|
314
|
+
|
315
|
+
def collection_path(prefix = '/')
|
316
|
+
# Just proxy this to the class method
|
317
|
+
self.class.collection_path(client, prefix)
|
318
|
+
end
|
319
|
+
|
320
|
+
# This returns the URL path component that is specific to this instance,
|
321
|
+
# for example for Issue id 123 it returns '/issue/123'. For an unsaved
|
322
|
+
# issue it returns '/issue'
|
323
|
+
def path_component
|
324
|
+
path_component = "/#{self.class.endpoint_name}"
|
325
|
+
path_component += '/' + key_value if key_value
|
326
|
+
path_component
|
327
|
+
end
|
328
|
+
|
329
|
+
# Fetches the attributes for the specified resource from JIRA unless
|
330
|
+
# the resource is already expanded and the optional force reload flag
|
331
|
+
# is not set
|
332
|
+
def fetch(reload = false, query_params = {})
|
333
|
+
return if expanded? && !reload
|
334
|
+
response = client.get(url_with_query_params(url, query_params))
|
335
|
+
set_attrs_from_response(response)
|
336
|
+
@expanded = true
|
337
|
+
end
|
338
|
+
|
339
|
+
# Saves the specified resource attributes by sending either a POST or PUT
|
340
|
+
# request to JIRA, depending on resource.new_record?
|
341
|
+
#
|
342
|
+
# Accepts an attributes hash of the values to be saved. Will throw a
|
343
|
+
# JIRA::HTTPError if the request fails (response is not HTTP 2xx).
|
344
|
+
def save!(attrs, path = nil)
|
345
|
+
path ||= new_record? ? url : patched_url
|
346
|
+
http_method = new_record? ? :post : :put
|
347
|
+
response = client.send(http_method, path, attrs.to_json)
|
348
|
+
set_attrs(attrs, false)
|
349
|
+
set_attrs_from_response(response)
|
350
|
+
@expanded = false
|
351
|
+
true
|
352
|
+
end
|
353
|
+
|
354
|
+
# Saves the specified resource attributes by sending either a POST or PUT
|
355
|
+
# request to JIRA, depending on resource.new_record?
|
356
|
+
#
|
357
|
+
# Accepts an attributes hash of the values to be saved. Will return false
|
358
|
+
# if the request fails.
|
359
|
+
def save(attrs, path = url)
|
360
|
+
begin
|
361
|
+
save_status = save!(attrs, path)
|
362
|
+
rescue JIRA::HTTPError => exception
|
363
|
+
begin
|
364
|
+
set_attrs_from_response(exception.response) # Merge error status generated by JIRA REST API
|
365
|
+
rescue JSON::ParserError => parse_exception
|
366
|
+
set_attrs('exception' => {
|
367
|
+
'class' => exception.response.class.name,
|
368
|
+
'code' => exception.response.code,
|
369
|
+
'message' => exception.response.message
|
370
|
+
})
|
371
|
+
end
|
372
|
+
# raise exception
|
373
|
+
save_status = false
|
374
|
+
end
|
375
|
+
save_status
|
376
|
+
end
|
377
|
+
|
378
|
+
# Sets the attributes hash from a HTTPResponse object from JIRA if it is
|
379
|
+
# not nil or is not a json response.
|
380
|
+
def set_attrs_from_response(response)
|
381
|
+
unless response.body.nil? || (response.body.length < 2)
|
382
|
+
json = self.class.parse_json(response.body)
|
383
|
+
set_attrs(json)
|
384
|
+
end
|
385
|
+
end
|
386
|
+
|
387
|
+
# Set the current attributes from a hash. If clobber is true, any existing
|
388
|
+
# hash values will be clobbered by the new hash, otherwise the hash will
|
389
|
+
# be deeply merged into attrs. The target paramater is for internal use only
|
390
|
+
# and should not be used.
|
391
|
+
def set_attrs(hash, clobber = true, target = nil)
|
392
|
+
target ||= @attrs
|
393
|
+
if clobber
|
394
|
+
target.merge!(hash)
|
395
|
+
hash
|
396
|
+
else
|
397
|
+
hash.each do |k, v|
|
398
|
+
if v.is_a?(Hash)
|
399
|
+
set_attrs(v, clobber, target[k])
|
400
|
+
else
|
401
|
+
target[k] = v
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
# Sends a delete request to the JIRA Api and sets the deleted instance
|
408
|
+
# variable on the object to true.
|
409
|
+
def delete
|
410
|
+
client.delete(url)
|
411
|
+
@deleted = true
|
412
|
+
end
|
413
|
+
|
414
|
+
def has_errors?
|
415
|
+
respond_to?('errors')
|
416
|
+
end
|
417
|
+
|
418
|
+
def url
|
419
|
+
prefix = '/'
|
420
|
+
unless self.class.belongs_to_relationships.empty?
|
421
|
+
prefix = self.class.belongs_to_relationships.inject(prefix) do |prefix_so_far, relationship|
|
422
|
+
prefix_so_far.to_s + relationship.to_s + '/' + send("#{relationship}_id").to_s + '/'
|
423
|
+
end
|
424
|
+
end
|
425
|
+
if @attrs['self']
|
426
|
+
the_url = @attrs['self']
|
427
|
+
the_url = the_url.sub(@client.options[:site].chomp('/'), '') if @client.options[:site]
|
428
|
+
the_url
|
429
|
+
elsif key_value
|
430
|
+
self.class.singular_path(client, key_value.to_s, prefix)
|
431
|
+
else
|
432
|
+
self.class.collection_path(client, prefix)
|
433
|
+
end
|
434
|
+
end
|
435
|
+
|
436
|
+
# This method fixes issue that there is no / prefix in url. It is happened when we call for instance
|
437
|
+
# Looks like this issue is actual only in case if you use atlassian sdk your app path is not root (like /jira in example below)
|
438
|
+
# issue.save() for existing resource.
|
439
|
+
# As a result we got error 400 from JIRA API:
|
440
|
+
# [07/Jun/2015:15:32:19 +0400] "PUT jira/rest/api/2/issue/10111 HTTP/1.1" 400 -
|
441
|
+
# After applying this fix we have normal response:
|
442
|
+
# [07/Jun/2015:15:17:18 +0400] "PUT /jira/rest/api/2/issue/10111 HTTP/1.1" 204 -
|
443
|
+
def patched_url
|
444
|
+
result = url
|
445
|
+
return result if result.start_with?('/', 'http')
|
446
|
+
"/#{result}"
|
447
|
+
end
|
448
|
+
|
449
|
+
def to_s
|
450
|
+
"#<#{self.class.name}:#{object_id} @attrs=#{@attrs.inspect}>"
|
451
|
+
end
|
452
|
+
|
453
|
+
# Returns a JSON representation of the current attributes hash.
|
454
|
+
def to_json(options = {})
|
455
|
+
attrs.to_json(options)
|
456
|
+
end
|
457
|
+
|
458
|
+
# Determines if the resource is newly created by checking whether its
|
459
|
+
# key_value is set. If it is nil, the record is new and the method
|
460
|
+
# will return true.
|
461
|
+
def new_record?
|
462
|
+
key_value.nil?
|
463
|
+
end
|
464
|
+
|
465
|
+
protected
|
466
|
+
|
467
|
+
# This allows conditional lookup of possibly nested attributes. Example usage:
|
468
|
+
#
|
469
|
+
# maybe_nested_attribute('foo') # => @attrs['foo']
|
470
|
+
# maybe_nested_attribute('foo', 'bar') # => @attrs['bar']['foo']
|
471
|
+
# maybe_nested_attribute('foo', ['bar', 'baz']) # => @attrs['bar']['baz']['foo']
|
472
|
+
#
|
473
|
+
def maybe_nested_attribute(attribute_name, nested_under = nil)
|
474
|
+
self.class.maybe_nested_attribute(@attrs, attribute_name, nested_under)
|
475
|
+
end
|
476
|
+
|
477
|
+
def self.maybe_nested_attribute(attributes, attribute_name, nested_under = nil)
|
478
|
+
return attributes[attribute_name] if nested_under.nil?
|
479
|
+
if nested_under.instance_of? Array
|
480
|
+
final = nested_under.inject(attributes) do |parent, key|
|
481
|
+
break if parent.nil?
|
482
|
+
parent[key]
|
483
|
+
end
|
484
|
+
return nil if final.nil?
|
485
|
+
final[attribute_name]
|
486
|
+
else
|
487
|
+
return attributes[nested_under][attribute_name]
|
488
|
+
end
|
489
|
+
end
|
490
|
+
|
491
|
+
def url_with_query_params(url, query_params)
|
492
|
+
self.class.url_with_query_params(url, query_params)
|
493
|
+
end
|
494
|
+
|
495
|
+
def self.url_with_query_params(url, query_params)
|
496
|
+
if !query_params.empty?
|
497
|
+
"#{url}?#{hash_to_query_string query_params}"
|
498
|
+
else
|
499
|
+
url
|
500
|
+
end
|
501
|
+
end
|
502
|
+
|
503
|
+
def hash_to_query_string(query_params)
|
504
|
+
self.class.hash_to_query_string(query_params)
|
505
|
+
end
|
506
|
+
|
507
|
+
def self.hash_to_query_string(query_params)
|
508
|
+
query_params.map do |k, v|
|
509
|
+
CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s)
|
510
|
+
end.join('&')
|
511
|
+
end
|
512
|
+
|
513
|
+
def self.query_params_for_single_fetch(options)
|
514
|
+
Hash[options.select do |k, _v|
|
515
|
+
QUERY_PARAMS_FOR_SINGLE_FETCH.include? k
|
516
|
+
end]
|
517
|
+
end
|
518
|
+
|
519
|
+
def self.query_params_for_search(options)
|
520
|
+
Hash[options.select do |k, _v|
|
521
|
+
QUERY_PARAMS_FOR_SEARCH.include? k
|
522
|
+
end]
|
523
|
+
end
|
524
|
+
end
|
525
|
+
end
|