From a8e93b7f51d968c1380ed210499869b62b07fd15 Mon Sep 17 00:00:00 2001 From: Job van der Voort Date: Tue, 21 Apr 2015 16:21:51 +0200 Subject: Version 7.10.0.rc5 --- lib/api/api.rb | 53 ---- lib/api/api_guard.rb | 172 ----------- lib/api/branches.rb | 119 -------- lib/api/commits.rb | 116 -------- lib/api/deploy_keys.rb | 75 ----- lib/api/entities.rb | 280 ------------------ lib/api/files.rb | 158 ---------- lib/api/group_members.rb | 87 ------ lib/api/groups.rb | 89 ------ lib/api/helpers.rb | 250 ---------------- lib/api/internal.rb | 85 ------ lib/api/issues.rb | 181 ------------ lib/api/labels.rb | 98 ------- lib/api/merge_requests.rb | 249 ---------------- lib/api/milestones.rb | 95 ------ lib/api/namespaces.rb | 23 -- lib/api/notes.rb | 103 ------- lib/api/project_hooks.rb | 108 ------- lib/api/project_members.rb | 106 ------- lib/api/project_snippets.rb | 124 -------- lib/api/projects.rb | 323 --------------------- lib/api/repositories.rb | 190 ------------ lib/api/services.rb | 78 ----- lib/api/session.rb | 21 -- lib/api/system_hooks.rb | 70 ----- lib/api/users.rb | 264 ----------------- lib/assets/.gitkeep | 0 lib/backup/database.rb | 78 ----- lib/backup/manager.rb | 170 ----------- lib/backup/repository.rb | 140 --------- lib/backup/uploads.rb | 30 -- lib/disable_email_interceptor.rb | 8 - lib/email_validator.rb | 21 -- lib/event_filter.rb | 68 ----- lib/extracts_path.rb | 123 -------- lib/file_size_validator.rb | 73 ----- lib/gitlab.rb | 5 - lib/gitlab/access.rb | 73 ----- lib/gitlab/app_logger.rb | 11 - lib/gitlab/auth.rb | 18 -- lib/gitlab/backend/grack_auth.rb | 182 ------------ lib/gitlab/backend/rack_attack_helpers.rb | 31 -- lib/gitlab/backend/shell.rb | 279 ------------------ lib/gitlab/backend/shell_adapter.rb | 11 - lib/gitlab/backend/shell_env.rb | 17 -- lib/gitlab/bitbucket_import.rb | 6 - lib/gitlab/bitbucket_import/client.rb | 99 ------- lib/gitlab/bitbucket_import/importer.rb | 52 ---- lib/gitlab/bitbucket_import/key_adder.rb | 23 -- lib/gitlab/bitbucket_import/key_deleter.rb | 23 -- lib/gitlab/bitbucket_import/project_creator.rb | 26 -- lib/gitlab/blacklist.rb | 34 --- lib/gitlab/closing_issue_extractor.rb | 20 -- lib/gitlab/compare_result.rb | 9 - lib/gitlab/config_helper.rb | 9 - lib/gitlab/contributions_calendar.rb | 56 ---- lib/gitlab/contributor.rb | 9 - lib/gitlab/current_settings.rb | 28 -- lib/gitlab/diff/file.rb | 49 ---- lib/gitlab/diff/line.rb | 12 - lib/gitlab/diff/line_code.rb | 9 - lib/gitlab/diff/parser.rb | 81 ------ lib/gitlab/force_push_check.rb | 15 - lib/gitlab/git.rb | 25 -- lib/gitlab/git_access.rb | 198 ------------- lib/gitlab/git_access_status.rb | 15 - lib/gitlab/git_access_wiki.rb | 11 - lib/gitlab/git_logger.rb | 11 - lib/gitlab/git_ref_validator.rb | 12 - lib/gitlab/github_import/client.rb | 53 ---- lib/gitlab/github_import/importer.rb | 46 --- lib/gitlab/github_import/project_creator.rb | 26 -- lib/gitlab/gitlab_import/client.rb | 82 ------ lib/gitlab/gitlab_import/importer.rb | 50 ---- lib/gitlab/gitlab_import/project_creator.rb | 26 -- lib/gitlab/gitorious_import/client.rb | 31 -- lib/gitlab/gitorious_import/project_creator.rb | 26 -- lib/gitlab/gitorious_import/repository.rb | 37 --- lib/gitlab/google_code_import/client.rb | 48 --- lib/gitlab/google_code_import/repository.rb | 43 --- lib/gitlab/graphs/commits.rb | 49 ---- lib/gitlab/identifier.rb | 23 -- lib/gitlab/import_formatter.rb | 15 - lib/gitlab/inline_diff.rb | 78 ----- lib/gitlab/issues_labels.rb | 27 -- lib/gitlab/key_fingerprint.rb | 55 ---- lib/gitlab/ldap/access.rb | 62 ---- lib/gitlab/ldap/adapter.rb | 89 ------ lib/gitlab/ldap/authentication.rb | 71 ----- lib/gitlab/ldap/config.rb | 122 -------- lib/gitlab/ldap/person.rb | 61 ---- lib/gitlab/ldap/user.rb | 71 ----- lib/gitlab/logger.rb | 32 -- lib/gitlab/markdown_helper.rb | 29 -- lib/gitlab/middleware/static.rb | 13 - lib/gitlab/note_data_builder.rb | 77 ----- lib/gitlab/o_auth/auth_hash.rb | 54 ---- lib/gitlab/o_auth/user.rb | 106 ------- lib/gitlab/popen.rb | 35 --- lib/gitlab/production_logger.rb | 7 - lib/gitlab/project_search_results.rb | 77 ----- lib/gitlab/push_data_builder.rb | 90 ------ lib/gitlab/reference_extractor.rb | 94 ------ lib/gitlab/satellite/action.rb | 56 ---- lib/gitlab/satellite/compare_action.rb | 44 --- lib/gitlab/satellite/files/delete_file_action.rb | 50 ---- lib/gitlab/satellite/files/edit_file_action.rb | 68 ----- lib/gitlab/satellite/files/file_action.rb | 25 -- lib/gitlab/satellite/files/new_file_action.rb | 67 ----- lib/gitlab/satellite/logger.rb | 13 - lib/gitlab/satellite/merge_action.rb | 146 ---------- lib/gitlab/satellite/satellite.rb | 148 ---------- lib/gitlab/search_results.rb | 69 ----- lib/gitlab/seeder.rb | 24 -- lib/gitlab/sidekiq_logger.rb | 7 - lib/gitlab/sidekiq_middleware/arguments_logger.rb | 10 - lib/gitlab/sidekiq_middleware/memory_killer.rb | 53 ---- lib/gitlab/snippet_search_results.rb | 131 --------- lib/gitlab/theme.rb | 39 --- lib/gitlab/upgrader.rb | 102 ------- lib/gitlab/url_builder.rb | 60 ---- lib/gitlab/user_access.rb | 13 - lib/gitlab/utils.rb | 13 - lib/gitlab/version_info.rb | 54 ---- lib/gitlab/visibility_level.rb | 64 ---- lib/gt_one_coercion.rb | 5 - lib/redcarpet/render/gitlab_html.rb | 72 ----- lib/repository_cache.rb | 21 -- lib/static_model.rb | 47 --- lib/support/deploy/deploy.sh | 45 --- lib/support/init.d/gitlab | 301 ------------------- lib/support/init.d/gitlab.default.example | 31 -- lib/support/logrotate/gitlab | 20 -- lib/support/nginx/gitlab | 115 -------- lib/support/nginx/gitlab-ssl | 162 ----------- lib/tasks/.gitkeep | 0 lib/tasks/brakeman.rake | 9 - lib/tasks/cache.rake | 11 - lib/tasks/dev.rake | 10 - lib/tasks/gitlab/backup.rake | 106 ------- lib/tasks/gitlab/bulk_add_permission.rake | 48 --- lib/tasks/gitlab/cleanup.rake | 113 ------- .../gitlab/db/drop_all_postgres_sequences.rake | 10 - lib/tasks/gitlab/db/drop_all_tables.rake | 10 - lib/tasks/gitlab/enable_automerge.rake | 39 --- lib/tasks/gitlab/generate_docs.rake | 7 - lib/tasks/gitlab/import.rake | 77 ----- lib/tasks/gitlab/info.rake | 72 ----- .../gitlab/mail_google_schema_whitelisting.rake | 73 ----- lib/tasks/gitlab/setup.rake | 24 -- lib/tasks/gitlab/shell.rake | 138 --------- lib/tasks/gitlab/sidekiq.rake | 47 --- lib/tasks/gitlab/task_helpers.rake | 131 --------- lib/tasks/gitlab/test.rake | 16 - lib/tasks/gitlab/web_hook.rake | 65 ----- lib/tasks/migrate/add_limits_mysql.rake | 7 - lib/tasks/migrate/migrate_iids.rake | 48 --- lib/tasks/rubocop.rake | 4 - lib/tasks/setup.rake | 4 - lib/tasks/sidekiq.rake | 21 -- lib/tasks/spec.rake | 45 --- lib/tasks/spinach.rake | 40 --- lib/tasks/test.rake | 13 - lib/unfold_form.rb | 11 - 164 files changed, 10766 deletions(-) delete mode 100644 lib/api/api.rb delete mode 100644 lib/api/api_guard.rb delete mode 100644 lib/api/branches.rb delete mode 100644 lib/api/commits.rb delete mode 100644 lib/api/deploy_keys.rb delete mode 100644 lib/api/entities.rb delete mode 100644 lib/api/files.rb delete mode 100644 lib/api/group_members.rb delete mode 100644 lib/api/groups.rb delete mode 100644 lib/api/helpers.rb delete mode 100644 lib/api/internal.rb delete mode 100644 lib/api/issues.rb delete mode 100644 lib/api/labels.rb delete mode 100644 lib/api/merge_requests.rb delete mode 100644 lib/api/milestones.rb delete mode 100644 lib/api/namespaces.rb delete mode 100644 lib/api/notes.rb delete mode 100644 lib/api/project_hooks.rb delete mode 100644 lib/api/project_members.rb delete mode 100644 lib/api/project_snippets.rb delete mode 100644 lib/api/projects.rb delete mode 100644 lib/api/repositories.rb delete mode 100644 lib/api/services.rb delete mode 100644 lib/api/session.rb delete mode 100644 lib/api/system_hooks.rb delete mode 100644 lib/api/users.rb delete mode 100644 lib/assets/.gitkeep delete mode 100644 lib/backup/database.rb delete mode 100644 lib/backup/manager.rb delete mode 100644 lib/backup/repository.rb delete mode 100644 lib/backup/uploads.rb delete mode 100644 lib/disable_email_interceptor.rb delete mode 100644 lib/email_validator.rb delete mode 100644 lib/event_filter.rb delete mode 100644 lib/extracts_path.rb delete mode 100644 lib/file_size_validator.rb delete mode 100644 lib/gitlab.rb delete mode 100644 lib/gitlab/access.rb delete mode 100644 lib/gitlab/app_logger.rb delete mode 100644 lib/gitlab/auth.rb delete mode 100644 lib/gitlab/backend/grack_auth.rb delete mode 100644 lib/gitlab/backend/rack_attack_helpers.rb delete mode 100644 lib/gitlab/backend/shell.rb delete mode 100644 lib/gitlab/backend/shell_adapter.rb delete mode 100644 lib/gitlab/backend/shell_env.rb delete mode 100644 lib/gitlab/bitbucket_import.rb delete mode 100644 lib/gitlab/bitbucket_import/client.rb delete mode 100644 lib/gitlab/bitbucket_import/importer.rb delete mode 100644 lib/gitlab/bitbucket_import/key_adder.rb delete mode 100644 lib/gitlab/bitbucket_import/key_deleter.rb delete mode 100644 lib/gitlab/bitbucket_import/project_creator.rb delete mode 100644 lib/gitlab/blacklist.rb delete mode 100644 lib/gitlab/closing_issue_extractor.rb delete mode 100644 lib/gitlab/compare_result.rb delete mode 100644 lib/gitlab/config_helper.rb delete mode 100644 lib/gitlab/contributions_calendar.rb delete mode 100644 lib/gitlab/contributor.rb delete mode 100644 lib/gitlab/current_settings.rb delete mode 100644 lib/gitlab/diff/file.rb delete mode 100644 lib/gitlab/diff/line.rb delete mode 100644 lib/gitlab/diff/line_code.rb delete mode 100644 lib/gitlab/diff/parser.rb delete mode 100644 lib/gitlab/force_push_check.rb delete mode 100644 lib/gitlab/git.rb delete mode 100644 lib/gitlab/git_access.rb delete mode 100644 lib/gitlab/git_access_status.rb delete mode 100644 lib/gitlab/git_access_wiki.rb delete mode 100644 lib/gitlab/git_logger.rb delete mode 100644 lib/gitlab/git_ref_validator.rb delete mode 100644 lib/gitlab/github_import/client.rb delete mode 100644 lib/gitlab/github_import/importer.rb delete mode 100644 lib/gitlab/github_import/project_creator.rb delete mode 100644 lib/gitlab/gitlab_import/client.rb delete mode 100644 lib/gitlab/gitlab_import/importer.rb delete mode 100644 lib/gitlab/gitlab_import/project_creator.rb delete mode 100644 lib/gitlab/gitorious_import/client.rb delete mode 100644 lib/gitlab/gitorious_import/project_creator.rb delete mode 100644 lib/gitlab/gitorious_import/repository.rb delete mode 100644 lib/gitlab/google_code_import/client.rb delete mode 100644 lib/gitlab/google_code_import/repository.rb delete mode 100644 lib/gitlab/graphs/commits.rb delete mode 100644 lib/gitlab/identifier.rb delete mode 100644 lib/gitlab/import_formatter.rb delete mode 100644 lib/gitlab/inline_diff.rb delete mode 100644 lib/gitlab/issues_labels.rb delete mode 100644 lib/gitlab/key_fingerprint.rb delete mode 100644 lib/gitlab/ldap/access.rb delete mode 100644 lib/gitlab/ldap/adapter.rb delete mode 100644 lib/gitlab/ldap/authentication.rb delete mode 100644 lib/gitlab/ldap/config.rb delete mode 100644 lib/gitlab/ldap/person.rb delete mode 100644 lib/gitlab/ldap/user.rb delete mode 100644 lib/gitlab/logger.rb delete mode 100644 lib/gitlab/markdown_helper.rb delete mode 100644 lib/gitlab/middleware/static.rb delete mode 100644 lib/gitlab/note_data_builder.rb delete mode 100644 lib/gitlab/o_auth/auth_hash.rb delete mode 100644 lib/gitlab/o_auth/user.rb delete mode 100644 lib/gitlab/popen.rb delete mode 100644 lib/gitlab/production_logger.rb delete mode 100644 lib/gitlab/project_search_results.rb delete mode 100644 lib/gitlab/push_data_builder.rb delete mode 100644 lib/gitlab/reference_extractor.rb delete mode 100644 lib/gitlab/satellite/action.rb delete mode 100644 lib/gitlab/satellite/compare_action.rb delete mode 100644 lib/gitlab/satellite/files/delete_file_action.rb delete mode 100644 lib/gitlab/satellite/files/edit_file_action.rb delete mode 100644 lib/gitlab/satellite/files/file_action.rb delete mode 100644 lib/gitlab/satellite/files/new_file_action.rb delete mode 100644 lib/gitlab/satellite/logger.rb delete mode 100644 lib/gitlab/satellite/merge_action.rb delete mode 100644 lib/gitlab/satellite/satellite.rb delete mode 100644 lib/gitlab/search_results.rb delete mode 100644 lib/gitlab/seeder.rb delete mode 100644 lib/gitlab/sidekiq_logger.rb delete mode 100644 lib/gitlab/sidekiq_middleware/arguments_logger.rb delete mode 100644 lib/gitlab/sidekiq_middleware/memory_killer.rb delete mode 100644 lib/gitlab/snippet_search_results.rb delete mode 100644 lib/gitlab/theme.rb delete mode 100644 lib/gitlab/upgrader.rb delete mode 100644 lib/gitlab/url_builder.rb delete mode 100644 lib/gitlab/user_access.rb delete mode 100644 lib/gitlab/utils.rb delete mode 100644 lib/gitlab/version_info.rb delete mode 100644 lib/gitlab/visibility_level.rb delete mode 100644 lib/gt_one_coercion.rb delete mode 100644 lib/redcarpet/render/gitlab_html.rb delete mode 100644 lib/repository_cache.rb delete mode 100644 lib/static_model.rb delete mode 100755 lib/support/deploy/deploy.sh delete mode 100755 lib/support/init.d/gitlab delete mode 100755 lib/support/init.d/gitlab.default.example delete mode 100644 lib/support/logrotate/gitlab delete mode 100644 lib/support/nginx/gitlab delete mode 100644 lib/support/nginx/gitlab-ssl delete mode 100644 lib/tasks/.gitkeep delete mode 100644 lib/tasks/brakeman.rake delete mode 100644 lib/tasks/cache.rake delete mode 100644 lib/tasks/dev.rake delete mode 100644 lib/tasks/gitlab/backup.rake delete mode 100644 lib/tasks/gitlab/bulk_add_permission.rake delete mode 100644 lib/tasks/gitlab/cleanup.rake delete mode 100644 lib/tasks/gitlab/db/drop_all_postgres_sequences.rake delete mode 100644 lib/tasks/gitlab/db/drop_all_tables.rake delete mode 100644 lib/tasks/gitlab/enable_automerge.rake delete mode 100644 lib/tasks/gitlab/generate_docs.rake delete mode 100644 lib/tasks/gitlab/import.rake delete mode 100644 lib/tasks/gitlab/info.rake delete mode 100644 lib/tasks/gitlab/mail_google_schema_whitelisting.rake delete mode 100644 lib/tasks/gitlab/setup.rake delete mode 100644 lib/tasks/gitlab/shell.rake delete mode 100644 lib/tasks/gitlab/sidekiq.rake delete mode 100644 lib/tasks/gitlab/task_helpers.rake delete mode 100644 lib/tasks/gitlab/test.rake delete mode 100644 lib/tasks/gitlab/web_hook.rake delete mode 100644 lib/tasks/migrate/add_limits_mysql.rake delete mode 100644 lib/tasks/migrate/migrate_iids.rake delete mode 100644 lib/tasks/rubocop.rake delete mode 100644 lib/tasks/setup.rake delete mode 100644 lib/tasks/sidekiq.rake delete mode 100644 lib/tasks/spec.rake delete mode 100644 lib/tasks/spinach.rake delete mode 100644 lib/tasks/test.rake delete mode 100644 lib/unfold_form.rb (limited to 'lib') diff --git a/lib/api/api.rb b/lib/api/api.rb deleted file mode 100644 index 60858a39407..00000000000 --- a/lib/api/api.rb +++ /dev/null @@ -1,53 +0,0 @@ -Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} - -module API - class API < Grape::API - include APIGuard - version 'v3', using: :path - - rescue_from ActiveRecord::RecordNotFound do - rack_response({ 'message' => '404 Not found' }.to_json, 404) - end - - rescue_from :all do |exception| - # lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60 - # why is this not wrapped in something reusable? - trace = exception.backtrace - - message = "\n#{exception.class} (#{exception.message}):\n" - message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code) - message << " " << trace.join("\n ") - - API.logger.add Logger::FATAL, message - rack_response({ 'message' => '500 Internal Server Error' }, 500) - end - - format :json - content_type :txt, "text/plain" - - helpers APIHelpers - - mount Groups - mount GroupMembers - mount Users - mount Projects - mount Repositories - mount Issues - mount Milestones - mount Session - mount MergeRequests - mount Notes - mount Internal - mount SystemHooks - mount ProjectSnippets - mount ProjectMembers - mount DeployKeys - mount ProjectHooks - mount Services - mount Files - mount Commits - mount Namespaces - mount Branches - mount Labels - end -end diff --git a/lib/api/api_guard.rb b/lib/api/api_guard.rb deleted file mode 100644 index b9994fcefda..00000000000 --- a/lib/api/api_guard.rb +++ /dev/null @@ -1,172 +0,0 @@ -# Guard API with OAuth 2.0 Access Token - -require 'rack/oauth2' - -module APIGuard - extend ActiveSupport::Concern - - included do |base| - # OAuth2 Resource Server Authentication - use Rack::OAuth2::Server::Resource::Bearer, 'The API' do |request| - # The authenticator only fetches the raw token string - - # Must yield access token to store it in the env - request.access_token - end - - helpers HelperMethods - - install_error_responders(base) - end - - # Helper Methods for Grape Endpoint - module HelperMethods - # Invokes the doorkeeper guard. - # - # If token is presented and valid, then it sets @current_user. - # - # If the token does not have sufficient scopes to cover the requred scopes, - # then it raises InsufficientScopeError. - # - # If the token is expired, then it raises ExpiredError. - # - # If the token is revoked, then it raises RevokedError. - # - # If the token is not found (nil), then it raises TokenNotFoundError. - # - # Arguments: - # - # scopes: (optional) scopes required for this guard. - # Defaults to empty array. - # - def doorkeeper_guard!(scopes: []) - if (access_token = find_access_token).nil? - raise TokenNotFoundError - - else - case validate_access_token(access_token, scopes) - when Oauth2::AccessTokenValidationService::INSUFFICIENT_SCOPE - raise InsufficientScopeError.new(scopes) - when Oauth2::AccessTokenValidationService::EXPIRED - raise ExpiredError - when Oauth2::AccessTokenValidationService::REVOKED - raise RevokedError - when Oauth2::AccessTokenValidationService::VALID - @current_user = User.find(access_token.resource_owner_id) - end - end - end - - def doorkeeper_guard(scopes: []) - if access_token = find_access_token - case validate_access_token(access_token, scopes) - when Oauth2::AccessTokenValidationService::INSUFFICIENT_SCOPE - raise InsufficientScopeError.new(scopes) - - when Oauth2::AccessTokenValidationService::EXPIRED - raise ExpiredError - - when Oauth2::AccessTokenValidationService::REVOKED - raise RevokedError - - when Oauth2::AccessTokenValidationService::VALID - @current_user = User.find(access_token.resource_owner_id) - end - end - end - - def current_user - @current_user - end - - private - def find_access_token - @access_token ||= Doorkeeper.authenticate(doorkeeper_request, Doorkeeper.configuration.access_token_methods) - end - - def doorkeeper_request - @doorkeeper_request ||= ActionDispatch::Request.new(env) - end - - def validate_access_token(access_token, scopes) - Oauth2::AccessTokenValidationService.validate(access_token, scopes: scopes) - end - end - - module ClassMethods - # Installs the doorkeeper guard on the whole Grape API endpoint. - # - # Arguments: - # - # scopes: (optional) scopes required for this guard. - # Defaults to empty array. - # - def guard_all!(scopes: []) - before do - guard! scopes: scopes - end - end - - private - def install_error_responders(base) - error_classes = [ MissingTokenError, TokenNotFoundError, - ExpiredError, RevokedError, InsufficientScopeError] - - base.send :rescue_from, *error_classes, oauth2_bearer_token_error_handler - end - - def oauth2_bearer_token_error_handler - Proc.new do |e| - response = - case e - when MissingTokenError - Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new - - when TokenNotFoundError - Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new( - :invalid_token, - "Bad Access Token.") - - when ExpiredError - Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new( - :invalid_token, - "Token is expired. You can either do re-authorization or token refresh.") - - when RevokedError - Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new( - :invalid_token, - "Token was revoked. You have to re-authorize from the user.") - - when InsufficientScopeError - # FIXME: ForbiddenError (inherited from Bearer::Forbidden of Rack::Oauth2) - # does not include WWW-Authenticate header, which breaks the standard. - Rack::OAuth2::Server::Resource::Bearer::Forbidden.new( - :insufficient_scope, - Rack::OAuth2::Server::Resource::ErrorMethods::DEFAULT_DESCRIPTION[:insufficient_scope], - { scope: e.scopes }) - end - - response.finish - end - end - end - - # - # Exceptions - # - - class MissingTokenError < StandardError; end - - class TokenNotFoundError < StandardError; end - - class ExpiredError < StandardError; end - - class RevokedError < StandardError; end - - class InsufficientScopeError < StandardError - attr_reader :scopes - def initialize(scopes) - @scopes = scopes - end - end -end diff --git a/lib/api/branches.rb b/lib/api/branches.rb deleted file mode 100644 index 592100a7045..00000000000 --- a/lib/api/branches.rb +++ /dev/null @@ -1,119 +0,0 @@ -require 'mime/types' - -module API - # Projects API - class Branches < Grape::API - before { authenticate! } - before { authorize! :download_code, user_project } - - resource :projects do - # Get a project repository branches - # - # Parameters: - # id (required) - The ID of a project - # Example Request: - # GET /projects/:id/repository/branches - get ":id/repository/branches" do - branches = user_project.repository.branches.sort_by(&:name) - present branches, with: Entities::RepoObject, project: user_project - end - - # Get a single branch - # - # Parameters: - # id (required) - The ID of a project - # branch (required) - The name of the branch - # Example Request: - # GET /projects/:id/repository/branches/:branch - get ':id/repository/branches/:branch', requirements: { branch: /.*/ } do - @branch = user_project.repository.branches.find { |item| item.name == params[:branch] } - not_found!("Branch") unless @branch - present @branch, with: Entities::RepoObject, project: user_project - end - - # Protect a single branch - # - # Parameters: - # id (required) - The ID of a project - # branch (required) - The name of the branch - # Example Request: - # PUT /projects/:id/repository/branches/:branch/protect - put ':id/repository/branches/:branch/protect', - requirements: { branch: /.*/ } do - - authorize_admin_project - - @branch = user_project.repository.find_branch(params[:branch]) - not_found!("Branch") unless @branch - protected_branch = user_project.protected_branches.find_by(name: @branch.name) - user_project.protected_branches.create(name: @branch.name) unless protected_branch - - present @branch, with: Entities::RepoObject, project: user_project - end - - # Unprotect a single branch - # - # Parameters: - # id (required) - The ID of a project - # branch (required) - The name of the branch - # Example Request: - # PUT /projects/:id/repository/branches/:branch/unprotect - put ':id/repository/branches/:branch/unprotect', - requirements: { branch: /.*/ } do - - authorize_admin_project - - @branch = user_project.repository.find_branch(params[:branch]) - not_found!("Branch does not exist") unless @branch - protected_branch = user_project.protected_branches.find_by(name: @branch.name) - protected_branch.destroy if protected_branch - - present @branch, with: Entities::RepoObject, project: user_project - end - - # Create branch - # - # Parameters: - # id (required) - The ID of a project - # branch_name (required) - The name of the branch - # ref (required) - Create branch from commit sha or existing branch - # Example Request: - # POST /projects/:id/repository/branches - post ":id/repository/branches" do - authorize_push_project - result = CreateBranchService.new(user_project, current_user). - execute(params[:branch_name], params[:ref]) - - if result[:status] == :success - present result[:branch], - with: Entities::RepoObject, - project: user_project - else - render_api_error!(result[:message], 400) - end - end - - # Delete branch - # - # Parameters: - # id (required) - The ID of a project - # branch (required) - The name of the branch - # Example Request: - # DELETE /projects/:id/repository/branches/:branch - delete ":id/repository/branches/:branch", - requirements: { branch: /.*/ } do - authorize_push_project - result = DeleteBranchService.new(user_project, current_user). - execute(params[:branch]) - - if result[:status] == :success - { - branch_name: params[:branch] - } - else - render_api_error!(result[:message], result[:return_code]) - end - end - end - end -end diff --git a/lib/api/commits.rb b/lib/api/commits.rb deleted file mode 100644 index 0de4e720ffe..00000000000 --- a/lib/api/commits.rb +++ /dev/null @@ -1,116 +0,0 @@ -require 'mime/types' - -module API - # Projects commits API - class Commits < Grape::API - before { authenticate! } - before { authorize! :download_code, user_project } - - resource :projects do - # Get a project repository commits - # - # Parameters: - # id (required) - The ID of a project - # ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used - # Example Request: - # GET /projects/:id/repository/commits - get ":id/repository/commits" do - page = (params[:page] || 0).to_i - per_page = (params[:per_page] || 20).to_i - ref = params[:ref_name] || user_project.try(:default_branch) || 'master' - - commits = user_project.repository.commits(ref, nil, per_page, page * per_page) - present commits, with: Entities::RepoCommit - end - - # Get a specific commit of a project - # - # Parameters: - # id (required) - The ID of a project - # sha (required) - The commit hash or name of a repository branch or tag - # Example Request: - # GET /projects/:id/repository/commits/:sha - get ":id/repository/commits/:sha" do - sha = params[:sha] - commit = user_project.repository.commit(sha) - not_found! "Commit" unless commit - present commit, with: Entities::RepoCommitDetail - end - - # Get the diff for a specific commit of a project - # - # Parameters: - # id (required) - The ID of a project - # sha (required) - The commit or branch name - # Example Request: - # GET /projects/:id/repository/commits/:sha/diff - get ":id/repository/commits/:sha/diff" do - sha = params[:sha] - commit = user_project.repository.commit(sha) - not_found! "Commit" unless commit - commit.diffs - end - - # Get a commit's comments - # - # Parameters: - # id (required) - The ID of a project - # sha (required) - The commit hash - # Examples: - # GET /projects/:id/repository/commits/:sha/comments - get ':id/repository/commits/:sha/comments' do - sha = params[:sha] - commit = user_project.repository.commit(sha) - not_found! 'Commit' unless commit - notes = Note.where(commit_id: commit.id) - present paginate(notes), with: Entities::CommitNote - end - - # Post comment to commit - # - # Parameters: - # id (required) - The ID of a project - # sha (required) - The commit hash - # note (required) - Text of comment - # path (optional) - The file path - # line (optional) - The line number - # line_type (optional) - The type of line (new or old) - # Examples: - # POST /projects/:id/repository/commits/:sha/comments - post ':id/repository/commits/:sha/comments' do - required_attributes! [:note] - - sha = params[:sha] - commit = user_project.repository.commit(sha) - not_found! 'Commit' unless commit - opts = { - note: params[:note], - noteable_type: 'Commit', - commit_id: commit.id - } - - if params[:path] && params[:line] && params[:line_type] - commit.diffs.each do |diff| - next unless diff.new_path == params[:path] - lines = Gitlab::Diff::Parser.new.parse(diff.diff.lines.to_a) - - lines.each do |line| - next unless line.new_pos == params[:line].to_i && line.type == params[:line_type] - break opts[:line_code] = Gitlab::Diff::LineCode.generate(diff.new_path, line.new_pos, line.old_pos) - end - - break if opts[:line_code] - end - end - - note = ::Notes::CreateService.new(user_project, current_user, opts).execute - - if note.save - present note, with: Entities::CommitNote - else - render_api_error!("Failed to save note #{note.errors.messages}", 400) - end - end - end - end -end diff --git a/lib/api/deploy_keys.rb b/lib/api/deploy_keys.rb deleted file mode 100644 index 06eb7756841..00000000000 --- a/lib/api/deploy_keys.rb +++ /dev/null @@ -1,75 +0,0 @@ -module API - # Projects API - class DeployKeys < Grape::API - before { authenticate! } - before { authorize_admin_project } - - resource :projects do - # Get a specific project's keys - # - # Example Request: - # GET /projects/:id/keys - get ":id/keys" do - present user_project.deploy_keys, with: Entities::SSHKey - end - - # Get single key owned by currently authenticated user - # - # Example Request: - # GET /projects/:id/keys/:id - get ":id/keys/:key_id" do - key = user_project.deploy_keys.find params[:key_id] - present key, with: Entities::SSHKey - end - - # Add new ssh key to currently authenticated user - # If deploy key already exists - it will be joined to project - # but only if original one was is accessible by same user - # - # Parameters: - # key (required) - New SSH Key - # title (required) - New SSH Key's title - # Example Request: - # POST /projects/:id/keys - post ":id/keys" do - attrs = attributes_for_keys [:title, :key] - - if attrs[:key].present? - attrs[:key].strip! - - # check if key already exist in project - key = user_project.deploy_keys.find_by(key: attrs[:key]) - if key - present key, with: Entities::SSHKey - return - end - - # Check for available deploy keys in other projects - key = current_user.accessible_deploy_keys.find_by(key: attrs[:key]) - if key - user_project.deploy_keys << key - present key, with: Entities::SSHKey - return - end - end - - key = DeployKey.new attrs - - if key.valid? && user_project.deploy_keys << key - present key, with: Entities::SSHKey - else - render_validation_error!(key) - end - end - - # Delete existed ssh key of currently authenticated user - # - # Example Request: - # DELETE /projects/:id/keys/:id - delete ":id/keys/:key_id" do - key = user_project.deploy_keys.find params[:key_id] - key.destroy - end - end - end -end diff --git a/lib/api/entities.rb b/lib/api/entities.rb deleted file mode 100644 index 36332bc6514..00000000000 --- a/lib/api/entities.rb +++ /dev/null @@ -1,280 +0,0 @@ -module API - module Entities - class UserSafe < Grape::Entity - expose :name, :username - end - - class UserBasic < UserSafe - expose :id, :state, :avatar_url - end - - class User < UserBasic - expose :created_at - expose :is_admin?, as: :is_admin - expose :bio, :skype, :linkedin, :twitter, :website_url - end - - class Identity < Grape::Entity - expose :provider, :extern_uid - end - - class UserFull < User - expose :email - expose :theme_id, :color_scheme_id, :projects_limit - expose :identities, using: Entities::Identity - expose :can_create_group?, as: :can_create_group - expose :can_create_project?, as: :can_create_project - end - - class UserLogin < UserFull - expose :private_token - end - - class Hook < Grape::Entity - expose :id, :url, :created_at - end - - class ProjectHook < Hook - expose :project_id, :push_events - expose :issues_events, :merge_requests_events, :tag_push_events - end - - class ForkedFromProject < Grape::Entity - expose :id - expose :name, :name_with_namespace - expose :path, :path_with_namespace - end - - class Project < Grape::Entity - expose :id, :description, :default_branch, :tag_list - expose :public?, as: :public - expose :archived?, as: :archived - expose :visibility_level, :ssh_url_to_repo, :http_url_to_repo, :web_url - expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group } - expose :name, :name_with_namespace - expose :path, :path_with_namespace - expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :snippets_enabled, :created_at, :last_activity_at - expose :creator_id - expose :namespace - expose :forked_from_project, using: Entities::ForkedFromProject, if: lambda{ | project, options | project.forked? } - expose :avatar_url - end - - class ProjectMember < UserBasic - expose :access_level do |user, options| - options[:project].project_members.find_by(user_id: user.id).access_level - end - end - - class Group < Grape::Entity - expose :id, :name, :path, :description - end - - class GroupDetail < Group - expose :projects, using: Entities::Project - end - - class GroupMember < UserBasic - expose :access_level do |user, options| - options[:group].group_members.find_by(user_id: user.id).access_level - end - end - - class RepoTag < Grape::Entity - expose :name - expose :message do |repo_obj, _options| - if repo_obj.respond_to?(:message) - repo_obj.message - else - nil - end - end - - expose :commit do |repo_obj, options| - if repo_obj.respond_to?(:commit) - repo_obj.commit - elsif options[:project] - options[:project].repository.commit(repo_obj.target) - end - end - end - - class RepoObject < Grape::Entity - expose :name - - expose :commit do |repo_obj, options| - if repo_obj.respond_to?(:commit) - repo_obj.commit - elsif options[:project] - options[:project].repository.commit(repo_obj.target) - end - end - - expose :protected do |repo, options| - if options[:project] - options[:project].protected_branch? repo.name - end - end - end - - class RepoTreeObject < Grape::Entity - expose :id, :name, :type - - expose :mode do |obj, options| - filemode = obj.mode.to_s(8) - filemode = "0" + filemode if filemode.length < 6 - filemode - end - end - - class RepoCommit < Grape::Entity - expose :id, :short_id, :title, :author_name, :author_email, :created_at - expose :safe_message, as: :message - end - - class RepoCommitDetail < RepoCommit - expose :parent_ids, :committed_date, :authored_date - end - - class ProjectSnippet < Grape::Entity - expose :id, :title, :file_name - expose :author, using: Entities::UserBasic - expose :expires_at, :updated_at, :created_at - end - - class ProjectEntity < Grape::Entity - expose :id, :iid - expose(:project_id) { |entity| entity.project.id } - expose :title, :description - expose :state, :created_at, :updated_at - end - - class RepoDiff < Grape::Entity - expose :old_path, :new_path, :a_mode, :b_mode, :diff - expose :new_file, :renamed_file, :deleted_file - end - - class Milestone < ProjectEntity - expose :due_date - end - - class Issue < ProjectEntity - expose :label_names, as: :labels - expose :milestone, using: Entities::Milestone - expose :assignee, :author, using: Entities::UserBasic - end - - class MergeRequest < ProjectEntity - expose :target_branch, :source_branch, :upvotes, :downvotes - expose :author, :assignee, using: Entities::UserBasic - expose :source_project_id, :target_project_id - expose :label_names, as: :labels - expose :description - expose :milestone, using: Entities::Milestone - end - - class MergeRequestChanges < MergeRequest - expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _| - compare.diffs - end - end - - class SSHKey < Grape::Entity - expose :id, :title, :key, :created_at - end - - class Note < Grape::Entity - expose :id - expose :note, as: :body - expose :attachment_identifier, as: :attachment - expose :author, using: Entities::UserBasic - expose :created_at - end - - class MRNote < Grape::Entity - expose :note - expose :author, using: Entities::UserBasic - end - - class CommitNote < Grape::Entity - expose :note - expose(:path) { |note| note.diff_file_name } - expose(:line) { |note| note.diff_new_line } - expose(:line_type) { |note| note.diff_line_type } - expose :author, using: Entities::UserBasic - end - - class Event < Grape::Entity - expose :title, :project_id, :action_name - expose :target_id, :target_type, :author_id - expose :data, :target_title - expose :created_at - - expose :author_username do |event, options| - if event.author - event.author.username - end - end - end - - class Namespace < Grape::Entity - expose :id, :path, :kind - end - - class ProjectAccess < Grape::Entity - expose :access_level - expose :notification_level - end - - class GroupAccess < Grape::Entity - expose :access_level - expose :notification_level - end - - class ProjectWithAccess < Project - expose :permissions do - expose :project_access, using: Entities::ProjectAccess do |project, options| - project.project_members.find_by(user_id: options[:user].id) - end - - expose :group_access, using: Entities::GroupAccess do |project, options| - if project.group - project.group.group_members.find_by(user_id: options[:user].id) - end - end - end - end - - class Label < Grape::Entity - expose :name, :color - end - - class Compare < Grape::Entity - expose :commit, using: Entities::RepoCommit do |compare, options| - Commit.decorate(compare.commits).last - end - - expose :commits, using: Entities::RepoCommit do |compare, options| - Commit.decorate(compare.commits) - end - - expose :diffs, using: Entities::RepoDiff do |compare, options| - compare.diffs - end - - expose :compare_timeout do |compare, options| - compare.timeout - end - - expose :same, as: :compare_same_ref - end - - class Contributor < Grape::Entity - expose :name, :email, :commits, :additions, :deletions - end - - class BroadcastMessage < Grape::Entity - expose :message, :starts_at, :ends_at, :color, :font - end - end -end diff --git a/lib/api/files.rb b/lib/api/files.rb deleted file mode 100644 index 3176ef0e256..00000000000 --- a/lib/api/files.rb +++ /dev/null @@ -1,158 +0,0 @@ -module API - # Projects API - class Files < Grape::API - before { authenticate! } - - resource :projects do - # Get file from repository - # File content is Base64 encoded - # - # Parameters: - # file_path (required) - The path to the file. Ex. lib/class.rb - # ref (required) - The name of branch, tag or commit - # - # Example Request: - # GET /projects/:id/repository/files - # - # Example response: - # { - # "file_name": "key.rb", - # "file_path": "app/models/key.rb", - # "size": 1476, - # "encoding": "base64", - # "content": "IyA9PSBTY2hlbWEgSW5mb3...", - # "ref": "master", - # "blob_id": "79f7bbd25901e8334750839545a9bd021f0e4c83", - # "commit_id": "d5a3ff139356ce33e37e73add446f16869741b50" - # } - # - get ":id/repository/files" do - authorize! :download_code, user_project - - required_attributes! [:file_path, :ref] - attrs = attributes_for_keys [:file_path, :ref] - ref = attrs.delete(:ref) - file_path = attrs.delete(:file_path) - - commit = user_project.repository.commit(ref) - not_found! 'Commit' unless commit - - blob = user_project.repository.blob_at(commit.sha, file_path) - - if blob - status(200) - - { - file_name: blob.name, - file_path: blob.path, - size: blob.size, - encoding: "base64", - content: Base64.encode64(blob.data), - ref: ref, - blob_id: blob.id, - commit_id: commit.id, - } - else - not_found! 'File' - end - end - - # Create new file in repository - # - # Parameters: - # file_path (required) - The path to new file. Ex. lib/class.rb - # branch_name (required) - The name of branch - # content (required) - File content - # commit_message (required) - Commit message - # - # Example Request: - # POST /projects/:id/repository/files - # - post ":id/repository/files" do - authorize! :push_code, user_project - - required_attributes! [:file_path, :branch_name, :content, :commit_message] - attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding] - branch_name = attrs.delete(:branch_name) - file_path = attrs.delete(:file_path) - result = ::Files::CreateService.new(user_project, current_user, attrs, branch_name, file_path).execute - - if result[:status] == :success - status(201) - - { - file_path: file_path, - branch_name: branch_name - } - else - render_api_error!(result[:message], 400) - end - end - - # Update existing file in repository - # - # Parameters: - # file_path (optional) - The path to file. Ex. lib/class.rb - # branch_name (required) - The name of branch - # content (required) - File content - # commit_message (required) - Commit message - # - # Example Request: - # PUT /projects/:id/repository/files - # - put ":id/repository/files" do - authorize! :push_code, user_project - - required_attributes! [:file_path, :branch_name, :content, :commit_message] - attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding] - branch_name = attrs.delete(:branch_name) - file_path = attrs.delete(:file_path) - result = ::Files::UpdateService.new(user_project, current_user, attrs, branch_name, file_path).execute - - if result[:status] == :success - status(200) - - { - file_path: file_path, - branch_name: branch_name - } - else - http_status = result[:http_status] || 400 - render_api_error!(result[:message], http_status) - end - end - - # Delete existing file in repository - # - # Parameters: - # file_path (optional) - The path to file. Ex. lib/class.rb - # branch_name (required) - The name of branch - # content (required) - File content - # commit_message (required) - Commit message - # - # Example Request: - # DELETE /projects/:id/repository/files - # - delete ":id/repository/files" do - authorize! :push_code, user_project - - required_attributes! [:file_path, :branch_name, :commit_message] - attrs = attributes_for_keys [:file_path, :branch_name, :commit_message] - branch_name = attrs.delete(:branch_name) - file_path = attrs.delete(:file_path) - result = ::Files::DeleteService.new(user_project, current_user, attrs, branch_name, file_path).execute - - if result[:status] == :success - status(200) - - { - file_path: file_path, - branch_name: branch_name - } - else - render_api_error!(result[:message], 400) - end - end - end - end -end diff --git a/lib/api/group_members.rb b/lib/api/group_members.rb deleted file mode 100644 index ab9b7c602b5..00000000000 --- a/lib/api/group_members.rb +++ /dev/null @@ -1,87 +0,0 @@ -module API - class GroupMembers < Grape::API - before { authenticate! } - - resource :groups do - # Get a list of group members viewable by the authenticated user. - # - # Example Request: - # GET /groups/:id/members - get ":id/members" do - group = find_group(params[:id]) - users = group.users - present users, with: Entities::GroupMember, group: group - end - - # Add a user to the list of group members - # - # Parameters: - # id (required) - group id - # user_id (required) - the users id - # access_level (required) - Project access level - # Example Request: - # POST /groups/:id/members - post ":id/members" do - group = find_group(params[:id]) - authorize! :admin_group, group - required_attributes! [:user_id, :access_level] - - unless validate_access_level?(params[:access_level]) - render_api_error!("Wrong access level", 422) - end - - if group.group_members.find_by(user_id: params[:user_id]) - render_api_error!("Already exists", 409) - end - - group.add_users([params[:user_id]], params[:access_level], current_user) - member = group.group_members.find_by(user_id: params[:user_id]) - present member.user, with: Entities::GroupMember, group: group - end - - # Update group member - # - # Parameters: - # id (required) - The ID of a group - # user_id (required) - The ID of a group member - # access_level (required) - Project access level - # Example Request: - # PUT /groups/:id/members/:user_id - put ':id/members/:user_id' do - group = find_group(params[:id]) - authorize! :admin_group, group - required_attributes! [:access_level] - - group_member = group.group_members.find_by(user_id: params[:user_id]) - not_found!('User can not be found') if group_member.nil? - - if group_member.update_attributes(access_level: params[:access_level]) - @member = group_member.user - present @member, with: Entities::GroupMember, group: group - else - handle_member_errors group_member.errors - end - end - - # Remove member. - # - # Parameters: - # id (required) - group id - # user_id (required) - the users id - # - # Example Request: - # DELETE /groups/:id/members/:user_id - delete ":id/members/:user_id" do - group = find_group(params[:id]) - authorize! :admin_group, group - member = group.group_members.find_by(user_id: params[:user_id]) - - if member.nil? - render_api_error!("404 Not Found - user_id:#{params[:user_id]} not a member of group #{group.name}",404) - else - member.destroy - end - end - end - end -end diff --git a/lib/api/groups.rb b/lib/api/groups.rb deleted file mode 100644 index 8cb9f920975..00000000000 --- a/lib/api/groups.rb +++ /dev/null @@ -1,89 +0,0 @@ -module API - # groups API - class Groups < Grape::API - before { authenticate! } - - resource :groups do - # Get a groups list - # - # Example Request: - # GET /groups - get do - @groups = if current_user.admin - Group.all - else - current_user.groups - end - - @groups = @groups.search(params[:search]) if params[:search].present? - @groups = paginate @groups - present @groups, with: Entities::Group - end - - # Create group. Available only for admin - # - # Parameters: - # name (required) - The name of the group - # path (required) - The path of the group - # Example Request: - # POST /groups - post do - authenticated_as_admin! - required_attributes! [:name, :path] - - attrs = attributes_for_keys [:name, :path, :description] - @group = Group.new(attrs) - - if @group.save - @group.add_owner(current_user) - present @group, with: Entities::Group - else - render_api_error!("Failed to save group #{@group.errors.messages}", 400) - end - end - - # Get a single group, with containing projects - # - # Parameters: - # id (required) - The ID of a group - # Example Request: - # GET /groups/:id - get ":id" do - group = find_group(params[:id]) - present group, with: Entities::GroupDetail - end - - # Remove group - # - # Parameters: - # id (required) - The ID of a group - # Example Request: - # DELETE /groups/:id - delete ":id" do - group = find_group(params[:id]) - authorize! :admin_group, group - group.destroy - end - - # Transfer a project to the Group namespace - # - # Parameters: - # id - group id - # project_id - project id - # Example Request: - # POST /groups/:id/projects/:project_id - post ":id/projects/:project_id" do - authenticated_as_admin! - group = Group.find(params[:id]) - project = Project.find(params[:project_id]) - result = ::Projects::TransferService.new(project, current_user, namespace_id: group.id).execute - - if result - present group - else - render_api_error!("Failed to transfer project #{project.errors.messages}", 400) - end - end - end - end -end diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb deleted file mode 100644 index be133a2920b..00000000000 --- a/lib/api/helpers.rb +++ /dev/null @@ -1,250 +0,0 @@ -module API - module APIHelpers - PRIVATE_TOKEN_HEADER = "HTTP_PRIVATE_TOKEN" - PRIVATE_TOKEN_PARAM = :private_token - SUDO_HEADER ="HTTP_SUDO" - SUDO_PARAM = :sudo - - def parse_boolean(value) - [ true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON' ].include?(value) - end - - def current_user - private_token = (params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER]).to_s - @current_user ||= (User.find_by(authentication_token: private_token) || doorkeeper_guard) - - unless @current_user && Gitlab::UserAccess.allowed?(@current_user) - return nil - end - - identifier = sudo_identifier() - - # If the sudo is the current user do nothing - if identifier && !(@current_user.id == identifier || @current_user.username == identifier) - render_api_error!('403 Forbidden: Must be admin to use sudo', 403) unless @current_user.is_admin? - @current_user = User.by_username_or_id(identifier) - not_found!("No user id or username for: #{identifier}") if @current_user.nil? - end - - @current_user - end - - def sudo_identifier() - identifier ||= params[SUDO_PARAM] ||= env[SUDO_HEADER] - - # Regex for integers - if !!(identifier =~ /^[0-9]+$/) - identifier.to_i - else - identifier - end - end - - def user_project - @project ||= find_project(params[:id]) - @project || not_found!("Project") - end - - def find_project(id) - project = Project.find_with_namespace(id) || Project.find_by(id: id) - - if project && can?(current_user, :read_project, project) - project - else - nil - end - end - - def find_group(id) - begin - group = Group.find(id) - rescue ActiveRecord::RecordNotFound - group = Group.find_by!(path: id) - end - - if can?(current_user, :read_group, group) - group - else - forbidden!("#{current_user.username} lacks sufficient "\ - "access to #{group.name}") - end - end - - def paginate(relation) - per_page = params[:per_page].to_i - paginated = relation.page(params[:page]).per(per_page) - add_pagination_headers(paginated, per_page) - - paginated - end - - def authenticate! - unauthorized! unless current_user - end - - def authenticate_by_gitlab_shell_token! - input = params['secret_token'].try(:chomp) - unless Devise.secure_compare(secret_token, input) - unauthorized! - end - end - - def authenticated_as_admin! - forbidden! unless current_user.is_admin? - end - - def authorize!(action, subject) - unless abilities.allowed?(current_user, action, subject) - forbidden! - end - end - - def authorize_push_project - authorize! :push_code, user_project - end - - def authorize_admin_project - authorize! :admin_project, user_project - end - - def can?(object, action, subject) - abilities.allowed?(object, action, subject) - end - - # Checks the occurrences of required attributes, each attribute must be present in the params hash - # or a Bad Request error is invoked. - # - # Parameters: - # keys (required) - A hash consisting of keys that must be present - def required_attributes!(keys) - keys.each do |key| - bad_request!(key) unless params[key].present? - end - end - - def attributes_for_keys(keys) - attrs = {} - - keys.each do |key| - if params[key].present? or (params.has_key?(key) and params[key] == false) - attrs[key] = params[key] - end - end - - ActionController::Parameters.new(attrs).permit! - end - - # Helper method for validating all labels against its names - def validate_label_params(params) - errors = {} - - if params[:labels].present? - params[:labels].split(',').each do |label_name| - label = user_project.labels.create_with( - color: Label::DEFAULT_COLOR).find_or_initialize_by( - title: label_name.strip) - - if label.invalid? - errors[label.title] = label.errors - end - end - end - - errors - end - - def validate_access_level?(level) - Gitlab::Access.options_with_owner.values.include? level.to_i - end - - def issuable_order_by - if params["order_by"] == 'updated_at' - 'updated_at' - else - 'created_at' - end - end - - def issuable_sort - if params["sort"] == 'asc' - :asc - else - :desc - end - end - - # error helpers - - def forbidden!(reason = nil) - message = ['403 Forbidden'] - message << " - #{reason}" if reason - render_api_error!(message.join(' '), 403) - end - - def bad_request!(attribute) - message = ["400 (Bad request)"] - message << "\"" + attribute.to_s + "\" not given" - render_api_error!(message.join(' '), 400) - end - - def not_found!(resource = nil) - message = ["404"] - message << resource if resource - message << "Not Found" - render_api_error!(message.join(' '), 404) - end - - def unauthorized! - render_api_error!('401 Unauthorized', 401) - end - - def not_allowed! - render_api_error!('405 Method Not Allowed', 405) - end - - def conflict!(message = nil) - render_api_error!(message || '409 Conflict', 409) - end - - def render_validation_error!(model) - if model.errors.any? - render_api_error!(model.errors.messages || '400 Bad Request', 400) - end - end - - def render_api_error!(message, status) - error!({ 'message' => message }, status) - end - - private - - def add_pagination_headers(paginated, per_page) - request_url = request.url.split('?').first - - links = [] - links << %(<#{request_url}?page=#{paginated.current_page - 1}&per_page=#{per_page}>; rel="prev") unless paginated.first_page? - links << %(<#{request_url}?page=#{paginated.current_page + 1}&per_page=#{per_page}>; rel="next") unless paginated.last_page? - links << %(<#{request_url}?page=1&per_page=#{per_page}>; rel="first") - links << %(<#{request_url}?page=#{paginated.total_pages}&per_page=#{per_page}>; rel="last") - - header 'Link', links.join(', ') - end - - def abilities - @abilities ||= begin - abilities = Six.new - abilities << Ability - abilities - end - end - - def secret_token - File.read(Rails.root.join('.gitlab_shell_secret')).chomp - end - - def handle_member_errors(errors) - error!(errors[:access_level], 422) if errors[:access_level].any? - not_found!(errors) - end - end -end diff --git a/lib/api/internal.rb b/lib/api/internal.rb deleted file mode 100644 index f98a17773e7..00000000000 --- a/lib/api/internal.rb +++ /dev/null @@ -1,85 +0,0 @@ -module API - # Internal access API - class Internal < Grape::API - before { authenticate_by_gitlab_shell_token! } - - namespace 'internal' do - # Check if git command is allowed to project - # - # Params: - # key_id - ssh key id for Git over SSH - # user_id - user id for Git over HTTP - # project - project path with namespace - # action - git action (git-upload-pack or git-receive-pack) - # ref - branch name - # forced_push - forced_push - # - post "/allowed" do - status 200 - - actor = - if params[:key_id] - Key.find_by(id: params[:key_id]) - elsif params[:user_id] - User.find_by(id: params[:user_id]) - end - - unless actor - return Gitlab::GitAccessStatus.new(false, 'No such user or key') - end - - project_path = params[:project] - - # Check for *.wiki repositories. - # Strip out the .wiki from the pathname before finding the - # project. This applies the correct project permissions to - # the wiki repository as well. - wiki = project_path.end_with?('.wiki') - project_path.chomp!('.wiki') if wiki - - project = Project.find_with_namespace(project_path) - - if project - access = - if wiki - Gitlab::GitAccessWiki.new(actor, project) - else - Gitlab::GitAccess.new(actor, project) - end - - status = access.check(params[:action], params[:changes]) - end - - if project && access.can_read_project? - status - else - Gitlab::GitAccessStatus.new(false, 'No such project') - end - end - - # - # Discover user by ssh key - # - get "/discover" do - key = Key.find(params[:key_id]) - present key.user, with: Entities::UserSafe - end - - get "/check" do - { - api_version: API.version, - gitlab_version: Gitlab::VERSION, - gitlab_rev: Gitlab::REVISION, - } - end - - get "/broadcast_message" do - if message = BroadcastMessage.current - present message, with: Entities::BroadcastMessage - else - {} - end - end - end - end -end diff --git a/lib/api/issues.rb b/lib/api/issues.rb deleted file mode 100644 index ff062be6040..00000000000 --- a/lib/api/issues.rb +++ /dev/null @@ -1,181 +0,0 @@ -module API - # Issues API - class Issues < Grape::API - before { authenticate! } - - helpers do - def filter_issues_state(issues, state) - case state - when 'opened' then issues.opened - when 'closed' then issues.closed - else issues - end - end - - def filter_issues_labels(issues, labels) - issues.includes(:labels).where('labels.title' => labels.split(',')) - end - - def filter_issues_milestone(issues, milestone) - issues.includes(:milestone).where('milestones.title' => milestone) - end - end - - resource :issues do - # Get currently authenticated user's issues - # - # Parameters: - # state (optional) - Return "opened" or "closed" issues - # labels (optional) - Comma-separated list of label names - # order_by (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at` - # sort (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc` - # - # Example Requests: - # GET /issues - # GET /issues?state=opened - # GET /issues?state=closed - # GET /issues?labels=foo - # GET /issues?labels=foo,bar - # GET /issues?labels=foo,bar&state=opened - get do - issues = current_user.issues - issues = filter_issues_state(issues, params[:state]) unless params[:state].nil? - issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil? - issues.reorder(issuable_order_by => issuable_sort) - present paginate(issues), with: Entities::Issue - end - end - - resource :projects do - # Get a list of project issues - # - # Parameters: - # id (required) - The ID of a project - # state (optional) - Return "opened" or "closed" issues - # labels (optional) - Comma-separated list of label names - # milestone (optional) - Milestone title - # order_by (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at` - # sort (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc` - # - # Example Requests: - # GET /projects/:id/issues - # GET /projects/:id/issues?state=opened - # GET /projects/:id/issues?state=closed - # GET /projects/:id/issues?labels=foo - # GET /projects/:id/issues?labels=foo,bar - # GET /projects/:id/issues?labels=foo,bar&state=opened - # GET /projects/:id/issues?milestone=1.0.0 - # GET /projects/:id/issues?milestone=1.0.0&state=closed - get ":id/issues" do - issues = user_project.issues - issues = filter_issues_state(issues, params[:state]) unless params[:state].nil? - issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil? - - unless params[:milestone].nil? - issues = filter_issues_milestone(issues, params[:milestone]) - end - - issues.reorder(issuable_order_by => issuable_sort) - present paginate(issues), with: Entities::Issue - end - - # Get a single project issue - # - # Parameters: - # id (required) - The ID of a project - # issue_id (required) - The ID of a project issue - # Example Request: - # GET /projects/:id/issues/:issue_id - get ":id/issues/:issue_id" do - @issue = user_project.issues.find(params[:issue_id]) - present @issue, with: Entities::Issue - end - - # Create a new project issue - # - # Parameters: - # id (required) - The ID of a project - # title (required) - The title of an issue - # description (optional) - The description of an issue - # assignee_id (optional) - The ID of a user to assign issue - # milestone_id (optional) - The ID of a milestone to assign issue - # labels (optional) - The labels of an issue - # Example Request: - # POST /projects/:id/issues - post ":id/issues" do - required_attributes! [:title] - attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id] - - # Validate label names in advance - if (errors = validate_label_params(params)).any? - render_api_error!({ labels: errors }, 400) - end - - issue = ::Issues::CreateService.new(user_project, current_user, attrs).execute - - if issue.valid? - # Find or create labels and attach to issue. Labels are valid because - # we already checked its name, so there can't be an error here - if params[:labels].present? - issue.add_labels_by_names(params[:labels].split(',')) - end - - present issue, with: Entities::Issue - else - render_validation_error!(issue) - end - end - - # Update an existing issue - # - # Parameters: - # id (required) - The ID of a project - # issue_id (required) - The ID of a project issue - # title (optional) - The title of an issue - # description (optional) - The description of an issue - # assignee_id (optional) - The ID of a user to assign issue - # milestone_id (optional) - The ID of a milestone to assign issue - # labels (optional) - The labels of an issue - # state_event (optional) - The state event of an issue (close|reopen) - # Example Request: - # PUT /projects/:id/issues/:issue_id - put ":id/issues/:issue_id" do - issue = user_project.issues.find(params[:issue_id]) - authorize! :modify_issue, issue - attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id, :state_event] - - # Validate label names in advance - if (errors = validate_label_params(params)).any? - render_api_error!({ labels: errors }, 400) - end - - issue = ::Issues::UpdateService.new(user_project, current_user, attrs).execute(issue) - - if issue.valid? - # Find or create labels and attach to issue. Labels are valid because - # we already checked its name, so there can't be an error here - unless params[:labels].nil? - issue.remove_labels - # Create and add labels to the new created issue - issue.add_labels_by_names(params[:labels].split(',')) - end - - present issue, with: Entities::Issue - else - render_validation_error!(issue) - end - end - - # Delete a project issue (deprecated) - # - # Parameters: - # id (required) - The ID of a project - # issue_id (required) - The ID of a project issue - # Example Request: - # DELETE /projects/:id/issues/:issue_id - delete ":id/issues/:issue_id" do - not_allowed! - end - end - end -end diff --git a/lib/api/labels.rb b/lib/api/labels.rb deleted file mode 100644 index 78ca58ad0d1..00000000000 --- a/lib/api/labels.rb +++ /dev/null @@ -1,98 +0,0 @@ -module API - # Labels API - class Labels < Grape::API - before { authenticate! } - - resource :projects do - # Get all labels of the project - # - # Parameters: - # id (required) - The ID of a project - # Example Request: - # GET /projects/:id/labels - get ':id/labels' do - present user_project.labels, with: Entities::Label - end - - # Creates a new label - # - # Parameters: - # id (required) - The ID of a project - # name (required) - The name of the label to be deleted - # color (required) - Color of the label given in 6-digit hex - # notation with leading '#' sign (e.g. #FFAABB) - # Example Request: - # POST /projects/:id/labels - post ':id/labels' do - authorize! :admin_label, user_project - required_attributes! [:name, :color] - - attrs = attributes_for_keys [:name, :color] - label = user_project.find_label(attrs[:name]) - - conflict!('Label already exists') if label - - label = user_project.labels.create(attrs) - - if label.valid? - present label, with: Entities::Label - else - render_validation_error!(label) - end - end - - # Deletes an existing label - # - # Parameters: - # id (required) - The ID of a project - # name (required) - The name of the label to be deleted - # - # Example Request: - # DELETE /projects/:id/labels - delete ':id/labels' do - authorize! :admin_label, user_project - required_attributes! [:name] - - label = user_project.find_label(params[:name]) - not_found!('Label') unless label - - label.destroy - end - - # Updates an existing label. At least one optional parameter is required. - # - # Parameters: - # id (required) - The ID of a project - # name (required) - The name of the label to be deleted - # new_name (optional) - The new name of the label - # color (optional) - Color of the label given in 6-digit hex - # notation with leading '#' sign (e.g. #FFAABB) - # Example Request: - # PUT /projects/:id/labels - put ':id/labels' do - authorize! :admin_label, user_project - required_attributes! [:name] - - label = user_project.find_label(params[:name]) - not_found!('Label not found') unless label - - attrs = attributes_for_keys [:new_name, :color] - - if attrs.empty? - render_api_error!('Required parameters "new_name" or "color" ' \ - 'missing', - 400) - end - - # Rename new name to the actual label attribute name - attrs[:name] = attrs.delete(:new_name) if attrs.key?(:new_name) - - if label.update(attrs) - present label, with: Entities::Label - else - render_validation_error!(label) - end - end - end - end -end diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb deleted file mode 100644 index f3765f5ab03..00000000000 --- a/lib/api/merge_requests.rb +++ /dev/null @@ -1,249 +0,0 @@ -module API - # MergeRequest API - class MergeRequests < Grape::API - before { authenticate! } - - resource :projects do - helpers do - def handle_merge_request_errors!(errors) - if errors[:project_access].any? - error!(errors[:project_access], 422) - elsif errors[:branch_conflict].any? - error!(errors[:branch_conflict], 422) - elsif errors[:validate_fork].any? - error!(errors[:validate_fork], 422) - elsif errors[:validate_branches].any? - conflict!(errors[:validate_branches]) - end - - render_api_error!(errors, 400) - end - end - - # List merge requests - # - # Parameters: - # id (required) - The ID of a project - # state (optional) - Return requests "merged", "opened" or "closed" - # order_by (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at` - # sort (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc` - # - # Example: - # GET /projects/:id/merge_requests - # GET /projects/:id/merge_requests?state=opened - # GET /projects/:id/merge_requests?state=closed - # GET /projects/:id/merge_requests?order_by=created_at - # GET /projects/:id/merge_requests?order_by=updated_at - # GET /projects/:id/merge_requests?sort=desc - # GET /projects/:id/merge_requests?sort=asc - # - get ":id/merge_requests" do - authorize! :read_merge_request, user_project - merge_requests = user_project.merge_requests - - merge_requests = - case params["state"] - when "opened" then merge_requests.opened - when "closed" then merge_requests.closed - when "merged" then merge_requests.merged - else merge_requests - end - - merge_requests.reorder(issuable_order_by => issuable_sort) - present paginate(merge_requests), with: Entities::MergeRequest - end - - # Show MR - # - # Parameters: - # id (required) - The ID of a project - # merge_request_id (required) - The ID of MR - # - # Example: - # GET /projects/:id/merge_request/:merge_request_id - # - get ":id/merge_request/:merge_request_id" do - merge_request = user_project.merge_requests.find(params[:merge_request_id]) - - authorize! :read_merge_request, merge_request - - present merge_request, with: Entities::MergeRequest - end - - # Show MR changes - # - # Parameters: - # id (required) - The ID of a project - # merge_request_id (required) - The ID of MR - # - # Example: - # GET /projects/:id/merge_request/:merge_request_id/changes - # - get ':id/merge_request/:merge_request_id/changes' do - merge_request = user_project.merge_requests. - find(params[:merge_request_id]) - authorize! :read_merge_request, merge_request - present merge_request, with: Entities::MergeRequestChanges - end - - # Create MR - # - # Parameters: - # - # id (required) - The ID of a project - this will be the source of the merge request - # source_branch (required) - The source branch - # target_branch (required) - The target branch - # target_project - The target project of the merge request defaults to the :id of the project - # assignee_id - Assignee user ID - # title (required) - Title of MR - # description - Description of MR - # labels (optional) - Labels for MR as a comma-separated list - # - # Example: - # POST /projects/:id/merge_requests - # - post ":id/merge_requests" do - authorize! :write_merge_request, user_project - required_attributes! [:source_branch, :target_branch, :title] - attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title, :target_project_id, :description] - - # Validate label names in advance - if (errors = validate_label_params(params)).any? - render_api_error!({ labels: errors }, 400) - end - - merge_request = ::MergeRequests::CreateService.new(user_project, current_user, attrs).execute - - if merge_request.valid? - # Find or create labels and attach to issue - if params[:labels].present? - merge_request.add_labels_by_names(params[:labels].split(",")) - end - - present merge_request, with: Entities::MergeRequest - else - handle_merge_request_errors! merge_request.errors - end - end - - # Update MR - # - # Parameters: - # id (required) - The ID of a project - # merge_request_id (required) - ID of MR - # source_branch - The source branch - # target_branch - The target branch - # assignee_id - Assignee user ID - # title - Title of MR - # state_event - Status of MR. (close|reopen|merge) - # description - Description of MR - # labels (optional) - Labels for a MR as a comma-separated list - # Example: - # PUT /projects/:id/merge_request/:merge_request_id - # - put ":id/merge_request/:merge_request_id" do - attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title, :state_event, :description] - merge_request = user_project.merge_requests.find(params[:merge_request_id]) - authorize! :modify_merge_request, merge_request - - # Validate label names in advance - if (errors = validate_label_params(params)).any? - render_api_error!({ labels: errors }, 400) - end - - merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, attrs).execute(merge_request) - - if merge_request.valid? - # Find or create labels and attach to issue - unless params[:labels].nil? - merge_request.remove_labels - merge_request.add_labels_by_names(params[:labels].split(",")) - end - - present merge_request, with: Entities::MergeRequest - else - handle_merge_request_errors! merge_request.errors - end - end - - # Merge MR - # - # Parameters: - # id (required) - The ID of a project - # merge_request_id (required) - ID of MR - # merge_commit_message (optional) - Custom merge commit message - # Example: - # PUT /projects/:id/merge_request/:merge_request_id/merge - # - put ":id/merge_request/:merge_request_id/merge" do - merge_request = user_project.merge_requests.find(params[:merge_request_id]) - - allowed = ::Gitlab::GitAccess.new(current_user, user_project). - can_push_to_branch?(merge_request.target_branch) - - if allowed - if merge_request.unchecked? - merge_request.check_if_can_be_merged - end - - if merge_request.open? - if merge_request.can_be_merged? - merge_request.automerge!(current_user, params[:merge_commit_message] || merge_request.merge_commit_message) - present merge_request, with: Entities::MergeRequest - else - render_api_error!('Branch cannot be merged', 405) - end - else - # Merge request can not be merged - # because it is already closed/merged - not_allowed! - end - else - # Merge request can not be merged - # because user dont have permissions to push into target branch - unauthorized! - end - end - - - # Get a merge request's comments - # - # Parameters: - # id (required) - The ID of a project - # merge_request_id (required) - ID of MR - # Examples: - # GET /projects/:id/merge_request/:merge_request_id/comments - # - get ":id/merge_request/:merge_request_id/comments" do - merge_request = user_project.merge_requests.find(params[:merge_request_id]) - - authorize! :read_merge_request, merge_request - - present paginate(merge_request.notes), with: Entities::MRNote - end - - # Post comment to merge request - # - # Parameters: - # id (required) - The ID of a project - # merge_request_id (required) - ID of MR - # note (required) - Text of comment - # Examples: - # POST /projects/:id/merge_request/:merge_request_id/comments - # - post ":id/merge_request/:merge_request_id/comments" do - required_attributes! [:note] - - merge_request = user_project.merge_requests.find(params[:merge_request_id]) - note = merge_request.notes.new(note: params[:note], project_id: user_project.id) - note.author = current_user - - if note.save - present note, with: Entities::MRNote - else - render_api_error!("Failed to save note #{note.errors.messages}", 400) - end - end - end - end -end diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb deleted file mode 100644 index c5cd73943fb..00000000000 --- a/lib/api/milestones.rb +++ /dev/null @@ -1,95 +0,0 @@ -module API - # Milestones API - class Milestones < Grape::API - before { authenticate! } - - resource :projects do - # Get a list of project milestones - # - # Parameters: - # id (required) - The ID of a project - # Example Request: - # GET /projects/:id/milestones - get ":id/milestones" do - authorize! :read_milestone, user_project - - present paginate(user_project.milestones), with: Entities::Milestone - end - - # Get a single project milestone - # - # Parameters: - # id (required) - The ID of a project - # milestone_id (required) - The ID of a project milestone - # Example Request: - # GET /projects/:id/milestones/:milestone_id - get ":id/milestones/:milestone_id" do - authorize! :read_milestone, user_project - - @milestone = user_project.milestones.find(params[:milestone_id]) - present @milestone, with: Entities::Milestone - end - - # Create a new project milestone - # - # Parameters: - # id (required) - The ID of the project - # title (required) - The title of the milestone - # description (optional) - The description of the milestone - # due_date (optional) - The due date of the milestone - # Example Request: - # POST /projects/:id/milestones - post ":id/milestones" do - authorize! :admin_milestone, user_project - required_attributes! [:title] - attrs = attributes_for_keys [:title, :description, :due_date] - milestone = ::Milestones::CreateService.new(user_project, current_user, attrs).execute - - if milestone.valid? - present milestone, with: Entities::Milestone - else - render_api_error!("Failed to create milestone #{milestone.errors.messages}", 400) - end - end - - # Update an existing project milestone - # - # Parameters: - # id (required) - The ID of a project - # milestone_id (required) - The ID of a project milestone - # title (optional) - The title of a milestone - # description (optional) - The description of a milestone - # due_date (optional) - The due date of a milestone - # state_event (optional) - The state event of the milestone (close|activate) - # Example Request: - # PUT /projects/:id/milestones/:milestone_id - put ":id/milestones/:milestone_id" do - authorize! :admin_milestone, user_project - attrs = attributes_for_keys [:title, :description, :due_date, :state_event] - milestone = user_project.milestones.find(params[:milestone_id]) - milestone = ::Milestones::UpdateService.new(user_project, current_user, attrs).execute(milestone) - - if milestone.valid? - present milestone, with: Entities::Milestone - else - render_api_error!("Failed to update milestone #{milestone.errors.messages}", 400) - end - end - - # Get all issues for a single project milestone - # - # Parameters: - # id (required) - The ID of a project - # milestone_id (required) - The ID of a project milestone - # Example Request: - # GET /projects/:id/milestones/:milestone_id/issues - get ":id/milestones/:milestone_id/issues" do - authorize! :read_milestone, user_project - - @milestone = user_project.milestones.find(params[:milestone_id]) - present paginate(@milestone.issues), with: Entities::Issue - end - - end - end -end diff --git a/lib/api/namespaces.rb b/lib/api/namespaces.rb deleted file mode 100644 index b90ed6af5fb..00000000000 --- a/lib/api/namespaces.rb +++ /dev/null @@ -1,23 +0,0 @@ -module API - # namespaces API - class Namespaces < Grape::API - before do - authenticate! - authenticated_as_admin! - end - - resource :namespaces do - # Get a namespaces list - # - # Example Request: - # GET /namespaces - get do - @namespaces = Namespace.all - @namespaces = @namespaces.search(params[:search]) if params[:search].present? - @namespaces = paginate @namespaces - - present @namespaces, with: Entities::Namespace - end - end - end -end diff --git a/lib/api/notes.rb b/lib/api/notes.rb deleted file mode 100644 index 3726be7c537..00000000000 --- a/lib/api/notes.rb +++ /dev/null @@ -1,103 +0,0 @@ -module API - # Notes API - class Notes < Grape::API - before { authenticate! } - - NOTEABLE_TYPES = [Issue, MergeRequest, Snippet] - - resource :projects do - NOTEABLE_TYPES.each do |noteable_type| - noteables_str = noteable_type.to_s.underscore.pluralize - noteable_id_str = "#{noteable_type.to_s.underscore}_id" - - # Get a list of project +noteable+ notes - # - # Parameters: - # id (required) - The ID of a project - # noteable_id (required) - The ID of an issue or snippet - # Example Request: - # GET /projects/:id/issues/:noteable_id/notes - # GET /projects/:id/snippets/:noteable_id/notes - get ":id/#{noteables_str}/:#{noteable_id_str}/notes" do - @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"]) - present paginate(@noteable.notes), with: Entities::Note - end - - # Get a single +noteable+ note - # - # Parameters: - # id (required) - The ID of a project - # noteable_id (required) - The ID of an issue or snippet - # note_id (required) - The ID of a note - # Example Request: - # GET /projects/:id/issues/:noteable_id/notes/:note_id - # GET /projects/:id/snippets/:noteable_id/notes/:note_id - get ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do - @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"]) - @note = @noteable.notes.find(params[:note_id]) - present @note, with: Entities::Note - end - - # Create a new +noteable+ note - # - # Parameters: - # id (required) - The ID of a project - # noteable_id (required) - The ID of an issue or snippet - # body (required) - The content of a note - # Example Request: - # POST /projects/:id/issues/:noteable_id/notes - # POST /projects/:id/snippets/:noteable_id/notes - post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do - required_attributes! [:body] - - opts = { - note: params[:body], - noteable_type: noteables_str.classify, - noteable_id: params[noteable_id_str] - } - - @note = ::Notes::CreateService.new(user_project, current_user, opts).execute - - if @note.valid? - present @note, with: Entities::Note - else - not_found!("Note #{@note.errors.messages}") - end - end - - # Modify existing +noteable+ note - # - # Parameters: - # id (required) - The ID of a project - # noteable_id (required) - The ID of an issue or snippet - # node_id (required) - The ID of a note - # body (required) - New content of a note - # Example Request: - # PUT /projects/:id/issues/:noteable_id/notes/:note_id - # PUT /projects/:id/snippets/:noteable_id/notes/:node_id - put ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do - required_attributes! [:body] - - authorize! :admin_note, user_project.notes.find(params[:note_id]) - - opts = { - note: params[:body], - note_id: params[:note_id], - noteable_type: noteables_str.classify, - noteable_id: params[noteable_id_str] - } - - @note = ::Notes::UpdateService.new(user_project, current_user, - opts).execute - - if @note.valid? - present @note, with: Entities::Note - else - render_api_error!("Failed to save note #{note.errors.messages}", 400) - end - end - - end - end - end -end diff --git a/lib/api/project_hooks.rb b/lib/api/project_hooks.rb deleted file mode 100644 index be9850367b9..00000000000 --- a/lib/api/project_hooks.rb +++ /dev/null @@ -1,108 +0,0 @@ -module API - # Projects API - class ProjectHooks < Grape::API - before { authenticate! } - before { authorize_admin_project } - - resource :projects do - # Get project hooks - # - # Parameters: - # id (required) - The ID of a project - # Example Request: - # GET /projects/:id/hooks - get ":id/hooks" do - @hooks = paginate user_project.hooks - present @hooks, with: Entities::ProjectHook - end - - # Get a project hook - # - # Parameters: - # id (required) - The ID of a project - # hook_id (required) - The ID of a project hook - # Example Request: - # GET /projects/:id/hooks/:hook_id - get ":id/hooks/:hook_id" do - @hook = user_project.hooks.find(params[:hook_id]) - present @hook, with: Entities::ProjectHook - end - - - # Add hook to project - # - # Parameters: - # id (required) - The ID of a project - # url (required) - The hook URL - # Example Request: - # POST /projects/:id/hooks - post ":id/hooks" do - required_attributes! [:url] - attrs = attributes_for_keys [ - :url, - :push_events, - :issues_events, - :merge_requests_events, - :tag_push_events - ] - @hook = user_project.hooks.new(attrs) - - if @hook.save - present @hook, with: Entities::ProjectHook - else - if @hook.errors[:url].present? - error!("Invalid url given", 422) - end - not_found!("Project hook #{@hook.errors.messages}") - end - end - - # Update an existing project hook - # - # Parameters: - # id (required) - The ID of a project - # hook_id (required) - The ID of a project hook - # url (required) - The hook URL - # Example Request: - # PUT /projects/:id/hooks/:hook_id - put ":id/hooks/:hook_id" do - @hook = user_project.hooks.find(params[:hook_id]) - required_attributes! [:url] - attrs = attributes_for_keys [ - :url, - :push_events, - :issues_events, - :merge_requests_events, - :tag_push_events - ] - - if @hook.update_attributes attrs - present @hook, with: Entities::ProjectHook - else - if @hook.errors[:url].present? - error!("Invalid url given", 422) - end - not_found!("Project hook #{@hook.errors.messages}") - end - end - - # Deletes project hook. This is an idempotent function. - # - # Parameters: - # id (required) - The ID of a project - # hook_id (required) - The ID of hook to delete - # Example Request: - # DELETE /projects/:id/hooks/:hook_id - delete ":id/hooks/:hook_id" do - required_attributes! [:hook_id] - - begin - @hook = ProjectHook.find(params[:hook_id]) - @hook.destroy - rescue - # ProjectHook can raise Error if hook_id not found - end - end - end - end -end diff --git a/lib/api/project_members.rb b/lib/api/project_members.rb deleted file mode 100644 index c756bb479fc..00000000000 --- a/lib/api/project_members.rb +++ /dev/null @@ -1,106 +0,0 @@ -module API - # Projects members API - class ProjectMembers < Grape::API - before { authenticate! } - - resource :projects do - - # Get a project team members - # - # Parameters: - # id (required) - The ID of a project - # query - Query string - # Example Request: - # GET /projects/:id/members - get ":id/members" do - if params[:query].present? - @members = paginate user_project.users.where("username LIKE ?", "%#{params[:query]}%") - else - @members = paginate user_project.users - end - present @members, with: Entities::ProjectMember, project: user_project - end - - # Get a project team members - # - # Parameters: - # id (required) - The ID of a project - # user_id (required) - The ID of a user - # Example Request: - # GET /projects/:id/members/:user_id - get ":id/members/:user_id" do - @member = user_project.users.find params[:user_id] - present @member, with: Entities::ProjectMember, project: user_project - end - - # Add a new project team member - # - # Parameters: - # id (required) - The ID of a project - # user_id (required) - The ID of a user - # access_level (required) - Project access level - # Example Request: - # POST /projects/:id/members - post ":id/members" do - authorize! :admin_project, user_project - required_attributes! [:user_id, :access_level] - - # either the user is already a team member or a new one - project_member = user_project.project_member_by_id(params[:user_id]) - if project_member.nil? - project_member = user_project.project_members.new( - user_id: params[:user_id], - access_level: params[:access_level] - ) - end - - if project_member.save - @member = project_member.user - present @member, with: Entities::ProjectMember, project: user_project - else - handle_member_errors project_member.errors - end - end - - # Update project team member - # - # Parameters: - # id (required) - The ID of a project - # user_id (required) - The ID of a team member - # access_level (required) - Project access level - # Example Request: - # PUT /projects/:id/members/:user_id - put ":id/members/:user_id" do - authorize! :admin_project, user_project - required_attributes! [:access_level] - - project_member = user_project.project_members.find_by(user_id: params[:user_id]) - not_found!("User can not be found") if project_member.nil? - - if project_member.update_attributes(access_level: params[:access_level]) - @member = project_member.user - present @member, with: Entities::ProjectMember, project: user_project - else - handle_member_errors project_member.errors - end - end - - # Remove a team member from project - # - # Parameters: - # id (required) - The ID of a project - # user_id (required) - The ID of a team member - # Example Request: - # DELETE /projects/:id/members/:user_id - delete ":id/members/:user_id" do - authorize! :admin_project, user_project - project_member = user_project.project_members.find_by(user_id: params[:user_id]) - unless project_member.nil? - project_member.destroy - else - { message: "Access revoked", id: params[:user_id].to_i } - end - end - end - end -end diff --git a/lib/api/project_snippets.rb b/lib/api/project_snippets.rb deleted file mode 100644 index 54f2555903f..00000000000 --- a/lib/api/project_snippets.rb +++ /dev/null @@ -1,124 +0,0 @@ -module API - # Projects API - class ProjectSnippets < Grape::API - before { authenticate! } - - resource :projects do - helpers do - def handle_project_member_errors(errors) - if errors[:project_access].any? - error!(errors[:project_access], 422) - end - not_found! - end - end - - # Get a project snippets - # - # Parameters: - # id (required) - The ID of a project - # Example Request: - # GET /projects/:id/snippets - get ":id/snippets" do - present paginate(user_project.snippets), with: Entities::ProjectSnippet - end - - # Get a project snippet - # - # Parameters: - # id (required) - The ID of a project - # snippet_id (required) - The ID of a project snippet - # Example Request: - # GET /projects/:id/snippets/:snippet_id - get ":id/snippets/:snippet_id" do - @snippet = user_project.snippets.find(params[:snippet_id]) - present @snippet, with: Entities::ProjectSnippet - end - - # Create a new project snippet - # - # Parameters: - # id (required) - The ID of a project - # title (required) - The title of a snippet - # file_name (required) - The name of a snippet file - # code (required) - The content of a snippet - # visibility_level (required) - The snippet's visibility - # Example Request: - # POST /projects/:id/snippets - post ":id/snippets" do - authorize! :write_project_snippet, user_project - required_attributes! [:title, :file_name, :code, :visibility_level] - - attrs = attributes_for_keys [:title, :file_name, :visibility_level] - attrs[:content] = params[:code] if params[:code].present? - @snippet = CreateSnippetService.new(user_project, current_user, - attrs).execute - - if @snippet.errors.any? - render_validation_error!(@snippet) - else - present @snippet, with: Entities::ProjectSnippet - end - end - - # Update an existing project snippet - # - # Parameters: - # id (required) - The ID of a project - # snippet_id (required) - The ID of a project snippet - # title (optional) - The title of a snippet - # file_name (optional) - The name of a snippet file - # code (optional) - The content of a snippet - # visibility_level (optional) - The snippet's visibility - # Example Request: - # PUT /projects/:id/snippets/:snippet_id - put ":id/snippets/:snippet_id" do - @snippet = user_project.snippets.find(params[:snippet_id]) - authorize! :modify_project_snippet, @snippet - - attrs = attributes_for_keys [:title, :file_name, :visibility_level] - attrs[:content] = params[:code] if params[:code].present? - - UpdateSnippetService.new(user_project, current_user, @snippet, - attrs).execute - if @snippet.errors.any? - render_validation_error!(@snippet) - else - present @snippet, with: Entities::ProjectSnippet - end - end - - # Delete a project snippet - # - # Parameters: - # id (required) - The ID of a project - # snippet_id (required) - The ID of a project snippet - # Example Request: - # DELETE /projects/:id/snippets/:snippet_id - delete ":id/snippets/:snippet_id" do - begin - @snippet = user_project.snippets.find(params[:snippet_id]) - authorize! :modify_project_snippet, @snippet - @snippet.destroy - rescue - not_found!('Snippet') - end - end - - # Get a raw project snippet - # - # Parameters: - # id (required) - The ID of a project - # snippet_id (required) - The ID of a project snippet - # Example Request: - # GET /projects/:id/snippets/:snippet_id/raw - get ":id/snippets/:snippet_id/raw" do - @snippet = user_project.snippets.find(params[:snippet_id]) - - env['api.format'] = :txt - content_type 'text/plain' - present @snippet.content - end - end - end -end diff --git a/lib/api/projects.rb b/lib/api/projects.rb deleted file mode 100644 index e3fff79d68f..00000000000 --- a/lib/api/projects.rb +++ /dev/null @@ -1,323 +0,0 @@ -module API - # Projects API - class Projects < Grape::API - before { authenticate! } - - resource :projects do - helpers do - def map_public_to_visibility_level(attrs) - publik = attrs.delete(:public) - publik = parse_boolean(publik) - attrs[:visibility_level] = Gitlab::VisibilityLevel::PUBLIC if !attrs[:visibility_level].present? && publik == true - attrs - end - - def filter_projects(projects) - # If the archived parameter is passed, limit results accordingly - if params[:archived].present? - projects = projects.where(archived: parse_boolean(params[:archived])) - end - - if params[:search].present? - projects = projects.search(params[:search]) - end - - projects.reorder(project_order_by => project_sort) - end - - def project_order_by - order_fields = %w(id name path created_at updated_at last_activity_at) - - if order_fields.include?(params['order_by']) - params['order_by'] - else - 'created_at' - end - end - - def project_sort - if params["sort"] == 'asc' - :asc - else - :desc - end - end - end - - # Get a projects list for authenticated user - # - # Example Request: - # GET /projects - get do - @projects = current_user.authorized_projects - @projects = filter_projects(@projects) - @projects = paginate @projects - present @projects, with: Entities::Project - end - - # Get an owned projects list for authenticated user - # - # Example Request: - # GET /projects/owned - get '/owned' do - @projects = current_user.owned_projects - @projects = filter_projects(@projects) - @projects = paginate @projects - present @projects, with: Entities::Project - end - - # Get all projects for admin user - # - # Example Request: - # GET /projects/all - get '/all' do - authenticated_as_admin! - @projects = Project.all - @projects = filter_projects(@projects) - @projects = paginate @projects - present @projects, with: Entities::Project - end - - # Get a single project - # - # Parameters: - # id (required) - The ID of a project - # Example Request: - # GET /projects/:id - get ":id" do - present user_project, with: Entities::ProjectWithAccess, user: current_user - end - - # Get events for a single project - # - # Parameters: - # id (required) - The ID of a project - # Example Request: - # GET /projects/:id/events - get ":id/events" do - events = paginate user_project.events.recent - present events, with: Entities::Event - end - - # Create new project - # - # Parameters: - # name (required) - name for new project - # description (optional) - short project description - # issues_enabled (optional) - # merge_requests_enabled (optional) - # wiki_enabled (optional) - # snippets_enabled (optional) - # namespace_id (optional) - defaults to user namespace - # public (optional) - if true same as setting visibility_level = 20 - # visibility_level (optional) - 0 by default - # import_url (optional) - # Example Request - # POST /projects - post do - required_attributes! [:name] - attrs = attributes_for_keys [:name, - :path, - :description, - :issues_enabled, - :merge_requests_enabled, - :wiki_enabled, - :snippets_enabled, - :namespace_id, - :public, - :visibility_level, - :import_url] - attrs = map_public_to_visibility_level(attrs) - @project = ::Projects::CreateService.new(current_user, attrs).execute - if @project.saved? - present @project, with: Entities::Project - else - if @project.errors[:limit_reached].present? - error!(@project.errors[:limit_reached], 403) - end - render_validation_error!(@project) - end - end - - # Create new project for a specified user. Only available to admin users. - # - # Parameters: - # user_id (required) - The ID of a user - # name (required) - name for new project - # description (optional) - short project description - # default_branch (optional) - 'master' by default - # issues_enabled (optional) - # merge_requests_enabled (optional) - # wiki_enabled (optional) - # snippets_enabled (optional) - # public (optional) - if true same as setting visibility_level = 20 - # visibility_level (optional) - # import_url (optional) - # Example Request - # POST /projects/user/:user_id - post "user/:user_id" do - authenticated_as_admin! - user = User.find(params[:user_id]) - attrs = attributes_for_keys [:name, - :description, - :default_branch, - :issues_enabled, - :merge_requests_enabled, - :wiki_enabled, - :snippets_enabled, - :public, - :visibility_level, - :import_url] - attrs = map_public_to_visibility_level(attrs) - @project = ::Projects::CreateService.new(user, attrs).execute - if @project.saved? - present @project, with: Entities::Project - else - render_validation_error!(@project) - end - end - - # Fork new project for the current user. - # - # Parameters: - # id (required) - The ID of a project - # Example Request - # POST /projects/fork/:id - post 'fork/:id' do - @forked_project = - ::Projects::ForkService.new(user_project, - current_user).execute - if @forked_project.errors.any? - conflict!(@forked_project.errors.messages) - else - present @forked_project, with: Entities::Project - end - end - - # Update an existing project - # - # Parameters: - # id (required) - the id of a project - # name (optional) - name of a project - # path (optional) - path of a project - # description (optional) - short project description - # issues_enabled (optional) - # merge_requests_enabled (optional) - # wiki_enabled (optional) - # snippets_enabled (optional) - # public (optional) - if true same as setting visibility_level = 20 - # visibility_level (optional) - visibility level of a project - # Example Request - # PUT /projects/:id - put ':id' do - attrs = attributes_for_keys [:name, - :path, - :description, - :default_branch, - :issues_enabled, - :merge_requests_enabled, - :wiki_enabled, - :snippets_enabled, - :public, - :visibility_level] - attrs = map_public_to_visibility_level(attrs) - authorize_admin_project - authorize! :rename_project, user_project if attrs[:name].present? - if attrs[:visibility_level].present? - authorize! :change_visibility_level, user_project - end - - ::Projects::UpdateService.new(user_project, - current_user, attrs).execute - - if user_project.errors.any? - render_validation_error!(user_project) - else - present user_project, with: Entities::Project - end - end - - # Remove project - # - # Parameters: - # id (required) - The ID of a project - # Example Request: - # DELETE /projects/:id - delete ":id" do - authorize! :remove_project, user_project - ::Projects::DestroyService.new(user_project, current_user, {}).execute - end - - # Mark this project as forked from another - # - # Parameters: - # id: (required) - The ID of the project being marked as a fork - # forked_from_id: (required) - The ID of the project it was forked from - # Example Request: - # POST /projects/:id/fork/:forked_from_id - post ":id/fork/:forked_from_id" do - authenticated_as_admin! - forked_from_project = find_project(params[:forked_from_id]) - unless forked_from_project.nil? - if user_project.forked_from_project.nil? - user_project.create_forked_project_link(forked_to_project_id: user_project.id, forked_from_project_id: forked_from_project.id) - else - render_api_error!("Project already forked", 409) - end - else - not_found!("Source Project") - end - - end - - # Remove a forked_from relationship - # - # Parameters: - # id: (required) - The ID of the project being marked as a fork - # Example Request: - # DELETE /projects/:id/fork - delete ":id/fork" do - authenticated_as_admin! - unless user_project.forked_project_link.nil? - user_project.forked_project_link.destroy - end - end - # search for projects current_user has access to - # - # Parameters: - # query (required) - A string contained in the project name - # per_page (optional) - number of projects to return per page - # page (optional) - the page to retrieve - # Example Request: - # GET /projects/search/:query - get "/search/:query" do - ids = current_user.authorized_projects.map(&:id) - visibility_levels = [ Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC ] - projects = Project.where("(id in (?) OR visibility_level in (?)) AND (name LIKE (?))", ids, visibility_levels, "%#{params[:query]}%") - sort = params[:sort] == 'desc' ? 'desc' : 'asc' - - projects = case params["order_by"] - when 'id' then projects.order("id #{sort}") - when 'name' then projects.order("name #{sort}") - when 'created_at' then projects.order("created_at #{sort}") - when 'last_activity_at' then projects.order("last_activity_at #{sort}") - else projects - end - - present paginate(projects), with: Entities::Project - end - - - # Get a users list - # - # Example Request: - # GET /users - get ':id/users' do - @users = User.where(id: user_project.team.users.map(&:id)) - @users = @users.search(params[:search]) if params[:search].present? - @users = paginate @users - present @users, with: Entities::UserBasic - end - end - end -end diff --git a/lib/api/repositories.rb b/lib/api/repositories.rb deleted file mode 100644 index 1fbf3dca3c6..00000000000 --- a/lib/api/repositories.rb +++ /dev/null @@ -1,190 +0,0 @@ -require 'mime/types' - -module API - # Projects API - class Repositories < Grape::API - before { authenticate! } - before { authorize! :download_code, user_project } - - resource :projects do - helpers do - def handle_project_member_errors(errors) - if errors[:project_access].any? - error!(errors[:project_access], 422) - end - not_found! - end - end - - # Get a project repository tags - # - # Parameters: - # id (required) - The ID of a project - # Example Request: - # GET /projects/:id/repository/tags - get ":id/repository/tags" do - present user_project.repo.tags.sort_by(&:name).reverse, - with: Entities::RepoTag, project: user_project - end - - # Create tag - # - # Parameters: - # id (required) - The ID of a project - # tag_name (required) - The name of the tag - # ref (required) - Create tag from commit sha or branch - # message (optional) - Specifying a message creates an annotated tag. - # Example Request: - # POST /projects/:id/repository/tags - post ':id/repository/tags' do - authorize_push_project - message = params[:message] || nil - result = CreateTagService.new(user_project, current_user). - execute(params[:tag_name], params[:ref], message) - - if result[:status] == :success - present result[:tag], - with: Entities::RepoTag, - project: user_project - else - render_api_error!(result[:message], 400) - end - end - - # Get a project repository tree - # - # Parameters: - # id (required) - The ID of a project - # ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used - # Example Request: - # GET /projects/:id/repository/tree - get ':id/repository/tree' do - ref = params[:ref_name] || user_project.try(:default_branch) || 'master' - path = params[:path] || nil - - commit = user_project.repository.commit(ref) - not_found!('Tree') unless commit - - tree = user_project.repository.tree(commit.id, path) - - present tree.sorted_entries, with: Entities::RepoTreeObject - end - - # Get a raw file contents - # - # Parameters: - # id (required) - The ID of a project - # sha (required) - The commit or branch name - # filepath (required) - The path to the file to display - # Example Request: - # GET /projects/:id/repository/blobs/:sha - get [ ":id/repository/blobs/:sha", ":id/repository/commits/:sha/blob" ] do - required_attributes! [:filepath] - - ref = params[:sha] - - repo = user_project.repository - - commit = repo.commit(ref) - not_found! "Commit" unless commit - - blob = Gitlab::Git::Blob.find(repo, commit.id, params[:filepath]) - not_found! "File" unless blob - - content_type 'text/plain' - present blob.data - end - - # Get a raw blob contents by blob sha - # - # Parameters: - # id (required) - The ID of a project - # sha (required) - The blob's sha - # Example Request: - # GET /projects/:id/repository/raw_blobs/:sha - get ':id/repository/raw_blobs/:sha' do - ref = params[:sha] - - repo = user_project.repository - - begin - blob = Gitlab::Git::Blob.raw(repo, ref) - rescue - not_found! 'Blob' - end - - not_found! 'Blob' unless blob - - env['api.format'] = :txt - - content_type blob.mime_type - present blob.data - end - - # Get a an archive of the repository - # - # Parameters: - # id (required) - The ID of a project - # sha (optional) - the commit sha to download defaults to the tip of the default branch - # Example Request: - # GET /projects/:id/repository/archive - get ':id/repository/archive', - requirements: { format: Gitlab::Regex.archive_formats_regex } do - authorize! :download_code, user_project - - begin - file_path = ArchiveRepositoryService.new( - user_project, - params[:sha], - params[:format] - ).execute - rescue - not_found!('File') - end - - if file_path && File.exists?(file_path) - data = File.open(file_path, 'rb').read - basename = File.basename(file_path) - header['Content-Disposition'] = "attachment; filename=\"#{basename}\"" - content_type MIME::Types.type_for(file_path).first.content_type - env['api.format'] = :binary - present data - else - redirect request.fullpath - end - end - - # Compare two branches, tags or commits - # - # Parameters: - # id (required) - The ID of a project - # from (required) - the commit sha or branch name - # to (required) - the commit sha or branch name - # Example Request: - # GET /projects/:id/repository/compare?from=master&to=feature - get ':id/repository/compare' do - authorize! :download_code, user_project - required_attributes! [:from, :to] - compare = Gitlab::Git::Compare.new(user_project.repository.raw_repository, params[:from], params[:to]) - present compare, with: Entities::Compare - end - - # Get repository contributors - # - # Parameters: - # id (required) - The ID of a project - # Example Request: - # GET /projects/:id/repository/contributors - get ':id/repository/contributors' do - authorize! :download_code, user_project - - begin - present user_project.repository.contributors, - with: Entities::Contributor - rescue - not_found! - end - end - end - end -end diff --git a/lib/api/services.rb b/lib/api/services.rb deleted file mode 100644 index 3ad59cf3adf..00000000000 --- a/lib/api/services.rb +++ /dev/null @@ -1,78 +0,0 @@ -module API - # Projects API - class Services < Grape::API - before { authenticate! } - before { authorize_admin_project } - - resource :projects do - # Set GitLab CI service for project - # - # Parameters: - # token (required) - CI project token - # project_url (required) - CI project url - # - # Example Request: - # PUT /projects/:id/services/gitlab-ci - put ":id/services/gitlab-ci" do - required_attributes! [:token, :project_url] - attrs = attributes_for_keys [:token, :project_url] - user_project.build_missing_services - - if user_project.gitlab_ci_service.update_attributes(attrs.merge(active: true)) - true - else - not_found! - end - end - - # Delete GitLab CI service settings - # - # Example Request: - # DELETE /projects/:id/services/gitlab-ci - delete ":id/services/gitlab-ci" do - if user_project.gitlab_ci_service - user_project.gitlab_ci_service.update_attributes( - active: false, - token: nil, - project_url: nil - ) - end - end - - # Set Hipchat service for project - # - # Parameters: - # token (required) - Hipchat token - # room (required) - Hipchat room name - # - # Example Request: - # PUT /projects/:id/services/hipchat - put ':id/services/hipchat' do - required_attributes! [:token, :room] - attrs = attributes_for_keys [:token, :room] - user_project.build_missing_services - - if user_project.hipchat_service.update_attributes( - attrs.merge(active: true)) - true - else - not_found! - end - end - - # Delete Hipchat service settings - # - # Example Request: - # DELETE /projects/:id/services/hipchat - delete ':id/services/hipchat' do - if user_project.hipchat_service - user_project.hipchat_service.update_attributes( - active: false, - token: nil, - room: nil - ) - end - end - end - end -end diff --git a/lib/api/session.rb b/lib/api/session.rb deleted file mode 100644 index cc646895914..00000000000 --- a/lib/api/session.rb +++ /dev/null @@ -1,21 +0,0 @@ -module API - # Users API - class Session < Grape::API - # Login to get token - # - # Parameters: - # login (*required) - user login - # email (*required) - user email - # password (required) - user password - # - # Example Request: - # POST /session - post "/session" do - auth = Gitlab::Auth.new - user = auth.find(params[:email] || params[:login], params[:password]) - - return unauthorized! unless user - present user, with: Entities::UserLogin - end - end -end diff --git a/lib/api/system_hooks.rb b/lib/api/system_hooks.rb deleted file mode 100644 index 518964db50d..00000000000 --- a/lib/api/system_hooks.rb +++ /dev/null @@ -1,70 +0,0 @@ -module API - # Hooks API - class SystemHooks < Grape::API - before do - authenticate! - authenticated_as_admin! - end - - resource :hooks do - # Get the list of system hooks - # - # Example Request: - # GET /hooks - get do - @hooks = SystemHook.all - present @hooks, with: Entities::Hook - end - - # Create new system hook - # - # Parameters: - # url (required) - url for system hook - # Example Request - # POST /hooks - post do - attrs = attributes_for_keys [:url] - required_attributes! [:url] - @hook = SystemHook.new attrs - if @hook.save - present @hook, with: Entities::Hook - else - not_found! - end - end - - # Test a hook - # - # Example Request - # GET /hooks/:id - get ":id" do - @hook = SystemHook.find(params[:id]) - data = { - event_name: "project_create", - name: "Ruby", - path: "ruby", - project_id: 1, - owner_name: "Someone", - owner_email: "example@gitlabhq.com" - } - @hook.execute(data) - data - end - - # Delete a hook. This is an idempotent function. - # - # Parameters: - # id (required) - ID of the hook - # Example Request: - # DELETE /hooks/:id - delete ":id" do - begin - @hook = SystemHook.find(params[:id]) - @hook.destroy - rescue - # SystemHook raises an Error if no hook with id found - end - end - end - end -end diff --git a/lib/api/users.rb b/lib/api/users.rb deleted file mode 100644 index 032a5d76e43..00000000000 --- a/lib/api/users.rb +++ /dev/null @@ -1,264 +0,0 @@ -module API - # Users API - class Users < Grape::API - before { authenticate! } - - resource :users do - # Get a users list - # - # Example Request: - # GET /users - get do - @users = User.all - @users = @users.active if params[:active].present? - @users = @users.search(params[:search]) if params[:search].present? - @users = paginate @users - - if current_user.is_admin? - present @users, with: Entities::UserFull - else - present @users, with: Entities::UserBasic - end - end - - # Get a single user - # - # Parameters: - # id (required) - The ID of a user - # Example Request: - # GET /users/:id - get ":id" do - @user = User.find(params[:id]) - - if current_user.is_admin? - present @user, with: Entities::UserFull - else - present @user, with: Entities::UserBasic - end - end - - # Create user. Available only for admin - # - # Parameters: - # email (required) - Email - # password (required) - Password - # name (required) - Name - # username (required) - Name - # skype - Skype ID - # linkedin - Linkedin - # twitter - Twitter account - # website_url - Website url - # projects_limit - Number of projects user can create - # extern_uid - External authentication provider UID - # provider - External provider - # bio - Bio - # admin - User is admin - true or false (default) - # can_create_group - User can create groups - true or false - # confirm - Require user confirmation - true (default) or false - # Example Request: - # POST /users - post do - authenticated_as_admin! - required_attributes! [:email, :password, :name, :username] - attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :projects_limit, :username, :bio, :can_create_group, :admin, :confirm] - admin = attrs.delete(:admin) - confirm = !(attrs.delete(:confirm) =~ (/(false|f|no|0)$/i)) - user = User.build_user(attrs) - user.admin = admin unless admin.nil? - user.skip_confirmation! unless confirm - - identity_attrs = attributes_for_keys [:provider, :extern_uid] - if identity_attrs.any? - user.identities.build(identity_attrs) - end - - if user.save - present user, with: Entities::UserFull - else - conflict!('Email has already been taken') if User. - where(email: user.email). - count > 0 - - conflict!('Username has already been taken') if User. - where(username: user.username). - count > 0 - - render_validation_error!(user) - end - end - - # Update user. Available only for admin - # - # Parameters: - # email - Email - # name - Name - # password - Password - # skype - Skype ID - # linkedin - Linkedin - # twitter - Twitter account - # website_url - Website url - # projects_limit - Limit projects each user can create - # bio - Bio - # admin - User is admin - true or false (default) - # can_create_group - User can create groups - true or false - # Example Request: - # PUT /users/:id - put ":id" do - authenticated_as_admin! - - attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :website_url, :projects_limit, :username, :bio, :can_create_group, :admin] - user = User.find(params[:id]) - not_found!('User') unless user - - admin = attrs.delete(:admin) - user.admin = admin unless admin.nil? - - conflict!('Email has already been taken') if attrs[:email] && - User.where(email: attrs[:email]). - where.not(id: user.id).count > 0 - - conflict!('Username has already been taken') if attrs[:username] && - User.where(username: attrs[:username]). - where.not(id: user.id).count > 0 - - if user.update_attributes(attrs) - present user, with: Entities::UserFull - else - render_validation_error!(user) - end - end - - # Add ssh key to a specified user. Only available to admin users. - # - # Parameters: - # id (required) - The ID of a user - # key (required) - New SSH Key - # title (required) - New SSH Key's title - # Example Request: - # POST /users/:id/keys - post ":id/keys" do - authenticated_as_admin! - required_attributes! [:title, :key] - - user = User.find(params[:id]) - attrs = attributes_for_keys [:title, :key] - key = user.keys.new attrs - if key.save - present key, with: Entities::SSHKey - else - render_validation_error!(key) - end - end - - # Get ssh keys of a specified user. Only available to admin users. - # - # Parameters: - # uid (required) - The ID of a user - # Example Request: - # GET /users/:uid/keys - get ':uid/keys' do - authenticated_as_admin! - user = User.find_by(id: params[:uid]) - not_found!('User') unless user - - present user.keys, with: Entities::SSHKey - end - - # Delete existing ssh key of a specified user. Only available to admin - # users. - # - # Parameters: - # uid (required) - The ID of a user - # id (required) - SSH Key ID - # Example Request: - # DELETE /users/:uid/keys/:id - delete ':uid/keys/:id' do - authenticated_as_admin! - user = User.find_by(id: params[:uid]) - not_found!('User') unless user - - begin - key = user.keys.find params[:id] - key.destroy - rescue ActiveRecord::RecordNotFound - not_found!('Key') - end - end - - # Delete user. Available only for admin - # - # Example Request: - # DELETE /users/:id - delete ":id" do - authenticated_as_admin! - user = User.find_by(id: params[:id]) - - if user - user.destroy - else - not_found!('User') - end - end - end - - resource :user do - # Get currently authenticated user - # - # Example Request: - # GET /user - get do - present @current_user, with: Entities::UserLogin - end - - # Get currently authenticated user's keys - # - # Example Request: - # GET /user/keys - get "keys" do - present current_user.keys, with: Entities::SSHKey - end - - # Get single key owned by currently authenticated user - # - # Example Request: - # GET /user/keys/:id - get "keys/:id" do - key = current_user.keys.find params[:id] - present key, with: Entities::SSHKey - end - - # Add new ssh key to currently authenticated user - # - # Parameters: - # key (required) - New SSH Key - # title (required) - New SSH Key's title - # Example Request: - # POST /user/keys - post "keys" do - required_attributes! [:title, :key] - - attrs = attributes_for_keys [:title, :key] - key = current_user.keys.new attrs - if key.save - present key, with: Entities::SSHKey - else - render_validation_error!(key) - end - end - - # Delete existing ssh key of currently authenticated user - # - # Parameters: - # id (required) - SSH Key ID - # Example Request: - # DELETE /user/keys/:id - delete "keys/:id" do - begin - key = current_user.keys.find params[:id] - key.destroy - rescue - end - end - end - end -end diff --git a/lib/assets/.gitkeep b/lib/assets/.gitkeep deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/lib/backup/database.rb b/lib/backup/database.rb deleted file mode 100644 index 9ab6aca276d..00000000000 --- a/lib/backup/database.rb +++ /dev/null @@ -1,78 +0,0 @@ -require 'yaml' - -module Backup - class Database - attr_reader :config, :db_dir - - def initialize - @config = YAML.load_file(File.join(Rails.root,'config','database.yml'))[Rails.env] - @db_dir = File.join(Gitlab.config.backup.path, 'db') - FileUtils.mkdir_p(@db_dir) unless Dir.exists?(@db_dir) - end - - def dump - success = case config["adapter"] - when /^mysql/ then - $progress.print "Dumping MySQL database #{config['database']} ... " - system('mysqldump', *mysql_args, config['database'], out: db_file_name) - when "postgresql" then - $progress.print "Dumping PostgreSQL database #{config['database']} ... " - pg_env - system('pg_dump', config['database'], out: db_file_name) - end - report_success(success) - abort 'Backup failed' unless success - end - - def restore - success = case config["adapter"] - when /^mysql/ then - $progress.print "Restoring MySQL database #{config['database']} ... " - system('mysql', *mysql_args, config['database'], in: db_file_name) - when "postgresql" then - $progress.print "Restoring PostgreSQL database #{config['database']} ... " - # Drop all tables because PostgreSQL DB dumps do not contain DROP TABLE - # statements like MySQL. - Rake::Task["gitlab:db:drop_all_tables"].invoke - Rake::Task["gitlab:db:drop_all_postgres_sequences"].invoke - pg_env - system('psql', config['database'], '-f', db_file_name) - end - report_success(success) - abort 'Restore failed' unless success - end - - protected - - def db_file_name - File.join(db_dir, 'database.sql') - end - - def mysql_args - args = { - 'host' => '--host', - 'port' => '--port', - 'socket' => '--socket', - 'username' => '--user', - 'encoding' => '--default-character-set', - 'password' => '--password' - } - args.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact - end - - def pg_env - ENV['PGUSER'] = config["username"] if config["username"] - ENV['PGHOST'] = config["host"] if config["host"] - ENV['PGPORT'] = config["port"].to_s if config["port"] - ENV['PGPASSWORD'] = config["password"].to_s if config["password"] - end - - def report_success(success) - if success - $progress.puts '[DONE]'.green - else - $progress.puts '[FAILED]'.red - end - end - end -end diff --git a/lib/backup/manager.rb b/lib/backup/manager.rb deleted file mode 100644 index b69aebf9fe1..00000000000 --- a/lib/backup/manager.rb +++ /dev/null @@ -1,170 +0,0 @@ -module Backup - class Manager - def pack - # saving additional informations - s = {} - s[:db_version] = "#{ActiveRecord::Migrator.current_version}" - s[:backup_created_at] = Time.now - s[:gitlab_version] = Gitlab::VERSION - s[:tar_version] = tar_version - s[:skipped] = ENV["SKIP"] - tar_file = "#{s[:backup_created_at].to_i}_gitlab_backup.tar" - - Dir.chdir(Gitlab.config.backup.path) do - File.open("#{Gitlab.config.backup.path}/backup_information.yml", - "w+") do |file| - file << s.to_yaml.gsub(/^---\n/,'') - end - - FileUtils.chmod(0700, folders_to_backup) - - # create archive - $progress.print "Creating backup archive: #{tar_file} ... " - orig_umask = File.umask(0077) - if Kernel.system('tar', '-cf', tar_file, *backup_contents) - $progress.puts "done".green - else - puts "creating archive #{tar_file} failed".red - abort 'Backup failed' - end - File.umask(orig_umask) - - upload(tar_file) - end - end - - def upload(tar_file) - remote_directory = Gitlab.config.backup.upload.remote_directory - $progress.print "Uploading backup archive to remote storage #{remote_directory} ... " - - connection_settings = Gitlab.config.backup.upload.connection - if connection_settings.blank? - $progress.puts "skipped".yellow - return - end - - connection = ::Fog::Storage.new(connection_settings) - directory = connection.directories.get(remote_directory) - - if directory.files.create(key: tar_file, body: File.open(tar_file), public: false) - $progress.puts "done".green - else - puts "uploading backup to #{remote_directory} failed".red - abort 'Backup failed' - end - end - - def cleanup - $progress.print "Deleting tmp directories ... " - - backup_contents.each do |dir| - next unless File.exist?(File.join(Gitlab.config.backup.path, dir)) - - if FileUtils.rm_rf(File.join(Gitlab.config.backup.path, dir)) - $progress.puts "done".green - else - puts "deleting tmp directory '#{dir}' failed".red - abort 'Backup failed' - end - end - end - - def remove_old - # delete backups - $progress.print "Deleting old backups ... " - keep_time = Gitlab.config.backup.keep_time.to_i - - if keep_time > 0 - removed = 0 - - Dir.chdir(Gitlab.config.backup.path) do - file_list = Dir.glob('*_gitlab_backup.tar') - file_list.map! { |f| $1.to_i if f =~ /(\d+)_gitlab_backup.tar/ } - file_list.sort.each do |timestamp| - if Time.at(timestamp) < (Time.now - keep_time) - if Kernel.system(*%W(rm #{timestamp}_gitlab_backup.tar)) - removed += 1 - end - end - end - end - - $progress.puts "done. (#{removed} removed)".green - else - $progress.puts "skipping".yellow - end - end - - def unpack - Dir.chdir(Gitlab.config.backup.path) - - # check for existing backups in the backup dir - file_list = Dir.glob("*_gitlab_backup.tar").each.map { |f| f.split(/_/).first.to_i } - puts "no backups found" if file_list.count == 0 - - if file_list.count > 1 && ENV["BACKUP"].nil? - puts "Found more than one backup, please specify which one you want to restore:" - puts "rake gitlab:backup:restore BACKUP=timestamp_of_backup" - exit 1 - end - - tar_file = ENV["BACKUP"].nil? ? File.join("#{file_list.first}_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar") - - unless File.exists?(tar_file) - puts "The specified backup doesn't exist!" - exit 1 - end - - $progress.print "Unpacking backup ... " - - unless Kernel.system(*%W(tar -xf #{tar_file})) - puts "unpacking backup failed".red - exit 1 - else - $progress.puts "done".green - end - - ENV["VERSION"] = "#{settings[:db_version]}" if settings[:db_version].to_i > 0 - - # restoring mismatching backups can lead to unexpected problems - if settings[:gitlab_version] != Gitlab::VERSION - puts "GitLab version mismatch:".red - puts " Your current GitLab version (#{Gitlab::VERSION}) differs from the GitLab version in the backup!".red - puts " Please switch to the following version and try again:".red - puts " version: #{settings[:gitlab_version]}".red - puts - puts "Hint: git checkout v#{settings[:gitlab_version]}" - exit 1 - end - end - - def tar_version - tar_version, _ = Gitlab::Popen.popen(%W(tar --version)) - tar_version.force_encoding('locale').split("\n").first - end - - def skipped?(item) - settings[:skipped] && settings[:skipped].include?(item) - end - - private - - def backup_contents - folders_to_backup + ["backup_information.yml"] - end - - def folders_to_backup - folders = %w{repositories db uploads} - - if ENV["SKIP"] - return folders.reject{ |folder| ENV["SKIP"].include?(folder) } - end - - folders - end - - def settings - @settings ||= YAML.load_file("backup_information.yml") - end - end -end diff --git a/lib/backup/repository.rb b/lib/backup/repository.rb deleted file mode 100644 index dfb2da9f84e..00000000000 --- a/lib/backup/repository.rb +++ /dev/null @@ -1,140 +0,0 @@ -require 'yaml' - -module Backup - class Repository - attr_reader :repos_path - - def dump - prepare - - Project.find_each(batch_size: 1000) do |project| - $progress.print " * #{project.path_with_namespace} ... " - - # Create namespace dir if missing - FileUtils.mkdir_p(File.join(backup_repos_path, project.namespace.path)) if project.namespace - - if project.empty_repo? - $progress.puts "[SKIPPED]".cyan - else - cmd = %W(tar -cf #{path_to_bundle(project)} -C #{path_to_repo(project)} .) - output, status = Gitlab::Popen.popen(cmd) - if status.zero? - $progress.puts "[DONE]".green - else - puts "[FAILED]".red - puts "failed: #{cmd.join(' ')}" - puts output - abort 'Backup failed' - end - end - - wiki = ProjectWiki.new(project) - - if File.exists?(path_to_repo(wiki)) - $progress.print " * #{wiki.path_with_namespace} ... " - if wiki.repository.empty? - $progress.puts " [SKIPPED]".cyan - else - cmd = %W(git --git-dir=#{path_to_repo(wiki)} bundle create #{path_to_bundle(wiki)} --all) - output, status = Gitlab::Popen.popen(cmd) - if status.zero? - $progress.puts " [DONE]".green - else - puts " [FAILED]".red - puts "failed: #{cmd.join(' ')}" - abort 'Backup failed' - end - end - end - end - end - - def restore - if File.exists?(repos_path) - # Move repos dir to 'repositories.old' dir - bk_repos_path = File.join(repos_path, '..', 'repositories.old.' + Time.now.to_i.to_s) - FileUtils.mv(repos_path, bk_repos_path) - end - - FileUtils.mkdir_p(repos_path) - - Project.find_each(batch_size: 1000) do |project| - $progress.print " * #{project.path_with_namespace} ... " - - project.namespace.ensure_dir_exist if project.namespace - - if File.exists?(path_to_bundle(project)) - FileUtils.mkdir_p(path_to_repo(project)) - cmd = %W(tar -xf #{path_to_bundle(project)} -C #{path_to_repo(project)}) - else - cmd = %W(git init --bare #{path_to_repo(project)}) - end - - if system(*cmd, silent) - $progress.puts "[DONE]".green - else - puts "[FAILED]".red - puts "failed: #{cmd.join(' ')}" - abort 'Restore failed' - end - - wiki = ProjectWiki.new(project) - - if File.exists?(path_to_bundle(wiki)) - $progress.print " * #{wiki.path_with_namespace} ... " - - # If a wiki bundle exists, first remove the empty repo - # that was initialized with ProjectWiki.new() and then - # try to restore with 'git clone --bare'. - FileUtils.rm_rf(path_to_repo(wiki)) - cmd = %W(git clone --bare #{path_to_bundle(wiki)} #{path_to_repo(wiki)}) - - if system(*cmd, silent) - $progress.puts " [DONE]".green - else - puts " [FAILED]".red - puts "failed: #{cmd.join(' ')}" - abort 'Restore failed' - end - end - end - - $progress.print 'Put GitLab hooks in repositories dirs'.yellow - cmd = "#{Gitlab.config.gitlab_shell.path}/bin/create-hooks" - if system(cmd) - $progress.puts " [DONE]".green - else - puts " [FAILED]".red - puts "failed: #{cmd}" - end - - end - - protected - - def path_to_repo(project) - project.repository.path_to_repo - end - - def path_to_bundle(project) - File.join(backup_repos_path, project.path_with_namespace + ".bundle") - end - - def repos_path - Gitlab.config.gitlab_shell.repos_path - end - - def backup_repos_path - File.join(Gitlab.config.backup.path, "repositories") - end - - def prepare - FileUtils.rm_rf(backup_repos_path) - FileUtils.mkdir_p(backup_repos_path) - end - - def silent - {err: '/dev/null', out: '/dev/null'} - end - end -end diff --git a/lib/backup/uploads.rb b/lib/backup/uploads.rb deleted file mode 100644 index e50e1ff4f13..00000000000 --- a/lib/backup/uploads.rb +++ /dev/null @@ -1,30 +0,0 @@ -module Backup - class Uploads - attr_reader :app_uploads_dir, :backup_uploads_dir, :backup_dir - - def initialize - @app_uploads_dir = File.realpath(Rails.root.join('public', 'uploads')) - @backup_dir = Gitlab.config.backup.path - @backup_uploads_dir = File.join(Gitlab.config.backup.path, 'uploads') - end - - # Copy uploads from public/uploads to backup/uploads - def dump - FileUtils.mkdir_p(backup_uploads_dir) - FileUtils.cp_r(app_uploads_dir, backup_dir) - end - - def restore - backup_existing_uploads_dir - - FileUtils.cp_r(backup_uploads_dir, app_uploads_dir) - end - - def backup_existing_uploads_dir - timestamped_uploads_path = File.join(app_uploads_dir, '..', "uploads.#{Time.now.to_i}") - if File.exists?(app_uploads_dir) - FileUtils.mv(app_uploads_dir, timestamped_uploads_path) - end - end - end -end diff --git a/lib/disable_email_interceptor.rb b/lib/disable_email_interceptor.rb deleted file mode 100644 index 1b80be112a4..00000000000 --- a/lib/disable_email_interceptor.rb +++ /dev/null @@ -1,8 +0,0 @@ -# Read about interceptors in http://guides.rubyonrails.org/action_mailer_basics.html#intercepting-emails -class DisableEmailInterceptor - - def self.delivering_email(message) - message.perform_deliveries = false - Rails.logger.info "Emails disabled! Interceptor prevented sending mail #{message.subject}" - end -end diff --git a/lib/email_validator.rb b/lib/email_validator.rb deleted file mode 100644 index f509f0a5843..00000000000 --- a/lib/email_validator.rb +++ /dev/null @@ -1,21 +0,0 @@ -# Based on https://github.com/balexand/email_validator -# -# Extended to use only strict mode with following allowed characters: -# ' - apostrophe -# -# See http://www.remote.org/jochen/mail/info/chars.html -# -class EmailValidator < ActiveModel::EachValidator - @@default_options = {} - - def self.default_options - @@default_options - end - - def validate_each(record, attribute, value) - options = @@default_options.merge(self.options) - unless value =~ /\A\s*([-a-z0-9+._']{1,64})@((?:[-a-z0-9]+\.)+[a-z]{2,})\s*\z/i - record.errors.add(attribute, options[:message] || :invalid) - end - end -end diff --git a/lib/event_filter.rb b/lib/event_filter.rb deleted file mode 100644 index 163937c02cf..00000000000 --- a/lib/event_filter.rb +++ /dev/null @@ -1,68 +0,0 @@ -class EventFilter - attr_accessor :params - - class << self - def default_filter - %w{ push issues merge_requests team} - end - - def push - 'push' - end - - def merged - 'merged' - end - - def comments - 'comments' - end - - def team - 'team' - end - end - - def initialize(params) - @params = if params - params.dup - else - []#EventFilter.default_filter - end - end - - def apply_filter(events) - return events unless params.present? - - filter = params.dup - - actions = [] - actions << Event::PUSHED if filter.include? 'push' - actions << Event::MERGED if filter.include? 'merged' - - if filter.include? 'team' - actions << Event::JOINED - actions << Event::LEFT - end - - actions << Event::COMMENTED if filter.include? 'comments' - - events = events.where(action: actions) - end - - def options(key) - filter = params.dup - - if filter.include? key - filter.delete key - else - filter << key - end - - filter - end - - def active?(key) - params.include? key - end -end diff --git a/lib/extracts_path.rb b/lib/extracts_path.rb deleted file mode 100644 index 6e4ed01e079..00000000000 --- a/lib/extracts_path.rb +++ /dev/null @@ -1,123 +0,0 @@ -# Module providing methods for dealing with separating a tree-ish string and a -# file path string when combined in a request parameter -module ExtractsPath - # Raised when given an invalid file path - class InvalidPathError < StandardError; end - - # Given a string containing both a Git tree-ish, such as a branch or tag, and - # a filesystem path joined by forward slashes, attempts to separate the two. - # - # Expects a @project instance variable to contain the active project. This is - # used to check the input against a list of valid repository refs. - # - # Examples - # - # # No @project available - # extract_ref('master') - # # => ['', ''] - # - # extract_ref('master') - # # => ['master', ''] - # - # extract_ref("f4b14494ef6abf3d144c28e4af0c20143383e062/CHANGELOG") - # # => ['f4b14494ef6abf3d144c28e4af0c20143383e062', 'CHANGELOG'] - # - # extract_ref("v2.0.0/README.md") - # # => ['v2.0.0', 'README.md'] - # - # extract_ref('master/app/models/project.rb') - # # => ['master', 'app/models/project.rb'] - # - # extract_ref('issues/1234/app/models/project.rb') - # # => ['issues/1234', 'app/models/project.rb'] - # - # # Given an invalid branch, we fall back to just splitting on the first slash - # extract_ref('non/existent/branch/README.md') - # # => ['non', 'existent/branch/README.md'] - # - # Returns an Array where the first value is the tree-ish and the second is the - # path - def extract_ref(id) - pair = ['', ''] - - return pair unless @project - - if id.match(/^([[:alnum:]]{40})(.+)/) - # If the ref appears to be a SHA, we're done, just split the string - pair = $~.captures - else - # Otherwise, attempt to detect the ref using a list of the project's - # branches and tags - - # Append a trailing slash if we only get a ref and no file path - id += '/' unless id.ends_with?('/') - - valid_refs = @project.repository.ref_names - valid_refs.select! { |v| id.start_with?("#{v}/") } - - if valid_refs.length != 1 - # No exact ref match, so just try our best - pair = id.match(/([^\/]+)(.*)/).captures - else - # Partition the string into the ref and the path, ignoring the empty first value - pair = id.partition(valid_refs.first)[1..-1] - end - end - - # Remove ending slashes from path - pair[1].gsub!(/^\/|\/$/, '') - - pair - end - - # Assigns common instance variables for views working with Git tree-ish objects - # - # Assignments are: - # - # - @id - A string representing the joined ref and path - # - @ref - A string representing the ref (e.g., the branch, tag, or commit SHA) - # - @path - A string representing the filesystem path - # - @commit - A Commit representing the commit from the given ref - # - # If the :id parameter appears to be requesting a specific response format, - # that will be handled as well. - # - # Automatically renders `not_found!` if a valid tree path could not be - # resolved (e.g., when a user inserts an invalid path or ref). - def assign_ref_vars - # assign allowed options - allowed_options = ["filter_ref", "extended_sha1"] - @options = params.select {|key, value| allowed_options.include?(key) && !value.blank? } - @options = HashWithIndifferentAccess.new(@options) - - @id = get_id - @ref, @path = extract_ref(@id) - @repo = @project.repository - if @options[:extended_sha1].blank? - @commit = @repo.commit(@ref) - else - @commit = @repo.commit(@options[:extended_sha1]) - end - - raise InvalidPathError unless @commit - - @hex_path = Digest::SHA1.hexdigest(@path) - @logs_path = logs_file_namespace_project_ref_path(@project.namespace, - @project, @ref, @path) - - rescue RuntimeError, NoMethodError, InvalidPathError - not_found! - end - - def tree - @tree ||= @repo.tree(@commit.id, @path) - end - - private - - def get_id - id = params[:id] || params[:ref] - id += "/" + params[:path] unless params[:path].blank? - id - end -end diff --git a/lib/file_size_validator.rb b/lib/file_size_validator.rb deleted file mode 100644 index 2eae55e534b..00000000000 --- a/lib/file_size_validator.rb +++ /dev/null @@ -1,73 +0,0 @@ -class FileSizeValidator < ActiveModel::EachValidator - MESSAGES = { is: :wrong_size, minimum: :size_too_small, maximum: :size_too_big }.freeze - CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze - - DEFAULT_TOKENIZER = lambda { |value| value.split(//) } - RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long] - - def initialize(options) - if range = (options.delete(:in) || options.delete(:within)) - raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range) - options[:minimum], options[:maximum] = range.begin, range.end - options[:maximum] -= 1 if range.exclude_end? - end - - super - end - - def check_validity! - keys = CHECKS.keys & options.keys - - if keys.empty? - raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.' - end - - keys.each do |key| - value = options[key] - - unless (value.is_a?(Integer) && value >= 0) || value.is_a?(Symbol) - raise ArgumentError, ":#{key} must be a nonnegative Integer or symbol" - end - end - end - - def validate_each(record, attribute, value) - raise(ArgumentError, "A CarrierWave::Uploader::Base object was expected") unless value.kind_of? CarrierWave::Uploader::Base - - value = (options[:tokenizer] || DEFAULT_TOKENIZER).call(value) if value.kind_of?(String) - - CHECKS.each do |key, validity_check| - next unless check_value = options[key] - - check_value = - case check_value - when Integer - check_value - when Symbol - record.send(check_value) - end - - value ||= [] if key == :maximum - - value_size = value.size - next if value_size.send(validity_check, check_value) - - errors_options = options.except(*RESERVED_OPTIONS) - errors_options[:file_size] = help.number_to_human_size check_value - - default_message = options[MESSAGES[key]] - errors_options[:message] ||= default_message if default_message - - record.errors.add(attribute, MESSAGES[key], errors_options) - end - end - - def help - Helper.instance - end - - class Helper - include Singleton - include ActionView::Helpers::NumberHelper - end -end diff --git a/lib/gitlab.rb b/lib/gitlab.rb deleted file mode 100644 index 5fc1862c3e9..00000000000 --- a/lib/gitlab.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'gitlab/git' - -module Gitlab - autoload :Satellite, 'gitlab/satellite/satellite' -end diff --git a/lib/gitlab/access.rb b/lib/gitlab/access.rb deleted file mode 100644 index 424541b4a04..00000000000 --- a/lib/gitlab/access.rb +++ /dev/null @@ -1,73 +0,0 @@ -# Gitlab::Access module -# -# Define allowed roles that can be used -# in GitLab code to determine authorization level -# -module Gitlab - module Access - GUEST = 10 - REPORTER = 20 - DEVELOPER = 30 - MASTER = 40 - OWNER = 50 - - # Branch protection settings - PROTECTION_NONE = 0 - PROTECTION_DEV_CAN_PUSH = 1 - PROTECTION_FULL = 2 - - class << self - def values - options.values - end - - def all_values - options_with_owner.values - end - - def options - { - "Guest" => GUEST, - "Reporter" => REPORTER, - "Developer" => DEVELOPER, - "Master" => MASTER, - } - end - - def options_with_owner - options.merge( - "Owner" => OWNER - ) - end - - def sym_options - { - guest: GUEST, - reporter: REPORTER, - developer: DEVELOPER, - master: MASTER, - } - end - - def protection_options - { - "Not protected, developers and masters can (force) push and delete the branch" => PROTECTION_NONE, - "Partially protected, developers can also push but prevent all force pushes and deletion" => PROTECTION_DEV_CAN_PUSH, - "Fully protected, only masters can push and prevent all force pushes and deletion" => PROTECTION_FULL, - } - end - - def protection_values - protection_options.values - end - end - - def human_access - Gitlab::Access.options_with_owner.key(access_field) - end - - def owner? - access_field == OWNER - end - end -end diff --git a/lib/gitlab/app_logger.rb b/lib/gitlab/app_logger.rb deleted file mode 100644 index dddcb2538f9..00000000000 --- a/lib/gitlab/app_logger.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Gitlab - class AppLogger < Gitlab::Logger - def self.file_name_noext - 'application' - end - - def format_message(severity, timestamp, progname, msg) - "#{timestamp.to_s(:long)}: #{msg}\n" - end - end -end diff --git a/lib/gitlab/auth.rb b/lib/gitlab/auth.rb deleted file mode 100644 index 30509528b8b..00000000000 --- a/lib/gitlab/auth.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Gitlab - class Auth - def find(login, password) - user = User.by_login(login) - - # If no user is found, or it's an LDAP server, try LDAP. - # LDAP users are only authenticated via LDAP - if user.nil? || user.ldap_user? - # Second chance - try LDAP authentication - return nil unless Gitlab::LDAP::Config.enabled? - - Gitlab::LDAP::Authentication.login(login, password) - else - user if user.valid_password?(password) - end - end - end -end diff --git a/lib/gitlab/backend/grack_auth.rb b/lib/gitlab/backend/grack_auth.rb deleted file mode 100644 index 050b5ba29dd..00000000000 --- a/lib/gitlab/backend/grack_auth.rb +++ /dev/null @@ -1,182 +0,0 @@ -require_relative 'rack_attack_helpers' -require_relative 'shell_env' - -module Grack - class Auth < Rack::Auth::Basic - - attr_accessor :user, :project, :env - - def call(env) - @env = env - @request = Rack::Request.new(env) - @auth = Request.new(env) - - @gitlab_ci = false - - # Need this patch due to the rails mount - # Need this if under RELATIVE_URL_ROOT - unless Gitlab.config.gitlab.relative_url_root.empty? - # If website is mounted using relative_url_root need to remove it first - @env['PATH_INFO'] = @request.path.sub(Gitlab.config.gitlab.relative_url_root,'') - else - @env['PATH_INFO'] = @request.path - end - - @env['SCRIPT_NAME'] = "" - - auth! - - if project && authorized_request? - @app.call(env) - elsif @user.nil? && !@gitlab_ci - unauthorized - else - render_not_found - end - end - - private - - def auth! - return unless @auth.provided? - - return bad_request unless @auth.basic? - - # Authentication with username and password - login, password = @auth.credentials - - # Allow authentication for GitLab CI service - # if valid token passed - if gitlab_ci_request?(login, password) - @gitlab_ci = true - return - end - - @user = authenticate_user(login, password) - - if @user - Gitlab::ShellEnv.set_env(@user) - @env['REMOTE_USER'] = @auth.username - end - end - - def gitlab_ci_request?(login, password) - if login == "gitlab-ci-token" && project && project.gitlab_ci? - token = project.gitlab_ci_service.token - - if token.present? && token == password && git_cmd == 'git-upload-pack' - return true - end - end - - false - end - - def oauth_access_token_check(login, password) - if login == "oauth2" && git_cmd == 'git-upload-pack' && password.present? - token = Doorkeeper::AccessToken.by_token(password) - token && token.accessible? && User.find_by(id: token.resource_owner_id) - end - end - - def authenticate_user(login, password) - user = Gitlab::Auth.new.find(login, password) - - unless user - user = oauth_access_token_check(login, password) - end - - # If the user authenticated successfully, we reset the auth failure count - # from Rack::Attack for that IP. A client may attempt to authenticate - # with a username and blank password first, and only after it receives - # a 401 error does it present a password. Resetting the count prevents - # false positives from occurring. - # - # Otherwise, we let Rack::Attack know there was a failed authentication - # attempt from this IP. This information is stored in the Rails cache - # (Redis) and will be used by the Rack::Attack middleware to decide - # whether to block requests from this IP. - config = Gitlab.config.rack_attack.git_basic_auth - - if config.enabled - if user - # A successful login will reset the auth failure count from this IP - Rack::Attack::Allow2Ban.reset(@request.ip, config) - else - banned = Rack::Attack::Allow2Ban.filter(@request.ip, config) do - # Unless the IP is whitelisted, return true so that Allow2Ban - # increments the counter (stored in Rails.cache) for the IP - if config.ip_whitelist.include?(@request.ip) - false - else - true - end - end - - if banned - Rails.logger.info "IP #{@request.ip} failed to login " \ - "as #{login} but has been temporarily banned from Git auth" - end - end - end - - user - end - - def authorized_request? - return true if @gitlab_ci - - case git_cmd - when *Gitlab::GitAccess::DOWNLOAD_COMMANDS - if user - Gitlab::GitAccess.new(user, project).download_access_check.allowed? - elsif project.public? - # Allow clone/fetch for public projects - true - else - false - end - when *Gitlab::GitAccess::PUSH_COMMANDS - if user - # Skip user authorization on upload request. - # It will be done by the pre-receive hook in the repository. - true - else - false - end - else - false - end - end - - def git_cmd - if @request.get? - @request.params['service'] - elsif @request.post? - File.basename(@request.path) - else - nil - end - end - - def project - return @project if defined?(@project) - - @project = project_by_path(@request.path_info) - end - - def project_by_path(path) - if m = /^([\w\.\/-]+)\.git/.match(path).to_a - path_with_namespace = m.last - path_with_namespace.gsub!(/\.wiki$/, '') - - path_with_namespace[0] = '' if path_with_namespace.start_with?('/') - Project.find_with_namespace(path_with_namespace) - end - end - - def render_not_found - [404, { "Content-Type" => "text/plain" }, ["Not Found"]] - end - end -end diff --git a/lib/gitlab/backend/rack_attack_helpers.rb b/lib/gitlab/backend/rack_attack_helpers.rb deleted file mode 100644 index 8538f3f6eca..00000000000 --- a/lib/gitlab/backend/rack_attack_helpers.rb +++ /dev/null @@ -1,31 +0,0 @@ -# rack-attack v4.2.0 doesn't yet support clearing of keys. -# Taken from https://github.com/kickstarter/rack-attack/issues/113 -class Rack::Attack::Allow2Ban - def self.reset(discriminator, options) - findtime = options[:findtime] or raise ArgumentError, "Must pass findtime option" - - cache.reset_count("#{key_prefix}:count:#{discriminator}", findtime) - cache.delete("#{key_prefix}:ban:#{discriminator}") - end -end - -class Rack::Attack::Cache - def reset_count(unprefixed_key, period) - epoch_time = Time.now.to_i - # Add 1 to expires_in to avoid timing error: http://git.io/i1PHXA - expires_in = period - (epoch_time % period) + 1 - key = "#{(epoch_time / period).to_i}:#{unprefixed_key}" - delete(key) - end - - def delete(unprefixed_key) - store.delete("#{prefix}:#{unprefixed_key}") - end -end - -class Rack::Attack::StoreProxy::RedisStoreProxy - def delete(key, options={}) - self.del(key) - rescue Redis::BaseError - end -end diff --git a/lib/gitlab/backend/shell.rb b/lib/gitlab/backend/shell.rb deleted file mode 100644 index 530f9d93de4..00000000000 --- a/lib/gitlab/backend/shell.rb +++ /dev/null @@ -1,279 +0,0 @@ -module Gitlab - class Shell - class AccessDenied < StandardError; end - - class KeyAdder < Struct.new(:io) - def add_key(id, key) - io.puts("#{id}\t#{key.strip}") - end - end - - class << self - def version_required - @version_required ||= File.read(Rails.root. - join('GITLAB_SHELL_VERSION')).strip - end - end - - # Init new repository - # - # name - project path with namespace - # - # Ex. - # add_repository("gitlab/gitlab-ci") - # - def add_repository(name) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, - 'add-project', "#{name}.git"]) - end - - # Import repository - # - # name - project path with namespace - # - # Ex. - # import_repository("gitlab/gitlab-ci", "https://github.com/randx/six.git") - # - def import_repository(name, url) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'import-project', - "#{name}.git", url, '240']) - end - - # Move repository - # - # path - project path with namespace - # new_path - new project path with namespace - # - # Ex. - # mv_repository("gitlab/gitlab-ci", "randx/gitlab-ci-new.git") - # - def mv_repository(path, new_path) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'mv-project', - "#{path}.git", "#{new_path}.git"]) - end - - # Update HEAD for repository - # - # path - project path with namespace - # branch - repository branch name - # - # Ex. - # update_repository_head("gitlab/gitlab-ci", "3-1-stable") - # - def update_repository_head(path, branch) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'update-head', - "#{path}.git", branch]) - end - - # Fork repository to new namespace - # - # path - project path with namespace - # fork_namespace - namespace for forked project - # - # Ex. - # fork_repository("gitlab/gitlab-ci", "randx") - # - def fork_repository(path, fork_namespace) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'fork-project', - "#{path}.git", fork_namespace]) - end - - # Remove repository from file system - # - # name - project path with namespace - # - # Ex. - # remove_repository("gitlab/gitlab-ci") - # - def remove_repository(name) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, - 'rm-project', "#{name}.git"]) - end - - # Add repository branch from passed ref - # - # path - project path with namespace - # branch_name - new branch name - # ref - HEAD for new branch - # - # Ex. - # add_branch("gitlab/gitlab-ci", "4-0-stable", "master") - # - def add_branch(path, branch_name, ref) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'create-branch', - "#{path}.git", branch_name, ref]) - end - - # Remove repository branch - # - # path - project path with namespace - # branch_name - branch name to remove - # - # Ex. - # rm_branch("gitlab/gitlab-ci", "4-0-stable") - # - def rm_branch(path, branch_name) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'rm-branch', - "#{path}.git", branch_name]) - end - - # Add repository tag from passed ref - # - # path - project path with namespace - # tag_name - new tag name - # ref - HEAD for new tag - # message - optional message for tag (annotated tag) - # - # Ex. - # add_tag("gitlab/gitlab-ci", "v4.0", "master") - # add_tag("gitlab/gitlab-ci", "v4.0", "master", "message") - # - def add_tag(path, tag_name, ref, message = nil) - cmd = %W(#{gitlab_shell_path}/bin/gitlab-projects create-tag #{path}.git - #{tag_name} #{ref}) - cmd << message unless message.nil? || message.empty? - Gitlab::Utils.system_silent(cmd) - end - - # Remove repository tag - # - # path - project path with namespace - # tag_name - tag name to remove - # - # Ex. - # rm_tag("gitlab/gitlab-ci", "v4.0") - # - def rm_tag(path, tag_name) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'rm-tag', - "#{path}.git", tag_name]) - end - - # Add new key to gitlab-shell - # - # Ex. - # add_key("key-42", "sha-rsa ...") - # - def add_key(key_id, key_content) - Gitlab::Utils.system_silent([gitlab_shell_keys_path, - 'add-key', key_id, key_content]) - end - - # Batch-add keys to authorized_keys - # - # Ex. - # batch_add_keys { |adder| adder.add_key("key-42", "sha-rsa ...") } - def batch_add_keys(&block) - IO.popen(%W(#{gitlab_shell_path}/bin/gitlab-keys batch-add-keys), 'w') do |io| - block.call(KeyAdder.new(io)) - end - end - - # Remove ssh key from gitlab shell - # - # Ex. - # remove_key("key-342", "sha-rsa ...") - # - def remove_key(key_id, key_content) - Gitlab::Utils.system_silent([gitlab_shell_keys_path, - 'rm-key', key_id, key_content]) - end - - # Remove all ssh keys from gitlab shell - # - # Ex. - # remove_all_keys - # - def remove_all_keys - Gitlab::Utils.system_silent([gitlab_shell_keys_path, 'clear']) - end - - # Add empty directory for storing repositories - # - # Ex. - # add_namespace("gitlab") - # - def add_namespace(name) - FileUtils.mkdir(full_path(name), mode: 0770) unless exists?(name) - end - - # Remove directory from repositories storage - # Every repository inside this directory will be removed too - # - # Ex. - # rm_namespace("gitlab") - # - def rm_namespace(name) - FileUtils.rm_r(full_path(name), force: true) - end - - # Move namespace directory inside repositories storage - # - # Ex. - # mv_namespace("gitlab", "gitlabhq") - # - def mv_namespace(old_name, new_name) - return false if exists?(new_name) || !exists?(old_name) - - FileUtils.mv(full_path(old_name), full_path(new_name)) - end - - # Remove GitLab Satellites for provided path (namespace or repo dir) - # - # Ex. - # rm_satellites("gitlab") - # - # rm_satellites("gitlab/gitlab-ci.git") - # - def rm_satellites(path) - raise ArgumentError.new("Path can't be blank") if path.blank? - - satellites_path = File.join(Gitlab.config.satellites.path, path) - FileUtils.rm_r(satellites_path, force: true) - end - - def url_to_repo(path) - Gitlab.config.gitlab_shell.ssh_path_prefix + "#{path}.git" - end - - # Return GitLab shell version - def version - gitlab_shell_version_file = "#{gitlab_shell_path}/VERSION" - - if File.readable?(gitlab_shell_version_file) - File.read(gitlab_shell_version_file).chomp - end - end - - protected - - def gitlab_shell_path - Gitlab.config.gitlab_shell.path - end - - def gitlab_shell_user_home - File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}") - end - - def repos_path - Gitlab.config.gitlab_shell.repos_path - end - - def full_path(dir_name) - raise ArgumentError.new("Directory name can't be blank") if dir_name.blank? - - File.join(repos_path, dir_name) - end - - def exists?(dir_name) - File.exists?(full_path(dir_name)) - end - - def gitlab_shell_projects_path - File.join(gitlab_shell_path, 'bin', 'gitlab-projects') - end - - def gitlab_shell_keys_path - File.join(gitlab_shell_path, 'bin', 'gitlab-keys') - end - end -end diff --git a/lib/gitlab/backend/shell_adapter.rb b/lib/gitlab/backend/shell_adapter.rb deleted file mode 100644 index fbe2a7a0d72..00000000000 --- a/lib/gitlab/backend/shell_adapter.rb +++ /dev/null @@ -1,11 +0,0 @@ -# == GitLab Shell mixin -# -# Provide a shortcut to Gitlab::Shell instance by gitlab_shell -# -module Gitlab - module ShellAdapter - def gitlab_shell - Gitlab::Shell.new - end - end -end diff --git a/lib/gitlab/backend/shell_env.rb b/lib/gitlab/backend/shell_env.rb deleted file mode 100644 index 044afb27f3f..00000000000 --- a/lib/gitlab/backend/shell_env.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Gitlab - # This module provide 2 methods - # to set specific ENV variables for GitLab Shell - module ShellEnv - extend self - - def set_env(user) - # Set GL_ID env variable - ENV['GL_ID'] = "user-#{user.id}" - end - - def reset_env - # Reset GL_ID env variable - ENV['GL_ID'] = nil - end - end -end diff --git a/lib/gitlab/bitbucket_import.rb b/lib/gitlab/bitbucket_import.rb deleted file mode 100644 index 7298152e7e9..00000000000 --- a/lib/gitlab/bitbucket_import.rb +++ /dev/null @@ -1,6 +0,0 @@ -module Gitlab - module BitbucketImport - mattr_accessor :public_key - @public_key = nil - end -end diff --git a/lib/gitlab/bitbucket_import/client.rb b/lib/gitlab/bitbucket_import/client.rb deleted file mode 100644 index 5b1952b9675..00000000000 --- a/lib/gitlab/bitbucket_import/client.rb +++ /dev/null @@ -1,99 +0,0 @@ -module Gitlab - module BitbucketImport - class Client - attr_reader :consumer, :api - - def initialize(access_token = nil, access_token_secret = nil) - @consumer = ::OAuth::Consumer.new( - config.app_id, - config.app_secret, - bitbucket_options - ) - - if access_token && access_token_secret - @api = ::OAuth::AccessToken.new(@consumer, access_token, access_token_secret) - end - end - - def request_token(redirect_uri) - request_token = consumer.get_request_token(oauth_callback: redirect_uri) - - { - oauth_token: request_token.token, - oauth_token_secret: request_token.secret, - oauth_callback_confirmed: request_token.callback_confirmed?.to_s - } - end - - def authorize_url(request_token, redirect_uri) - request_token = ::OAuth::RequestToken.from_hash(consumer, request_token) if request_token.is_a?(Hash) - - if request_token.callback_confirmed? - request_token.authorize_url - else - request_token.authorize_url(oauth_callback: redirect_uri) - end - end - - def get_token(request_token, oauth_verifier, redirect_uri) - request_token = ::OAuth::RequestToken.from_hash(consumer, request_token) if request_token.is_a?(Hash) - - if request_token.callback_confirmed? - request_token.get_access_token(oauth_verifier: oauth_verifier) - else - request_token.get_access_token(oauth_callback: redirect_uri) - end - end - - def user - JSON.parse(api.get("/api/1.0/user").body) - end - - def issues(project_identifier) - JSON.parse(api.get("/api/1.0/repositories/#{project_identifier}/issues").body) - end - - def issue_comments(project_identifier, issue_id) - JSON.parse(api.get("/api/1.0/repositories/#{project_identifier}/issues/#{issue_id}/comments").body) - end - - def project(project_identifier) - JSON.parse(api.get("/api/1.0/repositories/#{project_identifier}").body) - end - - def find_deploy_key(project_identifier, key) - JSON.parse(api.get("/api/1.0/repositories/#{project_identifier}/deploy-keys").body).find do |deploy_key| - deploy_key["key"].chomp == key.chomp - end - end - - def add_deploy_key(project_identifier, key) - deploy_key = find_deploy_key(project_identifier, key) - return if deploy_key - - JSON.parse(api.post("/api/1.0/repositories/#{project_identifier}/deploy-keys", key: key, label: "GitLab import key").body) - end - - def delete_deploy_key(project_identifier, key) - deploy_key = find_deploy_key(project_identifier, key) - return unless deploy_key - - api.delete("/api/1.0/repositories/#{project_identifier}/deploy-keys/#{deploy_key["pk"]}").code == "204" - end - - def projects - JSON.parse(api.get("/api/1.0/user/repositories").body).select { |repo| repo["scm"] == "git" } - end - - private - - def config - Gitlab.config.omniauth.providers.find { |provider| provider.name == "bitbucket"} - end - - def bitbucket_options - OmniAuth::Strategies::Bitbucket.default_options[:client_options].symbolize_keys - end - end - end -end diff --git a/lib/gitlab/bitbucket_import/importer.rb b/lib/gitlab/bitbucket_import/importer.rb deleted file mode 100644 index 42c93707caa..00000000000 --- a/lib/gitlab/bitbucket_import/importer.rb +++ /dev/null @@ -1,52 +0,0 @@ -module Gitlab - module BitbucketImport - class Importer - attr_reader :project, :client - - def initialize(project) - @project = project - @client = Client.new(project.creator.bitbucket_access_token, project.creator.bitbucket_access_token_secret) - @formatter = Gitlab::ImportFormatter.new - end - - def execute - project_identifier = project.import_source - - return true unless client.project(project_identifier)["has_issues"] - - #Issues && Comments - issues = client.issues(project_identifier) - - issues["issues"].each do |issue| - body = @formatter.author_line(issue["reported_by"]["username"], issue["content"]) - - comments = client.issue_comments(project_identifier, issue["local_id"]) - - if comments.any? - body += @formatter.comments_header - end - - comments.each do |comment| - body += @formatter.comment(comment["author_info"]["username"], comment["utc_created_on"], comment["content"]) - end - - project.issues.create!( - description: body, - title: issue["title"], - state: %w(resolved invalid duplicate wontfix).include?(issue["status"]) ? 'closed' : 'opened', - author_id: gl_user_id(project, issue["reported_by"]["username"]) - ) - end - - true - end - - private - - def gl_user_id(project, bitbucket_id) - user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'bitbucket'", bitbucket_id.to_s) - (user && user.id) || project.creator_id - end - end - end -end diff --git a/lib/gitlab/bitbucket_import/key_adder.rb b/lib/gitlab/bitbucket_import/key_adder.rb deleted file mode 100644 index 9931aa7e029..00000000000 --- a/lib/gitlab/bitbucket_import/key_adder.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Gitlab - module BitbucketImport - class KeyAdder - attr_reader :repo, :current_user, :client - - def initialize(repo, current_user) - @repo, @current_user = repo, current_user - @client = Client.new(current_user.bitbucket_access_token, current_user.bitbucket_access_token_secret) - end - - def execute - return false unless BitbucketImport.public_key.present? - - project_identifier = "#{repo["owner"]}/#{repo["slug"]}" - client.add_deploy_key(project_identifier, BitbucketImport.public_key) - - true - rescue - false - end - end - end -end diff --git a/lib/gitlab/bitbucket_import/key_deleter.rb b/lib/gitlab/bitbucket_import/key_deleter.rb deleted file mode 100644 index 1a24a86fc37..00000000000 --- a/lib/gitlab/bitbucket_import/key_deleter.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Gitlab - module BitbucketImport - class KeyDeleter - attr_reader :project, :current_user, :client - - def initialize(project) - @project = project - @current_user = project.creator - @client = Client.new(current_user.bitbucket_access_token, current_user.bitbucket_access_token_secret) - end - - def execute - return false unless BitbucketImport.public_key.present? - - client.delete_deploy_key(project.import_source, BitbucketImport.public_key) - - true - rescue - false - end - end - end -end diff --git a/lib/gitlab/bitbucket_import/project_creator.rb b/lib/gitlab/bitbucket_import/project_creator.rb deleted file mode 100644 index 54420e62c90..00000000000 --- a/lib/gitlab/bitbucket_import/project_creator.rb +++ /dev/null @@ -1,26 +0,0 @@ -module Gitlab - module BitbucketImport - class ProjectCreator - attr_reader :repo, :namespace, :current_user - - def initialize(repo, namespace, current_user) - @repo = repo - @namespace = namespace - @current_user = current_user - end - - def execute - ::Projects::CreateService.new(current_user, - name: repo["name"], - path: repo["slug"], - description: repo["description"], - namespace_id: namespace.id, - visibility_level: repo["is_private"] ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::VisibilityLevel::PUBLIC, - import_type: "bitbucket", - import_source: "#{repo["owner"]}/#{repo["slug"]}", - import_url: "ssh://git@bitbucket.org/#{repo["owner"]}/#{repo["slug"]}.git" - ).execute - end - end - end -end diff --git a/lib/gitlab/blacklist.rb b/lib/gitlab/blacklist.rb deleted file mode 100644 index 43145e0ee1b..00000000000 --- a/lib/gitlab/blacklist.rb +++ /dev/null @@ -1,34 +0,0 @@ -module Gitlab - module Blacklist - extend self - - def path - %w( - admin - dashboard - files - groups - help - profile - projects - search - public - assets - u - s - teams - merge_requests - issues - users - snippets - services - repository - hooks - notes - unsubscribes - all - ci - ) - end - end -end diff --git a/lib/gitlab/closing_issue_extractor.rb b/lib/gitlab/closing_issue_extractor.rb deleted file mode 100644 index ab184d95c05..00000000000 --- a/lib/gitlab/closing_issue_extractor.rb +++ /dev/null @@ -1,20 +0,0 @@ -module Gitlab - class ClosingIssueExtractor - ISSUE_CLOSING_REGEX = Regexp.new(Gitlab.config.gitlab.issue_closing_pattern) - - def initialize(project, current_user = nil) - @extractor = Gitlab::ReferenceExtractor.new(project, current_user) - end - - def closed_by_message(message) - return [] if message.nil? - - closing_statements = message.scan(ISSUE_CLOSING_REGEX). - map { |ref| ref[0] }.join(" ") - - @extractor.analyze(closing_statements) - - @extractor.issues - end - end -end diff --git a/lib/gitlab/compare_result.rb b/lib/gitlab/compare_result.rb deleted file mode 100644 index d72391dade5..00000000000 --- a/lib/gitlab/compare_result.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Gitlab - class CompareResult - attr_reader :commits, :diffs - - def initialize(compare) - @commits, @diffs = compare.commits, compare.diffs - end - end -end diff --git a/lib/gitlab/config_helper.rb b/lib/gitlab/config_helper.rb deleted file mode 100644 index 41880069e4c..00000000000 --- a/lib/gitlab/config_helper.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Gitlab::ConfigHelper - def gitlab_config_features - Gitlab.config.gitlab.default_projects_features - end - - def gitlab_config - Gitlab.config.gitlab - end -end diff --git a/lib/gitlab/contributions_calendar.rb b/lib/gitlab/contributions_calendar.rb deleted file mode 100644 index 3fd0823df06..00000000000 --- a/lib/gitlab/contributions_calendar.rb +++ /dev/null @@ -1,56 +0,0 @@ -module Gitlab - class ContributionsCalendar - attr_reader :timestamps, :projects, :user - - def initialize(projects, user) - @projects = projects - @user = user - end - - def timestamps - return @timestamps if @timestamps.present? - - @timestamps = {} - date_from = 1.year.ago - date_to = Date.today - - events = Event.reorder(nil).contributions.where(author_id: user.id). - where("created_at > ?", date_from).where(project_id: projects). - group('date(created_at)'). - select('date(created_at), count(id) as total_amount'). - map(&:attributes) - - dates = (1.year.ago.to_date..(Date.today + 1.day)).to_a - - dates.each do |date| - date_id = date.to_time.to_i.to_s - @timestamps[date_id] = 0 - day_events = events.find { |day_events| day_events["date"] == date } - - if day_events - @timestamps[date_id] = day_events["total_amount"] - end - end - - @timestamps - end - - def events_by_date(date) - events = Event.contributions.where(author_id: user.id). - where("created_at > ? AND created_at < ?", date.beginning_of_day, date.end_of_day). - where(project_id: projects) - - events.select do |event| - event.push? || event.issue? || event.merge_request? - end - end - - def starting_year - (Time.now - 1.year).strftime("%Y") - end - - def starting_month - Date.today.strftime("%m").to_i - end - end -end diff --git a/lib/gitlab/contributor.rb b/lib/gitlab/contributor.rb deleted file mode 100644 index c41e92b620f..00000000000 --- a/lib/gitlab/contributor.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Gitlab - class Contributor - attr_accessor :email, :name, :commits, :additions, :deletions - - def initialize - @commits, @additions, @deletions = 0, 0, 0 - end - end -end diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb deleted file mode 100644 index d8f696d247b..00000000000 --- a/lib/gitlab/current_settings.rb +++ /dev/null @@ -1,28 +0,0 @@ -module Gitlab - module CurrentSettings - def current_application_settings - key = :current_application_settings - - RequestStore.store[key] ||= begin - if ActiveRecord::Base.connected? && ActiveRecord::Base.connection.table_exists?('application_settings') - ApplicationSetting.current || ApplicationSetting.create_from_defaults - else - fake_application_settings - end - end - end - - def fake_application_settings - OpenStruct.new( - default_projects_limit: Settings.gitlab['default_projects_limit'], - default_branch_protection: Settings.gitlab['default_branch_protection'], - signup_enabled: Settings.gitlab['signup_enabled'], - signin_enabled: Settings.gitlab['signin_enabled'], - gravatar_enabled: Settings.gravatar['enabled'], - sign_in_text: Settings.extra['sign_in_text'], - restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'], - max_attachment_size: Settings.gitlab['max_attachment_size'] - ) - end - end -end diff --git a/lib/gitlab/diff/file.rb b/lib/gitlab/diff/file.rb deleted file mode 100644 index 4daf65331e8..00000000000 --- a/lib/gitlab/diff/file.rb +++ /dev/null @@ -1,49 +0,0 @@ -module Gitlab - module Diff - class File - attr_reader :diff - - delegate :new_file, :deleted_file, :renamed_file, - :old_path, :new_path, to: :diff, prefix: false - - def initialize(diff) - @diff = diff - end - - # Array of Gitlab::DIff::Line objects - def diff_lines - @lines ||= parser.parse(raw_diff.lines) - end - - def mode_changed? - !!(diff.a_mode && diff.b_mode && diff.a_mode != diff.b_mode) - end - - def parser - Gitlab::Diff::Parser.new - end - - def raw_diff - diff.diff.to_s - end - - def next_line(index) - diff_lines[index + 1] - end - - def prev_line(index) - if index > 0 - diff_lines[index - 1] - end - end - - def file_path - if diff.new_path.present? - diff.new_path - elsif diff.old_path.present? - diff.old_path - end - end - end - end -end diff --git a/lib/gitlab/diff/line.rb b/lib/gitlab/diff/line.rb deleted file mode 100644 index 8ac1b15e88a..00000000000 --- a/lib/gitlab/diff/line.rb +++ /dev/null @@ -1,12 +0,0 @@ -module Gitlab - module Diff - class Line - attr_reader :type, :text, :index, :old_pos, :new_pos - - def initialize(text, type, index, old_pos, new_pos) - @text, @type, @index = text, type, index - @old_pos, @new_pos = old_pos, new_pos - end - end - end -end diff --git a/lib/gitlab/diff/line_code.rb b/lib/gitlab/diff/line_code.rb deleted file mode 100644 index f3578ab3d35..00000000000 --- a/lib/gitlab/diff/line_code.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Gitlab - module Diff - class LineCode - def self.generate(file_path, new_line_position, old_line_position) - "#{Digest::SHA1.hexdigest(file_path)}_#{old_line_position}_#{new_line_position}" - end - end - end -end diff --git a/lib/gitlab/diff/parser.rb b/lib/gitlab/diff/parser.rb deleted file mode 100644 index c1d9520ddf1..00000000000 --- a/lib/gitlab/diff/parser.rb +++ /dev/null @@ -1,81 +0,0 @@ -module Gitlab - module Diff - class Parser - include Enumerable - - def parse(lines) - @lines = lines - lines_obj = [] - line_obj_index = 0 - line_old = 1 - line_new = 1 - type = nil - - lines_arr = ::Gitlab::InlineDiff.processing lines - - lines_arr.each do |line| - raw_line = line.dup - - next if filename?(line) - - full_line = html_escape(line.gsub(/\n/, '')) - full_line = ::Gitlab::InlineDiff.replace_markers full_line - - if line.match(/^@@ -/) - type = "match" - - line_old = line.match(/\-[0-9]*/)[0].to_i.abs rescue 0 - line_new = line.match(/\+[0-9]*/)[0].to_i.abs rescue 0 - - next if line_old <= 1 && line_new <= 1 #top of file - lines_obj << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new) - line_obj_index += 1 - next - else - type = identification_type(line) - lines_obj << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new) - line_obj_index += 1 - end - - - if line[0] == "+" - line_new += 1 - elsif line[0] == "-" - line_old += 1 - else - line_new += 1 - line_old += 1 - end - end - - lines_obj - end - - def empty? - @lines.empty? - end - - private - - def filename?(line) - line.start_with?('--- /dev/null', '+++ /dev/null', '--- a', '+++ b', - '--- /tmp/diffy', '+++ /tmp/diffy') - end - - def identification_type(line) - if line[0] == "+" - "new" - elsif line[0] == "-" - "old" - else - nil - end - end - - def html_escape(str) - replacements = { '&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => ''' } - str.gsub(/[&"'><]/, replacements) - end - end - end -end diff --git a/lib/gitlab/force_push_check.rb b/lib/gitlab/force_push_check.rb deleted file mode 100644 index fdb6a35c78d..00000000000 --- a/lib/gitlab/force_push_check.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Gitlab - class ForcePushCheck - def self.force_push?(project, oldrev, newrev) - return false if project.empty_repo? - - # Created or deleted branch - if Gitlab::Git.blank_ref?(oldrev) || Gitlab::Git.blank_ref?(newrev) - false - else - missed_refs, _ = Gitlab::Popen.popen(%W(git --git-dir=#{project.repository.path_to_repo} rev-list #{oldrev} ^#{newrev})) - missed_refs.split("\n").size > 0 - end - end - end -end diff --git a/lib/gitlab/git.rb b/lib/gitlab/git.rb deleted file mode 100644 index 0c350d7c675..00000000000 --- a/lib/gitlab/git.rb +++ /dev/null @@ -1,25 +0,0 @@ -module Gitlab - module Git - BLANK_SHA = '0' * 40 - TAG_REF_PREFIX = "refs/tags/" - BRANCH_REF_PREFIX = "refs/heads/" - - class << self - def ref_name(ref) - ref.gsub(/\Arefs\/(tags|heads)\//, '') - end - - def tag_ref?(ref) - ref.start_with?(TAG_REF_PREFIX) - end - - def branch_ref?(ref) - ref.start_with?(BRANCH_REF_PREFIX) - end - - def blank_ref?(ref) - ref == BLANK_SHA - end - end - end -end diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb deleted file mode 100644 index bc72b7528d5..00000000000 --- a/lib/gitlab/git_access.rb +++ /dev/null @@ -1,198 +0,0 @@ -module Gitlab - class GitAccess - DOWNLOAD_COMMANDS = %w{ git-upload-pack git-upload-archive } - PUSH_COMMANDS = %w{ git-receive-pack } - - attr_reader :actor, :project - - def initialize(actor, project) - @actor = actor - @project = project - end - - def user - return @user if defined?(@user) - - @user = - case actor - when User - actor - when DeployKey - nil - when Key - actor.user - end - end - - def deploy_key - actor if actor.is_a?(DeployKey) - end - - def can_push_to_branch?(ref) - return false unless user - - if project.protected_branch?(ref) && - !(project.developers_can_push_to_protected_branch?(ref) && project.team.developer?(user)) - user.can?(:push_code_to_protected_branches, project) - else - user.can?(:push_code, project) - end - end - - def can_read_project? - if user - user.can?(:read_project, project) - elsif deploy_key - deploy_key.projects.include?(project) - else - false - end - end - - def check(cmd, changes = nil) - case cmd - when *DOWNLOAD_COMMANDS - download_access_check - when *PUSH_COMMANDS - push_access_check(changes) - else - build_status_object(false, "Wrong command") - end - end - - def download_access_check - if user - user_download_access_check - elsif deploy_key - deploy_key_download_access_check - else - raise 'Wrong actor' - end - end - - def push_access_check(changes) - if user - user_push_access_check(changes) - elsif deploy_key - build_status_object(false, "Deploy key not allowed to push") - else - raise 'Wrong actor' - end - end - - def user_download_access_check - if user && user_allowed? && user.can?(:download_code, project) - build_status_object(true) - else - build_status_object(false, "You don't have access") - end - end - - def deploy_key_download_access_check - if can_read_project? - build_status_object(true) - else - build_status_object(false, "Deploy key not allowed to access this project") - end - end - - def user_push_access_check(changes) - unless user && user_allowed? - return build_status_object(false, "You don't have access") - end - - if changes.blank? - return build_status_object(true) - end - - unless project.repository.exists? - return build_status_object(false, "Repository does not exist") - end - - changes = changes.lines if changes.kind_of?(String) - - # Iterate over all changes to find if user allowed all of them to be applied - changes.map(&:strip).reject(&:blank?).each do |change| - status = change_access_check(change) - unless status.allowed? - # If user does not have access to make at least one change - cancel all push - return status - end - end - - build_status_object(true) - end - - def change_access_check(change) - oldrev, newrev, ref = change.split(' ') - - action = - if project.protected_branch?(branch_name(ref)) - protected_branch_action(oldrev, newrev, branch_name(ref)) - elsif protected_tag?(tag_name(ref)) - # Prevent any changes to existing git tag unless user has permissions - :admin_project - else - :push_code - end - - if user.can?(action, project) - build_status_object(true) - else - build_status_object(false, "You don't have permission") - end - end - - def forced_push?(oldrev, newrev) - Gitlab::ForcePushCheck.force_push?(project, oldrev, newrev) - end - - private - - def protected_branch_action(oldrev, newrev, branch_name) - # we dont allow force push to protected branch - if forced_push?(oldrev, newrev) - :force_push_code_to_protected_branches - elsif Gitlab::Git.blank_ref?(newrev) - # and we dont allow remove of protected branch - :remove_protected_branches - elsif project.developers_can_push_to_protected_branch?(branch_name) - :push_code - else - :push_code_to_protected_branches - end - end - - def protected_tag?(tag_name) - project.repository.tag_names.include?(tag_name) - end - - def user_allowed? - Gitlab::UserAccess.allowed?(user) - end - - def branch_name(ref) - ref = ref.to_s - if Gitlab::Git.branch_ref?(ref) - Gitlab::Git.ref_name(ref) - else - nil - end - end - - def tag_name(ref) - ref = ref.to_s - if Gitlab::Git.tag_ref?(ref) - Gitlab::Git.ref_name(ref) - else - nil - end - end - - protected - - def build_status_object(status, message = '') - GitAccessStatus.new(status, message) - end - end -end diff --git a/lib/gitlab/git_access_status.rb b/lib/gitlab/git_access_status.rb deleted file mode 100644 index 5a806ff6e0d..00000000000 --- a/lib/gitlab/git_access_status.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Gitlab - class GitAccessStatus - attr_accessor :status, :message - alias_method :allowed?, :status - - def initialize(status, message = '') - @status = status - @message = message - end - - def to_json - { status: @status, message: @message }.to_json - end - end -end diff --git a/lib/gitlab/git_access_wiki.rb b/lib/gitlab/git_access_wiki.rb deleted file mode 100644 index 73d99b96202..00000000000 --- a/lib/gitlab/git_access_wiki.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Gitlab - class GitAccessWiki < GitAccess - def change_access_check(change) - if user.can?(:write_wiki, project) - build_status_object(true) - else - build_status_object(false, "You don't have access") - end - end - end -end diff --git a/lib/gitlab/git_logger.rb b/lib/gitlab/git_logger.rb deleted file mode 100644 index 9e02ccc0f44..00000000000 --- a/lib/gitlab/git_logger.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Gitlab - class GitLogger < Gitlab::Logger - def self.file_name_noext - 'githost' - end - - def format_message(severity, timestamp, progname, msg) - "#{timestamp.to_s(:long)} -> #{severity} -> #{msg}\n" - end - end -end diff --git a/lib/gitlab/git_ref_validator.rb b/lib/gitlab/git_ref_validator.rb deleted file mode 100644 index 39d17def930..00000000000 --- a/lib/gitlab/git_ref_validator.rb +++ /dev/null @@ -1,12 +0,0 @@ -module Gitlab - module GitRefValidator - extend self - # Validates a given name against the git reference specification - # - # Returns true for a valid reference name, false otherwise - def validate(ref_name) - Gitlab::Utils.system_silent( - %W(git check-ref-format refs/#{ref_name})) - end - end -end diff --git a/lib/gitlab/github_import/client.rb b/lib/gitlab/github_import/client.rb deleted file mode 100644 index 270cbcd9ccd..00000000000 --- a/lib/gitlab/github_import/client.rb +++ /dev/null @@ -1,53 +0,0 @@ -module Gitlab - module GithubImport - class Client - attr_reader :client, :api - - def initialize(access_token) - @client = ::OAuth2::Client.new( - config.app_id, - config.app_secret, - github_options - ) - - if access_token - ::Octokit.auto_paginate = true - @api = ::Octokit::Client.new(access_token: access_token) - end - end - - def authorize_url(redirect_uri) - client.auth_code.authorize_url({ - redirect_uri: redirect_uri, - scope: "repo, user, user:email" - }) - end - - def get_token(code) - client.auth_code.get_token(code).token - end - - def method_missing(method, *args, &block) - if api.respond_to?(method) - api.send(method, *args, &block) - else - super(method, *args, &block) - end - end - - def respond_to?(method) - api.respond_to?(method) || super - end - - private - - def config - Gitlab.config.omniauth.providers.find{|provider| provider.name == "github"} - end - - def github_options - OmniAuth::Strategies::GitHub.default_options[:client_options].symbolize_keys - end - end - end -end diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb deleted file mode 100644 index 23832b3233c..00000000000 --- a/lib/gitlab/github_import/importer.rb +++ /dev/null @@ -1,46 +0,0 @@ -module Gitlab - module GithubImport - class Importer - attr_reader :project, :client - - def initialize(project) - @project = project - @client = Client.new(project.creator.github_access_token) - @formatter = Gitlab::ImportFormatter.new - end - - def execute - #Issues && Comments - client.list_issues(project.import_source, state: :all).each do |issue| - if issue.pull_request.nil? - - body = @formatter.author_line(issue.user.login, issue.body) - - if issue.comments > 0 - body += @formatter.comments_header - - client.issue_comments(project.import_source, issue.number).each do |c| - body += @formatter.comment(c.user.login, c.created_at, c.body) - end - end - - project.issues.create!( - description: body, - title: issue.title, - state: issue.state == 'closed' ? 'closed' : 'opened', - author_id: gl_user_id(project, issue.user.id) - ) - end - end - end - - private - - def gl_user_id(project, github_id) - user = User.joins(:identities). - find_by("identities.extern_uid = ? AND identities.provider = 'github'", github_id.to_s) - (user && user.id) || project.creator_id - end - end - end -end diff --git a/lib/gitlab/github_import/project_creator.rb b/lib/gitlab/github_import/project_creator.rb deleted file mode 100644 index 2723eec933e..00000000000 --- a/lib/gitlab/github_import/project_creator.rb +++ /dev/null @@ -1,26 +0,0 @@ -module Gitlab - module GithubImport - class ProjectCreator - attr_reader :repo, :namespace, :current_user - - def initialize(repo, namespace, current_user) - @repo = repo - @namespace = namespace - @current_user = current_user - end - - def execute - ::Projects::CreateService.new(current_user, - name: repo.name, - path: repo.name, - description: repo.description, - namespace_id: namespace.id, - visibility_level: repo.private ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::VisibilityLevel::PUBLIC, - import_type: "github", - import_source: repo.full_name, - import_url: repo.clone_url.sub("https://", "https://#{current_user.github_access_token}@") - ).execute - end - end - end -end diff --git a/lib/gitlab/gitlab_import/client.rb b/lib/gitlab/gitlab_import/client.rb deleted file mode 100644 index 9c00896c913..00000000000 --- a/lib/gitlab/gitlab_import/client.rb +++ /dev/null @@ -1,82 +0,0 @@ -module Gitlab - module GitlabImport - class Client - attr_reader :client, :api - - PER_PAGE = 100 - - def initialize(access_token) - @client = ::OAuth2::Client.new( - config.app_id, - config.app_secret, - gitlab_options - ) - - if access_token - @api = OAuth2::AccessToken.from_hash(@client, access_token: access_token) - end - end - - def authorize_url(redirect_uri) - client.auth_code.authorize_url({ - redirect_uri: redirect_uri, - scope: "api" - }) - end - - def get_token(code, redirect_uri) - client.auth_code.get_token(code, redirect_uri: redirect_uri).token - end - - def user - api.get("/api/v3/user").parsed - end - - def issues(project_identifier) - lazy_page_iterator(PER_PAGE) do |page| - api.get("/api/v3/projects/#{project_identifier}/issues?per_page=#{PER_PAGE}&page=#{page}").parsed - end - end - - def issue_comments(project_identifier, issue_id) - lazy_page_iterator(PER_PAGE) do |page| - api.get("/api/v3/projects/#{project_identifier}/issues/#{issue_id}/notes?per_page=#{PER_PAGE}&page=#{page}").parsed - end - end - - def project(id) - api.get("/api/v3/projects/#{id}").parsed - end - - def projects - lazy_page_iterator(PER_PAGE) do |page| - api.get("/api/v3/projects?per_page=#{PER_PAGE}&page=#{page}").parsed - end - end - - private - - def lazy_page_iterator(per_page) - Enumerator.new do |y| - page = 1 - loop do - items = yield(page) - items.each do |item| - y << item - end - break if items.empty? || items.size < per_page - page += 1 - end - end - end - - def config - Gitlab.config.omniauth.providers.find{|provider| provider.name == "gitlab"} - end - - def gitlab_options - OmniAuth::Strategies::GitLab.default_options[:client_options].symbolize_keys - end - end - end -end diff --git a/lib/gitlab/gitlab_import/importer.rb b/lib/gitlab/gitlab_import/importer.rb deleted file mode 100644 index c5304a0699b..00000000000 --- a/lib/gitlab/gitlab_import/importer.rb +++ /dev/null @@ -1,50 +0,0 @@ -module Gitlab - module GitlabImport - class Importer - attr_reader :project, :client - - def initialize(project) - @project = project - @client = Client.new(project.creator.gitlab_access_token) - @formatter = Gitlab::ImportFormatter.new - end - - def execute - project_identifier = URI.encode(project.import_source, '/') - - #Issues && Comments - issues = client.issues(project_identifier) - - issues.each do |issue| - body = @formatter.author_line(issue["author"]["name"], issue["description"]) - - comments = client.issue_comments(project_identifier, issue["id"]) - - if comments.any? - body += @formatter.comments_header - end - - comments.each do |comment| - body += @formatter.comment(comment["author"]["name"], comment["created_at"], comment["body"]) - end - - project.issues.create!( - description: body, - title: issue["title"], - state: issue["state"], - author_id: gl_user_id(project, issue["author"]["id"]) - ) - end - - true - end - - private - - def gl_user_id(project, gitlab_id) - user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'gitlab'", gitlab_id.to_s) - (user && user.id) || project.creator_id - end - end - end -end diff --git a/lib/gitlab/gitlab_import/project_creator.rb b/lib/gitlab/gitlab_import/project_creator.rb deleted file mode 100644 index f0d7141bf56..00000000000 --- a/lib/gitlab/gitlab_import/project_creator.rb +++ /dev/null @@ -1,26 +0,0 @@ -module Gitlab - module GitlabImport - class ProjectCreator - attr_reader :repo, :namespace, :current_user - - def initialize(repo, namespace, current_user) - @repo = repo - @namespace = namespace - @current_user = current_user - end - - def execute - ::Projects::CreateService.new(current_user, - name: repo["name"], - path: repo["path"], - description: repo["description"], - namespace_id: namespace.id, - visibility_level: repo["visibility_level"], - import_type: "gitlab", - import_source: repo["path_with_namespace"], - import_url: repo["http_url_to_repo"].sub("://", "://oauth2:#{current_user.gitlab_access_token}@") - ).execute - end - end - end -end diff --git a/lib/gitlab/gitorious_import/client.rb b/lib/gitlab/gitorious_import/client.rb deleted file mode 100644 index 8cdc3d4afae..00000000000 --- a/lib/gitlab/gitorious_import/client.rb +++ /dev/null @@ -1,31 +0,0 @@ -module Gitlab - module GitoriousImport - GITORIOUS_HOST = "https://gitorious.org" - - class Client - attr_reader :repo_list - - def initialize(repo_list) - @repo_list = repo_list - end - - def authorize_url(redirect_uri) - "#{GITORIOUS_HOST}/gitlab-import?callback_url=#{redirect_uri}" - end - - def repos - @repos ||= repo_names.map { |full_name| Repository.new(full_name) } - end - - def repo(id) - repos.find { |repo| repo.id == id } - end - - private - - def repo_names - repo_list.to_s.split(',').map(&:strip).reject(&:blank?) - end - end - end -end diff --git a/lib/gitlab/gitorious_import/project_creator.rb b/lib/gitlab/gitorious_import/project_creator.rb deleted file mode 100644 index cc9a91c91f4..00000000000 --- a/lib/gitlab/gitorious_import/project_creator.rb +++ /dev/null @@ -1,26 +0,0 @@ -module Gitlab - module GitoriousImport - class ProjectCreator - attr_reader :repo, :namespace, :current_user - - def initialize(repo, namespace, current_user) - @repo = repo - @namespace = namespace - @current_user = current_user - end - - def execute - ::Projects::CreateService.new(current_user, - name: repo.name, - path: repo.path, - description: repo.description, - namespace_id: namespace.id, - visibility_level: Gitlab::VisibilityLevel::PUBLIC, - import_type: "gitorious", - import_source: repo.full_name, - import_url: repo.import_url - ).execute - end - end - end -end diff --git a/lib/gitlab/gitorious_import/repository.rb b/lib/gitlab/gitorious_import/repository.rb deleted file mode 100644 index f702797dc6e..00000000000 --- a/lib/gitlab/gitorious_import/repository.rb +++ /dev/null @@ -1,37 +0,0 @@ -module Gitlab - module GitoriousImport - GITORIOUS_HOST = "https://gitorious.org" - - Repository = Struct.new(:full_name) do - def id - Digest::SHA1.hexdigest(full_name) - end - - def namespace - segments.first - end - - def path - segments.last - end - - def name - path.titleize - end - - def description - "" - end - - def import_url - "#{GITORIOUS_HOST}/#{full_name}.git" - end - - private - - def segments - full_name.split('/') - end - end - end -end diff --git a/lib/gitlab/google_code_import/client.rb b/lib/gitlab/google_code_import/client.rb deleted file mode 100644 index 02f31e45f88..00000000000 --- a/lib/gitlab/google_code_import/client.rb +++ /dev/null @@ -1,48 +0,0 @@ -module Gitlab - module GoogleCodeImport - class Client - attr_reader :raw_data - - def self.mask_email(author) - parts = author.split("@", 2) - parts[0] = "#{parts[0][0...-3]}..." - parts.join("@") - end - - def initialize(raw_data) - @raw_data = raw_data - end - - def valid? - raw_data.is_a?(Hash) && raw_data["kind"] == "projecthosting#user" && raw_data.has_key?("projects") - end - - def repos - @repos ||= raw_data["projects"].map { |raw_repo| GoogleCodeImport::Repository.new(raw_repo) }.select(&:git?) - end - - def repo(id) - repos.find { |repo| repo.id == id } - end - - def user_map - user_map = Hash.new { |hash, user| hash[user] = self.class.mask_email(user) } - - repos.each do |repo| - next unless repo.valid? && repo.issues - - repo.issues.each do |raw_issue| - # Touching is enough to add the entry and masked email. - user_map[raw_issue["author"]["name"]] - - raw_issue["comments"]["items"].each do |raw_comment| - user_map[raw_comment["author"]["name"]] - end - end - end - - Hash[user_map.sort] - end - end - end -end diff --git a/lib/gitlab/google_code_import/repository.rb b/lib/gitlab/google_code_import/repository.rb deleted file mode 100644 index ad33fc2cad2..00000000000 --- a/lib/gitlab/google_code_import/repository.rb +++ /dev/null @@ -1,43 +0,0 @@ -module Gitlab - module GoogleCodeImport - class Repository - attr_accessor :raw_data - - def initialize(raw_data) - @raw_data = raw_data - end - - def valid? - raw_data.is_a?(Hash) && raw_data["kind"] == "projecthosting#project" - end - - def id - raw_data["externalId"] - end - - def name - raw_data["name"] - end - - def summary - raw_data["summary"] - end - - def description - raw_data["description"] - end - - def git? - raw_data["versionControlSystem"] == "git" - end - - def import_url - raw_data["repositoryUrls"].first - end - - def issues - raw_data["issues"] && raw_data["issues"]["items"] - end - end - end -end diff --git a/lib/gitlab/graphs/commits.rb b/lib/gitlab/graphs/commits.rb deleted file mode 100644 index 2122339d2db..00000000000 --- a/lib/gitlab/graphs/commits.rb +++ /dev/null @@ -1,49 +0,0 @@ -module Gitlab - module Graphs - class Commits - attr_reader :commits, :start_date, :end_date, :duration, - :commits_per_week_days, :commits_per_time, :commits_per_month - - def initialize(commits) - @commits = commits - @start_date = commits.last.committed_date.to_date - @end_date = commits.first.committed_date.to_date - @duration = (@end_date - @start_date).to_i - - collect_data - end - - def authors - @authors ||= @commits.map(&:author_email).uniq.size - end - - def commit_per_day - @commit_per_day ||= (@commits.size.to_f / @duration).round(1) - end - - def collect_data - @commits_per_week_days = {} - Date::DAYNAMES.each { |day| @commits_per_week_days[day] = 0 } - - @commits_per_time = {} - (0..23).to_a.each { |hour| @commits_per_time[hour] = 0 } - - @commits_per_month = {} - (1..31).to_a.each { |day| @commits_per_month[day] = 0 } - - @commits.each do |commit| - hour = commit.committed_date.strftime('%k').to_i - day_of_month = commit.committed_date.strftime('%e').to_i - weekday = commit.committed_date.strftime('%A') - - @commits_per_week_days[weekday] ||= 0 - @commits_per_week_days[weekday] += 1 - @commits_per_time[hour] ||= 0 - @commits_per_time[hour] += 1 - @commits_per_month[day_of_month] ||= 0 - @commits_per_month[day_of_month] += 1 - end - end - end - end -end diff --git a/lib/gitlab/identifier.rb b/lib/gitlab/identifier.rb deleted file mode 100644 index 6e4de197eeb..00000000000 --- a/lib/gitlab/identifier.rb +++ /dev/null @@ -1,23 +0,0 @@ -# Detect user based on identifier like -# key-13 or user-36 or last commit -module Gitlab - module Identifier - def identify(identifier, project, newrev) - if identifier.blank? - # Local push from gitlab - email = project.repository.commit(newrev).author_email rescue nil - User.find_by(email: email) if email - - elsif identifier =~ /\Auser-\d+\Z/ - # git push over http - user_id = identifier.gsub("user-", "") - User.find_by(id: user_id) - - elsif identifier =~ /\Akey-\d+\Z/ - # git push over ssh - key_id = identifier.gsub("key-", "") - Key.find_by(id: key_id).try(:user) - end - end - end -end diff --git a/lib/gitlab/import_formatter.rb b/lib/gitlab/import_formatter.rb deleted file mode 100644 index 72e041a90b1..00000000000 --- a/lib/gitlab/import_formatter.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Gitlab - class ImportFormatter - def comment(author, date, body) - "\n\n*By #{author} on #{date}*\n\n#{body}" - end - - def comments_header - "\n\n\n**Imported comments:**\n" - end - - def author_line(author, body) - "*Created by: #{author}*\n\n#{body}" - end - end -end diff --git a/lib/gitlab/inline_diff.rb b/lib/gitlab/inline_diff.rb deleted file mode 100644 index 3517ecdf5cf..00000000000 --- a/lib/gitlab/inline_diff.rb +++ /dev/null @@ -1,78 +0,0 @@ -module Gitlab - class InlineDiff - class << self - - START = "#!idiff-start!#" - FINISH = "#!idiff-finish!#" - - def processing(diff_arr) - indexes = _indexes_of_changed_lines diff_arr - - indexes.each do |index| - first_line = diff_arr[index+1] - second_line = diff_arr[index+2] - max_length = [first_line.size, second_line.size].max - - # Skip inline diff if empty line was replaced with content - next if first_line == "-\n" - - first_the_same_symbols = 0 - (0..max_length + 1).each do |i| - first_the_same_symbols = i - 1 - if first_line[i] != second_line[i] && i > 0 - break - end - end - - first_token = first_line[0..first_the_same_symbols][1..-1] - start = first_token + START - - if first_token.empty? - # In case if we remove string of spaces in commit - diff_arr[index+1].sub!("-", "-" => "-#{START}") - diff_arr[index+2].sub!("+", "+" => "+#{START}") - else - diff_arr[index+1].sub!(first_token, first_token => start) - diff_arr[index+2].sub!(first_token, first_token => start) - end - - last_the_same_symbols = 0 - (1..max_length + 1).each do |i| - last_the_same_symbols = -i - shortest_line = second_line.size > first_line.size ? first_line : second_line - if ( first_line[-i] != second_line[-i] ) || "#{first_token}#{START}".size == shortest_line[1..-i].size - break - end - end - last_the_same_symbols += 1 - last_token = first_line[last_the_same_symbols..-1] - diff_arr[index+1].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token) - diff_arr[index+2].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token) - end - diff_arr - end - - def _indexes_of_changed_lines(diff_arr) - chain_of_first_symbols = "" - diff_arr.each_with_index do |line, i| - chain_of_first_symbols += line[0] - end - chain_of_first_symbols.gsub!(/[^\-\+]/, "#") - - offset = 0 - indexes = [] - while index = chain_of_first_symbols.index("#-+#", offset) - indexes << index - offset = index + 1 - end - indexes - end - - def replace_markers(line) - line.gsub!(START, "") - line.gsub!(FINISH, "") - line - end - end - end -end diff --git a/lib/gitlab/issues_labels.rb b/lib/gitlab/issues_labels.rb deleted file mode 100644 index 1bec6088292..00000000000 --- a/lib/gitlab/issues_labels.rb +++ /dev/null @@ -1,27 +0,0 @@ -module Gitlab - class IssuesLabels - class << self - def generate(project) - red = '#d9534f' - yellow = '#f0ad4e' - blue = '#428bca' - green = '#5cb85c' - - labels = [ - { title: "bug", color: red }, - { title: "critical", color: red }, - { title: "confirmed", color: red }, - { title: "documentation", color: yellow }, - { title: "support", color: yellow }, - { title: "discussion", color: blue }, - { title: "suggestion", color: blue }, - { title: "enhancement", color: green } - ] - - labels.each do |label| - project.labels.create(label) - end - end - end - end -end diff --git a/lib/gitlab/key_fingerprint.rb b/lib/gitlab/key_fingerprint.rb deleted file mode 100644 index baf52ff750d..00000000000 --- a/lib/gitlab/key_fingerprint.rb +++ /dev/null @@ -1,55 +0,0 @@ -module Gitlab - class KeyFingerprint - include Gitlab::Popen - - attr_accessor :key - - def initialize(key) - @key = key - end - - def fingerprint - cmd_status = 0 - cmd_output = '' - - Tempfile.open('gitlab_key_file') do |file| - file.puts key - file.rewind - - cmd = [] - cmd.push *%W(ssh-keygen) - cmd.push *%W(-E md5) if explicit_fingerprint_algorithm? - cmd.push *%W(-lf #{file.path}) - - cmd_output, cmd_status = popen(cmd, '/tmp') - end - - return nil unless cmd_status.zero? - - # 16 hex bytes separated by ':', optionally starting with "MD5:" - fingerprint_matches = cmd_output.match(/(MD5:)?(?(\h{2}:){15}\h{2})/) - return nil unless fingerprint_matches - - fingerprint_matches[:fingerprint] - end - - private - - def explicit_fingerprint_algorithm? - # OpenSSH 6.8 introduces a new default output format for fingerprints. - # Check the version and decide which command to use. - - version_output, version_status = popen(%W(ssh -V)) - return false unless version_status.zero? - - version_matches = version_output.match(/OpenSSH_(?\d+)\.(?\d+)/) - return false unless version_matches - - version_info = Gitlab::VersionInfo.new(version_matches[:major].to_i, version_matches[:minor].to_i) - - required_version_info = Gitlab::VersionInfo.new(6, 8) - - version_info >= required_version_info - end - end -end diff --git a/lib/gitlab/ldap/access.rb b/lib/gitlab/ldap/access.rb deleted file mode 100644 index 960fb3849b4..00000000000 --- a/lib/gitlab/ldap/access.rb +++ /dev/null @@ -1,62 +0,0 @@ -# LDAP authorization model -# -# * Check if we are allowed access (not blocked) -# -module Gitlab - module LDAP - class Access - attr_reader :adapter, :provider, :user - - def self.open(user, &block) - Gitlab::LDAP::Adapter.open(user.ldap_identity.provider) do |adapter| - block.call(self.new(user, adapter)) - end - end - - def self.allowed?(user) - self.open(user) do |access| - if access.allowed? - user.last_credential_check_at = Time.now - user.save - true - else - false - end - end - end - - def initialize(user, adapter=nil) - @adapter = adapter - @user = user - @provider = user.ldap_identity.provider - end - - def allowed? - if Gitlab::LDAP::Person.find_by_dn(user.ldap_identity.extern_uid, adapter) - return true unless ldap_config.active_directory - - # Block user in GitLab if he/she was blocked in AD - if Gitlab::LDAP::Person.disabled_via_active_directory?(user.ldap_identity.extern_uid, adapter) - user.block unless user.blocked? - false - else - user.activate if user.blocked? - true - end - else - false - end - rescue - false - end - - def adapter - @adapter ||= Gitlab::LDAP::Adapter.new(provider) - end - - def ldap_config - Gitlab::LDAP::Config.new(provider) - end - end - end -end diff --git a/lib/gitlab/ldap/adapter.rb b/lib/gitlab/ldap/adapter.rb deleted file mode 100644 index 577a890a7d9..00000000000 --- a/lib/gitlab/ldap/adapter.rb +++ /dev/null @@ -1,89 +0,0 @@ -module Gitlab - module LDAP - class Adapter - attr_reader :provider, :ldap - - def self.open(provider, &block) - Net::LDAP.open(config(provider).adapter_options) do |ldap| - block.call(self.new(provider, ldap)) - end - end - - def self.config(provider) - Gitlab::LDAP::Config.new(provider) - end - - def initialize(provider, ldap=nil) - @provider = provider - @ldap = ldap || Net::LDAP.new(config.adapter_options) - end - - def config - Gitlab::LDAP::Config.new(provider) - end - - def users(field, value, limit = nil) - if field.to_sym == :dn - options = { - base: value, - scope: Net::LDAP::SearchScope_BaseObject - } - else - options = { - base: config.base, - filter: Net::LDAP::Filter.eq(field, value) - } - end - - if config.user_filter.present? - user_filter = Net::LDAP::Filter.construct(config.user_filter) - - options[:filter] = if options[:filter] - Net::LDAP::Filter.join(options[:filter], user_filter) - else - user_filter - end - end - - if limit.present? - options.merge!(size: limit) - end - - entries = ldap_search(options).select do |entry| - entry.respond_to? config.uid - end - - entries.map do |entry| - Gitlab::LDAP::Person.new(entry, provider) - end - end - - def user(*args) - users(*args).first - end - - def dn_matches_filter?(dn, filter) - ldap_search(base: dn, - filter: filter, - scope: Net::LDAP::SearchScope_BaseObject, - attributes: %w{dn}).any? - end - - def ldap_search(*args) - results = ldap.search(*args) - - if results.nil? - response = ldap.get_operation_result - - unless response.code.zero? - Rails.logger.warn("LDAP search error: #{response.message}") - end - - [] - else - results - end - end - end - end -end diff --git a/lib/gitlab/ldap/authentication.rb b/lib/gitlab/ldap/authentication.rb deleted file mode 100644 index 649cf3194b8..00000000000 --- a/lib/gitlab/ldap/authentication.rb +++ /dev/null @@ -1,71 +0,0 @@ -# This calls helps to authenticate to LDAP by providing username and password -# -# Since multiple LDAP servers are supported, it will loop through all of them -# until a valid bind is found -# - -module Gitlab - module LDAP - class Authentication - def self.login(login, password) - return unless Gitlab::LDAP::Config.enabled? - return unless login.present? && password.present? - - auth = nil - # loop through providers until valid bind - providers.find do |provider| - auth = new(provider) - auth.login(login, password) # true will exit the loop - end - - # If (login, password) was invalid for all providers, the value of auth is now the last - # Gitlab::LDAP::Authentication instance we tried. - auth.user - end - - def self.providers - Gitlab::LDAP::Config.providers - end - - attr_accessor :provider, :ldap_user - - def initialize(provider) - @provider = provider - end - - def login(login, password) - @ldap_user = adapter.bind_as( - filter: user_filter(login), - size: 1, - password: password - ) - end - - def adapter - OmniAuth::LDAP::Adaptor.new(config.options.symbolize_keys) - end - - def config - Gitlab::LDAP::Config.new(provider) - end - - def user_filter(login) - filter = Net::LDAP::Filter.equals(config.uid, login) - - # Apply LDAP user filter if present - if config.user_filter.present? - filter = Net::LDAP::Filter.join( - filter, - Net::LDAP::Filter.construct(config.user_filter) - ) - end - filter - end - - def user - return nil unless ldap_user - Gitlab::LDAP::User.find_by_uid_and_provider(ldap_user.dn, provider) - end - end - end -end diff --git a/lib/gitlab/ldap/config.rb b/lib/gitlab/ldap/config.rb deleted file mode 100644 index d2ffa2e1fe8..00000000000 --- a/lib/gitlab/ldap/config.rb +++ /dev/null @@ -1,122 +0,0 @@ -# Load a specific server configuration -module Gitlab - module LDAP - class Config - attr_accessor :provider, :options - - def self.enabled? - Gitlab.config.ldap.enabled - end - - def self.servers - Gitlab.config.ldap.servers.values - end - - def self.providers - servers.map {|server| server['provider_name'] } - end - - def self.valid_provider?(provider) - providers.include?(provider) - end - - def self.invalid_provider(provider) - raise "Unknown provider (#{provider}). Available providers: #{providers}" - end - - def initialize(provider) - if self.class.valid_provider?(provider) - @provider = provider - else - self.class.invalid_provider(provider) - end - @options = config_for(@provider) # Use @provider, not provider - end - - def enabled? - base_config.enabled - end - - def adapter_options - { - host: options['host'], - port: options['port'], - encryption: encryption - }.tap do |options| - options.merge!(auth_options) if has_auth? - end - end - - def base - options['base'] - end - - def uid - options['uid'] - end - - def sync_ssh_keys? - sync_ssh_keys.present? - end - - # The LDAP attribute in which the ssh keys are stored - def sync_ssh_keys - options['sync_ssh_keys'] - end - - def user_filter - options['user_filter'] - end - - def group_base - options['group_base'] - end - - def admin_group - options['admin_group'] - end - - def active_directory - options['active_directory'] - end - - def block_auto_created_users - options['block_auto_created_users'] - end - - protected - def base_config - Gitlab.config.ldap - end - - def config_for(provider) - base_config.servers.values.find { |server| server['provider_name'] == provider } - end - - def encryption - case options['method'].to_s - when 'ssl' - :simple_tls - when 'tls' - :start_tls - else - nil - end - end - - def auth_options - { - auth: { - method: :simple, - username: options['bind_dn'], - password: options['password'] - } - } - end - - def has_auth? - options['password'] || options['bind_dn'] - end - end - end -end diff --git a/lib/gitlab/ldap/person.rb b/lib/gitlab/ldap/person.rb deleted file mode 100644 index b81f3e8e8f5..00000000000 --- a/lib/gitlab/ldap/person.rb +++ /dev/null @@ -1,61 +0,0 @@ -module Gitlab - module LDAP - class Person - # Active Directory-specific LDAP filter that checks if bit 2 of the - # userAccountControl attribute is set. - # Source: http://ctogonewild.com/2009/09/03/bitmask-searches-in-ldap/ - AD_USER_DISABLED = Net::LDAP::Filter.ex("userAccountControl:1.2.840.113556.1.4.803", "2") - - attr_accessor :entry, :provider - - def self.find_by_uid(uid, adapter) - uid = Net::LDAP::Filter.escape(uid) - adapter.user(adapter.config.uid, uid) - end - - def self.find_by_dn(dn, adapter) - adapter.user('dn', dn) - end - - def self.disabled_via_active_directory?(dn, adapter) - adapter.dn_matches_filter?(dn, AD_USER_DISABLED) - end - - def initialize(entry, provider) - Rails.logger.debug { "Instantiating #{self.class.name} with LDIF:\n#{entry.to_ldif}" } - @entry = entry - @provider = provider - end - - def name - entry.cn.first - end - - def uid - entry.send(config.uid).first - end - - def username - uid - end - - def email - entry.try(:mail) - end - - def dn - entry.dn - end - - private - - def entry - @entry - end - - def config - @config ||= Gitlab::LDAP::Config.new(provider) - end - end - end -end diff --git a/lib/gitlab/ldap/user.rb b/lib/gitlab/ldap/user.rb deleted file mode 100644 index f7f3ba9ad7d..00000000000 --- a/lib/gitlab/ldap/user.rb +++ /dev/null @@ -1,71 +0,0 @@ -require 'gitlab/o_auth/user' - -# LDAP extension for User model -# -# * Find or create user from omniauth.auth data -# * Links LDAP account with existing user -# * Auth LDAP user with login and password -# -module Gitlab - module LDAP - class User < Gitlab::OAuth::User - class << self - def find_by_uid_and_provider(uid, provider) - # LDAP distinguished name is case-insensitive - identity = ::Identity. - where(provider: provider). - where('lower(extern_uid) = ?', uid.downcase).last - identity && identity.user - end - end - - def initialize(auth_hash) - super - update_user_attributes - end - - # instance methods - def gl_user - @gl_user ||= find_by_uid_and_provider || find_by_email || build_new_user - end - - def find_by_uid_and_provider - self.class.find_by_uid_and_provider( - auth_hash.uid.downcase, auth_hash.provider) - end - - def find_by_email - ::User.find_by(email: auth_hash.email) - end - - def update_user_attributes - return unless persisted? - - gl_user.skip_reconfirmation! - gl_user.email = auth_hash.email - - # Build new identity only if we dont have have same one - gl_user.identities.find_or_initialize_by(provider: auth_hash.provider, - extern_uid: auth_hash.uid) - - gl_user - end - - def changed? - gl_user.changed? || gl_user.identities.any?(&:changed?) - end - - def block_after_signup? - ldap_config.block_auto_created_users - end - - def allowed? - Gitlab::LDAP::Access.allowed?(gl_user) - end - - def ldap_config - Gitlab::LDAP::Config.new(auth_hash.provider) - end - end - end -end diff --git a/lib/gitlab/logger.rb b/lib/gitlab/logger.rb deleted file mode 100644 index 59b21149a9a..00000000000 --- a/lib/gitlab/logger.rb +++ /dev/null @@ -1,32 +0,0 @@ -module Gitlab - class Logger < ::Logger - def self.file_name - file_name_noext + '.log' - end - - def self.error(message) - build.error(message) - end - - def self.info(message) - build.info(message) - end - - def self.read_latest - path = Rails.root.join("log", file_name) - self.build unless File.exist?(path) - tail_output, _ = Gitlab::Popen.popen(%W(tail -n 2000 #{path})) - tail_output.split("\n") - end - - def self.read_latest_for(filename) - path = Rails.root.join("log", filename) - tail_output, _ = Gitlab::Popen.popen(%W(tail -n 2000 #{path})) - tail_output.split("\n") - end - - def self.build - new(Rails.root.join("log", file_name)) - end - end -end diff --git a/lib/gitlab/markdown_helper.rb b/lib/gitlab/markdown_helper.rb deleted file mode 100644 index 5e3cfc0585b..00000000000 --- a/lib/gitlab/markdown_helper.rb +++ /dev/null @@ -1,29 +0,0 @@ -module Gitlab - module MarkdownHelper - module_function - - # Public: Determines if a given filename is compatible with GitHub::Markup. - # - # filename - Filename string to check - # - # Returns boolean - def markup?(filename) - filename.downcase.end_with?(*%w(.textile .rdoc .org .creole .wiki - .mediawiki .rst .adoc .asciidoc .asc)) - end - - # Public: Determines if a given filename is compatible with - # GitLab-flavored Markdown. - # - # filename - Filename string to check - # - # Returns boolean - def gitlab_markdown?(filename) - filename.downcase.end_with?(*%w(.mdown .md .markdown)) - end - - def previewable?(filename) - gitlab_markdown?(filename) || markup?(filename) - end - end -end diff --git a/lib/gitlab/middleware/static.rb b/lib/gitlab/middleware/static.rb deleted file mode 100644 index 85ffa8aca68..00000000000 --- a/lib/gitlab/middleware/static.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Gitlab - module Middleware - class Static < ActionDispatch::Static - UPLOADS_REGEX = /\A\/uploads(\/|\z)/.freeze - - def call(env) - return @app.call(env) if env['PATH_INFO'] =~ UPLOADS_REGEX - - super - end - end - end -end diff --git a/lib/gitlab/note_data_builder.rb b/lib/gitlab/note_data_builder.rb deleted file mode 100644 index 644dec45dca..00000000000 --- a/lib/gitlab/note_data_builder.rb +++ /dev/null @@ -1,77 +0,0 @@ -module Gitlab - class NoteDataBuilder - class << self - # Produce a hash of post-receive data - # - # For all notes: - # - # data = { - # object_kind: "note", - # user: { - # name: String, - # username: String, - # avatar_url: String - # } - # project_id: Integer, - # repository: { - # name: String, - # url: String, - # description: String, - # homepage: String, - # } - # object_attributes: { - # - # } - # : { - # } - # note-specific data is a hash with one of the following keys and contains - # the hook data for that type. - # - commit - # - issue - # - merge_request - # - snippet - # - def build(note, user) - project = note.project - data = build_base_data(project, user, note) - - if note.for_commit? - data[:commit] = build_data_for_commit(project, user, note) - elsif note.for_issue? - data[:issue] = note.noteable.hook_attrs - elsif note.for_merge_request? - data[:merge_request] = note.noteable.hook_attrs - elsif note.for_project_snippet? - data[:snippet] = note.noteable.hook_attrs - end - - data - end - - def build_base_data(project, user, note) - base_data = { - object_kind: "note", - user: user.hook_attrs, - project_id: project.id, - repository: { - name: project.name, - url: project.url_to_repo, - description: project.description, - homepage: project.web_url, - }, - object_attributes: note.hook_attrs - } - - base_data[:object_attributes][:url] = - Gitlab::UrlBuilder.new(:note).build(note.id) - base_data - end - - def build_data_for_commit(project, user, note) - # commit_id is the SHA hash - commit = project.repository.commit(note.commit_id) - commit.hook_attrs(project) - end - end - end -end diff --git a/lib/gitlab/o_auth/auth_hash.rb b/lib/gitlab/o_auth/auth_hash.rb deleted file mode 100644 index ce52beec78e..00000000000 --- a/lib/gitlab/o_auth/auth_hash.rb +++ /dev/null @@ -1,54 +0,0 @@ -# Class to parse and transform the info provided by omniauth -# -module Gitlab - module OAuth - class AuthHash - attr_reader :auth_hash - def initialize(auth_hash) - @auth_hash = auth_hash - end - - def uid - auth_hash.uid.to_s - end - - def provider - auth_hash.provider - end - - def info - auth_hash.info - end - - def name - (info.try(:name) || full_name).to_s.force_encoding('utf-8') - end - - def full_name - "#{info.first_name} #{info.last_name}" - end - - def username - (info.try(:nickname) || generate_username).to_s.force_encoding('utf-8') - end - - def email - (info.try(:email) || generate_temporarily_email).downcase - end - - def password - @password ||= Devise.friendly_token[0, 8].downcase - end - - # Get the first part of the email address (before @) - # In addtion in removes illegal characters - def generate_username - email.match(/^[^@]*/)[0].parameterize - end - - def generate_temporarily_email - "temp-email-for-oauth-#{username}@gitlab.localhost" - end - end - end -end diff --git a/lib/gitlab/o_auth/user.rb b/lib/gitlab/o_auth/user.rb deleted file mode 100644 index 2f5c217d764..00000000000 --- a/lib/gitlab/o_auth/user.rb +++ /dev/null @@ -1,106 +0,0 @@ -# OAuth extension for User model -# -# * Find GitLab user based on omniauth uid and provider -# * Create new user from omniauth data -# -module Gitlab - module OAuth - class ForbiddenAction < StandardError; end - - class User - attr_accessor :auth_hash, :gl_user - - def initialize(auth_hash) - self.auth_hash = auth_hash - end - - def persisted? - gl_user.try(:persisted?) - end - - def new? - !persisted? - end - - def valid? - gl_user.try(:valid?) - end - - def save - unauthorized_to_create unless gl_user - - if needs_blocking? - gl_user.save! - gl_user.block - else - gl_user.save! - end - - log.info "(OAuth) saving user #{auth_hash.email} from login with extern_uid => #{auth_hash.uid}" - gl_user - rescue ActiveRecord::RecordInvalid => e - log.info "(OAuth) Error saving user: #{gl_user.errors.full_messages}" - return self, e.record.errors - end - - def gl_user - @user ||= find_by_uid_and_provider - - if signup_enabled? - @user ||= build_new_user - end - - @user - end - - protected - - def needs_blocking? - new? && block_after_signup? - end - - def signup_enabled? - Gitlab.config.omniauth.allow_single_sign_on - end - - def block_after_signup? - Gitlab.config.omniauth.block_auto_created_users - end - - def auth_hash=(auth_hash) - @auth_hash = AuthHash.new(auth_hash) - end - - def find_by_uid_and_provider - identity = Identity.find_by(provider: auth_hash.provider, extern_uid: auth_hash.uid) - identity && identity.user - end - - def build_new_user - user = ::User.new(user_attributes) - user.skip_confirmation! - user.identities.new(extern_uid: auth_hash.uid, provider: auth_hash.provider) - user - end - - def user_attributes - { - name: auth_hash.name, - username: ::Namespace.clean_path(auth_hash.username), - email: auth_hash.email, - password: auth_hash.password, - password_confirmation: auth_hash.password, - password_automatically_set: true - } - end - - def log - Gitlab::AppLogger - end - - def unauthorized_to_create - raise ForbiddenAction.new("Unauthorized to create user, signup disabled for #{auth_hash.provider}") - end - end - end -end diff --git a/lib/gitlab/popen.rb b/lib/gitlab/popen.rb deleted file mode 100644 index 43e07e09160..00000000000 --- a/lib/gitlab/popen.rb +++ /dev/null @@ -1,35 +0,0 @@ -require 'fileutils' -require 'open3' - -module Gitlab - module Popen - extend self - - def popen(cmd, path=nil) - unless cmd.is_a?(Array) - raise "System commands must be given as an array of strings" - end - - path ||= Dir.pwd - vars = { "PWD" => path } - options = { chdir: path } - - unless File.directory?(path) - FileUtils.mkdir_p(path) - end - - @cmd_output = "" - @cmd_status = 0 - Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr| - # We are not using stdin so we should close it, in case the command we - # are running waits for input. - stdin.close - @cmd_output << stdout.read - @cmd_output << stderr.read - @cmd_status = wait_thr.value.exitstatus - end - - [@cmd_output, @cmd_status] - end - end -end diff --git a/lib/gitlab/production_logger.rb b/lib/gitlab/production_logger.rb deleted file mode 100644 index 89ce7144b1b..00000000000 --- a/lib/gitlab/production_logger.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Gitlab - class ProductionLogger < Gitlab::Logger - def self.file_name_noext - 'production' - end - end -end diff --git a/lib/gitlab/project_search_results.rb b/lib/gitlab/project_search_results.rb deleted file mode 100644 index 0dab7bcfa4d..00000000000 --- a/lib/gitlab/project_search_results.rb +++ /dev/null @@ -1,77 +0,0 @@ -module Gitlab - class ProjectSearchResults < SearchResults - attr_reader :project, :repository_ref - - def initialize(project_id, query, repository_ref = nil) - @project = Project.find(project_id) - @repository_ref = if repository_ref.present? - repository_ref - else - nil - end - @query = Shellwords.shellescape(query) if query.present? - end - - def objects(scope, page = nil) - case scope - when 'notes' - notes.page(page).per(per_page) - when 'blobs' - Kaminari.paginate_array(blobs).page(page).per(per_page) - when 'wiki_blobs' - Kaminari.paginate_array(wiki_blobs).page(page).per(per_page) - else - super - end - end - - def total_count - @total_count ||= issues_count + merge_requests_count + blobs_count + - notes_count + wiki_blobs_count - end - - def blobs_count - @blobs_count ||= blobs.count - end - - def notes_count - @notes_count ||= notes.count - end - - def wiki_blobs_count - @wiki_blobs_count ||= wiki_blobs.count - end - - private - - def blobs - if project.empty_repo? || query.blank? - [] - else - project.repository.search_files(query, repository_ref) - end - end - - def wiki_blobs - if project.wiki_enabled? && query.present? - project_wiki = ProjectWiki.new(project) - - unless project_wiki.empty? - project_wiki.search_files(query) - else - [] - end - else - [] - end - end - - def notes - Note.where(project_id: limit_project_ids).user.search(query).order('updated_at DESC') - end - - def limit_project_ids - [project.id] - end - end -end diff --git a/lib/gitlab/push_data_builder.rb b/lib/gitlab/push_data_builder.rb deleted file mode 100644 index f8da452e4c0..00000000000 --- a/lib/gitlab/push_data_builder.rb +++ /dev/null @@ -1,90 +0,0 @@ -module Gitlab - class PushDataBuilder - class << self - # Produce a hash of post-receive data - # - # data = { - # before: String, - # after: String, - # ref: String, - # user_id: String, - # user_name: String, - # user_email: String - # project_id: String, - # repository: { - # name: String, - # url: String, - # description: String, - # homepage: String, - # }, - # commits: Array, - # total_commits_count: Fixnum - # } - # - def build(project, user, oldrev, newrev, ref, commits = [], message = nil) - # Total commits count - commits_count = commits.size - - # Get latest 20 commits ASC - commits_limited = commits.last(20) - - # For performance purposes maximum 20 latest commits - # will be passed as post receive hook data. - commit_attrs = commits_limited.map do |commit| - commit.hook_attrs(project) - end - - type = Gitlab::Git.tag_ref?(ref) ? "tag_push" : "push" - # Hash to be passed as post_receive_data - data = { - object_kind: type, - before: oldrev, - after: newrev, - ref: ref, - checkout_sha: checkout_sha(project.repository, newrev, ref), - message: message, - user_id: user.id, - user_name: user.name, - user_email: user.email, - project_id: project.id, - repository: { - name: project.name, - url: project.url_to_repo, - description: project.description, - homepage: project.web_url, - git_http_url: project.http_url_to_repo, - git_ssh_url: project.ssh_url_to_repo, - visibility_level: project.visibility_level - }, - commits: commit_attrs, - total_commits_count: commits_count - } - - data - end - - # This method provide a sample data generated with - # existing project and commits to test web hooks - def build_sample(project, user) - commits = project.repository.commits(project.default_branch, nil, 3) - ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}" - build(project, user, commits.last.id, commits.first.id, ref, commits) - end - - def checkout_sha(repository, newrev, ref) - # Find sha for tag, except when it was deleted. - if Gitlab::Git.tag_ref?(ref) && !Gitlab::Git.blank_ref?(newrev) - tag_name = Gitlab::Git.ref_name(ref) - tag = repository.find_tag(tag_name) - - if tag - commit = repository.commit(tag.target) - commit.try(:sha) - end - else - newrev - end - end - end - end -end diff --git a/lib/gitlab/reference_extractor.rb b/lib/gitlab/reference_extractor.rb deleted file mode 100644 index a502a8fe9cd..00000000000 --- a/lib/gitlab/reference_extractor.rb +++ /dev/null @@ -1,94 +0,0 @@ -module Gitlab - # Extract possible GFM references from an arbitrary String for further processing. - class ReferenceExtractor - attr_accessor :project, :current_user, :references - - include ::Gitlab::Markdown - - def initialize(project, current_user = nil) - @project = project - @current_user = current_user - end - - def can?(user, action, subject) - Ability.abilities.allowed?(user, action, subject) - end - - def analyze(text) - text = text.dup - - # Remove preformatted/code blocks so that references are not included - text.gsub!(%r{
.*?
|.*?}m) { |match| '' } - text.gsub!(%r{^```.*?^```}m) { |match| '' } - - @references = Hash.new { |hash, type| hash[type] = [] } - parse_references(text) - end - - # Given a valid project, resolve the extracted identifiers of the requested type to - # model objects. - - def users - references[:user].uniq.map do |project, identifier| - if identifier == "all" - project.team.members.flatten - elsif namespace = Namespace.find_by(path: identifier) - if namespace.is_a?(Group) - namespace.users - else - namespace.owner - end - end - end.flatten.compact.uniq - end - - def labels - references[:label].uniq.map do |project, identifier| - project.labels.where(id: identifier).first - end.compact.uniq - end - - def issues - references[:issue].uniq.map do |project, identifier| - if project.default_issues_tracker? - project.issues.where(iid: identifier).first - end - end.compact.uniq - end - - def merge_requests - references[:merge_request].uniq.map do |project, identifier| - project.merge_requests.where(iid: identifier).first - end.compact.uniq - end - - def snippets - references[:snippet].uniq.map do |project, identifier| - project.snippets.where(id: identifier).first - end.compact.uniq - end - - def commits - references[:commit].uniq.map do |project, identifier| - repo = project.repository - repo.commit(identifier) if repo - end.compact.uniq - end - - def commit_ranges - references[:commit_range].uniq.map do |project, identifier| - repo = project.repository - if repo - from_id, to_id = identifier.split(/\.{2,3}/, 2) - [repo.commit(from_id), repo.commit(to_id)] - end - end.compact.uniq - end - - private - - def reference_link(type, identifier, project, _) - references[type] << [project, identifier] - end - end -end diff --git a/lib/gitlab/satellite/action.rb b/lib/gitlab/satellite/action.rb deleted file mode 100644 index 4890ccf21e6..00000000000 --- a/lib/gitlab/satellite/action.rb +++ /dev/null @@ -1,56 +0,0 @@ -module Gitlab - module Satellite - class Action - DEFAULT_OPTIONS = { git_timeout: Gitlab.config.satellites.timeout.seconds } - - attr_accessor :options, :project, :user - - def initialize(user, project, options = {}) - @options = DEFAULT_OPTIONS.merge(options) - @project = project - @user = user - end - - protected - - # * Sets a 30s timeout for Git - # * Locks the satellite repo - # * Yields the prepared satellite repo - def in_locked_and_timed_satellite - Gitlab::ShellEnv.set_env(user) - - Grit::Git.with_timeout(options[:git_timeout]) do - project.satellite.lock do - return yield project.satellite.repo - end - end - rescue Errno::ENOMEM => ex - return handle_exception(ex) - rescue Grit::Git::GitTimeout => ex - return handle_exception(ex) - ensure - Gitlab::ShellEnv.reset_env - end - - # * Recreates the satellite - # * Sets up Git variables for the user - # - # Note: use this within #in_locked_and_timed_satellite - def prepare_satellite!(repo) - project.satellite.clear_and_update! - - repo.config['user.name'] = user.name - repo.config['user.email'] = user.email - end - - def default_options(options = {}) - { raise: true, timeout: true }.merge(options) - end - - def handle_exception(exception) - Gitlab::GitLogger.error(exception.message) - false - end - end - end -end diff --git a/lib/gitlab/satellite/compare_action.rb b/lib/gitlab/satellite/compare_action.rb deleted file mode 100644 index 46c98a8f4ca..00000000000 --- a/lib/gitlab/satellite/compare_action.rb +++ /dev/null @@ -1,44 +0,0 @@ -module Gitlab - module Satellite - class BranchesWithoutParent < StandardError; end - - class CompareAction < Action - def initialize(user, target_project, target_branch, source_project, source_branch) - super user, target_project - - @target_project, @target_branch = target_project, target_branch - @source_project, @source_branch = source_project, source_branch - end - - # Compare 2 repositories and return Gitlab::CompareResult object - def result - in_locked_and_timed_satellite do |target_repo| - prepare_satellite!(target_repo) - update_satellite_source_and_target!(target_repo) - - Gitlab::CompareResult.new(compare(target_repo)) - end - rescue Grit::Git::CommandFailed => ex - raise BranchesWithoutParent - end - - private - - # Assumes a satellite exists that is a fresh clone of the projects repo, prepares satellite for diffs - def update_satellite_source_and_target!(target_repo) - target_repo.remote_add('source', @source_project.repository.path_to_repo) - target_repo.remote_fetch('source') - rescue Grit::Git::CommandFailed => ex - handle_exception(ex) - end - - def compare(repo) - @compare ||= Gitlab::Git::Compare.new( - Gitlab::Git::Repository.new(repo.path), - "origin/#{@target_branch}", - "source/#{@source_branch}" - ) - end - end - end -end diff --git a/lib/gitlab/satellite/files/delete_file_action.rb b/lib/gitlab/satellite/files/delete_file_action.rb deleted file mode 100644 index 0d37b9dea85..00000000000 --- a/lib/gitlab/satellite/files/delete_file_action.rb +++ /dev/null @@ -1,50 +0,0 @@ -require_relative 'file_action' - -module Gitlab - module Satellite - class DeleteFileAction < FileAction - # Deletes file and creates a new commit for it - # - # Returns false if committing the change fails - # Returns false if pushing from the satellite to bare repo failed or was rejected - # Returns true otherwise - def commit!(content, commit_message) - in_locked_and_timed_satellite do |repo| - prepare_satellite!(repo) - - # create target branch in satellite at the corresponding commit from bare repo - repo.git.checkout({ raise: true, timeout: true, b: true }, ref, "origin/#{ref}") - - # update the file in the satellite's working dir - file_path_in_satellite = File.join(repo.working_dir, file_path) - - # Prevent relative links - unless safe_path?(file_path_in_satellite) - Gitlab::GitLogger.error("FileAction: Relative path not allowed") - return false - end - - File.delete(file_path_in_satellite) - - # add removed file - repo.remove(file_path_in_satellite) - - # commit the changes - # will raise CommandFailed when commit fails - repo.git.commit(raise: true, timeout: true, a: true, m: commit_message) - - - # push commit back to bare repo - # will raise CommandFailed when push fails - repo.git.push({ raise: true, timeout: true }, :origin, ref) - - # everything worked - true - end - rescue Grit::Git::CommandFailed => ex - Gitlab::GitLogger.error(ex.message) - false - end - end - end -end diff --git a/lib/gitlab/satellite/files/edit_file_action.rb b/lib/gitlab/satellite/files/edit_file_action.rb deleted file mode 100644 index 3cb9c0b5ecb..00000000000 --- a/lib/gitlab/satellite/files/edit_file_action.rb +++ /dev/null @@ -1,68 +0,0 @@ -require_relative 'file_action' - -module Gitlab - module Satellite - # GitLab server-side file update and commit - class EditFileAction < FileAction - # Updates the files content and creates a new commit for it - # - # Returns false if the ref has been updated while editing the file - # Returns false if committing the change fails - # Returns false if pushing from the satellite to bare repo failed or was rejected - # Returns true otherwise - def commit!(content, commit_message, encoding, new_branch = nil) - in_locked_and_timed_satellite do |repo| - prepare_satellite!(repo) - - # create target branch in satellite at the corresponding commit from bare repo - begin - repo.git.checkout({ raise: true, timeout: true, b: true }, ref, "origin/#{ref}") - rescue Grit::Git::CommandFailed => ex - log_and_raise(CheckoutFailed, ex.message) - end - - # update the file in the satellite's working dir - file_path_in_satellite = File.join(repo.working_dir, file_path) - - # Prevent relative links - unless safe_path?(file_path_in_satellite) - Gitlab::GitLogger.error("FileAction: Relative path not allowed") - return false - end - - # Write file - write_file(file_path_in_satellite, content, encoding) - - # commit the changes - # will raise CommandFailed when commit fails - begin - repo.git.commit(raise: true, timeout: true, a: true, m: commit_message) - rescue Grit::Git::CommandFailed => ex - log_and_raise(CommitFailed, ex.message) - end - - - target_branch = new_branch.present? ? "#{ref}:#{new_branch}" : ref - - # push commit back to bare repo - # will raise CommandFailed when push fails - begin - repo.git.push({ raise: true, timeout: true }, :origin, target_branch) - rescue Grit::Git::CommandFailed => ex - log_and_raise(PushFailed, ex.message) - end - - # everything worked - true - end - end - - private - - def log_and_raise(errorClass, message) - Gitlab::GitLogger.error(message) - raise(errorClass, message) - end - end - end -end diff --git a/lib/gitlab/satellite/files/file_action.rb b/lib/gitlab/satellite/files/file_action.rb deleted file mode 100644 index 6446b14568a..00000000000 --- a/lib/gitlab/satellite/files/file_action.rb +++ /dev/null @@ -1,25 +0,0 @@ -module Gitlab - module Satellite - class FileAction < Action - attr_accessor :file_path, :ref - - def initialize(user, project, ref, file_path) - super user, project - @file_path = file_path - @ref = ref - end - - def safe_path?(path) - File.absolute_path(path) == path - end - - def write_file(abs_file_path, content, file_encoding = 'text') - if file_encoding == 'base64' - File.open(abs_file_path, 'wb') { |f| f.write(Base64.decode64(content)) } - else - File.open(abs_file_path, 'w') { |f| f.write(content) } - end - end - end - end -end diff --git a/lib/gitlab/satellite/files/new_file_action.rb b/lib/gitlab/satellite/files/new_file_action.rb deleted file mode 100644 index 724dfa0d042..00000000000 --- a/lib/gitlab/satellite/files/new_file_action.rb +++ /dev/null @@ -1,67 +0,0 @@ -require_relative 'file_action' - -module Gitlab - module Satellite - class NewFileAction < FileAction - # Updates the files content and creates a new commit for it - # - # Returns false if the ref has been updated while editing the file - # Returns false if committing the change fails - # Returns false if pushing from the satellite to bare repo failed or was rejected - # Returns true otherwise - def commit!(content, commit_message, encoding, new_branch = nil) - in_locked_and_timed_satellite do |repo| - prepare_satellite!(repo) - - # create target branch in satellite at the corresponding commit from bare repo - current_ref = - if @project.empty_repo? - # skip this step if we want to add first file to empty repo - Satellite::PARKING_BRANCH - else - repo.git.checkout({ raise: true, timeout: true, b: true }, ref, "origin/#{ref}") - ref - end - - file_path_in_satellite = File.join(repo.working_dir, file_path) - dir_name_in_satellite = File.dirname(file_path_in_satellite) - - # Prevent relative links - unless safe_path?(file_path_in_satellite) - Gitlab::GitLogger.error("FileAction: Relative path not allowed") - return false - end - - # Create dir if not exists - FileUtils.mkdir_p(dir_name_in_satellite) - - # Write file - write_file(file_path_in_satellite, content, encoding) - - # add new file - repo.add(file_path_in_satellite) - - # commit the changes - # will raise CommandFailed when commit fails - repo.git.commit(raise: true, timeout: true, a: true, m: commit_message) - - target_branch = if new_branch.present? && !@project.empty_repo? - "#{ref}:#{new_branch}" - else - "#{current_ref}:#{ref}" - end - - # push commit back to bare repo - # will raise CommandFailed when push fails - repo.git.push({ raise: true, timeout: true }, :origin, target_branch) - - # everything worked - true - end - rescue Grit::Git::CommandFailed => ex - Gitlab::GitLogger.error(ex.message) - false - end - end - end -end diff --git a/lib/gitlab/satellite/logger.rb b/lib/gitlab/satellite/logger.rb deleted file mode 100644 index 6f3f8255aca..00000000000 --- a/lib/gitlab/satellite/logger.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Gitlab - module Satellite - class Logger < Gitlab::Logger - def self.file_name - 'satellites.log' - end - - def format_message(severity, timestamp, progname, msg) - "#{timestamp.to_s(:long)}: #{msg}\n" - end - end - end -end diff --git a/lib/gitlab/satellite/merge_action.rb b/lib/gitlab/satellite/merge_action.rb deleted file mode 100644 index 1f2e5f82dd5..00000000000 --- a/lib/gitlab/satellite/merge_action.rb +++ /dev/null @@ -1,146 +0,0 @@ -module Gitlab - module Satellite - # GitLab server-side merge - class MergeAction < Action - attr_accessor :merge_request - - def initialize(user, merge_request) - super user, merge_request.target_project - @merge_request = merge_request - end - - # Checks if a merge request can be executed without user interaction - def can_be_merged? - in_locked_and_timed_satellite do |merge_repo| - prepare_satellite!(merge_repo) - merge_in_satellite!(merge_repo) - end - end - - # Merges the source branch into the target branch in the satellite and - # pushes it back to the repository. - # It also removes the source branch if requested in the merge request (and this is permitted by the merge request). - # - # Returns false if the merge produced conflicts - # Returns false if pushing from the satellite to the repository failed or was rejected - # Returns true otherwise - def merge!(merge_commit_message = nil) - in_locked_and_timed_satellite do |merge_repo| - prepare_satellite!(merge_repo) - if merge_in_satellite!(merge_repo, merge_commit_message) - # push merge back to bare repo - # will raise CommandFailed when push fails - merge_repo.git.push(default_options, :origin, merge_request.target_branch) - - # remove source branch - if merge_request.should_remove_source_branch && !project.root_ref?(merge_request.source_branch) && !merge_request.for_fork? - # will raise CommandFailed when push fails - merge_repo.git.push(default_options, :origin, ":#{merge_request.source_branch}") - end - # merge, push and branch removal successful - true - end - end - rescue Grit::Git::CommandFailed => ex - handle_exception(ex) - end - - def diff_in_satellite - in_locked_and_timed_satellite do |merge_repo| - prepare_satellite!(merge_repo) - update_satellite_source_and_target!(merge_repo) - - # Only show what is new in the source branch compared to the target branch, not the other way around. - # The line below with merge_base is equivalent to diff with three dots (git diff branch1...branch2) - # From the git documentation: "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B" - common_commit = merge_repo.git.native(:merge_base, default_options, ["origin/#{merge_request.target_branch}", "source/#{merge_request.source_branch}"]).strip - merge_repo.git.native(:diff, default_options, common_commit, "source/#{merge_request.source_branch}") - end - rescue Grit::Git::CommandFailed => ex - handle_exception(ex) - end - - def diffs_between_satellite - in_locked_and_timed_satellite do |merge_repo| - prepare_satellite!(merge_repo) - update_satellite_source_and_target!(merge_repo) - if merge_request.for_fork? - repository = Gitlab::Git::Repository.new(merge_repo.path) - diffs = Gitlab::Git::Diff.between( - repository, - "source/#{merge_request.source_branch}", - "origin/#{merge_request.target_branch}" - ) - else - raise "Attempt to determine diffs between for a non forked merge request in satellite MergeRequest.id:[#{merge_request.id}]" - end - - return diffs - end - rescue Grit::Git::CommandFailed => ex - handle_exception(ex) - end - - # Get commit as an email patch - def format_patch - in_locked_and_timed_satellite do |merge_repo| - prepare_satellite!(merge_repo) - update_satellite_source_and_target!(merge_repo) - patch = merge_repo.git.format_patch(default_options({ stdout: true }), "origin/#{merge_request.target_branch}..source/#{merge_request.source_branch}") - end - rescue Grit::Git::CommandFailed => ex - handle_exception(ex) - end - - # Retrieve an array of commits between the source and the target - def commits_between - in_locked_and_timed_satellite do |merge_repo| - prepare_satellite!(merge_repo) - update_satellite_source_and_target!(merge_repo) - if merge_request.for_fork? - repository = Gitlab::Git::Repository.new(merge_repo.path) - commits = Gitlab::Git::Commit.between( - repository, - "origin/#{merge_request.target_branch}", - "source/#{merge_request.source_branch}" - ) - else - raise "Attempt to determine commits between for a non forked merge request in satellite MergeRequest.id:[#{merge_request.id}]" - end - - return commits - end - rescue Grit::Git::CommandFailed => ex - handle_exception(ex) - end - - private - # Merges the source_branch into the target_branch in the satellite. - # - # Note: it will clear out the satellite before doing anything - # - # Returns false if the merge produced conflicts - # Returns true otherwise - def merge_in_satellite!(repo, message = nil) - update_satellite_source_and_target!(repo) - - message ||= "Merge branch '#{merge_request.source_branch}' into '#{merge_request.target_branch}'" - - # merge the source branch into the satellite - # will raise CommandFailed when merge fails - repo.git.merge(default_options({ no_ff: true }), "-m#{message}", "source/#{merge_request.source_branch}") - rescue Grit::Git::CommandFailed => ex - handle_exception(ex) - end - - # Assumes a satellite exists that is a fresh clone of the projects repo, prepares satellite for merges, diffs etc - def update_satellite_source_and_target!(repo) - repo.remote_add('source', merge_request.source_project.repository.path_to_repo) - repo.remote_fetch('source') - repo.git.checkout(default_options({ b: true }), merge_request.target_branch, "origin/#{merge_request.target_branch}") - rescue Grit::Git::CommandFailed => ex - handle_exception(ex) - end - end - end -end diff --git a/lib/gitlab/satellite/satellite.rb b/lib/gitlab/satellite/satellite.rb deleted file mode 100644 index 398643d68de..00000000000 --- a/lib/gitlab/satellite/satellite.rb +++ /dev/null @@ -1,148 +0,0 @@ -module Gitlab - module Satellite - autoload :DeleteFileAction, 'gitlab/satellite/files/delete_file_action' - autoload :EditFileAction, 'gitlab/satellite/files/edit_file_action' - autoload :FileAction, 'gitlab/satellite/files/file_action' - autoload :NewFileAction, 'gitlab/satellite/files/new_file_action' - - class CheckoutFailed < StandardError; end - class CommitFailed < StandardError; end - class PushFailed < StandardError; end - - class Satellite - include Gitlab::Popen - - PARKING_BRANCH = "__parking_branch" - - attr_accessor :project - - def initialize(project) - @project = project - end - - def log(message) - Gitlab::Satellite::Logger.error(message) - end - - def clear_and_update! - project.ensure_satellite_exists - - @repo = nil - clear_working_dir! - delete_heads! - remove_remotes! - update_from_source! - end - - def create - output, status = popen(%W(git clone -- #{project.repository.path_to_repo} #{path}), - Gitlab.config.satellites.path) - - log("PID: #{project.id}: git clone #{project.repository.path_to_repo} #{path}") - log("PID: #{project.id}: -> #{output}") - - if status.zero? - true - else - log("Failed to create satellite for #{project.name_with_namespace}") - false - end - end - - def exists? - File.exists? path - end - - # * Locks the satellite - # * Changes the current directory to the satellite's working dir - # * Yields - def lock - project.ensure_satellite_exists - - File.open(lock_file, "w+") do |f| - begin - f.flock File::LOCK_EX - yield - ensure - f.flock File::LOCK_UN - end - end - end - - def lock_file - create_locks_dir unless File.exists?(lock_files_dir) - File.join(lock_files_dir, "satellite_#{project.id}.lock") - end - - def path - File.join(Gitlab.config.satellites.path, project.path_with_namespace) - end - - def repo - project.ensure_satellite_exists - - @repo ||= Grit::Repo.new(path) - end - - def destroy - FileUtils.rm_rf(path) - end - - private - - # Clear the working directory - def clear_working_dir! - repo.git.reset(hard: true) - repo.git.clean(f: true, d: true, x: true) - end - - # Deletes all branches except the parking branch - # - # This ensures we have no name clashes or issues updating branches when - # working with the satellite. - def delete_heads! - heads = repo.heads.map(&:name) - - # update or create the parking branch - repo.git.checkout(default_options({ B: true }), PARKING_BRANCH) - - # remove the parking branch from the list of heads ... - heads.delete(PARKING_BRANCH) - # ... and delete all others - heads.each { |head| repo.git.branch(default_options({ D: true }), head) } - end - - # Deletes all remotes except origin - # - # This ensures we have no remote name clashes or issues updating branches when - # working with the satellite. - def remove_remotes! - remotes = repo.git.remote.split(' ') - remotes.delete('origin') - remotes.each { |name| repo.git.remote(default_options,'rm', name)} - end - - # Updates the satellite from bare repo - # - # Note: this will only update remote branches (i.e. origin/*) - def update_from_source! - repo.git.remote(default_options, 'set-url', :origin, project.repository.path_to_repo) - repo.git.fetch(default_options, :origin) - end - - def default_options(options = {}) - { raise: true, timeout: true }.merge(options) - end - - # Create directory for storing - # satellites lock files - def create_locks_dir - FileUtils.mkdir_p(lock_files_dir) - end - - def lock_files_dir - @lock_files_dir ||= File.join(Gitlab.config.satellites.path, "tmp") - end - end - end -end diff --git a/lib/gitlab/search_results.rb b/lib/gitlab/search_results.rb deleted file mode 100644 index 75a3dfe37c3..00000000000 --- a/lib/gitlab/search_results.rb +++ /dev/null @@ -1,69 +0,0 @@ -module Gitlab - class SearchResults - attr_reader :query - - # Limit search results by passed project ids - # It allows us to search only for projects user has access to - attr_reader :limit_project_ids - - def initialize(limit_project_ids, query) - @limit_project_ids = limit_project_ids || Project.all - @query = Shellwords.shellescape(query) if query.present? - end - - def objects(scope, page = nil) - case scope - when 'projects' - projects.page(page).per(per_page) - when 'issues' - issues.page(page).per(per_page) - when 'merge_requests' - merge_requests.page(page).per(per_page) - else - Kaminari.paginate_array([]).page(page).per(per_page) - end - end - - def total_count - @total_count ||= projects_count + issues_count + merge_requests_count - end - - def projects_count - @projects_count ||= projects.count - end - - def issues_count - @issues_count ||= issues.count - end - - def merge_requests_count - @merge_requests_count ||= merge_requests.count - end - - def empty? - total_count.zero? - end - - private - - def projects - Project.where(id: limit_project_ids).search(query) - end - - def issues - Issue.where(project_id: limit_project_ids).full_search(query).order('updated_at DESC') - end - - def merge_requests - MergeRequest.in_projects(limit_project_ids).full_search(query).order('updated_at DESC') - end - - def default_scope - 'projects' - end - - def per_page - 20 - end - end -end diff --git a/lib/gitlab/seeder.rb b/lib/gitlab/seeder.rb deleted file mode 100644 index 31aa3528c4c..00000000000 --- a/lib/gitlab/seeder.rb +++ /dev/null @@ -1,24 +0,0 @@ -module Gitlab - class Seeder - def self.quiet - mute_mailer - SeedFu.quiet = true - yield - SeedFu.quiet = false - puts "\nOK".green - end - - def self.by_user(user) - yield - end - - def self.mute_mailer - code = <<-eos -def Notify.delay - self -end - eos - eval(code) - end - end -end diff --git a/lib/gitlab/sidekiq_logger.rb b/lib/gitlab/sidekiq_logger.rb deleted file mode 100644 index c1dab87a432..00000000000 --- a/lib/gitlab/sidekiq_logger.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Gitlab - class SidekiqLogger < Gitlab::Logger - def self.file_name_noext - 'sidekiq' - end - end -end diff --git a/lib/gitlab/sidekiq_middleware/arguments_logger.rb b/lib/gitlab/sidekiq_middleware/arguments_logger.rb deleted file mode 100644 index 7813091ec7b..00000000000 --- a/lib/gitlab/sidekiq_middleware/arguments_logger.rb +++ /dev/null @@ -1,10 +0,0 @@ -module Gitlab - module SidekiqMiddleware - class ArgumentsLogger - def call(worker, job, queue) - Sidekiq.logger.info "arguments: #{job['args']}" - yield - end - end - end -end diff --git a/lib/gitlab/sidekiq_middleware/memory_killer.rb b/lib/gitlab/sidekiq_middleware/memory_killer.rb deleted file mode 100644 index 0f2db50e98c..00000000000 --- a/lib/gitlab/sidekiq_middleware/memory_killer.rb +++ /dev/null @@ -1,53 +0,0 @@ -module Gitlab - module SidekiqMiddleware - class MemoryKiller - # Default the RSS limit to 0, meaning the MemoryKiller is disabled - MAX_RSS = (ENV['SIDEKIQ_MEMORY_KILLER_MAX_RSS'] || 0).to_s.to_i - # Give Sidekiq 15 minutes of grace time after exceeding the RSS limit - GRACE_TIME = (ENV['SIDEKIQ_MEMORY_KILLER_GRACE_TIME'] || 15 * 60).to_s.to_i - # Wait 30 seconds for running jobs to finish during graceful shutdown - SHUTDOWN_WAIT = (ENV['SIDEKIQ_MEMORY_KILLER_SHUTDOWN_WAIT'] || 30).to_s.to_i - - # Create a mutex used to ensure there will be only one thread waiting to - # shut Sidekiq down - MUTEX = Mutex.new - - def call(worker, job, queue) - yield - current_rss = get_rss - - return unless MAX_RSS > 0 && current_rss > MAX_RSS - - Thread.new do - # Return if another thread is already waiting to shut Sidekiq down - return unless MUTEX.try_lock - - Sidekiq.logger.warn "current RSS #{current_rss} exceeds maximum RSS "\ - "#{MAX_RSS}" - Sidekiq.logger.warn "spawned thread that will shut down PID "\ - "#{Process.pid} in #{GRACE_TIME} seconds" - sleep(GRACE_TIME) - - Sidekiq.logger.warn "sending SIGUSR1 to PID #{Process.pid}" - Process.kill('SIGUSR1', Process.pid) - - Sidekiq.logger.warn "waiting #{SHUTDOWN_WAIT} seconds before sending "\ - "SIGTERM to PID #{Process.pid}" - sleep(SHUTDOWN_WAIT) - - Sidekiq.logger.warn "sending SIGTERM to PID #{Process.pid}" - Process.kill('SIGTERM', Process.pid) - end - end - - private - - def get_rss - output, status = Gitlab::Popen.popen(%W(ps -o rss= -p #{Process.pid})) - return 0 unless status.zero? - - output.to_i - end - end - end -end diff --git a/lib/gitlab/snippet_search_results.rb b/lib/gitlab/snippet_search_results.rb deleted file mode 100644 index 938219efdb2..00000000000 --- a/lib/gitlab/snippet_search_results.rb +++ /dev/null @@ -1,131 +0,0 @@ -module Gitlab - class SnippetSearchResults < SearchResults - attr_reader :limit_snippet_ids - - def initialize(limit_snippet_ids, query) - @limit_snippet_ids = limit_snippet_ids - @query = query - end - - def objects(scope, page = nil) - case scope - when 'snippet_titles' - Kaminari.paginate_array(snippet_titles).page(page).per(per_page) - when 'snippet_blobs' - Kaminari.paginate_array(snippet_blobs).page(page).per(per_page) - else - super - end - end - - def total_count - @total_count ||= snippet_titles_count + snippet_blobs_count - end - - def snippet_titles_count - @snippet_titles_count ||= snippet_titles.count - end - - def snippet_blobs_count - @snippet_blobs_count ||= snippet_blobs.count - end - - private - - def snippet_titles - Snippet.where(id: limit_snippet_ids).search(query).order('updated_at DESC') - end - - def snippet_blobs - search = Snippet.where(id: limit_snippet_ids).search_code(query) - search = search.order('updated_at DESC').to_a - snippets = [] - search.each { |e| snippets << chunk_snippet(e) } - snippets - end - - def default_scope - 'snippet_blobs' - end - - # Get an array of line numbers surrounding a matching - # line, bounded by min/max. - # - # @returns Array of line numbers - def bounded_line_numbers(line, min, max) - lower = line - surrounding_lines > min ? line - surrounding_lines : min - upper = line + surrounding_lines < max ? line + surrounding_lines : max - (lower..upper).to_a - end - - # Returns a sorted set of lines to be included in a snippet preview. - # This ensures matching adjacent lines do not display duplicated - # surrounding code. - # - # @returns Array, unique and sorted. - def matching_lines(lined_content) - used_lines = [] - lined_content.each_with_index do |line, line_number| - used_lines.concat bounded_line_numbers( - line_number, - 0, - lined_content.size - ) if line.include?(query) - end - - used_lines.uniq.sort - end - - # 'Chunkify' entire snippet. Splits the snippet data into matching lines + - # surrounding_lines() worth of unmatching lines. - # - # @returns a hash with {snippet_object, snippet_chunks:{data,start_line}} - def chunk_snippet(snippet) - lined_content = snippet.content.split("\n") - used_lines = matching_lines(lined_content) - - snippet_chunk = [] - snippet_chunks = [] - snippet_start_line = 0 - last_line = -1 - - # Go through each used line, and add consecutive lines as a single chunk - # to the snippet chunk array. - used_lines.each do |line_number| - if last_line < 0 - # Start a new chunk. - snippet_start_line = line_number - snippet_chunk << lined_content[line_number] - elsif last_line == line_number - 1 - # Consecutive line, continue chunk. - snippet_chunk << lined_content[line_number] - else - # Non-consecutive line, add chunk to chunk array. - snippet_chunks << { - data: snippet_chunk.join("\n"), - start_line: snippet_start_line + 1 - } - - # Start a new chunk. - snippet_chunk = [lined_content[line_number]] - snippet_start_line = line_number - end - last_line = line_number - end - # Add final chunk to chunk array - snippet_chunks << { - data: snippet_chunk.join("\n"), - start_line: snippet_start_line + 1 - } - - # Return snippet with chunk array - { snippet_object: snippet, snippet_chunks: snippet_chunks } - end - - # Defines how many unmatching lines should be - # included around the matching lines in a snippet - def surrounding_lines - 3 - end - end -end diff --git a/lib/gitlab/theme.rb b/lib/gitlab/theme.rb deleted file mode 100644 index 43093c7d27e..00000000000 --- a/lib/gitlab/theme.rb +++ /dev/null @@ -1,39 +0,0 @@ -module Gitlab - class Theme - BASIC = 1 unless const_defined?(:BASIC) - MARS = 2 unless const_defined?(:MARS) - MODERN = 3 unless const_defined?(:MODERN) - GRAY = 4 unless const_defined?(:GRAY) - COLOR = 5 unless const_defined?(:COLOR) - BLUE = 6 unless const_defined?(:BLUE) - - def self.css_class_by_id(id) - themes = { - BASIC => "ui_basic", - MARS => "ui_mars", - MODERN => "ui_modern", - GRAY => "ui_gray", - COLOR => "ui_color", - BLUE => "ui_blue" - } - - id ||= Gitlab.config.gitlab.default_theme - - themes[id] - end - - def self.type_css_class_by_id(id) - types = { - BASIC => 'light_theme', - MARS => 'dark_theme', - MODERN => 'dark_theme', - GRAY => 'dark_theme', - COLOR => 'dark_theme' - } - - id ||= Gitlab.config.gitlab.default_theme - - types[id] - end - end -end diff --git a/lib/gitlab/upgrader.rb b/lib/gitlab/upgrader.rb deleted file mode 100644 index 0570c2fbeb5..00000000000 --- a/lib/gitlab/upgrader.rb +++ /dev/null @@ -1,102 +0,0 @@ -require_relative "popen" -require_relative "version_info" - -module Gitlab - class Upgrader - def execute - puts "GitLab #{current_version.major} upgrade tool" - puts "Your version is #{current_version}" - puts "Latest available version for GitLab #{current_version.major} is #{latest_version}" - - if latest_version? - puts "You are using the latest GitLab version" - else - puts "Newer GitLab version is available" - answer = if ARGV.first == "-y" - "yes" - else - prompt("Do you want to upgrade (yes/no)? ", %w{yes no}) - end - - if answer == "yes" - upgrade - else - exit 0 - end - end - end - - def latest_version? - current_version >= latest_version - end - - def current_version - @current_version ||= Gitlab::VersionInfo.parse(current_version_raw) - end - - def latest_version - @latest_version ||= Gitlab::VersionInfo.parse(latest_version_raw) - end - - def current_version_raw - File.read(File.join(gitlab_path, "VERSION")).strip - end - - def latest_version_raw - remote_tags, _ = Gitlab::Popen.popen(%W(git ls-remote --tags https://gitlab.com/gitlab-org/gitlab-ce.git)) - git_tags = remote_tags.split("\n").grep(/tags\/v#{current_version.major}/) - git_tags = git_tags.select { |version| version =~ /v\d\.\d\.\d\Z/ } - last_tag = git_tags.last.match(/v\d\.\d\.\d/).to_s - end - - def update_commands - { - "Stash changed files" => %W(git stash), - "Get latest code" => %W(git fetch), - "Switch to new version" => %W(git checkout v#{latest_version}), - "Install gems" => %W(bundle), - "Migrate DB" => %W(bundle exec rake db:migrate), - "Recompile assets" => %W(bundle exec rake assets:clean assets:precompile), - "Clear cache" => %W(bundle exec rake cache:clear) - } - end - - def env - { 'RAILS_ENV' => 'production' } - end - - def upgrade - update_commands.each do |title, cmd| - puts title - puts " -> #{cmd.join(' ')}" - if system(env, *cmd) - puts " -> OK" - else - puts " -> FAILED" - puts "Failed to upgrade. Try to repeat task or proceed with upgrade manually " - exit 1 - end - end - - puts "Done" - end - - def gitlab_path - File.expand_path(File.join(File.dirname(__FILE__), '../..')) - end - - # Prompt the user to input something - # - # message - the message to display before input - # choices - array of strings of acceptable answers or nil for any answer - # - # Returns the user's answer - def prompt(message, choices = nil) - begin - print(message) - answer = STDIN.gets.chomp - end while !choices.include?(answer) - answer - end - end -end diff --git a/lib/gitlab/url_builder.rb b/lib/gitlab/url_builder.rb deleted file mode 100644 index 11b0d44f340..00000000000 --- a/lib/gitlab/url_builder.rb +++ /dev/null @@ -1,60 +0,0 @@ -module Gitlab - class UrlBuilder - include Rails.application.routes.url_helpers - include GitlabRoutingHelper - - def initialize(type) - @type = type - end - - def build(id) - case @type - when :issue - build_issue_url(id) - when :merge_request - build_merge_request_url(id) - when :note - build_note_url(id) - - end - end - - private - - def build_issue_url(id) - issue = Issue.find(id) - issue_url(issue, host: Gitlab.config.gitlab['url']) - end - - def build_merge_request_url(id) - merge_request = MergeRequest.find(id) - merge_request_url(merge_request, host: Gitlab.config.gitlab['url']) - end - - def build_note_url(id) - note = Note.find(id) - if note.for_commit? - namespace_project_commit_url(namespace_id: note.project.namespace, - id: note.commit_id, - project_id: note.project, - host: Gitlab.config.gitlab['url'], - anchor: "note_#{note.id}") - elsif note.for_issue? - issue = Issue.find(note.noteable_id) - issue_url(issue, - host: Gitlab.config.gitlab['url'], - anchor: "note_#{note.id}") - elsif note.for_merge_request? - merge_request = MergeRequest.find(note.noteable_id) - merge_request_url(merge_request, - host: Gitlab.config.gitlab['url'], - anchor: "note_#{note.id}") - elsif note.for_project_snippet? - snippet = Snippet.find(note.noteable_id) - project_snippet_url(snippet, - host: Gitlab.config.gitlab['url'], - anchor: "note_#{note.id}") - end - end - end -end diff --git a/lib/gitlab/user_access.rb b/lib/gitlab/user_access.rb deleted file mode 100644 index 4885baf9526..00000000000 --- a/lib/gitlab/user_access.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Gitlab - module UserAccess - def self.allowed?(user) - return false if user.blocked? - - if user.requires_ldap_check? - return false unless Gitlab::LDAP::Access.allowed?(user) - end - - true - end - end -end diff --git a/lib/gitlab/utils.rb b/lib/gitlab/utils.rb deleted file mode 100644 index bd184c27187..00000000000 --- a/lib/gitlab/utils.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Gitlab - module Utils - extend self - - # Run system command without outputting to stdout. - # - # @param cmd [Array] - # @return [Boolean] - def system_silent(cmd) - Popen::popen(cmd).last.zero? - end - end -end diff --git a/lib/gitlab/version_info.rb b/lib/gitlab/version_info.rb deleted file mode 100644 index 6ee41e85cc9..00000000000 --- a/lib/gitlab/version_info.rb +++ /dev/null @@ -1,54 +0,0 @@ -module Gitlab - class VersionInfo - include Comparable - - attr_reader :major, :minor, :patch - - def self.parse(str) - if str && m = str.match(/(\d+)\.(\d+)\.(\d+)/) - VersionInfo.new(m[1].to_i, m[2].to_i, m[3].to_i) - else - VersionInfo.new - end - end - - def initialize(major = 0, minor = 0, patch = 0) - @major = major - @minor = minor - @patch = patch - end - - def <=>(other) - return unless other.is_a? VersionInfo - return unless valid? && other.valid? - - if other.major < @major - 1 - elsif @major < other.major - -1 - elsif other.minor < @minor - 1 - elsif @minor < other.minor - -1 - elsif other.patch < @patch - 1 - elsif @patch < other.patch - -1 - else - 0 - end - end - - def to_s - if valid? - "%d.%d.%d" % [@major, @minor, @patch] - else - "Unknown" - end - end - - def valid? - @major >= 0 && @minor >= 0 && @patch >= 0 && @major + @minor + @patch > 0 - end - end -end diff --git a/lib/gitlab/visibility_level.rb b/lib/gitlab/visibility_level.rb deleted file mode 100644 index 582fc759efd..00000000000 --- a/lib/gitlab/visibility_level.rb +++ /dev/null @@ -1,64 +0,0 @@ -# Gitlab::VisibilityLevel module -# -# Define allowed public modes that can be used for -# GitLab projects to determine project public mode -# -module Gitlab - module VisibilityLevel - extend CurrentSettings - - PRIVATE = 0 unless const_defined?(:PRIVATE) - INTERNAL = 10 unless const_defined?(:INTERNAL) - PUBLIC = 20 unless const_defined?(:PUBLIC) - - class << self - def values - options.values - end - - def options - { - 'Private' => PRIVATE, - 'Internal' => INTERNAL, - 'Public' => PUBLIC - } - end - - def allowed_for?(user, level) - user.is_admin? || allowed_level?(level.to_i) - end - - # Return true if the specified level is allowed for the current user. - # Level should be a numeric value, e.g. `20`. - def allowed_level?(level) - valid_level?(level) && non_restricted_level?(level) - end - - def non_restricted_level?(level) - restricted_levels = current_application_settings.restricted_visibility_levels - - if restricted_levels.nil? - true - else - !restricted_levels.include?(level) - end - end - - def valid_level?(level) - options.has_value?(level) - end - end - - def private? - visibility_level_field == PRIVATE - end - - def internal? - visibility_level_field == INTERNAL - end - - def public? - visibility_level_field == PUBLIC - end - end -end diff --git a/lib/gt_one_coercion.rb b/lib/gt_one_coercion.rb deleted file mode 100644 index ef2dc09767c..00000000000 --- a/lib/gt_one_coercion.rb +++ /dev/null @@ -1,5 +0,0 @@ -class GtOneCoercion < Virtus::Attribute - def coerce(value) - [1, value.to_i].max - end -end diff --git a/lib/redcarpet/render/gitlab_html.rb b/lib/redcarpet/render/gitlab_html.rb deleted file mode 100644 index 10efff2ae9f..00000000000 --- a/lib/redcarpet/render/gitlab_html.rb +++ /dev/null @@ -1,72 +0,0 @@ -class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML - - attr_reader :template - alias_method :h, :template - - def initialize(template, color_scheme, options = {}) - @template = template - @color_scheme = color_scheme - @project = @template.instance_variable_get("@project") - @options = options.dup - super options - end - - def preprocess(full_document) - # Redcarpet doesn't allow SMB links when `safe_links_only` is enabled. - # FTP links are allowed, so we trick Redcarpet. - full_document.gsub("smb://", "ftp://smb:") - end - - # If project has issue number 39, apostrophe will be linked in - # regular text to the issue as Redcarpet will convert apostrophe to - # #39; - # We replace apostrophe with right single quote before Redcarpet - # does the processing and put the apostrophe back in postprocessing. - # This only influences regular text, code blocks are untouched. - def normal_text(text) - return text unless text.present? - text.gsub("'", "’") - end - - # Stolen from Rugments::Plugins::Redcarpet as this module is not required - # from Rugments's gem root. - def block_code(code, language) - lexer = Rugments::Lexer.find_fancy(language, code) || Rugments::Lexers::PlainText - - # XXX HACK: Redcarpet strips hard tabs out of code blocks, - # so we assume you're not using leading spaces that aren't tabs, - # and just replace them here. - if lexer.tag == 'make' - code.gsub! /^ /, "\t" - end - - formatter = Rugments::Formatters::HTML.new( - cssclass: "code highlight #{@color_scheme} #{lexer.tag}" - ) - formatter.format(lexer.lex(code)) - end - - def link(link, title, content) - h.link_to_gfm(content, link, title: title) - end - - def header(text, level) - if @options[:no_header_anchors] - "#{text}" - else - id = ActionController::Base.helpers.strip_tags(h.gfm(text)).downcase() \ - .gsub(/[^a-z0-9_-]/, '-').gsub(/-+/, '-').gsub(/^-/, '').gsub(/-$/, '') - "#{text}" - end - end - - def postprocess(full_document) - full_document.gsub!("ftp://smb:", "smb://") - - full_document.gsub!("’", "'") - unless @template.instance_variable_get("@project_wiki") || @project.nil? - full_document = h.create_relative_links(full_document) - end - h.gfm_with_options(full_document, @options) - end -end diff --git a/lib/repository_cache.rb b/lib/repository_cache.rb deleted file mode 100644 index fa016a170cd..00000000000 --- a/lib/repository_cache.rb +++ /dev/null @@ -1,21 +0,0 @@ -# Interface to the Redis-backed cache store used by the Repository model -class RepositoryCache - attr_reader :namespace, :backend - - def initialize(namespace, backend = Rails.cache) - @namespace = namespace - @backend = backend - end - - def cache_key(type) - "#{type}:#{namespace}" - end - - def expire(key) - backend.delete(cache_key(key)) - end - - def fetch(key, &block) - backend.fetch(cache_key(key), &block) - end -end diff --git a/lib/static_model.rb b/lib/static_model.rb deleted file mode 100644 index 185921d8fbe..00000000000 --- a/lib/static_model.rb +++ /dev/null @@ -1,47 +0,0 @@ -# Provides an ActiveRecord-like interface to a model whose data is not persisted to a database. -module StaticModel - extend ActiveSupport::Concern - - module ClassMethods - # Used by ActiveRecord's polymorphic association to set object_id - def primary_key - 'id' - end - - # Used by ActiveRecord's polymorphic association to set object_type - def base_class - self - end - end - - # Used by AR for fetching attributes - # - # Pass it along if we respond to it. - def [](key) - send(key) if respond_to?(key) - end - - def to_param - id - end - - def new_record? - false - end - - def persisted? - false - end - - def destroyed? - false - end - - def ==(other) - if other.is_a? ::StaticModel - id == other.id - else - super - end - end -end diff --git a/lib/support/deploy/deploy.sh b/lib/support/deploy/deploy.sh deleted file mode 100755 index adea4c7a747..00000000000 --- a/lib/support/deploy/deploy.sh +++ /dev/null @@ -1,45 +0,0 @@ -# This is deploy script we use to update staging server -# You can always modify it for your needs :) - -# If any command return non-zero status - stop deploy -set -e - -echo 'Deploy: Stopping sidekiq..' -cd /home/git/gitlab/ && sudo -u git -H bundle exec rake sidekiq:stop RAILS_ENV=production - -echo 'Deploy: Show deploy index page' -sudo -u git -H cp /home/git/gitlab/public/deploy.html /home/git/gitlab/public/index.html - -echo 'Deploy: Starting backup...' -cd /home/git/gitlab/ && sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production - -echo 'Deploy: Stop GitLab server' -sudo service gitlab stop - -echo 'Deploy: Get latest code' -cd /home/git/gitlab/ - -# clean working directory -sudo -u git -H git stash - -# change branch to -sudo -u git -H git pull origin master - -echo 'Deploy: Bundle and migrate' - -# change it to your needs -sudo -u git -H bundle --without aws development test mysql --deployment - -sudo -u git -H bundle exec rake db:migrate RAILS_ENV=production -sudo -u git -H bundle exec rake assets:clean RAILS_ENV=production -sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production -sudo -u git -H bundle exec rake cache:clear RAILS_ENV=production - -# return stashed changes (if necessary) -# sudo -u git -H git stash pop - -echo 'Deploy: Starting GitLab server...' -sudo service gitlab start - -sudo -u git -H rm /home/git/gitlab/public/index.html -echo 'Deploy: Done' diff --git a/lib/support/init.d/gitlab b/lib/support/init.d/gitlab deleted file mode 100755 index b066a1a6935..00000000000 --- a/lib/support/init.d/gitlab +++ /dev/null @@ -1,301 +0,0 @@ -#! /bin/sh - -# GITLAB -# Maintainer: @randx -# Authors: rovanion.luckey@gmail.com, @randx - -### BEGIN INIT INFO -# Provides: gitlab -# Required-Start: $local_fs $remote_fs $network $syslog redis-server -# Required-Stop: $local_fs $remote_fs $network $syslog -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: GitLab git repository management -# Description: GitLab git repository management -# chkconfig: - 85 14 -### END INIT INFO - - -### -# DO NOT EDIT THIS FILE! -# This file will be overwritten on update. -# Instead add/change your variables in /etc/default/gitlab -# An example defaults file can be found in lib/support/init.d/gitlab.default.example -### - - -### Environment variables -RAILS_ENV="production" - -# Script variable names should be lower-case not to conflict with -# internal /bin/sh variables such as PATH, EDITOR or SHELL. -app_user="git" -app_root="/home/$app_user/gitlab" -pid_path="$app_root/tmp/pids" -socket_path="$app_root/tmp/sockets" -web_server_pid_path="$pid_path/unicorn.pid" -sidekiq_pid_path="$pid_path/sidekiq.pid" - -# Read configuration variable file if it is present -test -f /etc/default/gitlab && . /etc/default/gitlab - -# Switch to the app_user if it is not he/she who is running the script. -if [ "$USER" != "$app_user" ]; then - eval su - "$app_user" -c $(echo \")$0 "$@"$(echo \"); exit; -fi - -# Switch to the gitlab path, exit on failure. -if ! cd "$app_root" ; then - echo "Failed to cd into $app_root, exiting!"; exit 1 -fi - - -### Init Script functions - -## Gets the pids from the files -check_pids(){ - if ! mkdir -p "$pid_path"; then - echo "Could not create the path $pid_path needed to store the pids." - exit 1 - fi - # If there exists a file which should hold the value of the Unicorn pid: read it. - if [ -f "$web_server_pid_path" ]; then - wpid=$(cat "$web_server_pid_path") - else - wpid=0 - fi - if [ -f "$sidekiq_pid_path" ]; then - spid=$(cat "$sidekiq_pid_path") - else - spid=0 - fi -} - -## Called when we have started the two processes and are waiting for their pid files. -wait_for_pids(){ - # We are sleeping a bit here mostly because sidekiq is slow at writing it's pid - i=0; - while [ ! -f $web_server_pid_path -o ! -f $sidekiq_pid_path ]; do - sleep 0.1; - i=$((i+1)) - if [ $((i%10)) = 0 ]; then - echo -n "." - elif [ $((i)) = 301 ]; then - echo "Waited 30s for the processes to write their pids, something probably went wrong." - exit 1; - fi - done - echo -} - -# We use the pids in so many parts of the script it makes sense to always check them. -# Only after start() is run should the pids change. Sidekiq sets it's own pid. -check_pids - - -## Checks whether the different parts of the service are already running or not. -check_status(){ - check_pids - # If the web server is running kill -0 $wpid returns true, or rather 0. - # Checks of *_status should only check for == 0 or != 0, never anything else. - if [ $wpid -ne 0 ]; then - kill -0 "$wpid" 2>/dev/null - web_status="$?" - else - web_status="-1" - fi - if [ $spid -ne 0 ]; then - kill -0 "$spid" 2>/dev/null - sidekiq_status="$?" - else - sidekiq_status="-1" - fi - if [ $web_status = 0 -a $sidekiq_status = 0 ]; then - gitlab_status=0 - else - # http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html - # code 3 means 'program is not running' - gitlab_status=3 - fi -} - -## Check for stale pids and remove them if necessary. -check_stale_pids(){ - check_status - # If there is a pid it is something else than 0, the service is running if - # *_status is == 0. - if [ "$wpid" != "0" -a "$web_status" != "0" ]; then - echo "Removing stale Unicorn web server pid. This is most likely caused by the web server crashing the last time it ran." - if ! rm "$web_server_pid_path"; then - echo "Unable to remove stale pid, exiting." - exit 1 - fi - fi - if [ "$spid" != "0" -a "$sidekiq_status" != "0" ]; then - echo "Removing stale Sidekiq job dispatcher pid. This is most likely caused by Sidekiq crashing the last time it ran." - if ! rm "$sidekiq_pid_path"; then - echo "Unable to remove stale pid, exiting" - exit 1 - fi - fi -} - -## If no parts of the service is running, bail out. -exit_if_not_running(){ - check_stale_pids - if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then - echo "GitLab is not running." - exit - fi -} - -## Starts Unicorn and Sidekiq if they're not running. -start_gitlab() { - check_stale_pids - - if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then - echo -n "Starting both the GitLab Unicorn and Sidekiq" - elif [ "$web_status" != "0" ]; then - echo -n "Starting GitLab Unicorn" - elif [ "$sidekiq_status" != "0" ]; then - echo -n "Starting GitLab Sidekiq" - fi - - # Then check if the service is running. If it is: don't start again. - if [ "$web_status" = "0" ]; then - echo "The Unicorn web server already running with pid $wpid, not restarting." - else - # Remove old socket if it exists - rm -f "$socket_path"/gitlab.socket 2>/dev/null - # Start the web server - RAILS_ENV=$RAILS_ENV bin/web start - fi - - # If sidekiq is already running, don't start it again. - if [ "$sidekiq_status" = "0" ]; then - echo "The Sidekiq job dispatcher is already running with pid $spid, not restarting" - else - RAILS_ENV=$RAILS_ENV bin/background_jobs start & - fi - - # Wait for the pids to be planted - wait_for_pids - # Finally check the status to tell wether or not GitLab is running - print_status -} - -## Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them. -stop_gitlab() { - exit_if_not_running - - if [ "$web_status" = "0" -a "$sidekiq_status" = "0" ]; then - echo -n "Shutting down both Unicorn and Sidekiq" - elif [ "$web_status" = "0" ]; then - echo -n "Shutting down Unicorn" - elif [ "$sidekiq_status" = "0" ]; then - echo -n "Shutting down Sidekiq" - fi - - # If the Unicorn web server is running, tell it to stop; - if [ "$web_status" = "0" ]; then - RAILS_ENV=$RAILS_ENV bin/web stop - fi - # And do the same thing for the Sidekiq. - if [ "$sidekiq_status" = "0" ]; then - RAILS_ENV=$RAILS_ENV bin/background_jobs stop - fi - - # If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script. - while [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; do - sleep 1 - check_status - printf "." - if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then - printf "\n" - break - fi - done - - sleep 1 - # Cleaning up unused pids - rm "$web_server_pid_path" 2>/dev/null - # rm "$sidekiq_pid_path" # Sidekiq seems to be cleaning up it's own pid. - - print_status -} - -## Prints the status of GitLab and it's components. -print_status() { - check_status - if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then - echo "GitLab is not running." - return - fi - if [ "$web_status" = "0" ]; then - echo "The GitLab Unicorn web server with pid $wpid is running." - else - printf "The GitLab Unicorn web server is \033[31mnot running\033[0m.\n" - fi - if [ "$sidekiq_status" = "0" ]; then - echo "The GitLab Sidekiq job dispatcher with pid $spid is running." - else - printf "The GitLab Sidekiq job dispatcher is \033[31mnot running\033[0m.\n" - fi - if [ "$web_status" = "0" -a "$sidekiq_status" = "0" ]; then - printf "GitLab and all its components are \033[32mup and running\033[0m.\n" - fi -} - -## Tells unicorn to reload it's config and Sidekiq to restart -reload_gitlab(){ - exit_if_not_running - if [ "$wpid" = "0" ];then - echo "The GitLab Unicorn Web server is not running thus its configuration can't be reloaded." - exit 1 - fi - printf "Reloading GitLab Unicorn configuration... " - RAILS_ENV=$RAILS_ENV bin/web reload - echo "Done." - echo "Restarting GitLab Sidekiq since it isn't capable of reloading its config..." - RAILS_ENV=$RAILS_ENV bin/background_jobs restart - - wait_for_pids - print_status -} - -## Restarts Sidekiq and Unicorn. -restart_gitlab(){ - check_status - if [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; then - stop_gitlab - fi - start_gitlab -} - - -### Finally the input handling. - -case "$1" in - start) - start_gitlab - ;; - stop) - stop_gitlab - ;; - restart) - restart_gitlab - ;; - reload|force-reload) - reload_gitlab - ;; - status) - print_status - exit $gitlab_status - ;; - *) - echo "Usage: service gitlab {start|stop|restart|reload|status}" - exit 1 - ;; -esac - -exit diff --git a/lib/support/init.d/gitlab.default.example b/lib/support/init.d/gitlab.default.example deleted file mode 100755 index 9951bacedf5..00000000000 --- a/lib/support/init.d/gitlab.default.example +++ /dev/null @@ -1,31 +0,0 @@ -# Copy this lib/support/init.d/gitlab.default.example file to -# /etc/default/gitlab in order for it to apply to your system. - -# RAILS_ENV defines the type of installation that is running. -# Normal values are "production", "test" and "development". -RAILS_ENV="production" - -# app_user defines the user that GitLab is run as. -# The default is "git". -app_user="git" - -# app_root defines the folder in which gitlab and it's components are installed. -# The default is "/home/$app_user/gitlab" -app_root="/home/$app_user/gitlab" - -# pid_path defines a folder in which the gitlab and it's components place their pids. -# This variable is also used below to define the relevant pids for the gitlab components. -# The default is "$app_root/tmp/pids" -pid_path="$app_root/tmp/pids" - -# socket_path defines the folder in which gitlab places the sockets -#The default is "$app_root/tmp/sockets" -socket_path="$app_root/tmp/sockets" - -# web_server_pid_path defines the path in which to create the pid file fo the web_server -# The default is "$pid_path/unicorn.pid" -web_server_pid_path="$pid_path/unicorn.pid" - -# sidekiq_pid_path defines the path in which to create the pid file for sidekiq -# The default is "$pid_path/sidekiq.pid" -sidekiq_pid_path="$pid_path/sidekiq.pid" diff --git a/lib/support/logrotate/gitlab b/lib/support/logrotate/gitlab deleted file mode 100644 index d9b07b61ec3..00000000000 --- a/lib/support/logrotate/gitlab +++ /dev/null @@ -1,20 +0,0 @@ -# GitLab logrotate settings -# based on: http://stackoverflow.com/a/4883967 - -/home/git/gitlab/log/*.log { - daily - missingok - rotate 90 - compress - notifempty - copytruncate -} - -/home/git/gitlab-shell/gitlab-shell.log { - daily - missingok - rotate 90 - compress - notifempty - copytruncate -} diff --git a/lib/support/nginx/gitlab b/lib/support/nginx/gitlab deleted file mode 100644 index 62a4276536c..00000000000 --- a/lib/support/nginx/gitlab +++ /dev/null @@ -1,115 +0,0 @@ -## GitLab -## Contributors: randx, yin8086, sashkab, orkoden, axilleas, bbodenmiller, DouweM -## -## Lines starting with two hashes (##) are comments with information. -## Lines starting with one hash (#) are configuration parameters that can be uncommented. -## -################################## -## CHUNKED TRANSFER ## -################################## -## -## It is a known issue that Git-over-HTTP requires chunked transfer encoding [0] -## which is not supported by Nginx < 1.3.9 [1]. As a result, pushing a large object -## with Git (i.e. a single large file) can lead to a 411 error. In theory you can get -## around this by tweaking this configuration file and either: -## - installing an old version of Nginx with the chunkin module [2] compiled in, or -## - using a newer version of Nginx. -## -## At the time of writing we do not know if either of these theoretical solutions works. -## As a workaround users can use Git over SSH to push large files. -## -## [0] https://git.kernel.org/cgit/git/git.git/tree/Documentation/technical/http-protocol.txt#n99 -## [1] https://github.com/agentzh/chunkin-nginx-module#status -## [2] https://github.com/agentzh/chunkin-nginx-module -## -################################### -## configuration ## -################################### -## -## See installation.md#using-https for additional HTTPS configuration details. - -upstream gitlab { - server unix:/home/git/gitlab/tmp/sockets/gitlab.socket fail_timeout=0; -} - -## Normal HTTP host -server { - listen 0.0.0.0:80 default_server; - listen [::]:80 default_server; - server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com - server_tokens off; ## Don't show the nginx version number, a security best practice - root /home/git/gitlab/public; - - ## Increase this if you want to upload large attachments - ## Or if you want to accept large git objects over http - client_max_body_size 20m; - - ## See app/controllers/application_controller.rb for headers set - - ## Individual nginx logs for this GitLab vhost - access_log /var/log/nginx/gitlab_access.log; - error_log /var/log/nginx/gitlab_error.log; - - location / { - ## Serve static files from defined root folder. - ## @gitlab is a named location for the upstream fallback, see below. - try_files $uri $uri/index.html $uri.html @gitlab; - } - - ## We route uploads through GitLab to prevent XSS and enforce access control. - location /uploads/ { - ## If you use HTTPS make sure you disable gzip compression - ## to be safe against BREACH attack. - # gzip off; - - ## https://github.com/gitlabhq/gitlabhq/issues/694 - ## Some requests take more than 30 seconds. - proxy_read_timeout 300; - proxy_connect_timeout 300; - proxy_redirect off; - - proxy_set_header Host $http_host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_set_header X-Frame-Options SAMEORIGIN; - - proxy_pass http://gitlab; - } - - ## If a file, which is not found in the root folder is requested, - ## then the proxy passes the request to the upsteam (gitlab unicorn). - location @gitlab { - ## If you use HTTPS make sure you disable gzip compression - ## to be safe against BREACH attack. - # gzip off; - - ## https://github.com/gitlabhq/gitlabhq/issues/694 - ## Some requests take more than 30 seconds. - proxy_read_timeout 300; - proxy_connect_timeout 300; - proxy_redirect off; - - proxy_set_header Host $http_host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_set_header X-Frame-Options SAMEORIGIN; - - proxy_pass http://gitlab; - } - - ## Enable gzip compression as per rails guide: - ## http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression - ## WARNING: If you are using relative urls remove the block below - ## See config/application.rb under "Relative url support" for the list of - ## other files that need to be changed for relative url support - location ~ ^/(assets)/ { - root /home/git/gitlab/public; - gzip_static on; # to serve pre-gzipped version - expires max; - add_header Cache-Control public; - } - - error_page 502 /502.html; -} diff --git a/lib/support/nginx/gitlab-ssl b/lib/support/nginx/gitlab-ssl deleted file mode 100644 index 2aefc944698..00000000000 --- a/lib/support/nginx/gitlab-ssl +++ /dev/null @@ -1,162 +0,0 @@ -## GitLab -## Contributors: randx, yin8086, sashkab, orkoden, axilleas, bbodenmiller, DouweM -## -## Modified from nginx http version -## Modified from http://blog.phusion.nl/2012/04/21/tutorial-setting-up-gitlab-on-debian-6/ -## Modified from https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html -## -## Lines starting with two hashes (##) are comments with information. -## Lines starting with one hash (#) are configuration parameters that can be uncommented. -## -################################## -## CHUNKED TRANSFER ## -################################## -## -## It is a known issue that Git-over-HTTP requires chunked transfer encoding [0] -## which is not supported by Nginx < 1.3.9 [1]. As a result, pushing a large object -## with Git (i.e. a single large file) can lead to a 411 error. In theory you can get -## around this by tweaking this configuration file and either: -## - installing an old version of Nginx with the chunkin module [2] compiled in, or -## - using a newer version of Nginx. -## -## At the time of writing we do not know if either of these theoretical solutions works. -## As a workaround users can use Git over SSH to push large files. -## -## [0] https://git.kernel.org/cgit/git/git.git/tree/Documentation/technical/http-protocol.txt#n99 -## [1] https://github.com/agentzh/chunkin-nginx-module#status -## [2] https://github.com/agentzh/chunkin-nginx-module -## -################################### -## configuration ## -################################### -## -## See installation.md#using-https for additional HTTPS configuration details. - -upstream gitlab { - server unix:/home/git/gitlab/tmp/sockets/gitlab.socket fail_timeout=0; -} - -## Redirects all HTTP traffic to the HTTPS host -server { - listen 0.0.0.0:80; - listen [::]:80 ipv6only=on default_server; - server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com - server_tokens off; ## Don't show the nginx version number, a security best practice - return 301 https://$server_name$request_uri; - access_log /var/log/nginx/gitlab_access.log; - error_log /var/log/nginx/gitlab_error.log; -} - - -## HTTPS host -server { - listen 0.0.0.0:443 ssl; - listen [::]:443 ipv6only=on ssl default_server; - server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com - server_tokens off; ## Don't show the nginx version number, a security best practice - root /home/git/gitlab/public; - - ## Increase this if you want to upload large attachments - ## Or if you want to accept large git objects over http - client_max_body_size 20m; - - ## Strong SSL Security - ## https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html & https://cipherli.st/ - ssl on; - ssl_certificate /etc/nginx/ssl/gitlab.crt; - ssl_certificate_key /etc/nginx/ssl/gitlab.key; - - # GitLab needs backwards compatible ciphers to retain compatibility with Java IDEs - ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4"; - ssl_protocols TLSv1 TLSv1.1 TLSv1.2; - ssl_prefer_server_ciphers on; - ssl_session_cache shared:SSL:10m; - ssl_session_timeout 5m; - - ## See app/controllers/application_controller.rb for headers set - - ## [Optional] If your certficate has OCSP, enable OCSP stapling to reduce the overhead and latency of running SSL. - ## Replace with your ssl_trusted_certificate. For more info see: - ## - https://medium.com/devops-programming/4445f4862461 - ## - https://www.ruby-forum.com/topic/4419319 - ## - https://www.digitalocean.com/community/tutorials/how-to-configure-ocsp-stapling-on-apache-and-nginx - # ssl_stapling on; - # ssl_stapling_verify on; - # ssl_trusted_certificate /etc/nginx/ssl/stapling.trusted.crt; - # resolver 208.67.222.222 208.67.222.220 valid=300s; # Can change to your DNS resolver if desired - # resolver_timeout 5s; - - ## [Optional] Generate a stronger DHE parameter: - ## sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 4096 - ## - # ssl_dhparam /etc/ssl/certs/dhparam.pem; - - ## Individual nginx logs for this GitLab vhost - access_log /var/log/nginx/gitlab_access.log; - error_log /var/log/nginx/gitlab_error.log; - - location / { - ## Serve static files from defined root folder. - ## @gitlab is a named location for the upstream fallback, see below. - try_files $uri $uri/index.html $uri.html @gitlab; - } - - ## We route uploads through GitLab to prevent XSS and enforce access control. - location /uploads/ { - ## If you use HTTPS make sure you disable gzip compression - ## to be safe against BREACH attack. - gzip off; - - ## https://github.com/gitlabhq/gitlabhq/issues/694 - ## Some requests take more than 30 seconds. - proxy_read_timeout 300; - proxy_connect_timeout 300; - proxy_redirect off; - - proxy_set_header Host $http_host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-Ssl on; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_set_header X-Frame-Options SAMEORIGIN; - - proxy_pass http://gitlab; - } - - ## If a file, which is not found in the root folder is requested, - ## then the proxy passes the request to the upsteam (gitlab unicorn). - location @gitlab { - ## If you use HTTPS make sure you disable gzip compression - ## to be safe against BREACH attack. - gzip off; - - ## https://github.com/gitlabhq/gitlabhq/issues/694 - ## Some requests take more than 30 seconds. - proxy_read_timeout 300; - proxy_connect_timeout 300; - proxy_redirect off; - - proxy_set_header Host $http_host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-Ssl on; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_set_header X-Frame-Options SAMEORIGIN; - - proxy_pass http://gitlab; - } - - ## Enable gzip compression as per rails guide: - ## http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression - ## WARNING: If you are using relative urls remove the block below - ## See config/application.rb under "Relative url support" for the list of - ## other files that need to be changed for relative url support - location ~ ^/(assets)/ { - root /home/git/gitlab/public; - gzip_static on; # to serve pre-gzipped version - expires max; - add_header Cache-Control public; - } - - error_page 502 /502.html; -} diff --git a/lib/tasks/.gitkeep b/lib/tasks/.gitkeep deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/lib/tasks/brakeman.rake b/lib/tasks/brakeman.rake deleted file mode 100644 index 3a225801ff2..00000000000 --- a/lib/tasks/brakeman.rake +++ /dev/null @@ -1,9 +0,0 @@ -desc 'Security check via brakeman' -task :brakeman do - if system("brakeman --skip-files lib/backup/repository.rb -w3 -z") - puts 'Security check succeed' - else - puts 'Security check failed' - exit 1 - end -end diff --git a/lib/tasks/cache.rake b/lib/tasks/cache.rake deleted file mode 100644 index 753a5a11070..00000000000 --- a/lib/tasks/cache.rake +++ /dev/null @@ -1,11 +0,0 @@ -namespace :cache do - desc "GITLAB | Clear redis cache" - task :clear => :environment do - # Hack into Rails.cache until https://github.com/redis-store/redis-store/pull/225 - # is accepted (I hope) and we can update the redis-store gem. - redis_store = Rails.cache.instance_variable_get(:@data) - redis_store.keys.each_slice(1000) do |key_slice| - redis_store.del(*key_slice) - end - end -end diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake deleted file mode 100644 index 058c7417040..00000000000 --- a/lib/tasks/dev.rake +++ /dev/null @@ -1,10 +0,0 @@ -task dev: ["dev:setup"] - -namespace :dev do - desc "GITLAB | Setup developer environment (db, fixtures)" - task :setup => :environment do - ENV['force'] = 'yes' - Rake::Task["gitlab:setup"].invoke - Rake::Task["gitlab:shell:setup"].invoke - end -end diff --git a/lib/tasks/gitlab/backup.rake b/lib/tasks/gitlab/backup.rake deleted file mode 100644 index 84445b3bf2f..00000000000 --- a/lib/tasks/gitlab/backup.rake +++ /dev/null @@ -1,106 +0,0 @@ -require 'active_record/fixtures' - -namespace :gitlab do - namespace :backup do - # Create backup of GitLab system - desc "GITLAB | Create a backup of the GitLab system" - task create: :environment do - warn_user_is_not_gitlab - configure_cron_mode - - Rake::Task["gitlab:backup:db:create"].invoke - Rake::Task["gitlab:backup:repo:create"].invoke - Rake::Task["gitlab:backup:uploads:create"].invoke - - backup = Backup::Manager.new - backup.pack - backup.cleanup - backup.remove_old - end - - # Restore backup of GitLab system - desc "GITLAB | Restore a previously created backup" - task restore: :environment do - warn_user_is_not_gitlab - configure_cron_mode - - backup = Backup::Manager.new - backup.unpack - - Rake::Task["gitlab:backup:db:restore"].invoke unless backup.skipped?("db") - Rake::Task["gitlab:backup:repo:restore"].invoke unless backup.skipped?("repositories") - Rake::Task["gitlab:backup:uploads:restore"].invoke unless backup.skipped?("uploads") - Rake::Task["gitlab:shell:setup"].invoke - - backup.cleanup - end - - namespace :repo do - task create: :environment do - $progress.puts "Dumping repositories ...".blue - - if ENV["SKIP"] && ENV["SKIP"].include?("repositories") - $progress.puts "[SKIPPED]".cyan - else - Backup::Repository.new.dump - $progress.puts "done".green - end - end - - task restore: :environment do - $progress.puts "Restoring repositories ...".blue - Backup::Repository.new.restore - $progress.puts "done".green - end - end - - namespace :db do - task create: :environment do - $progress.puts "Dumping database ... ".blue - - if ENV["SKIP"] && ENV["SKIP"].include?("db") - $progress.puts "[SKIPPED]".cyan - else - Backup::Database.new.dump - $progress.puts "done".green - end - end - - task restore: :environment do - $progress.puts "Restoring database ... ".blue - Backup::Database.new.restore - $progress.puts "done".green - end - end - - namespace :uploads do - task create: :environment do - $progress.puts "Dumping uploads ... ".blue - - if ENV["SKIP"] && ENV["SKIP"].include?("uploads") - $progress.puts "[SKIPPED]".cyan - else - Backup::Uploads.new.dump - $progress.puts "done".green - end - end - - task restore: :environment do - $progress.puts "Restoring uploads ... ".blue - Backup::Uploads.new.restore - $progress.puts "done".green - end - end - - def configure_cron_mode - if ENV['CRON'] - # We need an object we can say 'puts' and 'print' to; let's use a - # StringIO. - require 'stringio' - $progress = StringIO.new - else - $progress = $stdout - end - end - end # namespace end: backup -end # namespace end: gitlab diff --git a/lib/tasks/gitlab/bulk_add_permission.rake b/lib/tasks/gitlab/bulk_add_permission.rake deleted file mode 100644 index 3d8c171dfa3..00000000000 --- a/lib/tasks/gitlab/bulk_add_permission.rake +++ /dev/null @@ -1,48 +0,0 @@ -namespace :gitlab do - namespace :import do - desc "GITLAB | Add all users to all projects (admin users are added as masters)" - task all_users_to_all_projects: :environment do |t, args| - user_ids = User.where(admin: false).pluck(:id) - admin_ids = User.where(admin: true).pluck(:id) - projects_ids = Project.pluck(:id) - - puts "Importing #{user_ids.size} users into #{projects_ids.size} projects" - ProjectMember.add_users_into_projects(projects_ids, user_ids, ProjectMember::DEVELOPER) - - puts "Importing #{admin_ids.size} admins into #{projects_ids.size} projects" - ProjectMember.add_users_into_projects(projects_ids, admin_ids, ProjectMember::MASTER) - end - - desc "GITLAB | Add a specific user to all projects (as a developer)" - task :user_to_projects, [:email] => :environment do |t, args| - user = User.find_by(email: args.email) - project_ids = Project.pluck(:id) - puts "Importing #{user.email} users into #{project_ids.size} projects" - ProjectMember.add_users_into_projects(project_ids, Array.wrap(user.id), ProjectMember::DEVELOPER) - end - - desc "GITLAB | Add all users to all groups (admin users are added as owners)" - task all_users_to_all_groups: :environment do |t, args| - user_ids = User.where(admin: false).pluck(:id) - admin_ids = User.where(admin: true).pluck(:id) - groups = Group.all - - puts "Importing #{user_ids.size} users into #{groups.size} groups" - puts "Importing #{admin_ids.size} admins into #{groups.size} groups" - groups.each do |group| - group.add_users(user_ids, GroupMember::DEVELOPER) - group.add_users(admin_ids, GroupMember::OWNER) - end - end - - desc "GITLAB | Add a specific user to all groups (as a developer)" - task :user_to_groups, [:email] => :environment do |t, args| - user = User.find_by_email args.email - groups = Group.all - puts "Importing #{user.email} users into #{groups.size} groups" - groups.each do |group| - group.add_users(Array.wrap(user.id), GroupMember::DEVELOPER) - end - end - end -end diff --git a/lib/tasks/gitlab/cleanup.rake b/lib/tasks/gitlab/cleanup.rake deleted file mode 100644 index 3c9802a0be4..00000000000 --- a/lib/tasks/gitlab/cleanup.rake +++ /dev/null @@ -1,113 +0,0 @@ -namespace :gitlab do - namespace :cleanup do - desc "GITLAB | Cleanup | Clean namespaces" - task dirs: :environment do - warn_user_is_not_gitlab - remove_flag = ENV['REMOVE'] - - - namespaces = Namespace.pluck(:path) - git_base_path = Gitlab.config.gitlab_shell.repos_path - all_dirs = Dir.glob(git_base_path + '/*') - - puts git_base_path.yellow - puts "Looking for directories to remove... " - - all_dirs.reject! do |dir| - # skip if git repo - dir =~ /.git$/ - end - - all_dirs.reject! do |dir| - dir_name = File.basename dir - - # skip if namespace present - namespaces.include?(dir_name) - end - - all_dirs.each do |dir_path| - - if remove_flag - if FileUtils.rm_rf dir_path - puts "Removed...#{dir_path}".red - else - puts "Cannot remove #{dir_path}".red - end - else - puts "Can be removed: #{dir_path}".red - end - end - - unless remove_flag - puts "To cleanup this directories run this command with REMOVE=true".yellow - end - end - - desc "GITLAB | Cleanup | Clean repositories" - task repos: :environment do - warn_user_is_not_gitlab - remove_flag = ENV['REMOVE'] - - git_base_path = Gitlab.config.gitlab_shell.repos_path - all_dirs = Dir.glob(git_base_path + '/*') - - global_projects = Project.where(namespace_id: nil).pluck(:path) - - puts git_base_path.yellow - puts "Looking for global repos to remove... " - - # skip non git repo - all_dirs.select! do |dir| - dir =~ /.git$/ - end - - # skip existing repos - all_dirs.reject! do |dir| - repo_name = File.basename dir - path = repo_name.gsub(/\.git$/, "") - global_projects.include?(path) - end - - all_dirs.each do |dir_path| - if remove_flag - if FileUtils.rm_rf dir_path - puts "Removed...#{dir_path}".red - else - puts "Cannot remove #{dir_path}".red - end - else - puts "Can be removed: #{dir_path}".red - end - end - - unless remove_flag - puts "To cleanup this directories run this command with REMOVE=true".yellow - end - end - - desc "GITLAB | Cleanup | Block users that have been removed in LDAP" - task block_removed_ldap_users: :environment do - warn_user_is_not_gitlab - block_flag = ENV['BLOCK'] - - User.find_each do |user| - next unless user.ldap_user? - print "#{user.name} (#{user.ldap_identity.extern_uid}) ..." - if Gitlab::LDAP::Access.allowed?(user) - puts " [OK]".green - else - if block_flag - user.block! unless user.blocked? - puts " [BLOCKED]".red - else - puts " [NOT IN LDAP]".yellow - end - end - end - - unless block_flag - puts "To block these users run this command with BLOCK=true".yellow - end - end - end -end diff --git a/lib/tasks/gitlab/db/drop_all_postgres_sequences.rake b/lib/tasks/gitlab/db/drop_all_postgres_sequences.rake deleted file mode 100644 index e9cf0a9b5e8..00000000000 --- a/lib/tasks/gitlab/db/drop_all_postgres_sequences.rake +++ /dev/null @@ -1,10 +0,0 @@ -namespace :gitlab do - namespace :db do - task drop_all_postgres_sequences: :environment do - connection = ActiveRecord::Base.connection - connection.execute("SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';").each do |sequence| - connection.execute("DROP SEQUENCE #{sequence['relname']}") - end - end - end -end diff --git a/lib/tasks/gitlab/db/drop_all_tables.rake b/lib/tasks/gitlab/db/drop_all_tables.rake deleted file mode 100644 index a66030ab93a..00000000000 --- a/lib/tasks/gitlab/db/drop_all_tables.rake +++ /dev/null @@ -1,10 +0,0 @@ -namespace :gitlab do - namespace :db do - task drop_all_tables: :environment do - connection = ActiveRecord::Base.connection - connection.tables.each do |table| - connection.drop_table(table) - end - end - end -end diff --git a/lib/tasks/gitlab/enable_automerge.rake b/lib/tasks/gitlab/enable_automerge.rake deleted file mode 100644 index aa9869daf2f..00000000000 --- a/lib/tasks/gitlab/enable_automerge.rake +++ /dev/null @@ -1,39 +0,0 @@ -namespace :gitlab do - namespace :satellites do - desc "GITLAB | Create satellite repos" - task create: :environment do - create_satellites - end - end - - def create_satellites - warn_user_is_not_gitlab - - print "Creating satellites for ..." - unless Project.count > 0 - puts "skipping, because you have no projects".magenta - return - end - puts "" - - Project.find_each(batch_size: 100) do |project| - print "#{project.name_with_namespace.yellow} ... " - - unless project.repo_exists? - puts "skipping, because the repo is empty".magenta - next - end - - if project.satellite.exists? - puts "exists already".green - else - print "\n... " - if project.satellite.create - puts "created".green - else - puts "error".red - end - end - end - end -end diff --git a/lib/tasks/gitlab/generate_docs.rake b/lib/tasks/gitlab/generate_docs.rake deleted file mode 100644 index 332cd61f84c..00000000000 --- a/lib/tasks/gitlab/generate_docs.rake +++ /dev/null @@ -1,7 +0,0 @@ -namespace :gitlab do - desc "GITLAB | Generate sdocs for project" - task generate_docs: :environment do - system(*%W(bundle exec sdoc -o doc/code app lib)) - end -end - diff --git a/lib/tasks/gitlab/import.rake b/lib/tasks/gitlab/import.rake deleted file mode 100644 index 20abb2fa500..00000000000 --- a/lib/tasks/gitlab/import.rake +++ /dev/null @@ -1,77 +0,0 @@ -namespace :gitlab do - namespace :import do - # How to use: - # - # 1. copy the bare repos under the repos_path (commonly /home/git/repositories) - # 2. run: bundle exec rake gitlab:import:repos RAILS_ENV=production - # - # Notes: - # * The project owner will set to the first administator of the system - # * Existing projects will be skipped - # - desc "GITLAB | Import bare repositories from gitlab_shell -> repos_path into GitLab project instance" - task repos: :environment do - - git_base_path = Gitlab.config.gitlab_shell.repos_path - repos_to_import = Dir.glob(git_base_path + '/**/*.git') - - repos_to_import.each do |repo_path| - # strip repo base path - repo_path[0..git_base_path.length] = '' - - path = repo_path.sub(/\.git$/, '') - group_name, name = File.split(path) - group_name = nil if group_name == '.' - - puts "Processing #{repo_path}".yellow - - if path.end_with?('.wiki') - puts " * Skipping wiki repo" - next - end - - project = Project.find_with_namespace(path) - - if project - puts " * #{project.name} (#{repo_path}) exists" - else - user = User.admins.first - - project_params = { - name: name, - path: name - } - - # find group namespace - if group_name - group = Namespace.find_by(path: group_name) - # create group namespace - unless group - group = Group.new(:name => group_name) - group.path = group_name - group.owner = user - if group.save - puts " * Created Group #{group.name} (#{group.id})".green - else - puts " * Failed trying to create group #{group.name}".red - end - end - # set project group - project_params[:namespace_id] = group.id - end - - project = Projects::CreateService.new(user, project_params).execute - - if project.valid? - puts " * Created #{project.name} (#{repo_path})".green - else - puts " * Failed trying to create #{project.name} (#{repo_path})".red - puts " Validation Errors: #{project.errors.messages}".red - end - end - end - - puts "Done!".green - end - end -end diff --git a/lib/tasks/gitlab/info.rake b/lib/tasks/gitlab/info.rake deleted file mode 100644 index 72452e1d8ea..00000000000 --- a/lib/tasks/gitlab/info.rake +++ /dev/null @@ -1,72 +0,0 @@ -namespace :gitlab do - namespace :env do - desc "GITLAB | Show information about GitLab and its environment" - task info: :environment do - - # check if there is an RVM environment - rvm_version = run_and_match(%W(rvm --version), /[\d\.]+/).try(:to_s) - # check Ruby version - ruby_version = run_and_match(%W(ruby --version), /[\d\.p]+/).try(:to_s) - # check Gem version - gem_version = run(%W(gem --version)) - # check Bundler version - bunder_version = run_and_match(%W(bundle --version), /[\d\.]+/).try(:to_s) - # check Bundler version - rake_version = run_and_match(%W(rake --version), /[\d\.]+/).try(:to_s) - - puts "" - puts "System information".yellow - puts "System:\t\t#{os_name || "unknown".red}" - puts "Current User:\t#{run(%W(whoami))}" - puts "Using RVM:\t#{rvm_version.present? ? "yes".green : "no"}" - puts "RVM Version:\t#{rvm_version}" if rvm_version.present? - puts "Ruby Version:\t#{ruby_version || "unknown".red}" - puts "Gem Version:\t#{gem_version || "unknown".red}" - puts "Bundler Version:#{bunder_version || "unknown".red}" - puts "Rake Version:\t#{rake_version || "unknown".red}" - puts "Sidekiq Version:#{Sidekiq::VERSION}" - - - # check database adapter - database_adapter = ActiveRecord::Base.connection.adapter_name.downcase - - project = Project.new(path: "some-project") - project.path = "some-project" - # construct clone URLs - http_clone_url = project.http_url_to_repo - ssh_clone_url = project.ssh_url_to_repo - - omniauth_providers = Gitlab.config.omniauth.providers - omniauth_providers.map! { |provider| provider['name'] } - - puts "" - puts "GitLab information".yellow - puts "Version:\t#{Gitlab::VERSION}" - puts "Revision:\t#{Gitlab::REVISION}" - puts "Directory:\t#{Rails.root}" - puts "DB Adapter:\t#{database_adapter}" - puts "URL:\t\t#{Gitlab.config.gitlab.url}" - puts "HTTP Clone URL:\t#{http_clone_url}" - puts "SSH Clone URL:\t#{ssh_clone_url}" - puts "Using LDAP:\t#{Gitlab.config.ldap.enabled ? "yes".green : "no"}" - puts "Using Omniauth:\t#{Gitlab.config.omniauth.enabled ? "yes".green : "no"}" - puts "Omniauth Providers: #{omniauth_providers.map(&:magenta).join(', ')}" if Gitlab.config.omniauth.enabled - - - - # check Gitolite version - gitlab_shell_version_file = "#{Gitlab.config.gitlab_shell.hooks_path}/../VERSION" - if File.readable?(gitlab_shell_version_file) - gitlab_shell_version = File.read(gitlab_shell_version_file) - end - - puts "" - puts "GitLab Shell".yellow - puts "Version:\t#{gitlab_shell_version || "unknown".red}" - puts "Repositories:\t#{Gitlab.config.gitlab_shell.repos_path}" - puts "Hooks:\t\t#{Gitlab.config.gitlab_shell.hooks_path}" - puts "Git:\t\t#{Gitlab.config.git.bin_path}" - - end - end -end diff --git a/lib/tasks/gitlab/mail_google_schema_whitelisting.rake b/lib/tasks/gitlab/mail_google_schema_whitelisting.rake deleted file mode 100644 index 102c6ae55d5..00000000000 --- a/lib/tasks/gitlab/mail_google_schema_whitelisting.rake +++ /dev/null @@ -1,73 +0,0 @@ -require "#{Rails.root}/app/helpers/emails_helper" -require 'action_view/helpers' -extend ActionView::Helpers - -include ActionView::Context -include EmailsHelper - -namespace :gitlab do - desc "Email google whitelisting email with example email for actions in inbox" - task mail_google_schema_whitelisting: :environment do - subject = "Rails | Implemented feature" - url = "#{Gitlab.config.gitlab.url}/base/rails-project/issues/#{rand(1..100)}#note_#{rand(10..1000)}" - schema = email_action(url) - body = email_template(schema, url) - mail = Notify.test_email("schema.whitelisting+sample@gmail.com", subject, body.html_safe) - if send_now - mail.deliver - else - puts "WOULD SEND:" - end - puts mail - end - - def email_template(schema, url) - " - - - - GitLab - - - - - -
-
-

