jenkins_api_client 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -86,7 +86,9 @@ module JenkinsApi
86
86
 
87
87
  # Initializes a new node object
88
88
  #
89
- # @param [Object] client reference to Client
89
+ # @param client [Client] the client object
90
+ #
91
+ # @return [Node] the node object
90
92
  #
91
93
  def initialize(client)
92
94
  @client = client
@@ -31,7 +31,9 @@ module JenkinsApi
31
31
 
32
32
  # Initializes a new System object.
33
33
  #
34
- # @param [Object] client a reference to Client
34
+ # @param client [Client] the client object
35
+ #
36
+ # @return [System] the system object
35
37
  #
36
38
  def initialize(client)
37
39
  @client = client
@@ -61,7 +63,7 @@ module JenkinsApi
61
63
 
62
64
  # Restarts the Jenkins server
63
65
  #
64
- # @param [Bool] force whether to force restart or wait till all
66
+ # @param [Boolean] force whether to force restart or wait till all
65
67
  # jobs are completed.
66
68
  #
67
69
  def restart(force = false)
@@ -74,6 +76,12 @@ module JenkinsApi
74
76
  end
75
77
  end
76
78
 
79
+ # Performs a force restart of Jenkins server
80
+ #
81
+ def restart!
82
+ restart(true)
83
+ end
84
+
77
85
  # Reload the Jenkins server
78
86
  #
79
87
  def reload
@@ -84,6 +92,7 @@ module JenkinsApi
84
92
  # List all users known to Jenkins by their Full Name
85
93
  #
86
94
  def list_users
95
+ warn "DEPRECATION: System#list_users is deprecated. Please use User#list instead"
87
96
  @logger.info "Obtaining the list of users from jenkins"
88
97
  users = @client.api_get_request("/asynchPeople")
89
98
  names = []
@@ -0,0 +1,119 @@
1
+ #
2
+ # Copyright (c) 2012-2013 Douglas Henderson <dougforpres@gmail.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+
23
+ require 'timeout'
24
+
25
+ module JenkinsApi
26
+ class Client
27
+ # This class is used to communicate with Jenkins and performing some user
28
+ # level operations - currently limited to fetching user info, but can
29
+ # be extended to support updating user fields
30
+ #
31
+ # @since 0.14.0
32
+ #
33
+ class User
34
+
35
+ # Initializes a new User object.
36
+ #
37
+ # @param client [Client] the client object
38
+ #
39
+ # @return [User] the user object
40
+ #
41
+ def initialize(client)
42
+ @client = client
43
+ @logger = @client.logger
44
+ @timeout = @client.timeout
45
+ end
46
+
47
+ # Returns a string representation of System class.
48
+ #
49
+ def to_s
50
+ "#<JenkinsApi::Client::User>"
51
+ end
52
+
53
+ # Get a list of users
54
+ # Response will include same as is available from
55
+ # http://jenkins/user/#{username}
56
+ # userid, display name, and email-address
57
+ #
58
+ # @return [Hash] of [Hash], keyed by Jenkins user id
59
+ # * +fullName+ The jenkins user idoutput+ Console output of the job
60
+ # * +properties+ Size of the text. This ca
61
+ #
62
+ def list
63
+ @logger.info "Obtaining the list of users from jenkins"
64
+ # First we need to get the list of users.
65
+ # This is the same as "System.list_users", but since I didn't want to
66
+ # depend on that class I reproduced the request here.
67
+ userlist = @client.api_get_request("/asynchPeople")
68
+ users = {}
69
+
70
+ userlist['users'].each { |user|
71
+ # Jenkins seems ok to fetch by full-name, as long as perfect match
72
+ # since the name *came* from Jenkins this seems reasonably safe
73
+ user = get(user['user']['fullName'])
74
+ users[user['id']] = user if user
75
+ } unless userlist.nil?
76
+
77
+ return users
78
+ end
79
+
80
+ # Get a single user
81
+ #
82
+ # @param user_id [String] User ID or Full Name
83
+ #
84
+ # @return [Hash]
85
+ # * +id+ Jenkins user id
86
+ # * +fullName+ Full name of user (or user id if not set)
87
+ # * other fields populated by Jenkins - this may vary based on version/plugins
88
+ #
89
+ # @example Example JSON for user info
90
+ # {
91
+ # "absoluteUrl" : "https://myjenkins.example.com/jenkins/user/fred",
92
+ # "description" : "",
93
+ # "fullName" : "Fred Flintstone",
94
+ # "id" : "fred",
95
+ # "property" : [
96
+ # {
97
+ # },
98
+ # {
99
+ # },
100
+ # {
101
+ # "address" : "fred@slaterockandgravel.com"
102
+ # },
103
+ # {
104
+ # },
105
+ # {
106
+ # },
107
+ # {
108
+ # "insensitiveSearch" : false
109
+ # }
110
+ # ]
111
+ # }
112
+ #
113
+ def get(user_id)
114
+ response = @client.api_get_request("/user/#{user_id}")
115
+ end
116
+
117
+ end
118
+ end
119
+ end
@@ -25,7 +25,7 @@ module JenkinsApi
25
25
  # Major version of the gem
26
26
  MAJOR = 0
27
27
  # Minor version of the gem
28
- MINOR = 13
28
+ MINOR = 14
29
29
  # Tiny version of the gem used for patches
30
30
  TINY = 0
31
31
  # Used for pre-releases
@@ -30,7 +30,9 @@ module JenkinsApi
30
30
 
31
31
  # Initializes a new view object
32
32
  #
33
- # @param [Object] client reference to Client
33
+ # @param client [Client] the client object
34
+ #
35
+ # @return [View] the client object
34
36
  #
35
37
  def initialize(client)
36
38
  @client = client
@@ -78,8 +80,8 @@ module JenkinsApi
78
80
  # @option params [String] :status_filter Filter jobs based on the status.
79
81
  # Valid options: all_selected_jobs, enabled_jobs_only,
80
82
  # disabled_jobs_only. Default: all_selected_jobs
81
- # @option params [TrueClass|FalseClass] :filter_queue true or false
82
- # @option params [TrueClass|FalseClass] :filter_executors true or false
83
+ # @option params [Boolean] :filter_queue true or false
84
+ # @option params [Boolean] :filter_executors true or false
83
85
  # @option params [String] :regex Regular expression to filter jobs that
84
86
  # are to be added to the view
85
87
  #
@@ -500,6 +500,13 @@ describe JenkinsApi::Client::Job do
500
500
  end
501
501
  end
502
502
 
503
+ describe "#poll" do
504
+ it "Should poll the specified job for scm changes" do
505
+ response = @client.job.poll(@job_name)
506
+ @valid_post_responses.should include(response.to_i)
507
+ end
508
+ end
509
+
503
510
  describe "#disable" do
504
511
  it "Should disable the specified job and then enable it again" do
505
512
  @client.job.list_details(@job_name)['buildable'].should == true
@@ -74,7 +74,7 @@ describe JenkinsApi::Client::System do
74
74
 
75
75
  describe "#list_users" do
76
76
  it "Should be able to get a list of users" do
77
- @client.system.list_users.size == 1
77
+ @client.system.list_users.should be_an_instance_of(Array)
78
78
  end
79
79
  end
80
80
 
@@ -0,0 +1,49 @@
1
+ #
2
+ # Specifying JenkinsApi::Client::User class capabilities
3
+ # Author: Doug Henderson <dougforpres@gmail.com>
4
+ #
5
+
6
+ require File.expand_path('../spec_helper', __FILE__)
7
+ require 'yaml'
8
+
9
+ describe JenkinsApi::Client::User do
10
+ context "With properly initialized client" do
11
+ before(:all) do
12
+ @creds_file = '~/.jenkins_api_client/spec.yml'
13
+ @valid_post_responses = [200, 201, 302]
14
+ begin
15
+ @client = JenkinsApi::Client.new(
16
+ YAML.load_file(File.expand_path(@creds_file, __FILE__))
17
+ )
18
+ rescue Exception => e
19
+ puts "WARNING: Credentials are not set properly."
20
+ puts e.message
21
+ end
22
+ end
23
+
24
+ describe "InstanceMethods" do
25
+
26
+ describe "#list" do
27
+ it "Should be able to get a list of users" do
28
+ @client.user.list.should be_an_instance_of(Hash)
29
+ end
30
+ end
31
+
32
+ describe "#get" do
33
+ it "Should be able to get a specific user" do
34
+ # Actually, we're gonna get every user in the main user list
35
+ users = @client.user.list
36
+
37
+ users.each do |id, user|
38
+ id.should eq(user['id'])
39
+ fetched = @client.user.get(id)
40
+ fetched.should eq(user)
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
@@ -3,7 +3,7 @@ require File.expand_path('../spec_helper', __FILE__)
3
3
  describe JenkinsApi::Client::BuildQueue do
4
4
  context "With properly initialized Client" do
5
5
  before do
6
- @client = mock
6
+ @client = double
7
7
  mock_logger = Logger.new "/dev/null"
8
8
  @client.should_receive(:logger).and_return(mock_logger)
9
9
  @queue = JenkinsApi::Client::BuildQueue.new(@client)
@@ -6,7 +6,7 @@ describe JenkinsApi::Client::Job do
6
6
 
7
7
  before do
8
8
  mock_logger = Logger.new "/dev/null"
9
- @client = mock
9
+ @client = double
10
10
  @client.should_receive(:logger).and_return(mock_logger)
11
11
  @job = JenkinsApi::Client::Job.new(@client)
