2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-15 15:02:44 +00:00
|
|
|
module API
|
|
|
|
module Helpers
|
|
|
|
module InternalHelpers
|
2017-09-18 17:25:23 +00:00
|
|
|
attr_reader :redirected_path
|
|
|
|
|
2019-03-18 16:51:11 +00:00
|
|
|
delegate :wiki?, to: :repo_type
|
|
|
|
|
|
|
|
def repo_type
|
|
|
|
set_project unless defined?(@repo_type) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
@repo_type # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2016-11-15 15:02:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
2017-11-22 07:50:36 +00:00
|
|
|
set_project unless defined?(@project) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
@project # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2016-11-15 15:02:44 +00:00
|
|
|
end
|
2017-06-26 13:55:06 +00:00
|
|
|
|
2016-11-15 15:02:44 +00:00
|
|
|
def ssh_authentication_abilities
|
|
|
|
[
|
|
|
|
:read_project,
|
|
|
|
:download_code,
|
|
|
|
:push_code
|
|
|
|
]
|
|
|
|
end
|
Accept environment variables from the `pre-receive` script.
1. Starting version 2.11, git changed the way the pre-receive flow works.
- Previously, the new potential objects would be added to the main repo. If the
pre-receive passes, the new objects stay in the repo but are linked up. If
the pre-receive fails, the new objects stay orphaned in the repo, and are
cleaned up during the next `git gc`.
- In 2.11, the new potential objects are added to a temporary "alternate object
directory", that git creates for this purpose. If the pre-receive passes, the
objects from the alternate object directory are migrated to the main repo. If
the pre-receive fails the alternate object directory is simply deleted.
2. In our workflow, the pre-recieve script (in `gitlab-shell) calls the
`/allowed` endpoint, which calls out directly to git to perform
various checks. These direct calls to git do _not_ have the necessary
environment variables set which allow access to the "alternate object
directory" (explained above). Therefore these calls to git are not able to
access any of the new potential objects to be added during this push.
3. We fix this by accepting the relevant environment variables
(GIT_ALTERNATE_OBJECT_DIRECTORIES, GIT_OBJECT_DIRECTORY) on the
`/allowed` endpoint, and then include these environment variables while
calling out to git.
4. This commit includes (whitelisted) these environment variables while making
the "force push" check. A `Gitlab::Git::RevList` module is extracted to
prevent `ForcePush` from being littered with these checks.
2016-12-07 07:55:49 +00:00
|
|
|
|
2017-04-05 07:29:30 +00:00
|
|
|
def parse_env
|
|
|
|
return {} if params[:env].blank?
|
Accept environment variables from the `pre-receive` script.
1. Starting version 2.11, git changed the way the pre-receive flow works.
- Previously, the new potential objects would be added to the main repo. If the
pre-receive passes, the new objects stay in the repo but are linked up. If
the pre-receive fails, the new objects stay orphaned in the repo, and are
cleaned up during the next `git gc`.
- In 2.11, the new potential objects are added to a temporary "alternate object
directory", that git creates for this purpose. If the pre-receive passes, the
objects from the alternate object directory are migrated to the main repo. If
the pre-receive fails the alternate object directory is simply deleted.
2. In our workflow, the pre-recieve script (in `gitlab-shell) calls the
`/allowed` endpoint, which calls out directly to git to perform
various checks. These direct calls to git do _not_ have the necessary
environment variables set which allow access to the "alternate object
directory" (explained above). Therefore these calls to git are not able to
access any of the new potential objects to be added during this push.
3. We fix this by accepting the relevant environment variables
(GIT_ALTERNATE_OBJECT_DIRECTORIES, GIT_OBJECT_DIRECTORY) on the
`/allowed` endpoint, and then include these environment variables while
calling out to git.
4. This commit includes (whitelisted) these environment variables while making
the "force push" check. A `Gitlab::Git::RevList` module is extracted to
prevent `ForcePush` from being littered with these checks.
2016-12-07 07:55:49 +00:00
|
|
|
|
|
|
|
JSON.parse(params[:env])
|
|
|
|
rescue JSON::ParserError
|
2017-04-05 07:29:30 +00:00
|
|
|
{}
|
Accept environment variables from the `pre-receive` script.
1. Starting version 2.11, git changed the way the pre-receive flow works.
- Previously, the new potential objects would be added to the main repo. If the
pre-receive passes, the new objects stay in the repo but are linked up. If
the pre-receive fails, the new objects stay orphaned in the repo, and are
cleaned up during the next `git gc`.
- In 2.11, the new potential objects are added to a temporary "alternate object
directory", that git creates for this purpose. If the pre-receive passes, the
objects from the alternate object directory are migrated to the main repo. If
the pre-receive fails the alternate object directory is simply deleted.
2. In our workflow, the pre-recieve script (in `gitlab-shell) calls the
`/allowed` endpoint, which calls out directly to git to perform
various checks. These direct calls to git do _not_ have the necessary
environment variables set which allow access to the "alternate object
directory" (explained above). Therefore these calls to git are not able to
access any of the new potential objects to be added during this push.
3. We fix this by accepting the relevant environment variables
(GIT_ALTERNATE_OBJECT_DIRECTORIES, GIT_OBJECT_DIRECTORY) on the
`/allowed` endpoint, and then include these environment variables while
calling out to git.
4. This commit includes (whitelisted) these environment variables while making
the "force push" check. A `Gitlab::Git::RevList` module is extracted to
prevent `ForcePush` from being littered with these checks.
2016-12-07 07:55:49 +00:00
|
|
|
end
|
2016-11-25 16:10:25 +00:00
|
|
|
|
|
|
|
def log_user_activity(actor)
|
2017-04-12 15:13:24 +00:00
|
|
|
commands = Gitlab::GitAccess::DOWNLOAD_COMMANDS
|
2016-11-25 16:10:25 +00:00
|
|
|
|
|
|
|
::Users::ActivityService.new(actor, 'Git SSH').execute if commands.include?(params[:action])
|
|
|
|
end
|
2017-04-28 16:52:09 +00:00
|
|
|
|
2017-08-30 02:10:41 +00:00
|
|
|
def merge_request_urls
|
|
|
|
::MergeRequests::GetUrlsService.new(project).execute(params[:changes])
|
|
|
|
end
|
|
|
|
|
2019-04-08 05:07:53 +00:00
|
|
|
def process_mr_push_options(push_options, project, user, changes)
|
|
|
|
output = {}
|
|
|
|
|
|
|
|
service = ::MergeRequests::PushOptionsHandlerService.new(
|
|
|
|
project,
|
|
|
|
user,
|
|
|
|
changes,
|
|
|
|
push_options
|
|
|
|
).execute
|
|
|
|
|
|
|
|
if service.errors.present?
|
|
|
|
output[:warnings] = push_options_warning(service.errors.join("\n\n"))
|
|
|
|
end
|
|
|
|
|
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2019-03-29 01:07:03 +00:00
|
|
|
def push_options_warning(warning)
|
|
|
|
options = Array.wrap(params[:push_options]).map { |p| "'#{p}'" }.join(' ')
|
|
|
|
"Error encountered with push options #{options}: #{warning}"
|
|
|
|
end
|
|
|
|
|
2017-08-31 21:00:40 +00:00
|
|
|
def redis_ping
|
|
|
|
result = Gitlab::Redis::SharedState.with { |redis| redis.ping }
|
|
|
|
|
|
|
|
result == 'PONG'
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn("GitLab: An unexpected error occurred in pinging to Redis: #{e}")
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-02-02 15:27:30 +00:00
|
|
|
def project_path
|
|
|
|
project&.path || project_path_match[:project_path]
|
|
|
|
end
|
|
|
|
|
|
|
|
def namespace_path
|
|
|
|
project&.namespace&.full_path || project_path_match[:namespace_path]
|
2018-01-22 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
2017-04-28 16:52:09 +00:00
|
|
|
private
|
|
|
|
|
2018-02-02 15:27:30 +00:00
|
|
|
def project_path_match
|
|
|
|
@project_path_match ||= params[:project].match(Gitlab::PathRegex.full_project_git_path_regex) || {}
|
2018-01-19 13:04:14 +00:00
|
|
|
end
|
|
|
|
|
2017-11-22 07:50:36 +00:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-04-28 16:52:09 +00:00
|
|
|
def set_project
|
|
|
|
if params[:gl_repository]
|
2019-03-18 16:51:11 +00:00
|
|
|
@project, @repo_type = Gitlab::GlRepository.parse(params[:gl_repository])
|
2017-06-16 00:03:54 +00:00
|
|
|
@redirected_path = nil
|
2017-04-28 16:52:09 +00:00
|
|
|
else
|
2019-03-18 16:51:11 +00:00
|
|
|
@project, @repo_type, @redirected_path = Gitlab::RepoPath.parse(params[:project])
|
2017-04-28 16:52:09 +00:00
|
|
|
end
|
|
|
|
end
|
2017-11-22 07:50:36 +00:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2017-05-23 18:34:58 +00:00
|
|
|
|
|
|
|
# Project id to pass between components that don't share/don't have
|
|
|
|
# access to the same filesystem mounts
|
|
|
|
def gl_repository
|
2019-03-18 16:51:11 +00:00
|
|
|
repo_type.identifier_for_subject(project)
|
2017-05-23 18:34:58 +00:00
|
|
|
end
|
|
|
|
|
2019-02-16 14:54:32 +00:00
|
|
|
def gl_project_path
|
|
|
|
if wiki?
|
|
|
|
project.wiki.full_path
|
|
|
|
else
|
|
|
|
project.full_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-26 13:55:06 +00:00
|
|
|
# Return the repository depending on whether we want the wiki or the
|
|
|
|
# regular repository
|
|
|
|
def repository
|
2019-03-18 16:51:11 +00:00
|
|
|
if repo_type.wiki?
|
2017-06-26 13:55:06 +00:00
|
|
|
project.wiki.repository
|
2017-05-23 18:34:58 +00:00
|
|
|
else
|
2017-06-26 13:55:06 +00:00
|
|
|
project.repository
|
2017-05-23 18:34:58 +00:00
|
|
|
end
|
|
|
|
end
|
2017-06-26 13:55:06 +00:00
|
|
|
|
|
|
|
# Return the Gitaly Address if it is enabled
|
|
|
|
def gitaly_payload(action)
|
2018-03-21 13:57:06 +00:00
|
|
|
return unless %w[git-receive-pack git-upload-pack git-upload-archive].include?(action)
|
2018-01-22 16:51:47 +00:00
|
|
|
|
2017-06-26 13:55:06 +00:00
|
|
|
{
|
|
|
|
repository: repository.gitaly_repository,
|
|
|
|
address: Gitlab::GitalyClient.address(project.repository_storage),
|
|
|
|
token: Gitlab::GitalyClient.token(project.repository_storage)
|
|
|
|
}
|
|
|
|
end
|
2016-11-15 15:02:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|