I like it :+1:

-
-
- - - - " - end - - def send_now - if ENV['SEND'] == "true" - true - else - false - end - end -end diff --git a/lib/tasks/gitlab/setup.rake b/lib/tasks/gitlab/setup.rake deleted file mode 100644 index 8b4ccdfc3fe..00000000000 --- a/lib/tasks/gitlab/setup.rake +++ /dev/null @@ -1,24 +0,0 @@ -namespace :gitlab do - desc "GITLAB | Setup production application" - task setup: :environment do - setup_db - end - - def setup_db - warn_user_is_not_gitlab - - unless ENV['force'] == 'yes' - puts "This will create the necessary database tables and seed the database." - puts "You will lose any previous data stored in the database." - ask_to_continue - puts "" - end - - Rake::Task["db:setup"].invoke - Rake::Task["add_limits_mysql"].invoke - Rake::Task["db:seed_fu"].invoke - rescue Gitlab::TaskAbortedByUserError - puts "Quitting...".red - exit 1 - end -end diff --git a/lib/tasks/gitlab/shell.rake b/lib/tasks/gitlab/shell.rake deleted file mode 100644 index e835d6cb9b7..00000000000 --- a/lib/tasks/gitlab/shell.rake +++ /dev/null @@ -1,138 +0,0 @@ -namespace :gitlab do - namespace :shell do - desc "GITLAB | Install or upgrade gitlab-shell" - task :install, [:tag, :repo] => :environment do |t, args| - warn_user_is_not_gitlab - - default_version = Gitlab::Shell.version_required - args.with_defaults(tag: 'v' + default_version, repo: "https://gitlab.com/gitlab-org/gitlab-shell.git") - - user = Gitlab.config.gitlab.user - home_dir = Rails.env.test? ? Rails.root.join('tmp/tests') : Gitlab.config.gitlab.user_home - gitlab_url = Gitlab.config.gitlab.url - # gitlab-shell requires a / at the end of the url - gitlab_url += '/' unless gitlab_url.end_with?('/') - repos_path = Gitlab.config.gitlab_shell.repos_path - target_dir = Gitlab.config.gitlab_shell.path - - # Clone if needed - unless File.directory?(target_dir) - system(*%W(git clone -- #{args.repo} #{target_dir})) - end - - # Make sure we're on the right tag - Dir.chdir(target_dir) do - # First try to checkout without fetching - # to avoid stalling tests if the Internet is down. - reseted = reset_to_commit(args) - - unless reseted - system(*%W(git fetch origin)) - reset_to_commit(args) - end - - config = { - user: user, - gitlab_url: gitlab_url, - http_settings: {self_signed_cert: false}.stringify_keys, - repos_path: repos_path, - auth_file: File.join(home_dir, ".ssh", "authorized_keys"), - redis: { - bin: %x{which redis-cli}.chomp, - namespace: "resque:gitlab" - }.stringify_keys, - log_level: "INFO", - audit_usernames: false - }.stringify_keys - - redis_url = URI.parse(ENV['REDIS_URL'] || "redis://localhost:6379") - - if redis_url.scheme == 'unix' - config['redis']['socket'] = redis_url.path - else - config['redis']['host'] = redis_url.host - config['redis']['port'] = redis_url.port - end - - # Generate config.yml based on existing gitlab settings - File.open("config.yml", "w+") {|f| f.puts config.to_yaml} - - # Launch installation process - system(*%W(bin/install)) - end - - # Required for debian packaging with PKGR: Setup .ssh/environment with - # the current PATH, so that the correct ruby version gets loaded - # Requires to set "PermitUserEnvironment yes" in sshd config (should not - # be an issue since it is more than likely that there are no "normal" - # user accounts on a gitlab server). The alternative is for the admin to - # install a ruby (1.9.3+) in the global path. - File.open(File.join(home_dir, ".ssh", "environment"), "w+") do |f| - f.puts "PATH=#{ENV['PATH']}" - end - end - - desc "GITLAB | Setup gitlab-shell" - task setup: :environment do - setup - end - - desc "GITLAB | Build missing projects" - task build_missing_projects: :environment do - Project.find_each(batch_size: 1000) do |project| - path_to_repo = project.repository.path_to_repo - if File.exists?(path_to_repo) - print '-' - else - if Gitlab::Shell.new.add_repository(project.path_with_namespace) - print '.' - else - print 'F' - end - end - end - end - end - - def setup - warn_user_is_not_gitlab - - unless ENV['force'] == 'yes' - puts "This will rebuild an authorized_keys file." - puts "You will lose any data stored in authorized_keys file." - ask_to_continue - puts "" - end - - Gitlab::Shell.new.remove_all_keys - - Gitlab::Shell.new.batch_add_keys do |adder| - Key.find_each(batch_size: 1000) do |key| - adder.add_key(key.shell_id, key.key) - print '.' - end - end - puts "" - - unless $?.success? - puts "Failed to add keys...".red - exit 1 - end - - rescue Gitlab::TaskAbortedByUserError - puts "Quitting...".red - exit 1 - end - - def reset_to_commit(args) - tag, status = Gitlab::Popen.popen(%W(git describe -- #{args.tag})) - - unless status.zero? - tag, status = Gitlab::Popen.popen(%W(git describe -- origin/#{args.tag})) - end - - tag = tag.strip - system(*%W(git reset --hard #{tag})) - end -end - diff --git a/lib/tasks/gitlab/sidekiq.rake b/lib/tasks/gitlab/sidekiq.rake deleted file mode 100644 index 7e2a6668e59..00000000000 --- a/lib/tasks/gitlab/sidekiq.rake +++ /dev/null @@ -1,47 +0,0 @@ -namespace :gitlab do - namespace :sidekiq do - QUEUE = 'queue:post_receive' - - desc 'Drop all Sidekiq PostReceive jobs for a given project' - task :drop_post_receive , [:project] => :environment do |t, args| - unless args.project.present? - abort "Please specify the project you want to drop PostReceive jobs for:\n rake gitlab:sidekiq:drop_post_receive[group/project]" - end - project_path = Project.find_with_namespace(args.project).repository.path_to_repo - - Sidekiq.redis do |redis| - unless redis.exists(QUEUE) - abort "Queue #{QUEUE} is empty" - end - - temp_queue = "#{QUEUE}_#{Time.now.to_i}" - redis.rename(QUEUE, temp_queue) - - # At this point, then post_receive queue is empty. It may be receiving - # new jobs already. We will repopulate it with the old jobs, skipping the - # ones we want to drop. - dropped = 0 - while (job = redis.lpop(temp_queue)) do - if repo_path(job) == project_path - dropped += 1 - else - redis.rpush(QUEUE, job) - end - end - # The temp_queue will delete itself after we have popped all elements - # from it - - puts "Dropped #{dropped} jobs containing #{project_path} from #{QUEUE}" - end - end - - def repo_path(job) - job_args = JSON.parse(job)['args'] - if job_args - job_args.first - else - nil - end - end - end -end diff --git a/lib/tasks/gitlab/task_helpers.rake b/lib/tasks/gitlab/task_helpers.rake deleted file mode 100644 index 14a130be2ca..00000000000 --- a/lib/tasks/gitlab/task_helpers.rake +++ /dev/null @@ -1,131 +0,0 @@ -module Gitlab - class TaskAbortedByUserError < StandardError; end -end - -unless STDOUT.isatty - module Colored - extend self - - def colorize(string, options={}) - string - end - end -end - -namespace :gitlab do - - # Ask if the user wants to continue - # - # Returns "yes" the user chose to continue - # Raises Gitlab::TaskAbortedByUserError if the user chose *not* to continue - def ask_to_continue - answer = prompt("Do you want to continue (yes/no)? ".blue, %w{yes no}) - raise Gitlab::TaskAbortedByUserError unless answer == "yes" - end - - # Check which OS is running - # - # It will primarily use lsb_relase to determine the OS. - # It has fallbacks to Debian, SuSE, OS X and systems running systemd. - def os_name - os_name = run(%W(lsb_release -irs)) - os_name ||= if File.readable?('/etc/system-release') - File.read('/etc/system-release') - end - os_name ||= if File.readable?('/etc/debian_version') - debian_version = File.read('/etc/debian_version') - "Debian #{debian_version}" - end - os_name ||= if File.readable?('/etc/SuSE-release') - File.read('/etc/SuSE-release') - end - os_name ||= if os_x_version = run(%W(sw_vers -productVersion)) - "Mac OS X #{os_x_version}" - end - os_name ||= if File.readable?('/etc/os-release') - File.read('/etc/os-release').match(/PRETTY_NAME=\"(.+)\"/)[1] - end - os_name.try(:squish!) - end - - # Prompt the user to input something - # - # message - the message to display before input - # choices - array of strings of acceptable answers or nil for any answer - # - # Returns the user's answer - def prompt(message, choices = nil) - begin - print(message) - answer = STDIN.gets.chomp - end while choices.present? && !choices.include?(answer) - answer - end - - # Runs the given command and matches the output against the given pattern - # - # Returns nil if nothing matched - # Returns the MatchData if the pattern matched - # - # see also #run - # see also String#match - def run_and_match(command, regexp) - run(command).try(:match, regexp) - end - - # Runs the given command - # - # Returns nil if the command was not found - # Returns the output of the command otherwise - # - # see also #run_and_match - def run(command) - output, _ = Gitlab::Popen.popen(command) - output - rescue Errno::ENOENT - '' # if the command does not exist, return an empty string - end - - def uid_for(user_name) - run(%W(id -u #{user_name})).chomp.to_i - end - - def gid_for(group_name) - begin - Etc.getgrnam(group_name).gid - rescue ArgumentError # no group - "group #{group_name} doesn't exist" - end - end - - def warn_user_is_not_gitlab - unless @warned_user_not_gitlab - gitlab_user = Gitlab.config.gitlab.user - current_user = run(%W(whoami)).chomp - unless current_user == gitlab_user - puts "#{Colored.color(:black)+Colored.color(:on_yellow)} Warning #{Colored.extra(:clear)}" - puts " You are running as user #{current_user.magenta}, we hope you know what you are doing." - puts " Things may work\/fail for the wrong reasons." - puts " For correct results you should run this as user #{gitlab_user.magenta}." - puts "" - end - @warned_user_not_gitlab = true - end - end - - # Tries to configure git itself - # - # Returns true if all subcommands were successfull (according to their exit code) - # Returns false if any or all subcommands failed. - def auto_fix_git_config(options) - if !@warned_user_not_gitlab && options['user.email'] != 'example@example.com' # default email should be overridden? - command_success = options.map do |name, value| - system(%W(#{Gitlab.config.git.bin_path} config --global #{name} #{value})) - end - - command_success.all? - else - false - end - end -end diff --git a/lib/tasks/gitlab/test.rake b/lib/tasks/gitlab/test.rake deleted file mode 100644 index b4c0ae3ff79..00000000000 --- a/lib/tasks/gitlab/test.rake +++ /dev/null @@ -1,16 +0,0 @@ -namespace :gitlab do - desc "GITLAB | Run all tests" - task :test do - cmds = [ - %W(rake brakeman), - %W(rake rubocop), - %W(rake spinach), - %W(rake spec), - %W(rake jasmine:ci) - ] - - cmds.each do |cmd| - system({'RAILS_ENV' => 'test', 'force' => 'yes'}, *cmd) or raise("#{cmd} failed!") - end - end -end diff --git a/lib/tasks/gitlab/web_hook.rake b/lib/tasks/gitlab/web_hook.rake deleted file mode 100644 index f9f586db93c..00000000000 --- a/lib/tasks/gitlab/web_hook.rake +++ /dev/null @@ -1,65 +0,0 @@ -namespace :gitlab do - namespace :web_hook do - desc "GITLAB | Adds a web hook to the projects" - task :add => :environment do - web_hook_url = ENV['URL'] - namespace_path = ENV['NAMESPACE'] - - projects = find_projects(namespace_path) - - puts "Adding web hook '#{web_hook_url}' to:" - projects.find_each(batch_size: 1000) do |project| - print "- #{project.name} ... " - web_hook = project.hooks.new(url: web_hook_url) - if web_hook.save - puts "added".green - else - print "failed".red - puts " [#{web_hook.errors.full_messages.to_sentence}]" - end - end - end - - desc "GITLAB | Remove a web hook from the projects" - task :rm => :environment do - web_hook_url = ENV['URL'] - namespace_path = ENV['NAMESPACE'] - - projects = find_projects(namespace_path) - projects_ids = projects.pluck(:id) - - puts "Removing web hooks with the url '#{web_hook_url}' ... " - count = WebHook.where(url: web_hook_url, project_id: projects_ids, type: 'ProjectHook').delete_all - puts "#{count} web hooks were removed." - end - - desc "GITLAB | List web hooks" - task :list => :environment do - namespace_path = ENV['NAMESPACE'] - - projects = find_projects(namespace_path) - web_hooks = projects.all.map(&:hooks).flatten - web_hooks.each do |hook| - puts "#{hook.project.name.truncate(20).ljust(20)} -> #{hook.url}" - end - - puts "\n#{web_hooks.size} web hooks found." - end - end - - def find_projects(namespace_path) - if namespace_path.blank? - Project - elsif namespace_path == '/' - Project.where(namespace_id: nil) - else - namespace = Namespace.where(path: namespace_path).first - if namespace - Project.where(namespace_id: namespace.id) - else - puts "Namespace not found: #{namespace_path}".red - exit 2 - end - end - end -end diff --git a/lib/tasks/migrate/add_limits_mysql.rake b/lib/tasks/migrate/add_limits_mysql.rake deleted file mode 100644 index a1972a682d8..00000000000 --- a/lib/tasks/migrate/add_limits_mysql.rake +++ /dev/null @@ -1,7 +0,0 @@ -require Rails.root.join('db/migrate/limits_to_mysql') - -desc "GITLAB | Add limits to strings in mysql database" -task add_limits_mysql: :environment do - puts "Adding limits to schema.rb for mysql" - LimitsToMysql.new.up -end diff --git a/lib/tasks/migrate/migrate_iids.rake b/lib/tasks/migrate/migrate_iids.rake deleted file mode 100644 index 33271e1a2bb..00000000000 --- a/lib/tasks/migrate/migrate_iids.rake +++ /dev/null @@ -1,48 +0,0 @@ -desc "GITLAB | Build internal ids for issues and merge requests" -task migrate_iids: :environment do - puts 'Issues'.yellow - Issue.where(iid: nil).find_each(batch_size: 100) do |issue| - begin - issue.set_iid - if issue.update_attribute(:iid, issue.iid) - print '.' - else - print 'F' - end - rescue - print 'F' - end - end - - puts 'done' - puts 'Merge Requests'.yellow - MergeRequest.where(iid: nil).find_each(batch_size: 100) do |mr| - begin - mr.set_iid - if mr.update_attribute(:iid, mr.iid) - print '.' - else - print 'F' - end - rescue => ex - print 'F' - end - end - - puts 'done' - puts 'Milestones'.yellow - Milestone.where(iid: nil).find_each(batch_size: 100) do |m| - begin - m.set_iid - if m.update_attribute(:iid, m.iid) - print '.' - else - print 'F' - end - rescue - print 'F' - end - end - - puts 'done' -end diff --git a/lib/tasks/rubocop.rake b/lib/tasks/rubocop.rake deleted file mode 100644 index ddfaf5d51f2..00000000000 --- a/lib/tasks/rubocop.rake +++ /dev/null @@ -1,4 +0,0 @@ -unless Rails.env.production? - require 'rubocop/rake_task' - RuboCop::RakeTask.new -end diff --git a/lib/tasks/setup.rake b/lib/tasks/setup.rake deleted file mode 100644 index 93701de8f63..00000000000 --- a/lib/tasks/setup.rake +++ /dev/null @@ -1,4 +0,0 @@ -desc "GITLAB | Setup gitlab db" -task :setup do - Rake::Task["gitlab:setup"].invoke -end diff --git a/lib/tasks/sidekiq.rake b/lib/tasks/sidekiq.rake deleted file mode 100644 index e4bd6545755..00000000000 --- a/lib/tasks/sidekiq.rake +++ /dev/null @@ -1,21 +0,0 @@ -namespace :sidekiq do - desc "GITLAB | Stop sidekiq" - task :stop do - system *%W(bin/background_jobs stop) - end - - desc "GITLAB | Start sidekiq" - task :start do - system *%W(bin/background_jobs start) - end - - desc 'GitLab | Restart sidekiq' - task :restart do - system *%W(bin/background_jobs restart) - end - - desc "GITLAB | Start sidekiq with launchd on Mac OS X" - task :launchd do - system *%W(bin/background_jobs start_no_deamonize) - end -end diff --git a/lib/tasks/spec.rake b/lib/tasks/spec.rake deleted file mode 100644 index bee22300298..00000000000 --- a/lib/tasks/spec.rake +++ /dev/null @@ -1,45 +0,0 @@ -Rake::Task["spec"].clear if Rake::Task.task_defined?('spec') - -namespace :spec do - desc 'GITLAB | Run request specs' - task :api do - cmds = [ - %W(rake gitlab:setup), - %W(rspec spec --tag @api) - ] - run_commands(cmds) - end - - desc 'GITLAB | Run feature specs' - task :feature do - cmds = [ - %W(rake gitlab:setup), - %W(rspec spec --tag @feature) - ] - run_commands(cmds) - end - - desc 'GITLAB | Run other specs' - task :other do - cmds = [ - %W(rake gitlab:setup), - %W(rspec spec --tag ~@api --tag ~@feature) - ] - run_commands(cmds) - end -end - -desc "GITLAB | Run specs" -task :spec do - cmds = [ - %W(rake gitlab:setup), - %W(rspec spec), - ] - run_commands(cmds) -end - -def run_commands(cmds) - cmds.each do |cmd| - system({'RAILS_ENV' => 'test', 'force' => 'yes'}, *cmd) or raise("#{cmd} failed!") - end -end diff --git a/lib/tasks/spinach.rake b/lib/tasks/spinach.rake deleted file mode 100644 index 4aefc18ce14..00000000000 --- a/lib/tasks/spinach.rake +++ /dev/null @@ -1,40 +0,0 @@ -Rake::Task["spinach"].clear if Rake::Task.task_defined?('spinach') - -desc "GITLAB | Run spinach" -task :spinach do - tags = if ENV['SEMAPHORE'] - '~@tricky' - else - '~@semaphore' - end - - cmds = [ - %W(rake gitlab:setup), - %W(spinach --tags #{tags}), - ] - run_commands(cmds) -end - -desc "GITLAB | Run project spinach features" -task :spinach_project do - cmds = [ - %W(rake gitlab:setup), - %W(spinach --tags ~@admin,~@dashboard,~@profile,~@public,~@snippets), - ] - run_commands(cmds) -end - -desc "GITLAB | Run other spinach features" -task :spinach_other do - cmds = [ - %W(rake gitlab:setup), - %W(spinach --tags @admin,@dashboard,@profile,@public,@snippets), - ] - run_commands(cmds) -end - -def run_commands(cmds) - cmds.each do |cmd| - system({'RAILS_ENV' => 'test', 'force' => 'yes'}, *cmd) or raise("#{cmd} failed!") - end -end diff --git a/lib/tasks/test.rake b/lib/tasks/test.rake deleted file mode 100644 index a39d9649876..00000000000 --- a/lib/tasks/test.rake +++ /dev/null @@ -1,13 +0,0 @@ -Rake::Task["test"].clear - -desc "GITLAB | Run all tests" -task :test do - Rake::Task["gitlab:test"].invoke -end - -unless Rails.env.production? - require 'coveralls/rake/task' - Coveralls::RakeTask.new - desc "GITLAB | Run all tests on CI with simplecov" - task :test_ci => [:rubocop, :brakeman, 'jasmine:ci', :spinach, :spec, 'coveralls:push'] -end diff --git a/lib/unfold_form.rb b/lib/unfold_form.rb deleted file mode 100644 index 46b12beeaaf..00000000000 --- a/lib/unfold_form.rb +++ /dev/null @@ -1,11 +0,0 @@ -require_relative 'gt_one_coercion' - -class UnfoldForm - include Virtus.model - - attribute :since, GtOneCoercion - attribute :to, GtOneCoercion - attribute :bottom, Boolean - attribute :unfold, Boolean, default: true - attribute :offset, Integer -end -- cgit v1.2.3