Commit b5b91c6ed05 by Georges Racinet

WIP API for update of HGRC

This is #165

--HG--
branch : heptapod-0-8
parent 1fd2af0d049
class HgrcSetService < BaseService
UnsafeSetting = Class.new(StandardError)
def execute
values = @params[:values]
unless values.nil? || values.empty? || values.keys == ['heptapod']
raise UnsafeSetting("Only the 'heptapod' section is allowed")
end
begin
project.repository.set_hgrc(@user, @params.fetch(:inherit, true), values)
rescue Gitlab::Git::Repository::InvalidHgSetting
raise UnsafeSetting(
"One of the section, items or leaf values contains "\
"unsafe characters")
end
end
end
......@@ -302,6 +302,26 @@ module API
end
end
desc 'Completely replace the HGRC for an existing project'
# TODO detailed description (success etc)
params do
optional :inherit, type: Boolean, desc: 'If true (the default), the repository will load first the HGRC of its enclosing namespace.'
optional :values, type: Hash, desc: 'Two-level mapping, first level of keys is the section, second level is the item. Leaf values are strings. Unsafe sections or items will be refused.'
end
put ':id/hgrc' do
authorize_admin_project
unless ["hg", "hg_git"].include? user_project.vcs_type
render_api_error("Not a Mercurial project!", 400)
end
begin
::Projects::HgrcSetService.new(user_project,
params.fetch(:inherit, true),
params.fetch(:values))
rescue ::Projects::HgrcSetService::UnsafeSetting => e
render_api_error!(e.message, 403)
end
end
desc 'Archive a project' do
success Entities::Project
end
......
......@@ -33,6 +33,7 @@ module Gitlab
# Heptapod specific
MergeError = Class.new(StandardError)
InvalidHgsetting = Class.new(StandardError)
class << self
# Unlike `new`, `create` takes the repository path
......@@ -1016,6 +1017,39 @@ module Gitlab
node_branch.split(':')
end
# write the whole repository-local HGRC file.
#
# inherit: if true, include directive for the namespace HGRC is included
# values: two-level Hash, with String leaf values
#
# Newlines in values are prohibited (use plain whitespace if needed),
# as well as in keys (section and item names).
def set_hgrc(user, inherit, values)
key_rx = /^\w*$/
File.open(File.join(@hgpath, '.hg', 'hgrc'), 'w') do |hgrcf|
hgrcf.puts(
"# This file has been overwritten through the GitLab Rails app")
hgrcf.puts(
"# by user #{user.username} (id=#{user.id}) at #{Time.new}\n")
if inherit
hgrc.puts("%include ../../hgrc\n")
if !values.nil?
values.each do |section, items|
raise InvalidHgSetting(section) unless key_rx.match(section)
hgrcf.puts("[#{section}]")
items.each do |item, value|
raise InvalidHgSetting(item) unless key_rx.match(section)
raise InvalidHgSetting(value) if value.include?('\n')
hgrcf.puts("#{item} = #{value}")
end
hgrcf.puts()
end
end
end
end
end
def revert(user:, commit:, branch_name:, message:, start_branch_name:, start_repository:)
gitaly_migrate(:revert) do |is_enabled|
args = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment