gitlab_ci 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ gitlab_ci
2
+ ============
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ begin
6
+ require 'bundler/setup'
7
+ rescue LoadError => e
8
+ abort e.message
9
+ end
10
+
11
+ require 'rake'
12
+
13
+ require 'rubygems/tasks'
14
+ Gem::Tasks.new
15
+
16
+ require 'rspec/core/rake_task'
17
+ RSpec::Core::RakeTask.new
18
+
19
+ task :test => :spec
20
+ task :default => :spec
data/gitlab_ci.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'gitlab/ci/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "gitlab_ci"
9
+ gem.version = GitlabCi::VERSION
10
+ gem.summary = %q{Gitlab is a Ruby wrapper and CLI for the GitLab API}
11
+ gem.description = %q{Gitlab is a Ruby wrapper and CLI for the [GitLab-CI API](https://gitlab.com/gitlab-org/gitlab-ee/tree/master/doc/ci/api).}
12
+ gem.license = "MIT"
13
+ gem.authors = ["Christo De Lange"]
14
+ gem.email = "rubygems@dldinternet.com"
15
+ gem.homepage = "http://github.com/dldinternet/gitlab_ci"
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+
19
+ `git submodule --quiet foreach --recursive pwd`.split($/).each do |submodule|
20
+ submodule.sub!("#{Dir.pwd}/",'')
21
+
22
+ Dir.chdir(submodule) do
23
+ `git ls-files`.split($/).map do |subpath|
24
+ gem.files << File.join(submodule,subpath)
25
+ end
26
+ end
27
+ end
28
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
29
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
30
+ gem.require_paths = ['lib']
31
+
32
+ gem.add_runtime_dependency 'gitlab', '~> 3.6', '>= 3.6.2'
33
+
34
+ gem.add_development_dependency 'bundler', '~> 1.10'
35
+ gem.add_development_dependency 'rake', '~> 10.0'
36
+ gem.add_development_dependency 'rspec', '~> 3.0'
37
+ gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
38
+ end
data/lib/gitlab/ci.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'gitlab/ci/version'
2
+ require 'gitlab'
3
+ require 'terminal-table/import'
4
+ require 'gitlab/configuration'
5
+ require 'gitlab/ci/client'
6
+
7
+ module Gitlab
8
+ module CI
9
+ extend Configuration
10
+
11
+ # Alias for Gitlab::Client.new
12
+ #
13
+ # @return [Gitlab::Client]
14
+ def self.client(options={})
15
+ Gitlab::Client.new(options)
16
+ end
17
+
18
+ # Delegate to Gitlab::Client
19
+ def self.method_missing(method, *args, &block)
20
+ return super unless client.respond_to?(method)
21
+ client.send(method, *args, &block)
22
+ end
23
+
24
+ # Delegate to Gitlab::Client
25
+ def self.respond_to?(method)
26
+ client.respond_to?(method) || super
27
+ end
28
+
29
+ # Delegate to HTTParty.http_proxy
30
+ def self.http_proxy(address=nil, port=nil, username=nil, password=nil)
31
+ Gitlab::Request.http_proxy(address, port, username, password)
32
+ end
33
+
34
+ # Returns an unsorted array of available client methods.
35
+ #
36
+ # @return [Array<Symbol>]
37
+ def self.actions
38
+ hidden = /endpoint|private_token|auth_token|user_agent|sudo|get|post|put|\Adelete\z|validate|set_request_defaults|httparty/
39
+ (Gitlab::CI::Client.instance_methods - Object.methods).reject { |e| e[hidden] }
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,90 @@
1
+ require 'gitlab'
2
+ require 'gitlab/ci/help'
3
+ require 'gitlab/ci/cli_helpers'
4
+
5
+ module Gitlab
6
+ module CI
7
+ class CLI
8
+ extend Helpers
9
+
10
+ # Starts a new CLI session.
11
+ #
12
+ # @example
13
+ # Gitlab::CLI.start(['help'])
14
+ # Gitlab::CLI.start(['help', 'issues'])
15
+ #
16
+ # @param [Array] args The command and it's optional arguments.
17
+ def self.start(args)
18
+ command = args.shift.strip rescue 'help'
19
+ run(command, args)
20
+ end
21
+
22
+ # Processes a CLI command and outputs a result to the stream (stdout).
23
+ #
24
+ # @example
25
+ # Gitlab::CLI.run('help')
26
+ # Gitlab::CLI.run('help', ['issues'])
27
+ #
28
+ # @param [String] cmd The name of a command.
29
+ # @param [Array] args The optional arguments for a command.
30
+ # @return [nil]
31
+ def self.run(cmd, args=[])
32
+ case cmd
33
+ when 'help'
34
+ puts help(args.shift) { |out| out.gsub!(/Gitlab\./, 'gitlab ') }
35
+ when 'info'
36
+ endpoint = Gitlab.endpoint ? Gitlab.endpoint : 'not set'
37
+ private_token = Gitlab.private_token ? Gitlab.private_token : 'not set'
38
+ puts "Gitlab endpoint is #{endpoint}"
39
+ puts "Gitlab private token is #{private_token}"
40
+ puts "Ruby Version is #{RUBY_VERSION}"
41
+ puts "Gitlab Ruby Gem #{Gitlab::VERSION}"
42
+ when '-v', '--version'
43
+ puts "Gitlab Ruby Gem #{Gitlab::VERSION}"
44
+ when 'shell'
45
+ Gitlab::Shell.start
46
+ else
47
+ if args.include? '--json'
48
+ @json_output = true
49
+ args.delete '--json'
50
+ end
51
+
52
+ unless valid_command?(cmd)
53
+ puts "Unknown command. Run `gitlab help` for a list of available commands."
54
+ exit(1)
55
+ end
56
+
57
+ if args.any? && (args.last.start_with?('--only=') || args.last.start_with?('--except='))
58
+ command_args = args[0..-2]
59
+ else
60
+ command_args = args
61
+ end
62
+
63
+ begin
64
+ command_args.map! { |arg| symbolize_keys(yaml_load(arg)) }
65
+ rescue => e
66
+ puts e.message
67
+ exit 1
68
+ end
69
+
70
+ confirm_command(cmd)
71
+
72
+ data = gitlab_helper(cmd, command_args) { exit(1) }
73
+
74
+ render_output(cmd, args, data)
75
+ end
76
+ end
77
+
78
+ # Helper method that checks whether we want to get the output as json
79
+ # @return [nil]
80
+ def self.render_output(cmd, args, data)
81
+ if @json_output
82
+ output_json(cmd, args, data)
83
+ else
84
+ output_table(cmd, args, data)
85
+ end
86
+ end
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,50 @@
1
+ require 'yaml'
2
+ require 'json'
3
+ require 'gitlab/cli_helpers'
4
+
5
+ module Gitlab
6
+ module CI
7
+ class CLI
8
+ # Defines methods related to CLI output and formatting.
9
+ module Helpers
10
+ include Gitlab::CLI::Helpers
11
+ extend Gitlab::CI::CLI::Helpers
12
+
13
+ # Returns actions available to CLI & Shell
14
+ #
15
+ # @return [Array]
16
+ def actions
17
+ @actions ||= Gitlab::CI.actions
18
+ end
19
+
20
+ # Returns Gitlab::Client instance
21
+ #
22
+ # @return [Gitlab::Client]
23
+ def client
24
+ @client ||= Gitlab::CI::Client.new(endpoint: (Gitlab.endpoint || ''))
25
+ end
26
+
27
+ # Confirms command is valid.
28
+ #
29
+ # @return [Boolean]
30
+ def valid_command?(cmd)
31
+ command = cmd.is_a?(Symbol) ? cmd : cmd.to_sym
32
+ Gitlab::CI.actions.include?(command)
33
+ end
34
+
35
+ # Gets defined help for a specific command/action.
36
+ #
37
+ # @return [String]
38
+ def help(cmd=nil, &block)
39
+ if cmd.nil? || Gitlab::CI::Help.help_map.key?(cmd)
40
+ Gitlab::CI::Help.actions_table(cmd)
41
+ else
42
+ Gitlab::CI::Help.get_help(cmd, &block)
43
+ end
44
+ end
45
+
46
+ end
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,13 @@
1
+ module Gitlab
2
+ module CI
3
+ # Wrapper for the Gitlab REST API.
4
+ class Client < API
5
+ Dir[File.expand_path('../client/*.rb', __FILE__)].each { |f| require f }
6
+
7
+ include Gitlab::CI::Client::Builds
8
+ include Gitlab::CI::Client::Commits
9
+ include Gitlab::CI::Client::Projects
10
+ include Gitlab::CI::Client::Runners
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ class Gitlab::CI::Client
2
+ # Defines methods related to builds.
3
+ # @see https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/builds.md
4
+ module Builds
5
+ # Runs oldest pending build by runner
6
+ #
7
+ # @example
8
+ # Gitlab.register_build('7a14d63e4b1af83171f4eb3d4a5246')
9
+ #
10
+ # @param [String] token (required) - The unique token of runner
11
+ # @return [Array<Gitlab::ObjectifiedHash>]
12
+ def register_build(token)
13
+ post("/builds/register", body: { token: token })
14
+ end
15
+
16
+ # Update details of an existing build
17
+ #
18
+ # @example
19
+ # Gitlab.update_build(5)
20
+ #
21
+ # @param [Integer,String] id (required) - The ID of a project
22
+ # @param [String] state (optional) - The state of a build
23
+ # @param [String] trace (optional) - The trace of a build
24
+ # @return <Gitlab::ObjectifiedHash]
25
+ def update_build(id, state=nil, trace=nil)
26
+ if state or trace
27
+ put("/builds/#{id}", body: { state: state, trace: trace })
28
+ else
29
+ "{}"
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,44 @@
1
+ class Gitlab::CI::Client
2
+ # Defines methods related to repository commits.
3
+ # @see https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/commits.md
4
+ module Commits
5
+ # Get list of commits per project
6
+ #
7
+ # @example
8
+ # Gitlab.commits('viking')
9
+ # Gitlab.repo_commits('gitlab', :ref_name => 'api')
10
+ #
11
+ # @param [Integer, String] project_id (required) - The ID of a project
12
+ # @param [String] project_token (required) - Project token
13
+ # @param [Hash] options A customizable set of options.
14
+ # @option options [Integer] :page The page number.
15
+ # @option options [Integer] :per_page The number of results per page.
16
+ # @return [Array<Gitlab::ObjectifiedHash>]
17
+ def commits(project, token)
18
+ get("/commits", body: { project_id: project, project_token: token })
19
+ end
20
+
21
+ # Inform GitLab CI about new commit you want it to build.
22
+ #
23
+ # @example
24
+ # Gitlab.commit(14, '6104942438c14ec7bd21c6cd5bd995272b3faff6', hash)
25
+ # Gitlab.repo_commit(3, 'ed899a2f4b50b4370feeea94676502b42383c746', hash)
26
+ #
27
+ # @param [Integer] project_id (required) - The ID of a project
28
+ # @param [Integer] project_token (requires) - Project token
29
+ # @param [Hash] data The commit data.
30
+ # Parameters:
31
+ # id (required) - The ID of a project
32
+ # sha (required) - The commit hash
33
+ # note (required) - Text of comment
34
+ # path (optional) - The file path
35
+ # line (optional) - The line number
36
+ # line_type (optional) - The type of line (new or old)
37
+ # @return [Gitlab::ObjectifiedHash]
38
+ def commit(project, token, data)
39
+ post("/commits", body: data.merge({ project_id: project, project_token: token }))
40
+ end
41
+ alias_method :create_commit, :commit
42
+
43
+ end
44
+ end
@@ -0,0 +1,98 @@
1
+ class Gitlab::CI::Client
2
+ # Defines methods related to projects.
3
+ # @see https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/projects.md
4
+ module Projects
5
+ # Lists all projects that the authenticated user has access to.
6
+ #
7
+ # @example
8
+ # Gitlab::CI.projects
9
+ #
10
+ # @param [Hash] options A customizable set of options.
11
+ # @option options [Integer] :page The page number.
12
+ # @option options [Integer] :per_page The number of results per page.
13
+ # @option options [String] :scope Scope of projects. 'owned' for list of projects owned by the authenticated user, 'all' to get all projects (admin only)
14
+ # @return [Array<Gitlab::ObjectifiedHash>]
15
+ def projects(options={})
16
+ if options[:scope]
17
+ get("/projects/#{options[:scope]}", query: options)
18
+ else
19
+ get("/projects", query: options)
20
+ end
21
+ end
22
+
23
+ # Returns information about a single project for which the user is authorized.
24
+ #
25
+ # @example
26
+ # Gitlab::CI.project(3)
27
+ # Gitlab::CI.project('gitlab')
28
+ #
29
+ # @param [Integer, String] id The ID of the GitLab CI project
30
+ # @return [Gitlab::ObjectifiedHash]
31
+ def project(id)
32
+ get("/projects/#{id}")
33
+ end
34
+
35
+ # Creates a GitLab CI project using GitLab project details.
36
+ #
37
+ # @example
38
+ # Gitlab::CI.create_project('gitlab')
39
+ # Gitlab::CI.create_project('viking', :description => 'Awesome project')
40
+ # Gitlab::CI.create_project('Red', :wall_enabled => false)
41
+ #
42
+ # @param [String] name (required) - The name of the project
43
+ # @param [String] gitlab_id (required) - The ID of the project on the GitLab instance
44
+ # @param [String] default_ref (optional) - The branch to run on (default to master)
45
+ # @return [Gitlab::ObjectifiedHash] Information about created project.
46
+ def create_project(name, id, ref='master')
47
+ url = "/projects"
48
+ post(url, body: { name: name, gitlab_id: id, default_ref: ref })
49
+ end
50
+
51
+ # Updates a GitLab CI project using GitLab project details that the authenticated user has access to.
52
+ #
53
+ # @example
54
+ # Gitlab::CI.edit_project(4, "name", "branch")
55
+ #
56
+ # @param [String] name - The name of the project
57
+ # @param [String] default_ref - The branch to run on (default to master)
58
+ # @return [Gitlab::ObjectifiedHash] Information about deleted project.
59
+ def edit_project(id, name, ref='master')
60
+ put("/projects/#{id}", body: { name: name, gitlab_id: id, default_ref: ref })
61
+ end
62
+
63
+ # Removes a GitLab CI project that the authenticated user has access to.
64
+ #
65
+ # @example
66
+ # Gitlab::CI.delete_project(4)
67
+ #
68
+ # @param [Integer, String] id (required) - The ID of the GitLab CI project
69
+ # @return [Gitlab::ObjectifiedHash] Information about deleted project.
70
+ def delete_project(id)
71
+ delete("/projects/#{id}")
72
+ end
73
+
74
+ # Links a runner to a project so that it can make builds (only via authorized user).
75
+ #
76
+ # @example
77
+ # Gitlab::CI.link_project_to_runner(4,5)
78
+ #
79
+ # @param [Integer, String] id (required) - The ID of the GitLab CI project
80
+ # @param [Integer, String] runner_id (required) - The ID of the GitLab CI runner
81
+ # @return [Gitlab::ObjectifiedHash] Information about deleted project.
82
+ def link_project_to_runner(id,runner)
83
+ post("/projects/#{id}/runners/#{runner}", body: { id: id, runner_id: runner })
84
+ end
85
+
86
+ # Removes a runner from a project so that it can not make builds (only via authorized user).
87
+ #
88
+ # @example
89
+ # Gitlab::CI.remove_project_from_runner(4,5)
90
+ #
91
+ # @param [Integer, String] id (required) - The ID of the GitLab CI project
92
+ # @param [Integer, String] runner_id (required) - The ID of the GitLab CI runner
93
+ # @return [Gitlab::ObjectifiedHash] Information about deleted project.
94
+ def remove_project_from_runner(id,runner)
95
+ delete("/projects/#{id}/runners/#{runner}")
96
+ end
97
+ end
98
+ end