module GitFusion module Util # Messages back to client REPO_INITIALISED = 'Repository already initialised' PATCH_PARAMS_DONT_EXIST = 'Chosen repo configuration p4gf_file does not have all of the specified parameters' KEY_ALREADY_EXISTS = 'Key under this name already uploaded, choose a different name' USER_NOT_MATCHING = 'User is authenticated, but not authorised to view the requested data.' # Error codes from perforce CLIENT_ERROR = 6153 NO_FILES = 7214 NO_FILES_TO_SUBMIT = 7218 CONFIG_ERROR = 7215 # TODO: make sure temp clients dissapear. *poof* def modified_temp_client(depo_map, client_map, description=nil) require_p4_with_temp_client p4 = env['p4'] client = p4.fetch_client hws_settings = env['hws_settings'] client._view = ["//#{hws_settings.GIT_FUSION_DEPOT}/#{depo_map} //#{p4.client}/#{client_map}"] p4.at_exception_level(P4::RAISE_NONE) do p4.save_client(client) end if description change_id = init_changelist(p4, description) return p4, change_id end return p4 end def modified_temp_client_with_a_file(depo_map, file_content, description) require_p4_with_temp_client p4 = env['p4'] change_id = init_changelist(p4, description) client = p4.fetch_client file = File.new("#{client["Root"]}/p4gf_config", 'w+') hws_settings = env['hws_settings'] client._view = ["//#{hws_settings.GIT_FUSION_DEPOT}/#{depo_map} //#{p4.client}/#{File.basename(file)}"] p4.at_exception_level(P4::RAISE_NONE) do p4.save_client(client) end file.write(file_content) file.close return p4, file, change_id end def get_all_keys # get all keys for all users with names and contents hws_settings = env['hws_settings'] p4 = env['p4'] keys = [] file_list = p4.run_files('-e', "//#{hws_settings.GIT_FUSION_DEPOT}/users/...") file_list.each do | file | file_name = file['depotFile'] user = file['depotFile'].split('/')[-3] content = p4.run('print', file_name) content.shift keys << {'key_name' => file_name.split('/')[-1], 'user' => user, 'key' => content.join} end keys end def get_keys_for_user(user) keys = get_all_keys.select { |key| key['user'] == user} keys end def check_if_key_exists(key) keys = get_all_keys return keys.any? {|h| h['key'] == key} end def check_if_key_name_exists(key, user) keys = get_all_keys return keys.any? {|h| h['key_name'] == key && h['user'] == user} end def get_by_key(key, value) val = get_all_keys.select { |record| record['key'] == key} val.first.nil? ? '' : val.first[value] end def init_changelist(p4, description) change_spec = p4.fetch_change change_spec._description = description results = p4.save_change(change_spec) results[0].gsub(/^Change (\d+) created./, '\1') end def to_message(message) { # 0 means use HTTP error code MessageCode: 0, MessageSeverity: :ERROR, MessageText: message } end def clean_up(p4, change_id) p4.run('revert', '-c', change_id, '//...') p4.run_change('-d', change_id) end def error_and_clean_up(error, change_id=nil, p4=nil) clean_up(p4, change_id) if p4 && change_id halt 400, to_message(error).to_json end def check_patch_data(config, match_data) match_data.each do |param| unless config =~ /#{param['param']}/ return false end end end def replace_partial_config(config, match_data) match_data.each do |param| new_line = "#{param['param'].strip} = #{param['value'].strip}\n" config.gsub!(/#{param['param']}.*(\r\n|\r|\n|$)/, new_line) end return config end def config_error_message(error) error.message_text.match(/error: (?.*)/)['error_message'] end def process_error(p4, change_id, error) if error.message_code != 15360 message = error message = config_error_message(error) if error.message_code == CONFIG_ERROR error_and_clean_up(message, change_id, p4) end end def self.error?(p4) !p4.errors.empty? end def print_results(print_results) content = ''.b print_results.shift puts print_results print_results.each do |result| if result content << result end end content end end end