gitlab_cli 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ module GitlabCli
2
+ class User
3
+ attr_accessor :id, :username, :email, :name, :blocked, :created_at, :bio, :skype, :linkedin, :twitter, :extern_uid, :provider
4
+
5
+ def initialize(id, username, email, name, blocked, created_at, bio=nil, skype=nil, linkedin=nil, twitter=nil, extern_uid=nil, provider=nil)
6
+ @id = id
7
+ @username = username
8
+ @email = email
9
+ @name = name
10
+ @blocked = blocked
11
+ @created_at = created_at
12
+ @bio = bio
13
+ @skype = skype
14
+ @linkedin = linkedin
15
+ @twitter = twitter
16
+ @extern_uid = extern_uid
17
+ @provider = provider
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,64 @@
1
+ module GitlabCli
2
+ module Util
3
+ autoload :Projects, 'gitlab_cli/util/projects'
4
+ autoload :Snippets, 'gitlab_cli/util/snippets'
5
+ autoload :Project, 'gitlab_cli/util/project'
6
+ autoload :Snippet, 'gitlab_cli/util/snippet'
7
+
8
+ autoload :RestClient, 'rest-client'
9
+ autoload :JSON, 'json'
10
+
11
+ ## Internal methods
12
+ # Get project id
13
+ def self.get_project_id(project)
14
+ projects = GitlabCli::Util::Projects.get_all
15
+ project = projects.detect do |p|
16
+ p.path_with_namespace == project
17
+ end
18
+
19
+ unless project
20
+ GitlabCli.ui.error "Invalid project name or id."
21
+ exit 1
22
+ end
23
+ project.id
24
+ end
25
+
26
+ # Construct private token for URL
27
+ def self.url_token
28
+ "?private_token=#{GitlabCli::Config[:private_token]}"
29
+ end
30
+
31
+ def self.numeric?(string)
32
+ Float(string) != nil rescue false
33
+ end
34
+
35
+ # Check RestClient response code
36
+ def self.check_response_code(response)
37
+ if response =~ /401/
38
+ ## Unauthorized
39
+ GitlabCli.ui.error "User token was not present or is not valid."
40
+ exit 1
41
+ elsif response =~ /403/
42
+ ## Forbidden
43
+ GitlabCli.ui.error "You are not authorized to complete this action"
44
+ exit 1
45
+ elsif response =~ /404/
46
+ ## Not found
47
+ GitlabCli.ui.error "Resource could not be found."
48
+ exit 1
49
+ elsif response =~ /405/
50
+ ## Method not allowed
51
+ GitlabCli.ui.error "This request is not supported."
52
+ exit 1
53
+ elsif response =~ /409/
54
+ ## Conflicting resource
55
+ GitlabCli.ui.error "A conflicting resource already exists."
56
+ exit 1
57
+ elsif response =~ /500/
58
+ ## Server error
59
+ GitlabCli.ui.error "Oops. Something went wrong. Please try again."
60
+ end
61
+ response
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,32 @@
1
+ module GitlabCli
2
+ module Util
3
+ class Project
4
+ # Get project object
5
+ def self.get(project)
6
+ id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)
7
+ url = "api/v3/projects/%s%s" % [id,GitlabCli::Util.url_token]
8
+
9
+ begin
10
+ response = RestClient.get URI.join(GitlabCli::Config[:gitlab_url],url).to_s
11
+ rescue SocketError => e
12
+ GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
13
+ exit 1
14
+ rescue Exception => e
15
+ GitlabCli::Util.check_response_code(e.response)
16
+ end
17
+
18
+ data = JSON.parse(response)
19
+ GitlabCli::Project.new(data['id'],data['name'],data['description'],data['default_branch'],data['public'],data['path'],data['path_with_namespace'],data['issues_enabled'],data['merge_requests_enabled'],data['wall_enabled'],data['wiki_enabled'],data['created_at'],data['owner'])
20
+ end
21
+
22
+ # Get a project's path with namespace
23
+ def self.get_project_path_with_namespace(project_id)
24
+ projects = GitlabCli::Util::Projects.get_all
25
+ projects.each do |p|
26
+ # Return id of matching project. Return statement must be used for proper return here.
27
+ return p.path_with_namespace if p.id.to_i == project_id.to_i
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ module GitlabCli
2
+ module Util
3
+ class Projects
4
+ def self.get_all
5
+ begin
6
+ response = Array.new
7
+ page = 1
8
+ while true do
9
+ url = "api/v3/projects%s&page=%s&per_page=100" % [GitlabCli::Util.url_token, page]
10
+ page_data = RestClient.get URI.join(GitlabCli::Config[:gitlab_url],url).to_s
11
+ # Length of 2 indicates empty Array (represented as a string)
12
+ break if page_data.length == 2
13
+ response.concat JSON.parse(page_data)
14
+ page += 1
15
+ end
16
+ rescue SocketError => e
17
+ GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
18
+ exit 1
19
+ rescue Exception => e
20
+ GitlabCli::Util.check_response_code(e.response)
21
+ end
22
+
23
+ projects = response.map do |p|
24
+ GitlabCli::Project.new(p['id'],p['name'],p['description'],p['default_branch'],p['public'],p['path'],p['path_with_namespace'],p['issues_enabled'],p['merge_requests_enabled'],p['wall_enabled'],p['wiki_enabled'],p['created_at'],p['owner'])
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,123 @@
1
+ module GitlabCli
2
+ module Util
3
+ class Snippet
4
+ # Snippet - Get snippet object
5
+ def self.get(project, snippet)
6
+ id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)
7
+
8
+ begin
9
+ response = RestClient.get URI.join(GitlabCli::Config[:gitlab_url],"api/v3/projects/#{id}/snippets/#{snippet}#{GitlabCli::Util.url_token}").to_s
10
+ rescue SocketError => e
11
+ GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
12
+ exit 1
13
+ rescue Exception => e
14
+ GitlabCli::Util.check_response_code(e.response)
15
+ end
16
+ data = JSON.parse(response)
17
+ GitlabCli::Snippet.new(data['id'],data['title'],data['file_name'],data['expires_at'],data['updated_at'],data['created_at'],id,data['author'])
18
+ end
19
+
20
+ # Snippet - Create
21
+ def self.create(project, title, file_name, code)
22
+ ## Adapted from https://github.com/ripienaar/snipper/blob/master/lib/snipper/util.rb
23
+ if STDIN.tty?
24
+ if File.readable?(code)
25
+ content = File.read(code)
26
+ else
27
+ GitlabCli.ui.error "Cannot read the specified file."
28
+ exit 1
29
+ end
30
+ else
31
+ content = STDIN.read
32
+ end
33
+ ##
34
+
35
+ id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)
36
+
37
+ url = "api/v3/projects/%s/snippets%s" % [id, GitlabCli::Util.url_token]
38
+ payload = {:title => title, :file_name => file_name, :code => content}
39
+
40
+ begin
41
+ response = RestClient.post URI.join(GitlabCli::Config[:gitlab_url],url).to_s, payload
42
+ rescue SocketError => e
43
+ GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
44
+ exit 1
45
+ rescue Exception => e
46
+ GitlabCli::Util.check_response_code(e.response)
47
+ end
48
+ data = JSON.parse(response)
49
+ GitlabCli::Snippet.new(data['id'],data['title'],data['file_name'],data['expires_at'],data['updated_at'],data['created_at'],id,data['author'])
50
+ end
51
+
52
+ # Snippet - View
53
+ def self.view(project, snippet)
54
+ id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)
55
+
56
+ url = "api/v3/projects/%s/snippets/%s/raw%s" % [id, snippet, GitlabCli::Util.url_token]
57
+
58
+ begin
59
+ response = RestClient.get URI.join(GitlabCli::Config[:gitlab_url],url).to_s
60
+ rescue SocketError => e
61
+ GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
62
+ exit 1
63
+ rescue Exception => e
64
+ GitlabCli::Util.check_response_code(e.response)
65
+ end
66
+ end
67
+
68
+ # Snippet - Update
69
+ def self.update(project, snippet, content)
70
+ id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)
71
+
72
+ url = "api/v3/projects/%s/snippets/%s%s" % [id, snippet.id, GitlabCli::Util.url_token]
73
+ payload = {:title => snippet.title, :file_name => snippet.file_name, :code => content}
74
+
75
+ begin
76
+ response = RestClient.put URI.join(GitlabCli::Config[:gitlab_url],url).to_s, payload
77
+ rescue SocketError => e
78
+ GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
79
+ exit 1
80
+ rescue Exception => e
81
+ GitlabCli::Util.check_response_code(e.response)
82
+ end
83
+
84
+ data = JSON.parse(response)
85
+ GitlabCli::Snippet.new(data['id'],data['title'],data['file_name'],data['expires_at'],data['updated_at'],data['created_at'],id,data['author'])
86
+ end
87
+
88
+ # Snippet - Delete
89
+ def self.delete(project, snippet)
90
+ id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)
91
+
92
+ snippet_get = get(project, snippet)
93
+
94
+ if snippet_get
95
+ begin
96
+ response = RestClient.delete URI.join(GitlabCli::Config[:gitlab_url],"api/v3/projects/#{id}/snippets/#{snippet}#{GitlabCli::Util.url_token}").to_s
97
+ rescue SocketError => e
98
+ GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
99
+ exit 1
100
+ rescue Exception => e
101
+ GitlabCli::Util.check_response_code(e.response)
102
+ end
103
+ end
104
+ end
105
+
106
+ # /snippet - Download/Save
107
+ def self.download(project, snippet, file_path)
108
+ id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)
109
+ snippet_content = view(project, snippet)
110
+
111
+ begin
112
+ File.open(file_path, 'w') { |file| file.write(snippet_content) }
113
+ rescue IOError => e
114
+ GitlabCli.ui.error "Cannot save snippet as file. Please check permissions for %s" % [file_path]
115
+ exit 1
116
+ rescue Errno::ENOENT => e
117
+ GitlabCli.ui.error "Specified directory does not exist. Directory must exist to save the snippet file."
118
+ exit 1
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,31 @@
1
+ module GitlabCli
2
+ module Util
3
+ class Snippets
4
+ def self.get_all(project)
5
+ id = GitlabCli::Util.numeric?(project) ? project : GitlabCli::Util.get_project_id(project)
6
+
7
+ begin
8
+ response = Array.new
9
+ page = 1
10
+ while true do
11
+ url = "api/v3/projects/%s/snippets%s&page=%s&per_page=100" % [id, GitlabCli::Util.url_token, page]
12
+ page_data = RestClient.get URI.join(GitlabCli::Config[:gitlab_url],url).to_s
13
+ # Length of 2 indicates empty Array (represented as a string)
14
+ break if page_data.length == 2
15
+ response.concat JSON.parse(page_data)
16
+ page += 1
17
+ end
18
+ rescue SocketError => e
19
+ GitlabCli.ui.error "Could not contact the GitLab server. Please check connectivity and verify the 'gitlab_url' configuration setting."
20
+ exit 1
21
+ rescue Exception => e
22
+ GitlabCli::Util.check_response_code(e.response)
23
+ end
24
+
25
+ snippets = response.map do |s|
26
+ GitlabCli::Snippet.new(s['id'],s['title'],s['file_name'],s['expires_at'],s['updated_at'],s['created_at'],id,s['author'])
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module GitlabCli
2
+ VERSION = "2.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitlab_cli
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 2
7
+ - 0
8
+ - 0
9
+ version: 2.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Drew Blessing
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-05-17 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thor
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 17
30
+ - 0
31
+ version: 0.17.0
32
+ - - <
33
+ - !ruby/object:Gem::Version
34
+ segments:
35
+ - 0
36
+ - 19
37
+ version: "0.19"
38
+ type: :runtime
39
+ version_requirements: *id001
40
+ - !ruby/object:Gem::Dependency
41
+ name: json
42
+ prerelease: false
43
+ requirement: &id002 !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 1
49
+ - 7
50
+ - 7
51
+ version: 1.7.7
52
+ type: :runtime
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: rest-client
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 1
63
+ - 6
64
+ - 7
65
+ version: 1.6.7
66
+ type: :runtime
67
+ version_requirements: *id003
68
+ description: Gitlab Command Line Tool
69
+ email:
70
+ - drew.blessing@buckle.com
71
+ executables:
72
+ - gitlab
73
+ extensions: []
74
+
75
+ extra_rdoc_files: []
76
+
77
+ files:
78
+ - .gitignore
79
+ - CHANGELOG
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/gitlab
85
+ - config.yml.sample
86
+ - doc/Classes.md
87
+ - doc/Commands.md
88
+ - doc/Library.md
89
+ - doc/Objects.md
90
+ - gitlab_cli.gemspec
91
+ - lib/gitlab_cli.rb
92
+ - lib/gitlab_cli/cli.rb
93
+ - lib/gitlab_cli/command.rb
94
+ - lib/gitlab_cli/command/project.rb
95
+ - lib/gitlab_cli/command/snippet.rb
96
+ - lib/gitlab_cli/config.rb
97
+ - lib/gitlab_cli/project.rb
98
+ - lib/gitlab_cli/snippet.rb
99
+ - lib/gitlab_cli/ui.rb
100
+ - lib/gitlab_cli/user.rb
101
+ - lib/gitlab_cli/util.rb
102
+ - lib/gitlab_cli/util/project.rb
103
+ - lib/gitlab_cli/util/projects.rb
104
+ - lib/gitlab_cli/util/snippet.rb
105
+ - lib/gitlab_cli/util/snippets.rb
106
+ - lib/gitlab_cli/version.rb
107
+ has_rdoc: true
108
+ homepage: https://github.com/drewblessing/gitlab-cli
109
+ licenses: []
110
+
111
+ post_install_message:
112
+ rdoc_options: []
113
+
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project:
133
+ rubygems_version: 1.3.6
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Many people prefer to work from the CLI when possible. This tool aims to bring some of the GitLab functionality into the CLI to avoid repeated trips to the web UI
137
+ test_files: []
138
+