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
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bcf0ab43655e91daec8717bd78b413211df7846519b39ecc5ac672ee35fa7124
|
4
|
+
data.tar.gz: e779f0d1bf105e78316d3060d0f0148ee2866a0f5484508018a24363f0f8bda0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '049dac7533b75f9cdf6400fcf3df7c4af6219fcc37a24fd5d86a90071cb5a5eb2f1d9ea6b218268922d36d0598859a042fb1317022d00e67193dd3ff36c33c88'
|
7
|
+
data.tar.gz: 3a6292015ab1f4c9a91629b5b972f618d87b89c1a842cc77444fa351535b5e6b8bf1d30f69c7740c6d6df52b9965e83ad955a2caadad03cde03b3e964d795fff
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
group :development do
|
4
|
+
gem 'guard'
|
5
|
+
gem 'guard-rspec'
|
6
|
+
gem 'wdm', '>= 0.1.0' if Gem.win_platform?
|
7
|
+
end
|
8
|
+
|
9
|
+
group :development, :test do
|
10
|
+
gem 'pry' # this was in the original Gemfile - but only needed in development & test
|
11
|
+
end
|
12
|
+
|
13
|
+
# Specify your gem's dependencies in jira_api.gemspec
|
14
|
+
gemspec
|
data/Guardfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
gem 'wdm', '>= 0.1.0' if Gem.win_platform?
|
2
|
+
gem 'rspec', '~> 3.0.0'
|
3
|
+
|
4
|
+
guard 'rspec', cmd: 'bundle exec rspec --color --format doc' do
|
5
|
+
# watch /lib/ files
|
6
|
+
watch(%r{^lib/(.+).rb$}) do |m|
|
7
|
+
"spec/#{m[1]}_spec.rb"
|
8
|
+
end
|
9
|
+
|
10
|
+
# watch /spec/ files
|
11
|
+
watch(%r{^spec/(.+).rb$}) do |m|
|
12
|
+
"spec/#{m[1]}.rb"
|
13
|
+
end
|
14
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2015-2016 SUMO Heavy Industries, LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,427 @@
|
|
1
|
+
# JIRA API Gem
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/sumoheavy/zk-jira-ruby.svg)](https://codeclimate.com/github/sumoheavy/zk-jira-ruby)
|
4
|
+
[![Build Status](https://travis-ci.org/sumoheavy/zk-jira-ruby.svg?branch=master)](https://travis-ci.org/sumoheavy/zk-jira-ruby)
|
5
|
+
|
6
|
+
This gem provides access to the Atlassian JIRA REST API.
|
7
|
+
|
8
|
+
## Slack
|
9
|
+
|
10
|
+
Join our Slack channel! You can find us [here](https://zk-jira-ruby-slackin.herokuapp.com/)
|
11
|
+
|
12
|
+
## Example usage
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
require 'rubygems'
|
16
|
+
require 'zk-jira-ruby'
|
17
|
+
|
18
|
+
options = {
|
19
|
+
:username => 'username',
|
20
|
+
:password => 'pass1234',
|
21
|
+
:site => 'http://mydomain.atlassian.net:443/',
|
22
|
+
:context_path => '',
|
23
|
+
:auth_type => :basic
|
24
|
+
}
|
25
|
+
|
26
|
+
client = JIRA::Client.new(options)
|
27
|
+
|
28
|
+
project = client.Project.find('SAMPLEPROJECT')
|
29
|
+
|
30
|
+
project.issues.each do |issue|
|
31
|
+
puts "#{issue.id} - #{issue.summary}"
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
## Links to JIRA REST API documentation
|
36
|
+
|
37
|
+
* [Overview](https://developer.atlassian.com/display/JIRADEV/JIRA+REST+APIs)
|
38
|
+
|
39
|
+
* [Reference](http://docs.atlassian.com/jira/REST/latest/)
|
40
|
+
|
41
|
+
## Running tests
|
42
|
+
|
43
|
+
Before running tests, you will need a public certificate generated.
|
44
|
+
|
45
|
+
```shell
|
46
|
+
rake jira:generate_public_cert
|
47
|
+
```
|
48
|
+
|
49
|
+
## Setting up the JIRA SDK
|
50
|
+
|
51
|
+
On Mac OS,
|
52
|
+
|
53
|
+
* Follow the instructions under "Mac OSX Installer" here: https://developer.atlassian.com/server/framework/atlassian-sdk/install-the-atlassian-sdk-on-a-linux-or-mac-system
|
54
|
+
* From within the archive directory, run:
|
55
|
+
|
56
|
+
```shell
|
57
|
+
./bin/atlas-run-standalone --product jira
|
58
|
+
```
|
59
|
+
|
60
|
+
Once this is running, you should be able to connect to
|
61
|
+
http://localhost:2990/ and login to the JIRA admin system using `admin:admin`
|
62
|
+
|
63
|
+
You'll need to create a dummy project and probably some issues to test using
|
64
|
+
this library.
|
65
|
+
|
66
|
+
## Configuring JIRA to use OAuth
|
67
|
+
|
68
|
+
From the JIRA API tutorial
|
69
|
+
|
70
|
+
> The first step is to register a new consumer in JIRA. This is done through
|
71
|
+
> the Application Links administration screens in JIRA. Create a new
|
72
|
+
> Application Link.
|
73
|
+
> [Administration/Plugins/Application Links](http://localhost:2990/jira/plugins/servlet/applinks/listApplicationLinks)
|
74
|
+
>
|
75
|
+
> When creating the Application Link use a placeholder URL or the correct URL
|
76
|
+
> to your client (e.g. `http://localhost:3000`), if your client can be reached
|
77
|
+
> via HTTP and choose the Generic Application type. After this Application Link
|
78
|
+
> has been created, edit the configuration and go to the incoming
|
79
|
+
> authentication configuration screen and select OAuth. Enter in this the
|
80
|
+
> public key and the consumer key which your client will use when making
|
81
|
+
> requests to JIRA.
|
82
|
+
|
83
|
+
This public key and consumer key will need to be generated by the Gem user, using OpenSSL
|
84
|
+
or similar to generate the public key and the provided rake task to generate the consumer
|
85
|
+
key.
|
86
|
+
|
87
|
+
> After you have entered all the information click OK and ensure OAuth authentication is
|
88
|
+
> enabled.
|
89
|
+
|
90
|
+
For 2 legged oauth in server mode only, not in cloud based JIRA, make sure to `Allow 2-Legged OAuth`
|
91
|
+
|
92
|
+
## Configuring JIRA to use HTTP Basic Auth
|
93
|
+
|
94
|
+
Follow the same steps described above to set up a new Application Link in JIRA,
|
95
|
+
however there is no need to set up any "incoming authentication" as this
|
96
|
+
defaults to HTTP Basic Auth.
|
97
|
+
|
98
|
+
## Configuring JIRA to use Cookie-Based Auth
|
99
|
+
|
100
|
+
Jira supports cookie based authentication whereby user credentials are passed
|
101
|
+
to JIRA via a JIRA REST API call. This call returns a session cookie which must
|
102
|
+
then be sent to all following JIRA REST API calls.
|
103
|
+
|
104
|
+
To enable cookie based authentication, set `:auth_type` to `:cookie`,
|
105
|
+
set `:use_cookies` to `true` and set `:username` and `:password` accordingly.
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
require 'zk-jira-ruby'
|
109
|
+
|
110
|
+
options = {
|
111
|
+
:username => 'username',
|
112
|
+
:password => 'pass1234',
|
113
|
+
:site => 'http://mydomain.atlassian.net:443/',
|
114
|
+
:context_path => '',
|
115
|
+
:auth_type => :cookie, # Set cookie based authentication
|
116
|
+
:use_cookies => true, # Send cookies with each request
|
117
|
+
:additional_cookies => ['AUTH=vV7uzixt0SScJKg7'] # Optional cookies to send
|
118
|
+
# with each request
|
119
|
+
}
|
120
|
+
|
121
|
+
client = JIRA::Client.new(options)
|
122
|
+
|
123
|
+
project = client.Project.find('SAMPLEPROJECT')
|
124
|
+
|
125
|
+
project.issues.each do |issue|
|
126
|
+
puts "#{issue.id} - #{issue.summary}"
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
Some authentication schemes might require additional cookies to be sent with
|
131
|
+
each request. Cookies added to the `:additional_cookies` option will be added
|
132
|
+
to each request. This option should be an array of strings representing each
|
133
|
+
cookie to add to the request.
|
134
|
+
|
135
|
+
Some authentication schemes that require additional cookies ignore the username
|
136
|
+
and password sent in the JIRA REST API call. For those use cases, `:username`
|
137
|
+
and `:password` may be omitted from `options`.
|
138
|
+
|
139
|
+
## Using the API Gem in a command line application
|
140
|
+
|
141
|
+
Using HTTP Basic Authentication, configure and connect a client to your instance
|
142
|
+
of JIRA.
|
143
|
+
|
144
|
+
Note: If your Jira install is hosted on [atlassian.net](atlassian.net), it will have no context
|
145
|
+
path by default. If you're having issues connecting, try setting context_path
|
146
|
+
to an empty string in the options hash.
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
require 'rubygems'
|
150
|
+
require 'pp'
|
151
|
+
require 'zk-jira-ruby'
|
152
|
+
|
153
|
+
# Consider the use of :use_ssl and :ssl_verify_mode options if running locally
|
154
|
+
# for tests.
|
155
|
+
|
156
|
+
# NOTE basic auth no longer works with Jira, you must generate an API token, to do so you must have jira instance access rights. You can generate a token here: https://id.atlassian.com/manage/api-tokens
|
157
|
+
|
158
|
+
# You will see JIRA::HTTPError (JIRA::HTTPError) if you attempt to use basic auth with your user's password
|
159
|
+
|
160
|
+
username = "myremoteuser"
|
161
|
+
api_token = "myApiToken"
|
162
|
+
|
163
|
+
options = {
|
164
|
+
:username => username,
|
165
|
+
:password => api_token,
|
166
|
+
:site => 'http://localhost:8080/', # or 'https://<your_subdomain>.atlassian.net/'
|
167
|
+
:context_path => '/myjira', # often blank
|
168
|
+
:auth_type => :basic,
|
169
|
+
:read_timeout => 120
|
170
|
+
}
|
171
|
+
|
172
|
+
client = JIRA::Client.new(options)
|
173
|
+
|
174
|
+
# Show all projects
|
175
|
+
projects = client.Project.all
|
176
|
+
|
177
|
+
projects.each do |project|
|
178
|
+
puts "Project -> key: #{project.key}, name: #{project.name}"
|
179
|
+
end
|
180
|
+
```
|
181
|
+
|
182
|
+
## Using the API Gem in your Rails application
|
183
|
+
|
184
|
+
Using oauth, the gem requires the consumer key and public certificate file (which
|
185
|
+
are generated in their respective rake tasks) to initialize an access token for
|
186
|
+
using the JIRA API.
|
187
|
+
|
188
|
+
Note that currently the rake task which generates the public certificate
|
189
|
+
requires OpenSSL to be installed on the machine.
|
190
|
+
|
191
|
+
Below is an example for setting up a rails application for OAuth authorization.
|
192
|
+
|
193
|
+
Ensure the JIRA gem is loaded correctly
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
# Gemfile
|
197
|
+
...
|
198
|
+
gem 'zk-jira-ruby', :require => 'zk-jira-ruby'
|
199
|
+
...
|
200
|
+
```
|
201
|
+
|
202
|
+
Add common methods to your application controller and ensure access token
|
203
|
+
errors are handled gracefully
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
# app/controllers/application_controller.rb
|
207
|
+
class ApplicationController < ActionController::Base
|
208
|
+
protect_from_forgery
|
209
|
+
|
210
|
+
rescue_from JIRA::OauthClient::UninitializedAccessTokenError do
|
211
|
+
redirect_to new_jira_session_url
|
212
|
+
end
|
213
|
+
|
214
|
+
private
|
215
|
+
|
216
|
+
def get_jira_client
|
217
|
+
|
218
|
+
# add any extra configuration options for your instance of JIRA,
|
219
|
+
# e.g. :use_ssl, :ssl_verify_mode, :context_path, :site
|
220
|
+
options = {
|
221
|
+
:private_key_file => "rsakey.pem",
|
222
|
+
:consumer_key => 'test'
|
223
|
+
}
|
224
|
+
|
225
|
+
@jira_client = JIRA::Client.new(options)
|
226
|
+
|
227
|
+
# Add AccessToken if authorised previously.
|
228
|
+
if session[:jira_auth]
|
229
|
+
@jira_client.set_access_token(
|
230
|
+
session[:jira_auth]['access_token'],
|
231
|
+
session[:jira_auth]['access_key']
|
232
|
+
)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
```
|
237
|
+
|
238
|
+
Create a controller for handling the OAuth conversation.
|
239
|
+
|
240
|
+
```ruby
|
241
|
+
# app/controllers/jira_sessions_controller.rb
|
242
|
+
class JiraSessionsController < ApplicationController
|
243
|
+
|
244
|
+
before_filter :get_jira_client
|
245
|
+
|
246
|
+
def new
|
247
|
+
callback_url = 'http://callback'
|
248
|
+
request_token = @jira_client.request_token(oauth_callback: callback_url)
|
249
|
+
session[:request_token] = request_token.token
|
250
|
+
session[:request_secret] = request_token.secret
|
251
|
+
|
252
|
+
redirect_to request_token.authorize_url
|
253
|
+
end
|
254
|
+
|
255
|
+
def authorize
|
256
|
+
request_token = @jira_client.set_request_token(
|
257
|
+
session[:request_token], session[:request_secret]
|
258
|
+
)
|
259
|
+
access_token = @jira_client.init_access_token(
|
260
|
+
:oauth_verifier => params[:oauth_verifier]
|
261
|
+
)
|
262
|
+
|
263
|
+
session[:jira_auth] = {
|
264
|
+
:access_token => access_token.token,
|
265
|
+
:access_key => access_token.secret
|
266
|
+
}
|
267
|
+
|
268
|
+
session.delete(:request_token)
|
269
|
+
session.delete(:request_secret)
|
270
|
+
|
271
|
+
redirect_to projects_path
|
272
|
+
end
|
273
|
+
|
274
|
+
def destroy
|
275
|
+
session.data.delete(:jira_auth)
|
276
|
+
end
|
277
|
+
end
|
278
|
+
```
|
279
|
+
|
280
|
+
Create your own controllers for the JIRA resources you wish to access.
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
# app/controllers/issues_controller.rb
|
284
|
+
class IssuesController < ApplicationController
|
285
|
+
before_filter :get_jira_client
|
286
|
+
def index
|
287
|
+
@issues = @jira_client.Issue.all
|
288
|
+
end
|
289
|
+
|
290
|
+
def show
|
291
|
+
@issue = @jira_client.Issue.find(params[:id])
|
292
|
+
end
|
293
|
+
end
|
294
|
+
```
|
295
|
+
|
296
|
+
## Using the API Gem in your Sinatra application
|
297
|
+
|
298
|
+
Here's the same example as a Sinatra application:
|
299
|
+
|
300
|
+
```ruby
|
301
|
+
require 'zk-jira-ruby'
|
302
|
+
class App < Sinatra::Base
|
303
|
+
enable :sessions
|
304
|
+
|
305
|
+
# This section gets called before every request. Here, we set up the
|
306
|
+
# OAuth consumer details including the consumer key, private key,
|
307
|
+
# site uri, and the request token, access token, and authorize paths
|
308
|
+
before do
|
309
|
+
options = {
|
310
|
+
:site => 'http://localhost:2990/',
|
311
|
+
:context_path => '/jira',
|
312
|
+
:signature_method => 'RSA-SHA1',
|
313
|
+
:request_token_path => "/plugins/servlet/oauth/request-token",
|
314
|
+
:authorize_path => "/plugins/servlet/oauth/authorize",
|
315
|
+
:access_token_path => "/plugins/servlet/oauth/access-token",
|
316
|
+
:private_key_file => "rsakey.pem",
|
317
|
+
:rest_base_path => "/rest/api/2",
|
318
|
+
:consumer_key => "zk-jira-ruby-example"
|
319
|
+
}
|
320
|
+
|
321
|
+
@jira_client = JIRA::Client.new(options)
|
322
|
+
@jira_client.consumer.http.set_debug_output($stderr)
|
323
|
+
|
324
|
+
# Add AccessToken if authorised previously.
|
325
|
+
if session[:jira_auth]
|
326
|
+
@jira_client.set_access_token(
|
327
|
+
session[:jira_auth][:access_token],
|
328
|
+
session[:jira_auth][:access_key]
|
329
|
+
)
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
|
334
|
+
# Starting point: http://<yourserver>/
|
335
|
+
# This will serve up a login link if you're not logged in. If you are, it'll show some user info and a
|
336
|
+
# signout link
|
337
|
+
get '/' do
|
338
|
+
if !session[:jira_auth]
|
339
|
+
# not logged in
|
340
|
+
<<-eos
|
341
|
+
<h1>zk-jira-ruby (JIRA 5 Ruby Gem) demo </h1>You're not signed in. Why don't you
|
342
|
+
<a href=/signin>sign in</a> first.
|
343
|
+
eos
|
344
|
+
else
|
345
|
+
#logged in
|
346
|
+
@issues = @jira_client.Issue.all
|
347
|
+
|
348
|
+
# HTTP response inlined with bind data below...
|
349
|
+
<<-eos
|
350
|
+
You're now signed in. There #{@issues.count == 1 ? "is" : "are"} #{@issues.count}
|
351
|
+
issue#{@issues.count == 1 ? "" : "s"} in this JIRA instance. <a href='/signout'>Signout</a>
|
352
|
+
eos
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
# http://<yourserver>/signin
|
357
|
+
# Initiates the OAuth dance by first requesting a token then redirecting to
|
358
|
+
# http://<yourserver>/auth to get the @access_token
|
359
|
+
get '/signin' do
|
360
|
+
callback_url = "#{request.base_url}/callback"
|
361
|
+
request_token = @jira_client.request_token(oauth_callback: callback_url)
|
362
|
+
session[:request_token] = request_token.token
|
363
|
+
session[:request_secret] = request_token.secret
|
364
|
+
|
365
|
+
redirect request_token.authorize_url
|
366
|
+
end
|
367
|
+
|
368
|
+
# http://<yourserver>/callback
|
369
|
+
# Retrieves the @access_token then stores it inside a session cookie. In a real app,
|
370
|
+
# you'll want to persist the token in a datastore associated with the user.
|
371
|
+
get "/callback" do
|
372
|
+
request_token = @jira_client.set_request_token(
|
373
|
+
session[:request_token], session[:request_secret]
|
374
|
+
)
|
375
|
+
access_token = @jira_client.init_access_token(
|
376
|
+
:oauth_verifier => params[:oauth_verifier]
|
377
|
+
)
|
378
|
+
|
379
|
+
session[:jira_auth] = {
|
380
|
+
:access_token => access_token.token,
|
381
|
+
:access_key => access_token.secret
|
382
|
+
}
|
383
|
+
|
384
|
+
session.delete(:request_token)
|
385
|
+
session.delete(:request_secret)
|
386
|
+
|
387
|
+
redirect "/"
|
388
|
+
end
|
389
|
+
|
390
|
+
# http://<yourserver>/signout
|
391
|
+
# Expires the session
|
392
|
+
get "/signout" do
|
393
|
+
session.delete(:jira_auth)
|
394
|
+
redirect "/"
|
395
|
+
end
|
396
|
+
end
|
397
|
+
```
|
398
|
+
|
399
|
+
## Using the API Gem in a 2 legged context
|
400
|
+
|
401
|
+
Here's an example on how to use 2 legged OAuth:
|
402
|
+
```ruby
|
403
|
+
require 'rubygems'
|
404
|
+
require 'pp'
|
405
|
+
require 'zk-jira-ruby'
|
406
|
+
|
407
|
+
options = {
|
408
|
+
:site => 'http://localhost:2990/',
|
409
|
+
:context_path => '/jira',
|
410
|
+
:signature_method => 'RSA-SHA1',
|
411
|
+
:private_key_file => "rsakey.pem",
|
412
|
+
:rest_base_path => "/rest/api/2",
|
413
|
+
:auth_type => :oauth_2legged,
|
414
|
+
:consumer_key => "zk-jira-ruby-example"
|
415
|
+
}
|
416
|
+
|
417
|
+
client = JIRA::Client.new(options)
|
418
|
+
|
419
|
+
client.set_access_token("","")
|
420
|
+
|
421
|
+
# Show all projects
|
422
|
+
projects = client.Project.all
|
423
|
+
|
424
|
+
projects.each do |project|
|
425
|
+
puts "Project -> key: #{project.key}, name: #{project.name}"
|
426
|
+
end
|
427
|
+
```
|