12
12
  @sample_json_response = {
@@ -369,6 +369,15 @@ describe JenkinsApi::Client::Job do
369
369
  end
370
370
  end
371
371
 
372
+ describe "#poll" do
373
+ it "accepts the job name and polls the job for scm changes" do
374
+ @client.should_receive(:api_post_request).with(
375
+ "/job/test_job/polling"
376
+ ).and_return(302)
377
+ @job.poll("test_job").should == 302
378
+ end
379
+ end
380
+
372
381
  describe "#enable" do
373
382
  it "accepts the job name and enables the job" do
374
383
  @client.should_receive(:api_post_request).with(
@@ -3,7 +3,7 @@ require File.expand_path('../spec_helper', __FILE__)
3
3
  describe JenkinsApi::Client::Node do
4
4
  context "With properly initialized Client" do
5
5
  before do
6
- @client = mock
6
+ @client = double
7
7
  mock_logger = Logger.new "/dev/null"
8
8
  @client.should_receive(:logger).and_return(mock_logger)
9
9
  @node = JenkinsApi::Client::Node.new(@client)
@@ -5,7 +5,7 @@ describe JenkinsApi::Client::System do
5
5
  before do
6
6
  mock_logger = Logger.new "/dev/null"
7
7
  mock_timeout = 300
8
- @client = mock
8
+ @client = double
9
9
  @client.should_receive(:logger).and_return(mock_logger)
10
10
  @client.should_receive(:timeout).and_return(mock_timeout)
11
11
  @system = JenkinsApi::Client::System.new(@client)
@@ -0,0 +1,126 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe JenkinsApi::Client::User do
4
+ context "With properly initialized Client" do
5
+ FRED_TXT = <<__FRED
6
+ {
7
+ "absoluteUrl" : "https://myjenkins.example.com/jenkins/user/fred",
8
+ "description" : "",
9
+ "fullName" : "Fred Flintstone",
10
+ "id" : "fred",
11
+ "property" : [
12
+ {
13
+ },
14
+ {
15
+ },
16
+ {
17
+ "address" : "fred@slaterockandgravel.com"
18
+ },
19
+ {
20
+ },
21
+ {
22
+ },
23
+ {
24
+ "insensitiveSearch" : false
25
+ }
26
+ ]
27
+ }
28
+ __FRED
29
+
30
+ WILMA_TXT = <<__WILMA
31
+ {
32
+ "absoluteUrl" : "https://myjenkins.example.com/jenkins/user/wilma",
33
+ "description" : "",
34
+ "fullName" : "wilma",
35
+ "id" : "wilma",
36
+ "property" : [
37
+ {
38
+ },
39
+ {
40
+ },
41
+ {
42
+ },
43
+ {
44
+ },
45
+ {
46
+ },
47
+ {
48
+ }
49
+ ]
50
+ }
51
+ __WILMA
52
+
53
+ FRED_JSON = JSON.parse(FRED_TXT)
54
+ WILMA_JSON = JSON.parse(WILMA_TXT)
55
+ PEOPLE_JSON = JSON.parse(<<__PEEPS
56
+ {
57
+ "users" : [
58
+ {
59
+ "lastChange" : 1375293464494,
60
+ "project" : {
61
+ "name" : "a project",
62
+ "url" : "a url to a project"
63
+ },
64
+ "user" : {
65
+ "absoluteUrl" : "a url to a user",
66
+ "fullName" : "Fred Flintstone"
67
+ }
68
+ }
69
+ ]
70
+ }
71
+ __PEEPS
72
+ )
73
+
74
+ USERLIST_JSON = JSON.parse(<<__USERLIST
75
+ {
76
+ "fred": #{FRED_TXT}
77
+ }
78
+ __USERLIST
79
+ )
80
+
81
+ before do
82
+ mock_logger = Logger.new "/dev/null"
83
+ mock_timeout = 300
84
+ @client = double
85
+ @client.should_receive(:logger).and_return(mock_logger)
86
+ @client.should_receive(:timeout).and_return(mock_timeout)
87
+ @client.stub(:api_get_request).with('/asynchPeople').and_return(PEOPLE_JSON)
88
+ @client.stub(:api_get_request).with('/user/Fred Flintstone').and_return(FRED_JSON)
89
+ @client.stub(:api_get_request).with('/user/fred').and_return(FRED_JSON)
90
+ @client.stub(:api_get_request).with('/user/wilma').and_return(WILMA_JSON)
91
+ @user = JenkinsApi::Client::User.new(@client)
92
+ end
93
+
94
+ describe "InstanceMethods" do
95
+ describe "#initialize" do
96
+ it "initializes by receiving an instance of client object" do
97
+ mock_logger = Logger.new "/dev/null"
98
+ mock_timeout = 300
99
+ @client.should_receive(:logger).and_return(mock_logger)
100
+ @client.should_receive(:timeout).and_return(mock_timeout)
101
+ expect(
102
+ lambda{ JenkinsApi::Client::User.new(@client) }
103
+ ).not_to raise_error
104
+ end
105
+ end
106
+
107
+ describe "#list" do
108
+ it "sends a request to list the users" do
109
+ @user.list.should eq(USERLIST_JSON)
110
+ end
111
+ end
112
+
113
+ describe "#get" do
114
+ it "returns dummy user if user cannot be found" do
115
+ # This is artifact of Jenkins - It'll create a user to match the name you give it - even on a fetch
116
+ @user.get("wilma").should eq(WILMA_JSON)
117
+ end
118
+
119
+ it "returns valid user if user can be found" do
120
+ @user.get("fred").should eq(FRED_JSON)
121
+ end
122
+ end
123
+
124
+ end
125
+ end
126
+ end